Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: get column connection and table prefix #5

Merged
merged 3 commits into from
Jun 28, 2024
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
5 changes: 4 additions & 1 deletion src/SQLLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Contracts\Process\ProcessResult;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Process;
use Illuminate\Support\Facades\Schema;
Expand Down Expand Up @@ -87,6 +88,8 @@ public function into(

$columns = array_merge($columns, $this->constants);

$table = DB::connection($this->getConnection())->getQueryGrammar()->wrapTable($table);

$this->tables[] = new TableDefinition(
$table, $columns, $terminatedBy, $enclosedBy, $trailing, $formatOptions, $when, $csv, $withEmbedded
);
Expand All @@ -97,7 +100,7 @@ public function into(
public function createColumnsFromHeaders(string $table, array $columns): array
{
$columns = array_map('strtolower', $columns);
$schemaColumns = collect(Schema::connection(config('sql-loader.connection'))->getColumns($table));
$schemaColumns = collect(Schema::connection($this->getConnection())->getColumns($table));

$dates = $schemaColumns->filter(fn ($column) => in_array($column['type'], [
'date',
Expand Down
232 changes: 117 additions & 115 deletions tests/Feature/ControlFileBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,119 +3,121 @@
use Yajra\SQLLoader\ControlFileBuilder;
use Yajra\SQLLoader\SQLLoader;

test('it can build a control file', function () {
$loader = new SQLLoader(['skip=1', 'load=2']);
$loader->inFile(__DIR__.'/../data/users.dat')
->as('users.ctl')
->into(
table: 'users',
columns: ['id', 'name', 'email'],
trailing: 'TRAILING NULLCOLS'
);

$ctl = new ControlFileBuilder($loader);
$controlFile = $ctl->build();

expect($controlFile)->toBeString()
->and($controlFile)->toContain('OPTIONS(skip=1, load=2)')
->and($controlFile)->toContain("INFILE '".__DIR__."/../data/users.dat'")
->and($controlFile)->toContain('APPEND')
->and($controlFile)->toContain('INTO TABLE users')
->and($controlFile)->toContain("FIELDS TERMINATED BY ','")
->and($controlFile)->toContain('OPTIONALLY')
->and($controlFile)->toContain("ENCLOSED BY '\"'")
->and($controlFile)->toContain('TRAILING NULLCOLS')
->and($controlFile)->toContain('(')
->and($controlFile)->toContain('id,')
->and($controlFile)->toContain('name,')
->and($controlFile)->toContain('email')
->and($controlFile)->toContain(')');
});

test('it can build multiple input files', function () {
$loader = new SQLLoader(['skip=1', 'load=2']);
$loader->inFile(__DIR__.'/../data/users.dat')
->inFile(__DIR__.'/../data/roles.dat')
->as('users.ctl')
->into(
table: 'users',
columns: ['id', 'name', 'email'],
trailing: 'TRAILING NULLCOLS'
);

$ctl = new ControlFileBuilder($loader);
$controlFile = $ctl->build();

expect($controlFile)->toBeString()
->and($controlFile)->toContain("INFILE '".__DIR__."/../data/users.dat'")
->and($controlFile)->toContain("INFILE '".__DIR__."/../data/roles.dat'");
});

test('it can build with bad file, discard file and discard max', function () {
$loader = new SQLLoader(['skip=1', 'load=2']);
$loader->inFile(__DIR__.'/../data/users.dat', badFile: 'users.bad', discardFile: 'users.dis', discardMax: '1')
->as('users.ctl')
->into(
table: 'users',
columns: ['id', 'name', 'email'],
trailing: 'TRAILING NULLCOLS'
);

$ctl = new ControlFileBuilder($loader);
$controlFile = $ctl->build();

expect($controlFile)->toBeString()
->and($controlFile)->toContain("BADFILE 'users.bad'")
->and($controlFile)->toContain("DISCARDFILE 'users.dis'")
->and($controlFile)->toContain('DISCARDMAX 1');
});

test('it can build table with formatting options', function () {
$loader = new SQLLoader(['skip=1', 'load=2']);
$loader->inFile(__DIR__.'/../data/users.dat')
->as('users.ctl')
->into(
table: 'users',
columns: ['id', 'name', 'email'],
trailing: 'TRAILING NULLCOLS',
formatOptions: [
'DATE_FORMAT "YYYY-MM-DD"',
'TIMESTAMP FORMAT "YYYY-MM-DD HH24:MI:SS"',
'TIMESTAMP WITH TIME ZONE "YYYY-MM-DD HH24:MI:SS TZH:TZM"',
'TIMESTAMP WITH LOCAL TIME ZONE "YYYY-MM-DD HH24:MI:SS"',
]
);

$ctl = new ControlFileBuilder($loader);
$controlFile = $ctl->build();

expect($controlFile)->toBeString()
->and($controlFile)->toContain('DATE_FORMAT "YYYY-MM-DD"')
->and($controlFile)->toContain('TIMESTAMP FORMAT "YYYY-MM-DD HH24:MI:SS')
->and($controlFile)->toContain('TIMESTAMP WITH TIME ZONE "YYYY-MM-DD HH24:MI:SS TZH:TZM"')
->and($controlFile)->toContain('TIMESTAMP WITH LOCAL TIME ZONE "YYYY-MM-DD HH24:MI:SS"');
});

test('it can build using begin data', function () {
$loader = new SQLLoader(['skip=1', 'load=2']);
$loader->as('users.ctl')
->into(
table: 'users',
columns: ['id', 'name', 'email'],
)
->beginData([
['1', 'name-1', 'email-1'],
['2', 'name-2', 'email-2'],
['3', 'name,-2', 'email"-2'],
]);

$ctl = new ControlFileBuilder($loader);
$controlFile = $ctl->build();

expect($controlFile)->toBeString()
->and($controlFile)->toContain('INFILE *')
->and($controlFile)->toContain("BEGINDATA\n")
->and($controlFile)->toContain('1,name-1,email-1')
->and($controlFile)->toContain('2,name-2,email-2')
->and($controlFile)->toContain('3,"name,-2","email""-2"');
describe('Control File Builder', function () {
test('it can build a control file', function () {
$loader = new SQLLoader(['skip=1', 'load=2']);
$loader->inFile(__DIR__.'/../data/users.dat')
->as('users.ctl')
->into(
table: 'users',
columns: ['id', 'name', 'email'],
trailing: 'TRAILING NULLCOLS'
);

$ctl = new ControlFileBuilder($loader);
$controlFile = $ctl->build();

expect($controlFile)->toBeString()
->and($controlFile)->toContain('OPTIONS(skip=1, load=2)')
->and($controlFile)->toContain("INFILE '".__DIR__."/../data/users.dat'")
->and($controlFile)->toContain('APPEND')
->and($controlFile)->toContain('INTO TABLE "USERS"')
->and($controlFile)->toContain("FIELDS TERMINATED BY ','")
->and($controlFile)->toContain('OPTIONALLY')
->and($controlFile)->toContain("ENCLOSED BY '\"'")
->and($controlFile)->toContain('TRAILING NULLCOLS')
->and($controlFile)->toContain('(')
->and($controlFile)->toContain('id,')
->and($controlFile)->toContain('name,')
->and($controlFile)->toContain('email')
->and($controlFile)->toContain(')');
});

test('it can build multiple input files', function () {
$loader = new SQLLoader(['skip=1', 'load=2']);
$loader->inFile(__DIR__.'/../data/users.dat')
->inFile(__DIR__.'/../data/roles.dat')
->as('users.ctl')
->into(
table: 'users',
columns: ['id', 'name', 'email'],
trailing: 'TRAILING NULLCOLS'
);

$ctl = new ControlFileBuilder($loader);
$controlFile = $ctl->build();

expect($controlFile)->toBeString()
->and($controlFile)->toContain("INFILE '".__DIR__."/../data/users.dat'")
->and($controlFile)->toContain("INFILE '".__DIR__."/../data/roles.dat'");
});

test('it can build with bad file, discard file and discard max', function () {
$loader = new SQLLoader(['skip=1', 'load=2']);
$loader->inFile(__DIR__.'/../data/users.dat', badFile: 'users.bad', discardFile: 'users.dis', discardMax: '1')
->as('users.ctl')
->into(
table: 'users',
columns: ['id', 'name', 'email'],
trailing: 'TRAILING NULLCOLS'
);

$ctl = new ControlFileBuilder($loader);
$controlFile = $ctl->build();

expect($controlFile)->toBeString()
->and($controlFile)->toContain("BADFILE 'users.bad'")
->and($controlFile)->toContain("DISCARDFILE 'users.dis'")
->and($controlFile)->toContain('DISCARDMAX 1');
});

test('it can build table with formatting options', function () {
$loader = new SQLLoader(['skip=1', 'load=2']);
$loader->inFile(__DIR__.'/../data/users.dat')
->as('users.ctl')
->into(
table: 'users',
columns: ['id', 'name', 'email'],
trailing: 'TRAILING NULLCOLS',
formatOptions: [
'DATE_FORMAT "YYYY-MM-DD"',
'TIMESTAMP FORMAT "YYYY-MM-DD HH24:MI:SS"',
'TIMESTAMP WITH TIME ZONE "YYYY-MM-DD HH24:MI:SS TZH:TZM"',
'TIMESTAMP WITH LOCAL TIME ZONE "YYYY-MM-DD HH24:MI:SS"',
]
);

$ctl = new ControlFileBuilder($loader);
$controlFile = $ctl->build();

expect($controlFile)->toBeString()
->and($controlFile)->toContain('DATE_FORMAT "YYYY-MM-DD"')
->and($controlFile)->toContain('TIMESTAMP FORMAT "YYYY-MM-DD HH24:MI:SS')
->and($controlFile)->toContain('TIMESTAMP WITH TIME ZONE "YYYY-MM-DD HH24:MI:SS TZH:TZM"')
->and($controlFile)->toContain('TIMESTAMP WITH LOCAL TIME ZONE "YYYY-MM-DD HH24:MI:SS"');
});

test('it can build using begin data', function () {
$loader = new SQLLoader(['skip=1', 'load=2']);
$loader->as('users.ctl')
->into(
table: 'users',
columns: ['id', 'name', 'email'],
)
->beginData([
['1', 'name-1', 'email-1'],
['2', 'name-2', 'email-2'],
['3', 'name,-2', 'email"-2'],
]);

$ctl = new ControlFileBuilder($loader);
$controlFile = $ctl->build();

expect($controlFile)->toBeString()
->and($controlFile)->toContain('INFILE *')
->and($controlFile)->toContain("BEGINDATA\n")
->and($controlFile)->toContain('1,name-1,email-1')
->and($controlFile)->toContain('2,name-2,email-2')
->and($controlFile)->toContain('3,"name,-2","email""-2"');
});
});
102 changes: 52 additions & 50 deletions tests/Feature/CsvFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,67 @@

use Yajra\SQLLoader\CsvFile;

test('it can create a blank csv file', function () {
$file = CsvFile::blank(storage_path('test.csv'));
describe('CSV File', function () {
test('it can create a blank csv file', function () {
$file = CsvFile::blank(storage_path('test.csv'));

expect($file)->toBeString()
->and($file)->toBe(storage_path('test.csv'))
->and(file_exists($file))->toBeTrue();
});
expect($file)->toBeString()
->and($file)->toBe(storage_path('test.csv'))
->and(file_exists($file))->toBeTrue();
});

test('it can create a csv file', function () {
$file = CsvFile::create(storage_path('test.csv'));
test('it can create a csv file', function () {
$file = CsvFile::create(storage_path('test.csv'));

expect($file)->toBeString()
->and($file)->toBe(storage_path('test.csv'))
->and(file_exists($file))->toBeTrue();
});
expect($file)->toBeString()
->and($file)->toBe(storage_path('test.csv'))
->and(file_exists($file))->toBeTrue();
});

test('it can make a csv file with array content', function () {
$file = CsvFile::make(storage_path('test.csv'), 'w');
$file->append(['id', 'name', 'email']);
test('it can make a csv file with array content', function () {
$file = CsvFile::make(storage_path('test.csv'), 'w');
$file->append(['id', 'name', 'email']);

expect($file)->toBeInstanceOf(CsvFile::class)
->and($file->isEmpty())->toBeFalse()
->and($file->get())->toContain('id,name,email'.PHP_EOL);
});
expect($file)->toBeInstanceOf(CsvFile::class)
->and($file->isEmpty())->toBeFalse()
->and($file->get())->toContain('id,name,email'.PHP_EOL);
});

test('it can have a csv header', function () {
$file = CsvFile::make(storage_path('test.csv'), 'w');
$file->headers(['id', 'name', 'email']);
test('it can have a csv header', function () {
$file = CsvFile::make(storage_path('test.csv'), 'w');
$file->headers(['id', 'name', 'email']);

expect($file)->toBeInstanceOf(CsvFile::class)
->and($file->isEmpty())->toBeFalse()
->and($file->get())->toContain('id,name,email'.PHP_EOL);
});
expect($file)->toBeInstanceOf(CsvFile::class)
->and($file->isEmpty())->toBeFalse()
->and($file->get())->toContain('id,name,email'.PHP_EOL);
});

test('it can insert data using array', function () {
$file = CsvFile::make(storage_path('test.csv'), 'w');
$file->headers(['id', 'name', 'email']);
$file->insert([
['1', 'John Doe', 'email@example.com'],
['2', 'Jane Doe', 'e'],
['3', 'Jane, Doe', '3'],
['3', 'Jane" Doe', '3'],
]);
test('it can insert data using array', function () {
$file = CsvFile::make(storage_path('test.csv'), 'w');
$file->headers(['id', 'name', 'email']);
$file->insert([
['1', 'John Doe', 'email@example.com'],
['2', 'Jane Doe', 'e'],
['3', 'Jane, Doe', '3'],
['3', 'Jane" Doe', '3'],
]);

$content = $file->get();
$content = $file->get();

expect($file)->toBeInstanceOf(CsvFile::class)
->and($file->isEmpty())->toBeFalse()
->and($content)->toContain('1,"John Doe",email@example.com'.PHP_EOL)
->and($content)->toContain('2,"Jane Doe",e'.PHP_EOL)
->and($content)->toContain('3,"Jane, Doe",3'.PHP_EOL)
->and($content)->toContain('3,"Jane"" Doe",3'.PHP_EOL);
});
expect($file)->toBeInstanceOf(CsvFile::class)
->and($file->isEmpty())->toBeFalse()
->and($content)->toContain('1,"John Doe",email@example.com'.PHP_EOL)
->and($content)->toContain('2,"Jane Doe",e'.PHP_EOL)
->and($content)->toContain('3,"Jane, Doe",3'.PHP_EOL)
->and($content)->toContain('3,"Jane"" Doe",3'.PHP_EOL);
});

test('it can sanitize headers', function () {
$headers = CsvFile::make(__DIR__.'/../data/bad-header.dat', 'r')->getHeaders();
expect($headers)->toBe([
'NAME',
'EMAIL',
'PHONE',
]);
test('it can sanitize headers', function () {
$headers = CsvFile::make(__DIR__.'/../data/bad-header.dat', 'r')->getHeaders();
expect($headers)->toBe([
'NAME',
'EMAIL',
'PHONE',
]);
});
});
Loading
Loading