@@ -34,6 +34,10 @@ abstract class BaseHelper
3434 * @var int
3535 */
3636 private $ allowedWhereInQueryNumber = 0 ;
37+ /**
38+ * @var array
39+ */
40+ private $ ignoredTables = ['migrations ' , 'password_resets ' , 'failed_jobs ' ];
3741
3842 /**
3943 * @return int
@@ -162,6 +166,24 @@ public function setTables($tables)
162166 return $ this ;
163167 }
164168
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+
165187 /**
166188 * @return string
167189 */
@@ -178,6 +200,24 @@ protected function setQuery($query)
178200 $ this ->query = $ query ;
179201 }
180202
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+
181221 /**
182222 * @return array
183223 * @author karam mustafa
@@ -278,13 +318,13 @@ public function setSavedItems($savedItems)
278318 */
279319 public function checkIfQueryAllowed ($ ids , $ callbackIfPassed = null , $ chunkCountAllowed = null )
280320 {
281- if (! isset ($ chunckCountAllowed )) {
321+ if (!isset ($ chunckCountAllowed )) {
282322 $ chunkCountAllowed = $ this ->getAllowedWhereInQueryNumber ();
283323 }
284324
285325 $ items = [];
286326 $ lists = collect ($ ids )->chunk ($ chunkCountAllowed + 1 );
287- if (! is_null ($ callbackIfPassed )) {
327+ if (!is_null ($ callbackIfPassed )) {
288328 foreach ($ lists as $ index => $ list ) {
289329 $ items [] = $ callbackIfPassed ($ list , $ index );
290330 }
@@ -317,7 +357,7 @@ public function executeAll($callback = null)
317357 return DB ::select (DB ::raw ($ this ->getQuery ()));
318358 }
319359
320- // if we are not , then execute what evere this statement.
360+ // otherwise , then execute what ever this statement.
321361 DB ::statement ($ this ->getQuery ());
322362
323363 return $ this ;
@@ -326,6 +366,27 @@ public function executeAll($callback = null)
326366 }
327367 }
328368
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+
329390 /**
330391 * clear all inserted data in class properties.
331392 * this function ignore the savedDataItems property,
@@ -379,6 +440,25 @@ protected function loopThrough($arr, $callback)
379440 }
380441 }
381442
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+
382462 /**
383463 * return the saved items.
384464 *
@@ -389,4 +469,16 @@ public function done()
389469 {
390470 return $ this ->getSavedItems ();
391471 }
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+ }
392484}
0 commit comments