Skip to content

Commit 6c0f747

Browse files
committed
fixed
1 parent 32d2573 commit 6c0f747

4 files changed

Lines changed: 62 additions & 166 deletions

File tree

system/Database/BaseBuilder.php

Lines changed: 62 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,15 @@ class BaseBuilder
156156
*/
157157
protected $QBWhereGroupCount = 0;
158158

159+
/**
160+
* Ignore data that cause certain
161+
* exceptions, for example in case of
162+
* duplicate keys.
163+
*
164+
* @var boolean
165+
*/
166+
protected $QBIgnore = false;
167+
159168
/**
160169
* A reference to the database connection.
161170
*
@@ -208,21 +217,12 @@ class BaseBuilder
208217
*/
209218
protected $canLimitWhereUpdates = true;
210219

211-
/**
212-
* Ignore data that cause certain
213-
* exceptions, for example in case of
214-
* duplicate keys.
215-
*
216-
* @var bool
217-
*/
218-
protected $ignore = false;
219-
220-
/**
221-
* Specifies which sql statements
222-
* support the ignore option.
223-
*
224-
* @var array
225-
*/
220+
/**
221+
* Specifies which sql statements
222+
* support the ignore option.
223+
*
224+
* @var array
225+
*/
226226
protected $supportedIgnoreStatements = [];
227227

228228
//--------------------------------------------------------------------
@@ -271,24 +271,24 @@ public function getBinds(): array
271271
return $this->binds;
272272
}
273273

274-
//--------------------------------------------------------------------
274+
//--------------------------------------------------------------------
275275

276-
/**
277-
* Ignore
278-
*
279-
* Set ignore Flag for next insert,
280-
* update or delete query.
281-
*
282-
* @param bool $ignore
283-
*
284-
* @return BaseBuilder
285-
*/
286-
public function ignore(bool $ignore = true)
287-
{
288-
$this->ignore = $ignore;
276+
/**
277+
* Ignore
278+
*
279+
* Set ignore Flag for next insert,
280+
* update or delete query.
281+
*
282+
* @param boolean $ignore
283+
*
284+
* @return BaseBuilder
285+
*/
286+
public function ignore(bool $ignore = true)
287+
{
288+
$this->QBIgnore = $ignore;
289289

290-
return $this;
291-
}
290+
return $this;
291+
}
292292

293293
//--------------------------------------------------------------------
294294

@@ -1702,8 +1702,6 @@ public function insertBatch($set = null, $escape = null, $batchSize = 100, $test
17021702
}
17031703
}
17041704

1705-
$this->ignore = false;
1706-
17071705
if (! $testing)
17081706
{
17091707
$this->resetWrite();
@@ -1796,8 +1794,8 @@ public function setInsertBatch($key, $value = '', $escape = null)
17961794
*
17971795
* @param boolean $reset TRUE: reset QB values; FALSE: leave QB values alone
17981796
*
1799-
* @throws DatabaseException
1800-
*
1797+
* @throws DatabaseException
1798+
*
18011799
* @return string
18021800
*/
18031801
public function getCompiledInsert($reset = true)
@@ -1831,8 +1829,8 @@ public function getCompiledInsert($reset = true)
18311829
* @param array $set An associative array of insert values
18321830
* @param boolean $escape Whether to escape values and identifiers
18331831
* @param boolean $test Used when running tests
1834-
*
1835-
* @throws DatabaseException
1832+
*
1833+
* @throws DatabaseException
18361834
*
18371835
* @return BaseResult|Query|false
18381836
*/
@@ -1854,8 +1852,6 @@ public function insert($set = null, $escape = null, $test = false)
18541852
), array_keys($this->QBSet), array_values($this->QBSet)
18551853
);
18561854

