Skip to content

Commit 1ec8454

Browse files
authored
Merge pull request #4 from ejgandelaberon/main
feat: Specifying CSV Format Files
2 parents 456bb7d + df57d42 commit 1ec8454

File tree

6 files changed

+63
-22
lines changed

6 files changed

+63
-22
lines changed

src/ControlFileBuilder.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111

1212
class ControlFileBuilder implements Stringable
1313
{
14-
public function __construct(public SQLLoader $loader)
15-
{
16-
}
14+
public function __construct(public SQLLoader $loader) {}
1715

1816
public function __toString(): string
1917
{

src/CsvFile.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ final class CsvFile
1111
/**
1212
* @param resource $stream
1313
*/
14-
private function __construct(public string $file, public $stream)
15-
{
16-
}
14+
private function __construct(public string $file, public $stream) {}
1715

1816
/**
1917
* A list of possible modes. The default is 'w' (open for writing).:

src/InputFile.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ public function __construct(
1414
public ?string $discardFile = null,
1515
public ?string $discardMax = null,
1616
public ?string $osFileProcClause = null,
17-
) {
18-
}
17+
) {}
1918

2019
public function __toString(): string
2120
{

src/SQLLoader.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ class SQLLoader
4646

4747
protected string $dateFormat = 'YYYY-MM-DD"T"HH24:MI:SS."000000Z"';
4848

49-
public function __construct(public array $options = [])
50-
{
51-
}
49+
public function __construct(public array $options = []) {}
5250

5351
/**
5452
* Define mode to use.
@@ -71,6 +69,8 @@ public function into(
7169
bool $trailing = true,
7270
array $formatOptions = [],
7371
?string $when = null,
72+
bool $csv = false,
73+
bool $withEmbedded = true,
7474
): static {
7575
if (! $columns && $this->defaultColumns) {
7676
$columns = $this->createColumnsFromHeaders($table, $this->defaultColumns);
@@ -88,7 +88,7 @@ public function into(
8888
$columns = array_merge($columns, $this->constants);
8989

9090
$this->tables[] = new TableDefinition(
91-
$table, $columns, $terminatedBy, $enclosedBy, $trailing, $formatOptions, $when
91+
$table, $columns, $terminatedBy, $enclosedBy, $trailing, $formatOptions, $when, $csv, $withEmbedded
9292
);
9393

9494
return $this;

src/TableDefinition.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ public function __construct(
1616
public bool $trailing = false,
1717
public array $formatOptions = [],
1818
public ?string $when = null,
19-
) {
20-
}
19+
public bool $csv = false,
20+
public bool $withEmbedded = true,
21+
) {}
2122

2223
public function __toString(): string
2324
{
@@ -27,13 +28,7 @@ public function __toString(): string
2728
$sql .= "WHEN {$this->when}".PHP_EOL;
2829
}
2930

30-
if ($this->terminatedBy) {
31-
$sql .= "FIELDS TERMINATED BY '{$this->terminatedBy}' ";
32-
}
33-
34-
if ($this->enclosedBy) {
35-
$sql .= "OPTIONALLY ENCLOSED BY '{$this->enclosedBy}'".PHP_EOL;
36-
}
31+
$sql .= $this->delimiterSpecification();
3732

3833
if ($this->formatOptions) {
3934
$sql .= implode(PHP_EOL, $this->formatOptions).PHP_EOL;
@@ -54,4 +49,28 @@ public function __toString(): string
5449

5550
return $sql;
5651
}
52+
53+
private function delimiterSpecification(): string
54+
{
55+
$specs = ['FIELDS'];
56+
57+
if ($this->csv) {
58+
$specs[] = 'CSV';
59+
$specs[] = $this->withEmbedded ? 'WITH EMBEDDED' : 'WITHOUT EMBEDDED';
60+
}
61+
62+
if ($this->terminatedBy) {
63+
$specs[] = "TERMINATED BY '{$this->terminatedBy}'";
64+
}
65+
66+
if ($this->enclosedBy) {
67+
$specs[] = "OPTIONALLY ENCLOSED BY '{$this->enclosedBy}'";
68+
}
69+
70+
if (count($specs) > 1) {
71+
return implode(' ', $specs).PHP_EOL;
72+
}
73+
74+
return '';
75+
}
5776
}

tests/Unit/TableDefinitionTest.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
);
6060

6161
assertEquals(
62-
"INTO TABLE users\nFIELDS TERMINATED BY ',' (\n id,\n name,\n email\n)\n",
62+
"INTO TABLE users\nFIELDS TERMINATED BY ','\n(\n id,\n name,\n email\n)\n",
6363
$table
6464
);
6565
});
@@ -100,3 +100,30 @@
100100
$table
101101
);
102102
});
103+
104+
test('it can build with csv format', function () {
105+
$table = new TableDefinition(
106+
'users',
107+
['id', 'name', 'email'],
108+
csv: true,
109+
);
110+
111+
assertEquals(
112+
"INTO TABLE users\nFIELDS CSV WITH EMBEDDED\n(\n id,\n name,\n email\n)\n",
113+
$table->__toString()
114+
);
115+
});
116+
117+
test('it can build with csv format without embedded', function () {
118+
$table = new TableDefinition(
119+
'users',
120+
['id', 'name', 'email'],
121+
csv: true,
122+
withEmbedded: false,
123+
);
124+
125+
assertEquals(
126+
"INTO TABLE users\nFIELDS CSV WITHOUT EMBEDDED\n(\n id,\n name,\n email\n)\n",
127+
$table
128+
);
129+
});

0 commit comments

Comments
 (0)