Skip to content

Commit 37ff334

Browse files
committed
feat: withHeaders to automatically set the columns
1 parent 8359ed7 commit 37ff334

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/SQLLoader.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class SQLLoader
2121
/** @var TableDefinition[] */
2222
public array $tables = [];
2323

24+
protected array $defaultColumns = [];
25+
2426
public Mode $mode = Mode::APPEND;
2527

2628
public ?string $controlFile = null;
@@ -84,13 +86,17 @@ public function mode(Mode $mode): static
8486
*/
8587
public function into(
8688
string $table,
87-
array $columns,
89+
array $columns = [],
8890
?string $terminatedBy = ',',
8991
?string $enclosedBy = '"',
9092
bool $trailing = false,
9193
array $formatOptions = [],
9294
?string $when = null,
9395
): static {
96+
if (empty($columns)) {
97+
$columns = $this->defaultColumns;
98+
}
99+
94100
$this->tables[] = new TableDefinition($table, $columns, $terminatedBy, $enclosedBy, $trailing, $formatOptions, $when);
95101

96102
return $this;
@@ -366,4 +372,12 @@ public function beginData(array $data): static
366372

367373
return $this;
368374
}
375+
376+
public function withHeaders(): static
377+
{
378+
$this->options(['skip' => 1]);
379+
$this->defaultColumns = CsvFile::make($this->inputFiles[0]->path, 'r')->getHeaders();
380+
381+
return $this;
382+
}
369383
}

0 commit comments

Comments
 (0)