1857-
$this->ignore = false;
1858-
18591855
if ($test === false)
18601856
{
18611857
$this->resetWrite();
@@ -2026,8 +2022,8 @@ public function getCompiledUpdate($reset = true)
20262022
* @param mixed $where
20272023
* @param integer $limit
20282024
* @param boolean $test Are we testing the code?
2029-
*
2030-
* @throws DatabaseException
2025+
*
2026+
* @throws DatabaseException
20312027
*
20322028
* @return boolean TRUE on success, FALSE on failure
20332029
*/
@@ -2060,8 +2056,6 @@ public function update($set = null, $where = null, int $limit = null, $test = fa
20602056

20612057
$sql = $this->_update($this->QBFrom[0], $this->QBSet);
20622058

2063-
$this->ignore = false;
2064-
20652059
if (! $test)
20662060
{
20672061
$this->resetWrite();
@@ -2094,7 +2088,7 @@ public function update($set = null, $where = null, int $limit = null, $test = fa
20942088
*/
20952089
protected function _update($table, $values)
20962090
{
2097-
$valStr = [];
2091+
$valStr = [];
20982092

20992093
foreach ($values as $key => $val)
21002094
{
@@ -2209,8 +2203,6 @@ public function updateBatch($set = null, $index = null, $batchSize = 100, $retur
22092203
$this->QBWhere = [];
22102204
}
22112205

2212-
$this->ignore = false;
2213-
22142206
$this->resetWrite();
22152207

22162208
return $returnSQL ? $savedSQL : $affected_rows;
@@ -2231,7 +2223,7 @@ public function updateBatch($set = null, $index = null, $batchSize = 100, $retur
22312223
*/
22322224
protected function _updateBatch($table, $values, $index)
22332225
{
2234-
$ids = [];
2226+
$ids = [];
22352227
$final = [];
22362228

22372229
foreach ($values as $key => $val)
@@ -2326,8 +2318,6 @@ public function emptyTable($test = false)
23262318

23272319
$sql = $this->_delete($table);
23282320

2329-
$this->ignore = false;
2330-
23312321
if ($test)
23322322
{
23332323
return $sql;
@@ -2444,8 +2434,6 @@ public function delete($where = '', $limit = null, $reset_data = true, $returnSQ
24442434

24452435
$sql = $this->_delete($table);
24462436

2447-
$this->ignore = false;
2448-
24492437
if (! empty($limit))
24502438
{
24512439
$this->QBLimit = $limit;
@@ -2635,31 +2623,31 @@ protected function compileSelect($select_override = false)
26352623
return $sql;
26362624
}
26372625

2638-
//--------------------------------------------------------------------
2626+
//--------------------------------------------------------------------
26392627

2640-
/**
2641-
* Compile Ignore Statement
2642-
*
2643-
* Checks if the ignore option is supported by
2644-
* the Database Driver for the specific statement.
2645-
*
2646-
* @param string $statement
2647-
*
2648-
* @return string
2649-
*/
2650-
protected function compileIgnore(string $statement)
2651-
{
2652-
$sql = '';
2628+
/**
2629+
* Compile Ignore Statement
2630+
*
2631+
* Checks if the ignore option is supported by
2632+
* the Database Driver for the specific statement.
2633+
*
2634+
* @param string $statement
2635+
*
2636+
* @return string
2637+
*/
2638+
public function compileIgnore(string $statement)
2639+
{
2640+
$sql = '';
26532641

2654-
if(
2655-
$this->ignore &&
2656-
isset($this->supportedIgnoreStatements[$statement])
2657-
) {
2658-
$sql = trim($this->supportedIgnoreStatements[$statement]).' ';
2659-
}
2642+
if ($this->QBIgnore &&
2643+
isset($this->supportedIgnoreStatements[$statement])
2644+
)
2645+
{
2646+
$sql = trim($this->supportedIgnoreStatements[$statement]) . ' ';
2647+
}
26602648

2661-
return $sql;
2662-
}
2649+
return $sql;
2650+
}
26632651

26642652
//--------------------------------------------------------------------
26652653

@@ -2983,6 +2971,7 @@ protected function resetWrite()
29832971
'QBOrderBy' => [],
29842972
'QBKeys' => [],
29852973
'QBLimit' => false,
2974+
'QBIgnore' => false,
29862975
]);
29872976
}
29882977

tests/system/Database/Builder/DeleteTest.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,4 @@ public function testDelete()
3535
$this->assertEquals($expectedBinds, $builder->getBinds());
3636
}
3737

38-
//--------------------------------------------------------------------
39-
40-
public function testDeleteIgnore()
41-
{
42-
$builder = $this->db->table('jobs');
43-
44-
$answer = $builder->ignore()->delete(['id' => 1], null, true, true);
45-
46-
if($this->db->getPlatform() == 'MySQLi') {
47-
$expectedSQL = 'DELETE IGNORE FROM "jobs" WHERE "id" = :id:';
48-
} else {
49-
$expectedSQL = 'DELETE FROM "jobs" WHERE "id" = :id:';
50-
}
51-
52-
$expectedBinds = [
53-
'id' => [
54-
1,
55-
true,
56-
],
57-
];
58-
59-
$this->assertEquals($expectedSQL, str_replace("\n", ' ', $answer));
60-
$this->assertEquals($expectedBinds, $builder->getBinds());
61-
}
6238
}

tests/system/Database/Builder/InsertTest.php

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -44,46 +44,6 @@ public function testSimpleInsert()
4444
$this->assertEquals($expectedBinds, $builder->getBinds());
4545
}
4646

47-
//--------------------------------------------------------------------
48-
49-
public function testInsertIgnore()
50-
{
51-
$builder = $this->db->table('jobs');
52-
53-
$insertData = [
54-
'id' => 1,
55-
'name' => 'Grocery Sales',
56-
];
57-
58-
$builder->ignore()->insert($insertData, true, true);
59-
60-
switch($this->db->getPlatform()) {
61-
case 'MySQLi':
62-
$expectedSQL = 'INSERT IGNORE INTO "jobs" ("id", "name") VALUES (1, \'Grocery Sales\')';
63-
break;
64-
case 'postgre':
65-
$expectedSQL = 'INSERT INTO "jobs" ("id", "name") VALUES (1, \'Grocery Sales\') ON CONFLICT DO NOTHING';
66-
break;
67-
case 'SQLite3':
68-
$expectedSQL = 'INSERT OR IGNORE INTO "jobs" ("id", "name") VALUES (1, \'Grocery Sales\') ';
69-
breaK;
70-
}
71-
72-
$expectedBinds = [
73-
'id' => [
74-
1,
75-
true,
76-
],
77-
'name' => [
78-
'Grocery Sales',
79-
true,
80-
],
81-
];
82-
83-
$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getCompiledInsert()));
84-
$this->assertEquals($expectedBinds, $builder->getBinds());
85-
}
86-
8747
//--------------------------------------------------------------------
8848

8949
public function testThrowsExceptionOnNoValuesSet()

tests/system/Database/Builder/UpdateTest.php

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -41,35 +41,6 @@ public function testUpdate()
4141
$this->assertEquals($expectedBinds, $builder->getBinds());
4242
}
4343

44-
//--------------------------------------------------------------------
45-
46-
public function testUpdateIgnore()
47-
{
48-
$builder = new BaseBuilder('jobs', $this->db);
49-
50-
$builder->where('id', 1)->ignore()->update(['name' => 'Programmer'], null, null, true);
51-
52-
if($this->db->getPlatform() == 'MySQLi') {
53-
$expectedSQL = 'UPDATE IGNORE "jobs" SET "name" = \'Programmer\' WHERE "id" = 1';
54-
} else {
55-
$expectedSQL = 'UPDATE "jobs" SET "name" = \'Programmer\' WHERE "id" = 1';
56-
}
57-
58-
$expectedBinds = [
59-
'id' => [
60-
1,
61-
true,
62-
],
63-
'name' => [
64-
'Programmer',
65-
true,
66-
],
67-
];
68-
69-
$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getCompiledUpdate()));
70-
$this->assertEquals($expectedBinds, $builder->getBinds());
71-
}
72-
7344
//--------------------------------------------------------------------
7445

7546
public function testUpdateInternalWhereAndLimit()

0 commit comments

Comments
 (0)