@@ -34,6 +34,10 @@ abstract class BaseHelper
34
34
* @var int
35
35
*/
36
36
private $ allowedWhereInQueryNumber = 0 ;
37
+ /**
38
+ * @var array
39
+ */
40
+ private $ ignoredTables = ['migrations ' , 'password_resets ' , 'failed_jobs ' ];
37
41
38
42
/**
39
43
* @return int
@@ -162,6 +166,24 @@ public function setTables($tables)
162
166
return $ this ;
163
167
}
164
168
169
+ /**
170
+ * @return BaseHelper
171
+ */
172
+ public function setAllTablesFromDatabase ()
173
+ {
174
+ $ columnName = 'Tables_in_ ' .env ('DB_DATABASE ' );
175
+
176
+ $ this ->loopThrough (
177
+ $ this ->getAllTablesFromDatabase ()->getSavedItems (),
178
+ function ($ key , $ item ) use ($ columnName ) {
179
+ if (!in_array ($ item ->$ columnName , $ this ->ignoredTables )) {
180
+ $ this ->setTables ($ item ->$ columnName );
181
+ }
182
+ });
183
+
184
+ return $ this ;
185
+ }
186
+
165
187
/**
166
188
* @return string
167
189
*/
@@ -178,6 +200,24 @@ protected function setQuery($query)
178
200
$ this ->query = $ query ;
179
201
}
180
202
203
+ /**
204
+ * @param string $query
205
+ */
206
+ protected function appendToQuery ($ query )
207
+ {
208
+ $ this ->query .= $ query ;
209
+ }
210
+
211
+ /**
212
+ * set the query string in the first of strings.
213
+ *
214
+ * @param string $query
215
+ */
216
+ protected function unshiftInQuery ($ query )
217
+ {
218
+ $ this ->query = $ query ." " .$ this ->query ;
219
+ }
220
+
181
221
/**
182
222
* @return array
183
223
* @author karam mustafa
@@ -278,13 +318,13 @@ public function setSavedItems($savedItems)
278
318
*/
279
319
public function checkIfQueryAllowed ($ ids , $ callbackIfPassed = null , $ chunkCountAllowed = null )
280
320
{
281
- if (! isset ($ chunckCountAllowed )) {
321
+ if (!isset ($ chunckCountAllowed )) {
282
322
$ chunkCountAllowed = $ this ->getAllowedWhereInQueryNumber ();
283
323
}
284
324
285
325
$ items = [];
286
326
$ lists = collect ($ ids )->chunk ($ chunkCountAllowed + 1 );
287
- if (! is_null ($ callbackIfPassed )) {
327
+ if (!is_null ($ callbackIfPassed )) {
288
328
foreach ($ lists as $ index => $ list ) {
289
329
$ items [] = $ callbackIfPassed ($ list , $ index );
290
330
}
@@ -317,7 +357,7 @@ public function executeAll($callback = null)
317
357
return DB ::select (DB ::raw ($ this ->getQuery ()));
318
358
}
319
359
320
- // if we are not , then execute what evere this statement.
360
+ // otherwise , then execute what ever this statement.
321
361
DB ::statement ($ this ->getQuery ());
322
362
323
363
return $ this ;
@@ -326,6 +366,27 @@ public function executeAll($callback = null)
326
366
}
327
367
}
328
368
369
+ /**
370
+ * execute query statements without preparing them, this will help us ignore the laravel query preparing.
371
+ *
372
+ * @return BaseHelper
373
+ * @throws \Exception
374
+ * @author karam mustafa
375
+ */
376
+ public function executeWithoutPrepare ()
377
+ {
378
+ try {
379
+ $ this ->executeAll (function () {
380
+ DB ::unprepared ($ this ->getQuery ());
381
+ });
382
+
383
+ return $ this ;
384
+
385
+ } catch (\Exception $ e ) {
386
+ throw new \Exception ($ e ->getMessage ());
387
+ }
388
+ }
389
+
329
390
/**
330
391
* clear all inserted data in class properties.
331
392
* this function ignore the savedDataItems property,
@@ -379,6 +440,25 @@ protected function loopThrough($arr, $callback)
379
440
}
380
441
}
381
442
443
+ /**
444
+ * loop through specific array and each iteration will execute by a callback.
445
+ *
446
+ * @param callable $callback
447
+ *
448
+ * @return void
449
+ * @author karam mustafa
450
+ */
451
+ protected function disableAndEnableForeignChecks ($ callback )
452
+ {
453
+ $ this ->unshiftInQuery ("SET FOREIGN_KEY_CHECKS=0; " );
454
+
455
+ if (is_callable ($ callback )) {
456
+ $ callback ();
457
+ }
458
+
459
+ $ this ->appendToQuery ("SET FOREIGN_KEY_CHECKS=1; " );
460
+ }
461
+
382
462
/**
383
463
* return the saved items.
384
464
*
@@ -389,4 +469,16 @@ public function done()
389
469
{
390
470
return $ this ->getSavedItems ();
391
471
}
472
+
473
+ /**
474
+ * fetch all database tables.
475
+ *
476
+ * @author karam mustafa
477
+ */
478
+ public function getAllTablesFromDatabase ()
479
+ {
480
+ $ this ->setSavedItems (DB ::select ('SHOW TABLES ' ));
481
+
482
+ return $ this ;
483
+ }
392
484
}
0 commit comments