Skip to content

Commit 2410f6a

Browse files
committed
feat: constant columns
1 parent 39ec2c9 commit 2410f6a

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

README.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,10 @@ $loader->into('employees', ['name', 'dept_id']);
143143
Using `withHeaders` will skip the first row of the CSV file.
144144
145145
> **IMPORTANT**
146-
> This method assumes that the headers are the same as the columns.
147-
> If the headers are different from the columns, you should define the `columns` in the `into` method.
148-
> This works well with the Eloquent Collection as the data source and saving it to a CSV file for bulk import.
146+
> 1. `withHeaders` must be called before the `into` method.
147+
> 2. This method assumes that the headers are the same as the columns.
148+
> 3. If the headers are different from the columns, you should define the `columns` in the `into` method.
149+
> 4. This works well with the Eloquent Collection as the data source and saving it to a CSV file for bulk import.
149150
150151
#### Building a CSV File from Eloquent Collection
151152
@@ -197,6 +198,23 @@ John Doe,1
197198
Jane Doe,2
198199
```
199200
201+
### Constants
202+
203+
In some cases, we need to insert constant values to the table. You can use the `constants` method to set the constant value.
204+
205+
> **IMPORTANT**
206+
>`constants` must be called before the `into` method.
207+
208+
```php
209+
$loader->withHeaders()
210+
->constants([
211+
'file_id CONSTANT 1',
212+
'created_at EXPRESSION "current_timestamp(3)"',
213+
'updated_at EXPRESSION "current_timestamp(3)"',
214+
])
215+
->into('users');
216+
```
217+
200218
### Connection
201219
202220
You can set the connection name to use for the SQL*Loader command using the `connection` method.

src/SQLLoader.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class SQLLoader
3232

3333
protected array $defaultColumns = [];
3434

35+
public array $constants = [];
36+
3537
protected ?string $disk = null;
3638

3739
protected ?string $logPath = null;
@@ -83,6 +85,8 @@ public function into(
8385
];
8486
}
8587

88+
$columns = array_merge($columns, $this->constants);
89+
8690
$this->tables[] = new TableDefinition(
8791
$table, $columns, $terminatedBy, $enclosedBy, $trailing, $formatOptions, $when
8892
);
@@ -470,4 +474,11 @@ public function dateFormat(string $format): static
470474

471475
return $this;
472476
}
477+
478+
public function constants(array $constants): static
479+
{
480+
$this->constants = $constants;
481+
482+
return $this;
483+
}
473484
}

tests/Feature/SQLLoaderTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,30 @@
230230
->toContain("TIMESTAMP WITH TIME ZONE 'YYYY-MM-DD'\n")
231231
->toContain("TIMESTAMP WITH LOCAL TIME ZONE 'YYYY-MM-DD'\n");
232232
});
233+
234+
test('it can process constants columns', function () {
235+
Process::fake();
236+
237+
$loader = new SQLLoader();
238+
$loader->inFile(__DIR__.'/../data/users.dat')
239+
->as('users.ctl')
240+
->withHeaders()
241+
->constants([
242+
'created_by CONSTANT 1',
243+
'created_at EXPRESSION "current_timestamp(3)"',
244+
'updated_by CONSTANT 1',
245+
'updated_at EXPRESSION "current_timestamp(3)"',
246+
])
247+
->into('users')
248+
->execute();
249+
250+
$controlFile = $loader->buildControlFile();
251+
252+
expect($controlFile)->toBeString()
253+
->toContain("\"NAME\",\n")
254+
->toContain("\"EMAIL\",\n")
255+
->toContain("created_by CONSTANT 1,\n")
256+
->toContain("created_at EXPRESSION \"current_timestamp(3)\",\n")
257+
->toContain("updated_by CONSTANT 1,\n")
258+
->toContain("updated_at EXPRESSION \"current_timestamp(3)\"\n");
259+
});

0 commit comments

Comments
 (0)