4
4
5
5
namespace Yajra \SQLLoader ;
6
6
7
+ use Exception ;
7
8
use Illuminate \Contracts \Filesystem \Filesystem ;
8
9
use Illuminate \Contracts \Process \ProcessResult ;
9
10
use Illuminate \Support \Facades \File ;
14
15
15
16
class SQLLoader
16
17
{
17
- protected string $ file ;
18
+ protected ? string $ file = null ;
18
19
19
20
protected Method $ method = Method::INSERT ;
20
21
@@ -24,17 +25,17 @@ class SQLLoader
24
25
25
26
protected string $ delimiter = ', ' ;
26
27
27
- protected string $ controlFile = '' ;
28
+ protected ? string $ controlFile = null ;
28
29
29
- protected string $ disk ;
30
+ protected ? string $ disk = null ;
30
31
31
- protected string $ logPath = '' ;
32
+ protected ? string $ logPath = null ;
32
33
33
- protected ProcessResult $ output ;
34
+ protected ? ProcessResult $ output = null ;
34
35
35
- protected string $ badFile ;
36
+ protected ? string $ badFile = null ;
36
37
37
- protected string $ discardFile ;
38
+ protected ? string $ discardFile = null ;
38
39
39
40
public function __construct (
40
41
protected array $ options = []
@@ -91,25 +92,29 @@ public function execute(): ProcessResult
91
92
throw new InvalidArgumentException ('Input file is required. ' );
92
93
}
93
94
94
- return $ this ->output = Process::command ($ this ->buildCommand ())->run ();
95
+ $ command = $ this ->buildCommand ();
96
+
97
+ $ this ->output = Process::command ($ command )->run ();
98
+
99
+ return $ this ->output ;
95
100
}
96
101
97
102
protected function buildCommand (): string
98
103
{
99
- $ file = ($ this ->controlFile ?: Str::uuid ()).'.ctl ' ;
100
- $ this ->getDisk ()->put ($ file , $ this ->buildControlFile ());
104
+ $ filesystem = $ this ->getDisk ();
101
105
106
+ $ file = ($ this ->controlFile ?: Str::uuid ()).'.ctl ' ;
107
+ $ filesystem ->put ($ file , $ this ->buildControlFile ());
102
108
$ tns = $ this ->buildTNS ();
103
109
$ binary = $ this ->getSqlLoaderBinary ();
104
- $ filePath = $ this -> getDisk () ->path ($ file );
110
+ $ filePath = $ filesystem ->path ($ file );
105
111
106
112
$ command = "$ binary userid= $ tns control= {$ filePath }" ;
107
- if (empty ($ this ->logPath )) {
108
- $ this ->logPath = str_replace ('.ctl ' , '.log ' , (string ) $ this ->getDisk ()->path ($ file ));
113
+ if (! $ this ->logPath ) {
114
+ $ this ->logPath = str_replace ('.ctl ' , '.log ' , $ filePath );
115
+ $ command .= " log= {$ this ->logPath }" ;
109
116
}
110
117
111
- $ command .= " log= {$ this ->logPath }" ;
112
-
113
118
return $ command ;
114
119
}
115
120
@@ -138,7 +143,7 @@ protected function buildControlFile(): string
138
143
->replace ('$BADFILE ' , $ this ->buildBadFile ())
139
144
->replace ('$DISCARDFILE ' , $ this ->buildDiscardFile ())
140
145
->replace ('$FILE ' , "INFILE ' {$ this ->file }' " )
141
- ->replace ('$METHOD ' , $ this ->method -> value )
146
+ ->replace ('$METHOD ' , $ this ->buildMethod () )
142
147
->replace ('$DELIMITER ' , $ this ->delimiter )
143
148
->replace ('$ENCLOSURE ' , $ this ->enclosure )
144
149
->replace ('$INSERTS ' , $ this ->buildInserts ())
@@ -224,28 +229,45 @@ public function logsTo(string $path): static
224
229
225
230
public function successful (): bool
226
231
{
227
- return $ this ->output ->exitCode () === 0 ;
232
+ if (is_null ($ this ->output )) {
233
+ return false ;
234
+ }
235
+
236
+ return $ this ->output ->successful ();
228
237
}
229
238
230
239
public function debug (): string
231
240
{
232
241
$ debug = 'Command: ' .PHP_EOL .$ this ->buildCommand ().PHP_EOL .PHP_EOL ;
233
- $ debug .= 'Output: ' .$ this ->output ().PHP_EOL .PHP_EOL ;
234
- $ debug .= 'Error Output: ' .PHP_EOL .$ this ->errorOutput ().PHP_EOL ;
235
- $ debug .= 'Exit Code: ' .$ this ->output ->exitCode ().PHP_EOL .PHP_EOL ;
236
242
$ debug .= 'Control File: ' .PHP_EOL .$ this ->buildControlFile ().PHP_EOL ;
237
243
244
+ if ($ this ->output ) {
245
+ $ debug .= 'Output: ' .$ this ->output ->output ().PHP_EOL .PHP_EOL ;
246
+ $ debug .= 'Error Output: ' .PHP_EOL .$ this ->output ->errorOutput ().PHP_EOL ;
247
+ $ debug .= 'Exit Code: ' .$ this ->output ->exitCode ().PHP_EOL .PHP_EOL ;
248
+ }
249
+
238
250
return $ debug ;
239
251
}
240
252
241
- public function output (): string
253
+ /**
254
+ * @throws \Exception
255
+ */
256
+ public function output (): ProcessResult
242
257
{
243
- return $ this ->output ->output ();
258
+ if (is_null ($ this ->output )) {
259
+ throw new Exception ('No output available ' );
260
+ }
261
+
262
+ return $ this ->output ;
244
263
}
245
264
265
+ /**
266
+ * @throws \Exception
267
+ */
246
268
public function errorOutput (): string
247
269
{
248
- return $ this ->output ->errorOutput ();
270
+ return $ this ->output () ->errorOutput ();
249
271
}
250
272
251
273
public function delimiter (string $ delimiter ): static
@@ -261,4 +283,12 @@ public function enclosure(string $enclosure): static
261
283
262
284
return $ this ;
263
285
}
286
+
287
+ protected function buildMethod (): string
288
+ {
289
+ return in_array ($ this ->method , [
290
+ Method::INSERT ,
291
+ Method::TRUNCATE ,
292
+ ]) ? Method::TRUNCATE ->value : $ this ->method ->value ;
293
+ }
264
294
}
0 commit comments