Skip to content

Commit 6abb363

Browse files
committed
refactor: TableDefinition
1 parent 105b417 commit 6abb363

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

src/TableDefinition.php

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,7 @@ public function __toString(): string
2828
$sql .= "WHEN {$this->when}".PHP_EOL;
2929
}
3030

31-
if ($this->csv) {
32-
$sql .= 'FIELDS CSV '.($this->withEmbedded ? 'WITH' : 'WITHOUT').' EMBEDDED';
33-
}
34-
35-
if ($this->terminatedBy) {
36-
$sql .= ! str_contains($sql, 'FIELDS') ? 'FIELDS ' : ' ';
37-
$sql .= "TERMINATED BY '{$this->terminatedBy}' ";
38-
}
39-
40-
if ($this->enclosedBy) {
41-
$sql .= "OPTIONALLY ENCLOSED BY '{$this->enclosedBy}'";
42-
}
43-
44-
if ($this->csv || $this->terminatedBy || $this->enclosedBy) {
45-
$sql .= PHP_EOL;
46-
}
31+
$sql .= $this->delimiterSpecification();
4732

4833
if ($this->formatOptions) {
4934
$sql .= implode(PHP_EOL, $this->formatOptions).PHP_EOL;
@@ -64,4 +49,28 @@ public function __toString(): string
6449

6550
return $sql;
6651
}
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+
}
6776
}

tests/Unit/TableDefinitionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@
5959
);
6060

6161
assertEquals(
62-
"INTO TABLE users\nFIELDS TERMINATED BY ',' \n(\n id,\n name,\n email\n)\n",
63-
$table,
62+
"INTO TABLE users\nFIELDS TERMINATED BY ','\n(\n id,\n name,\n email\n)\n",
63+
$table
6464
);
6565
});
6666

0 commit comments

Comments
 (0)