Skip to content

Commit 055c580

Browse files
committed
Add query to deleteBatch()
1 parent 70d25ed commit 055c580

5 files changed

Lines changed: 89 additions & 12 deletions

File tree

system/Database/BaseBuilder.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,8 +2125,8 @@ public function setQueryAsData($query, $alias = null, $columns = null): BaseBuil
21252125
}
21262126

21272127
$this->QBOptions['setQueryAsData'] = $query;
2128-
$this->QBKeys = $columns;
2129-
$this->QBSet = [];
2128+
$this->QBKeys = $columns;
2129+
$this->QBSet = [];
21302130
}
21312131

21322132
return $this;
@@ -2786,15 +2786,33 @@ public function delete($where = '', ?int $limit = null, bool $resetData = true)
27862786
/**
27872787
* Sets data and calls batchExecute to run queries
27882788
*
2789-
* @param array|object|null $set a dataset or select query
2790-
* @param array|RawSql|null $constraints
2789+
* @param array|BaseBuilder|object|RawSql|null $set a dataset
2790+
* @param array|RawSql|null $constraints
27912791
*
27922792
* @return false|int|string[] Number of rows affected or FALSE on failure, SQL array when testMode
27932793
*/
27942794
public function deleteBatch($set = null, $constraints = null, int $batchSize = 100)
27952795
{
2796+
$this->setQueryAsData($set);
2797+
27962798
$this->onConstraint($constraints);
27972799

2800+
if (isset($this->QBOptions['setQueryAsData'])) {
2801+
$sql = $this->_deleteBatch($this->QBFrom[0], $this->QBKeys, []);
2802+
2803+
if ($sql === '') {
2804+
return false; // @codeCoverageIgnore
2805+
}
2806+
2807+
if ($this->testMode === false) {
2808+
$this->db->query($sql, null, false);
2809+
}
2810+
2811+
$this->resetWrite();
2812+
2813+
return $this->testMode ? $sql : $this->db->affectedRows();
2814+
}
2815+
27982816
if ($set !== null && $set !== []) {
27992817
$this->setData($set, true);
28002818
}
@@ -2866,8 +2884,8 @@ protected function _deleteBatch(string $table, array $keys, array $values): stri
28662884
$this->QBOptions['sql'] = trim($sql);
28672885
}
28682886

2869-
if (isset($this->QBOptions['fromQuery'])) {
2870-
$data = $this->QBOptions['fromQuery'];
2887+
if (isset($this->QBOptions['setQueryAsData'])) {
2888+
$data = $this->QBOptions['setQueryAsData'];
28712889
} else {
28722890
$data = implode(
28732891
" UNION ALL\n",

system/Database/OCI8/Builder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,8 +495,8 @@ protected function _deleteBatch(string $table, array $keys, array $values): stri
495495
$this->QBOptions['sql'] = $sql;
496496
}
497497

498-
if (isset($this->QBOptions['fromQuery'])) {
499-
$data = $this->QBOptions['fromQuery'];
498+
if (isset($this->QBOptions['setQueryAsData'])) {
499+
$data = $this->QBOptions['setQueryAsData'];
500500
} else {
501501
$data = implode(
502502
" FROM DUAL UNION ALL\n",

system/Database/Postgre/Builder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,8 +469,8 @@ protected function _deleteBatch(string $table, array $keys, array $values): stri
469469
$this->QBOptions['sql'] = $sql;
470470
}
471471

472-
if (isset($this->QBOptions['fromQuery'])) {
473-
$data = $this->QBOptions['fromQuery'];
472+
if (isset($this->QBOptions['setQueryAsData'])) {
473+
$data = $this->QBOptions['setQueryAsData'];
474474
} else {
475475
$data = implode(
476476
" UNION ALL\n",

system/Database/SQLite3/Builder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ protected function _deleteBatch(string $table, array $keys, array $values): stri
257257
$this->QBOptions['sql'] = $sql;
258258
}
259259

260-
if (isset($this->QBOptions['fromQuery'])) {
261-
$data = $this->QBOptions['fromQuery'];
260+
if (isset($this->QBOptions['setQueryAsData'])) {
261+
$data = $this->QBOptions['setQueryAsData'];
262262
} else {
263263
$data = implode(
264264
" UNION ALL\n",

tests/system/Database/Live/DeleteTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use CodeIgniter\Database\Exceptions\DatabaseException;
1515
use CodeIgniter\Test\CIUnitTestCase;
1616
use CodeIgniter\Test\DatabaseTestTrait;
17+
use Config\Database;
1718
use Tests\Support\Database\Seeds\CITestSeeder;
1819

1920
/**
@@ -90,4 +91,62 @@ public function testDeleteBatch()
9091

9192
$this->dontSeeInDatabase('user', ['email' => 'ahmadinejad@world.com', 'name' => 'Ahmadinejad']);
9293
}
94+
95+
public function testDeleteBatchWithQuery()
96+
{
97+
$this->forge = Database::forge($this->DBGroup);
98+
99+
$this->forge->dropTable('user2', true);
100+
101+
$this->forge->addField([
102+
'id' => ['type' => 'INTEGER', 'constraint' => 3, 'auto_increment' => true],
103+
'name' => ['type' => 'VARCHAR', 'constraint' => 80],
104+
'email' => ['type' => 'VARCHAR', 'constraint' => 100],
105+
'country' => ['type' => 'VARCHAR', 'constraint' => 40],
106+
'created_at' => ['type' => 'DATETIME', 'null' => true],
107+
'updated_at' => ['type' => 'DATETIME', 'null' => true],
108+
'deleted_at' => ['type' => 'DATETIME', 'null' => true],
109+
'last_loggin' => ['type' => 'DATETIME', 'null' => true],
110+
])->addKey('id', true)->addUniqueKey('email')->addKey('country')->createTable('user2', true);
111+
112+
$data = [
113+
[
114+
'name' => 'Derek Jones',
115+
'email' => 'derek@world.com',
116+
'country' => 'France',
117+
],
118+
[
119+
'name' => 'Ahmadinejad does not match',
120+
'email' => 'ahmadinejad@world.com',
121+
'country' => 'Greece',
122+
],
123+
[
124+
'name' => 'Chris Martin',
125+
'email' => 'chris@world.com',
126+
'country' => 'Greece',
127+
],
128+
];
129+
$this->db->table('user2')->insertBatch($data);
130+
131+
$query = $this->db->table('user2')->select('email, name, country')->where('country', 'Greece');
132+
133+
$builder = $this->db->table('user')->setQueryAsData($query, 'alias');
134+
135+
if ($this->db->DBDriver === 'SQLite3') {
136+
$builder->onConstraint('email, name');
137+
} else {
138+
$builder->onConstraint('email');
139+
$builder->where('alias.name = user.name');
140+
}
141+
142+
$builder->deleteBatch();
143+
144+
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'email' => 'derek@world.com']);
145+
$this->seeInDatabase('user', ['name' => 'Ahmadinejad', 'email' => 'ahmadinejad@world.com']);
146+
$this->dontSeeInDatabase('user', ['name' => 'Chris Martin', 'email' => 'chris@world.com']);
147+
148+
$result = $this->db->table('user')->get()->getResultArray();
149+
150+
$this->forge->dropTable('user2', true);
151+
}
93152
}

0 commit comments

Comments
 (0)