@@ -22,12 +22,16 @@ class SQLLoader
2222 /** @var TableDefinition[] */
2323 public array $ tables = [];
2424
25- protected array $ defaultColumns = [];
26-
2725 public Mode $ mode = Mode::APPEND ;
2826
2927 public ?string $ controlFile = null ;
3028
29+ public array $ beginData = [];
30+
31+ public ?string $ connection = null ;
32+
33+ protected array $ defaultColumns = [];
34+
3135 protected ?string $ disk = null ;
3236
3337 protected ?string $ logPath = null ;
@@ -38,42 +42,12 @@ class SQLLoader
3842
3943 protected string $ logs = '' ;
4044
41- public array $ beginData = [];
42-
4345 protected string $ dateFormat = 'YYYY-MM-DD"T"HH24:MI:SS."000000Z" ' ;
4446
4547 public function __construct (public array $ options = [])
4648 {
4749 }
4850
49- /**
50- * Set SQL Loader options.
51- */
52- public function options (array $ options ): static
53- {
54- $ this ->options = $ options ;
55-
56- return $ this ;
57- }
58-
59- /**
60- * Define input file to load data from.
61- */
62- public function inFile (
63- string $ path ,
64- ?string $ badFile = null ,
65- ?string $ discardFile = null ,
66- ?string $ discardMax = null
67- ): static {
68- if (! File::exists ($ path ) && $ path !== '* ' ) {
69- throw new InvalidArgumentException ("File [ {$ path }] does not exist. " );
70- }
71-
72- $ this ->inputFiles [] = new InputFile ($ path , $ badFile , $ discardFile , $ discardMax );
73-
74- return $ this ;
75- }
76-
7751 /**
7852 * Define mode to use.
7953 */
@@ -114,6 +88,49 @@ public function into(
11488 return $ this ;
11589 }
11690
91+ protected function buildDefaultColumns (string $ table , array $ columns ): array
92+ {
93+ $ columns = $ this ->defaultColumns ;
94+ $ schemaColumns = collect (Schema::connection (config ('sql-loader.connection ' ))->getColumns ($ table ));
95+
96+ $ dates = $ schemaColumns ->filter (fn ($ column ) => in_array ($ column ['type ' ], [
97+ 'DATE ' ,
98+ 'DATETIME ' ,
99+ 'TIMESTAMP ' ,
100+ 'TIMESTAMP(6) ' ,
101+ ]))->pluck ('name ' )->toArray ();
102+
103+ $ booleans = $ schemaColumns ->filter (fn ($ column ) => $ column ['nullable ' ] === 'N ' && $ column ['type ' ] === 'CHAR ' )->pluck ('name ' )->toArray ();
104+
105+ foreach ($ columns as $ key => $ column ) {
106+ $ column = strtoupper ((string ) $ column );
107+
108+ if (in_array ($ column , $ dates )) {
109+ $ columns [$ key ] = "\"$ column \" DATE " ;
110+
111+ continue ;
112+ }
113+
114+ if (in_array ($ column , $ booleans )) {
115+ $ default = $ schemaColumns ->where ('name ' , $ column )->first ()['default ' ];
116+ $ columns [$ key ] = "\"$ column \" \"DECODE(: $ column, '', $ default, : $ column) \"" ;
117+
118+ continue ;
119+ }
120+
121+ $ columns [$ key ] = "\"$ column \"" ;
122+ }
123+
124+ return $ columns ;
125+ }
126+
127+ public function connection (string $ connection ): static
128+ {
129+ $ this ->connection = $ connection ;
130+
131+ return $ this ;
132+ }
133+
117134 /**
118135 * Execute SQL Loader command.
119136 */
@@ -209,7 +226,7 @@ public function buildControlFile(): string
209226 */
210227 protected function buildTNS (): string
211228 {
212- return TnsBuilder::make ();
229+ return TnsBuilder::make ($ this -> getConnection () );
213230 }
214231
215232 /**
@@ -385,6 +402,24 @@ public function beginData(array $data): static
385402 return $ this ;
386403 }
387404
405+ /**
406+ * Define input file to load data from.
407+ */
408+ public function inFile (
409+ string $ path ,
410+ ?string $ badFile = null ,
411+ ?string $ discardFile = null ,
412+ ?string $ discardMax = null
413+ ): static {
414+ if (! File::exists ($ path ) && $ path !== '* ' ) {
415+ throw new InvalidArgumentException ("File [ {$ path }] does not exist. " );
416+ }
417+
418+ $ this ->inputFiles [] = new InputFile ($ path , $ badFile , $ discardFile , $ discardMax );
419+
420+ return $ this ;
421+ }
422+
388423 public function withHeaders (): static
389424 {
390425 $ this ->options (['skip=1 ' ]);
@@ -394,46 +429,25 @@ public function withHeaders(): static
394429 return $ this ;
395430 }
396431
397- public function dateFormat (string $ format ): static
432+ /**
433+ * Set SQL Loader options.
434+ */
435+ public function options (array $ options ): static
398436 {
399- $ this ->dateFormat = $ format ;
437+ $ this ->options = $ options ;
400438
401439 return $ this ;
402440 }
403441
404- protected function buildDefaultColumns (string $ table , array $ columns ): array
442+ public function dateFormat (string $ format ): static
405443 {
406- $ columns = $ this ->defaultColumns ;
407- $ schemaColumns = collect (Schema::connection (config ('sql-loader.connection ' ))->getColumns ($ table ));
408-
409- $ dates = $ schemaColumns ->filter (fn ($ column ) => in_array ($ column ['type ' ], [
410- 'DATE ' ,
411- 'DATETIME ' ,
412- 'TIMESTAMP ' ,
413- 'TIMESTAMP(6) ' ,
414- ]))->pluck ('name ' )->toArray ();
415-
416- $ booleans = $ schemaColumns ->filter (fn ($ column ) => $ column ['nullable ' ] === 'N ' && $ column ['type ' ] === 'CHAR ' )->pluck ('name ' )->toArray ();
417-
418- foreach ($ columns as $ key => $ column ) {
419- $ column = strtoupper ((string ) $ column );
420-
421- if (in_array ($ column , $ dates )) {
422- $ columns [$ key ] = "\"$ column \" DATE " ;
423-
424- continue ;
425- }
426-
427- if (in_array ($ column , $ booleans )) {
428- $ default = $ schemaColumns ->where ('name ' , $ column )->first ()['default ' ];
429- $ columns [$ key ] = "\"$ column \" \"DECODE(: $ column, '', $ default, : $ column) \"" ;
430-
431- continue ;
432- }
444+ $ this ->dateFormat = $ format ;
433445
434- $ columns [ $ key ] = "\" $ column \"" ;
435- }
446+ return $ this ;
447+ }
436448
437- return $ columns ;
449+ public function getConnection (): string
450+ {
451+ return $ this ->connection ?? config ('sql-loader.connection ' , 'oracle ' );
438452 }
439453}
0 commit comments