Skip to content

Commit 1b28c26

Browse files
Merge pull request #3 from karam-mustafa/main
v1.1.0
2 parents ea1df2c + c415a33 commit 1b28c26

File tree

3 files changed

+137
-22
lines changed

3 files changed

+137
-22
lines changed

docs/delete.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
11
Delete helper
22
-----------
3+
#### drop multi tables
34
Suppose you want to drop multiple tables by their names in the database, you can do it with the following implementation.
45
```php
56

67
QueryHelperFacade::deleteInstance()
7-
->setTables(['posts', 'users' , 'comments']) // tables names.
8+
->setTables(['posts', 'users' , 'comments'])
9+
// tables names.
10+
// or you can select all tables that exists in the database from the below function
11+
// ->setAllTablesFromDatabase()
812
->dropMultiTables()
913
->executeAll();
1014

1115
```
16+
#### truncate multi tables
17+
18+
Suppose you want to truncate multiple tables by their names in the database, you can do it with the following implementation.
19+
```php
20+
21+
QueryHelperFacade::deleteInstance()
22+
// tables names.
23+
->setTables(['posts', 'users' , 'comments'])
24+
// or you can select all tables that exists in the database from the below function
25+
// ->setAllTablesFromDatabase()
26+
->truncateMultiTables()
27+
->executeWithoutPrepare();
28+
29+
```
30+
31+
#### delete large data
1232
If you have a table that contains a large number of data (maybe millions of records)
1333
and you want to delete everything contained in this table,
1434
if you execute the command with one query,
@@ -31,6 +51,8 @@ so this function divides the large query into more queries with an easy-to-use s
3151
return $table->where('id', '<', 100)->pluck('id')->toArray();
3252
}); // this will implement the delete process only on the result of this callback.
3353
```
54+
55+
#### drop all tables
3456
If you want to drop all tables from the database.
3557
```php
3658

src/Classes/BaseHelper.php

+95-3
Original file line numberDiff line numberDiff line change
@@ -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
}

src/Classes/DeleteHelper.php

+19-18
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,23 @@ public function dropMultiTables()
3333
return $this;
3434
}
3535

36+
/**
37+
* truncate multiple tables by their names in the database
38+
*
39+
* @return DeleteHelper
40+
* @author karam mustafa
41+
*/
42+
public function truncateMultiTables()
43+
{
44+
$this->disableAndEnableForeignChecks(function () {
45+
foreach ($this->tables as $index => $table) {
46+
$this->appendToQuery("TRUNCATE $table;");
47+
}
48+
});
49+
50+
return $this;
51+
}
52+
3653
/**
3754
* this function is divide the process of deleting data into a number of queries,
3855
* instead of making a large query that may take longer.
@@ -68,26 +85,10 @@ public function deleteLargeData(\Closure $callback = null)
6885
*/
6986
public function prepareDataBaseTablesToDrop()
7087
{
71-
$this->getAllTablesFromDatabase();
72-
73-
$columnName = 'Tables_in_'.env('DB_DATABASE');
74-
75-
foreach ($this->getSavedItems() as $table) {
76-
$this->setTables($table->$columnName);
77-
}
78-
79-
$this->dropMultiTables();
88+
$this->getAllTablesFromDatabase()
89+
->dropMultiTables();
8090

8191
return $this;
8292
}
8393

84-
/**
85-
* fetch all database tables.
86-
*
87-
* @author karam mustafa
88-
*/
89-
private function getAllTablesFromDatabase()
90-
{
91-
$this->setSavedItems(DB::select('SHOW TABLES'));
92-
}
9394
}

0 commit comments

Comments
 (0)