Skip to content

Commit 2f39681

Browse files
committed
feat: add support for table when clause
1 parent a83a94c commit 2f39681

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/SQLLoader.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,10 @@ public function into(
8888
?string $terminatedBy = ',',
8989
?string $enclosedBy = '"',
9090
bool $trailing = false,
91-
array $formatOptions = []
91+
array $formatOptions = [],
92+
?string $when = null,
9293
): static {
93-
$this->tables[] = new TableDefinition($table, $columns, $terminatedBy, $enclosedBy, $trailing, $formatOptions);
94+
$this->tables[] = new TableDefinition($table, $columns, $terminatedBy, $enclosedBy, $trailing, $formatOptions, $when);
9495

9596
return $this;
9697
}

src/TableDefinition.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,19 @@ public function __construct(
1414
public ?string $terminatedBy = null,
1515
public ?string $enclosedBy = null,
1616
public bool $trailing = false,
17-
public array $formatOptions = []
17+
public array $formatOptions = [],
18+
public ?string $when = null,
1819
) {
1920
}
2021

2122
public function __toString(): string
2223
{
2324
$sql = "INTO TABLE {$this->table}".PHP_EOL;
2425

26+
if ($this->when) {
27+
$sql .= "WHEN {$this->when}".PHP_EOL;
28+
}
29+
2530
if ($this->terminatedBy) {
2631
$sql .= "FIELDS TERMINATED BY '{$this->terminatedBy}' ";
2732
}

tests/Unit/TableDefinitionTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,16 @@
8787
$table
8888
);
8989
});
90+
91+
test('it can build with when clause', function () {
92+
$table = new TableDefinition(
93+
'users',
94+
['id', 'name', 'email'],
95+
when: 'id > 0',
96+
);
97+
98+
assertEquals(
99+
"INTO TABLE users\nWHEN id > 0\n(\n id,\n name,\n email\n)\n",
100+
$table
101+
);
102+
});

0 commit comments

Comments
 (0)