diff --git a/src/ControlFileBuilder.php b/src/ControlFileBuilder.php index fde4a00..208891e 100644 --- a/src/ControlFileBuilder.php +++ b/src/ControlFileBuilder.php @@ -11,9 +11,7 @@ class ControlFileBuilder implements Stringable { - public function __construct(public SQLLoader $loader) - { - } + public function __construct(public SQLLoader $loader) {} public function __toString(): string { diff --git a/src/CsvFile.php b/src/CsvFile.php index 3b47b3e..60f3c58 100644 --- a/src/CsvFile.php +++ b/src/CsvFile.php @@ -11,9 +11,7 @@ final class CsvFile /** * @param resource $stream */ - private function __construct(public string $file, public $stream) - { - } + private function __construct(public string $file, public $stream) {} /** * A list of possible modes. The default is 'w' (open for writing).: diff --git a/src/InputFile.php b/src/InputFile.php index ad5c2a4..c8fff63 100644 --- a/src/InputFile.php +++ b/src/InputFile.php @@ -14,8 +14,7 @@ public function __construct( public ?string $discardFile = null, public ?string $discardMax = null, public ?string $osFileProcClause = null, - ) { - } + ) {} public function __toString(): string { diff --git a/src/SQLLoader.php b/src/SQLLoader.php index ef63d57..d83ebe8 100644 --- a/src/SQLLoader.php +++ b/src/SQLLoader.php @@ -46,9 +46,7 @@ class SQLLoader protected string $dateFormat = 'YYYY-MM-DD"T"HH24:MI:SS."000000Z"'; - public function __construct(public array $options = []) - { - } + public function __construct(public array $options = []) {} /** * Define mode to use. @@ -71,6 +69,8 @@ public function into( bool $trailing = true, array $formatOptions = [], ?string $when = null, + bool $csv = false, + bool $withEmbedded = true, ): static { if (! $columns && $this->defaultColumns) { $columns = $this->createColumnsFromHeaders($table, $this->defaultColumns); @@ -88,7 +88,7 @@ public function into( $columns = array_merge($columns, $this->constants); $this->tables[] = new TableDefinition( - $table, $columns, $terminatedBy, $enclosedBy, $trailing, $formatOptions, $when + $table, $columns, $terminatedBy, $enclosedBy, $trailing, $formatOptions, $when, $csv, $withEmbedded ); return $this; diff --git a/src/TableDefinition.php b/src/TableDefinition.php index 9c40b7f..51cd486 100644 --- a/src/TableDefinition.php +++ b/src/TableDefinition.php @@ -16,8 +16,9 @@ public function __construct( public bool $trailing = false, public array $formatOptions = [], public ?string $when = null, - ) { - } + public bool $csv = false, + public bool $withEmbedded = true, + ) {} public function __toString(): string { @@ -27,13 +28,7 @@ public function __toString(): string $sql .= "WHEN {$this->when}".PHP_EOL; } - if ($this->terminatedBy) { - $sql .= "FIELDS TERMINATED BY '{$this->terminatedBy}' "; - } - - if ($this->enclosedBy) { - $sql .= "OPTIONALLY ENCLOSED BY '{$this->enclosedBy}'".PHP_EOL; - } + $sql .= $this->delimiterSpecification(); if ($this->formatOptions) { $sql .= implode(PHP_EOL, $this->formatOptions).PHP_EOL; @@ -54,4 +49,28 @@ public function __toString(): string return $sql; } + + private function delimiterSpecification(): string + { + $specs = ['FIELDS']; + + if ($this->csv) { + $specs[] = 'CSV'; + $specs[] = $this->withEmbedded ? 'WITH EMBEDDED' : 'WITHOUT EMBEDDED'; + } + + if ($this->terminatedBy) { + $specs[] = "TERMINATED BY '{$this->terminatedBy}'"; + } + + if ($this->enclosedBy) { + $specs[] = "OPTIONALLY ENCLOSED BY '{$this->enclosedBy}'"; + } + + if (count($specs) > 1) { + return implode(' ', $specs).PHP_EOL; + } + + return ''; + } } diff --git a/tests/Unit/TableDefinitionTest.php b/tests/Unit/TableDefinitionTest.php index 3b33fbc..99bdb2f 100644 --- a/tests/Unit/TableDefinitionTest.php +++ b/tests/Unit/TableDefinitionTest.php @@ -59,7 +59,7 @@ ); assertEquals( - "INTO TABLE users\nFIELDS TERMINATED BY ',' (\n id,\n name,\n email\n)\n", + "INTO TABLE users\nFIELDS TERMINATED BY ','\n(\n id,\n name,\n email\n)\n", $table ); }); @@ -100,3 +100,30 @@ $table ); }); + +test('it can build with csv format', function () { + $table = new TableDefinition( + 'users', + ['id', 'name', 'email'], + csv: true, + ); + + assertEquals( + "INTO TABLE users\nFIELDS CSV WITH EMBEDDED\n(\n id,\n name,\n email\n)\n", + $table->__toString() + ); +}); + +test('it can build with csv format without embedded', function () { + $table = new TableDefinition( + 'users', + ['id', 'name', 'email'], + csv: true, + withEmbedded: false, + ); + + assertEquals( + "INTO TABLE users\nFIELDS CSV WITHOUT EMBEDDED\n(\n id,\n name,\n email\n)\n", + $table + ); +});