Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/ControlFileBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@

class ControlFileBuilder implements Stringable
{
public function __construct(public SQLLoader $loader)
{
}
public function __construct(public SQLLoader $loader) {}

public function __toString(): string
{
Expand Down
4 changes: 1 addition & 3 deletions src/CsvFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).:
Expand Down
3 changes: 1 addition & 2 deletions src/InputFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ public function __construct(
public ?string $discardFile = null,
public ?string $discardMax = null,
public ?string $osFileProcClause = null,
) {
}
) {}

public function __toString(): string
{
Expand Down
8 changes: 4 additions & 4 deletions src/SQLLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand All @@ -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;
Expand Down
37 changes: 28 additions & 9 deletions src/TableDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
Expand All @@ -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 '';
}
}
29 changes: 28 additions & 1 deletion tests/Unit/TableDefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
});
Expand Down Expand Up @@ -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
);
});