88use Illuminate \Contracts \Process \ProcessResult ;
99use Illuminate \Support \Facades \File ;
1010use Illuminate \Support \Facades \Process ;
11+ use Illuminate \Support \Facades \Schema ;
1112use Illuminate \Support \Facades \Storage ;
1213use Illuminate \Support \Str ;
1314use InvalidArgumentException ;
@@ -96,7 +97,7 @@ public function into(
9697 ?string $ when = null ,
9798 ): static {
9899 if (empty ($ columns )) {
99- $ columns = $ this ->defaultColumns ;
100+ $ columns = $ this ->buildDefaultColumns ( $ table , $ columns ) ;
100101 }
101102
102103 if (! $ formatOptions ) {
@@ -388,7 +389,6 @@ public function withHeaders(): static
388389 {
389390 $ this ->options (['skip=1 ' ]);
390391 $ headers = CsvFile::make ($ this ->inputFiles [0 ]->path , 'r ' )->getHeaders ();
391- $ headers = array_map (fn ($ field ) => strtoupper ('" ' .$ field .'" ' ), $ headers );
392392 $ this ->defaultColumns = $ headers ;
393393
394394 return $ this ;
@@ -400,4 +400,44 @@ public function dateFormat(string $format): static
400400
401401 return $ this ;
402402 }
403+
404+ protected function buildDefaultColumns (string $ table , array $ columns ): array
405+ {
406+ $ columns = $ this ->defaultColumns ;
407+ $ schemaColumns = collect (Schema::connection (config ('sql-loader.connection ' ))->getColumns ($ table ));
408+
409+ $ dates = $ schemaColumns ->filter (function ($ column ) {
410+ return in_array ($ column ['type ' ], [
411+ 'DATE ' ,
412+ 'DATETIME ' ,
413+ 'TIMESTAMP ' ,
414+ 'TIMESTAMP(6) ' ,
415+ ]);
416+ })->pluck ('name ' )->toArray ();
417+
418+ $ booleans = $ schemaColumns ->filter (function ($ column ) {
419+ return $ column ['nullable ' ] === 'N ' && $ column ['type ' ] === 'CHAR ' ;
420+ })->pluck ('name ' )->toArray ();
421+
422+ foreach ($ columns as $ key => $ column ) {
423+ $ column = strtoupper ($ column );
424+
425+ if (in_array ($ column , $ dates )) {
426+ $ columns [$ key ] = "\"$ column \" DATE " ;
427+
428+ continue ;
429+ }
430+
431+ if (in_array ($ column , $ booleans )) {
432+ $ default = $ schemaColumns ->where ('name ' , $ column )->first ()['default ' ];
433+ $ columns [$ key ] = "\"$ column \" \"DECODE(: $ column, '', $ default, : $ column) \"" ;
434+
435+ continue ;
436+ }
437+
438+ $ columns [$ key ] = "\"$ column \"" ;
439+ }
440+
441+ return $ columns ;
442+ }
403443}
0 commit comments