Skip to content

Commit 6581002

Browse files
committed
added compileIgnore + tests
1 parent 2dbe643 commit 6581002

7 files changed

Lines changed: 243 additions & 82 deletions

File tree

system/Database/BaseBuilder.php

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,21 @@ class BaseBuilder
209209
protected $canLimitWhereUpdates = true;
210210

211211
/**
212-
* Ignore errors on insert statements
213-
* for example for duplicate keys.
212+
* Ignore data that cause certain
213+
* exceptions, for example in case of
214+
* duplicate keys.
214215
*
215216
* @var bool
216217
*/
217-
protected $insertIgnore = false;
218+
protected $ignore = false;
219+
220+
/**
221+
* Specifies which sql statements
222+
* support the ignore option.
223+
*
224+
* @var array
225+
*/
226+
protected $supportedIgnoreStatements = [];
218227

219228
//--------------------------------------------------------------------
220229

@@ -267,15 +276,16 @@ public function getBinds(): array
267276
/**
268277
* Ignore
269278
*
270-
* Set ignore Flag for next insert query.
279+
* Set ignore Flag for next insert,
280+
* update or delete query.
271281
*
272282
* @param bool $ignore
273283
*
274284
* @return BaseBuilder
275285
*/
276286
public function ignore(bool $ignore = true)
277287
{
278-
$this->insertIgnore = $ignore;
288+
$this->ignore = $ignore;
279289

280290
return $this;
281291
}
@@ -1692,7 +1702,7 @@ public function insertBatch($set = null, $escape = null, $batchSize = 100, $test
16921702
}
16931703
}
16941704

1695-
$this->insertIgnore = false;
1705+
$this->ignore = false;
16961706

16971707
if (! $testing)
16981708
{
@@ -1717,7 +1727,7 @@ public function insertBatch($set = null, $escape = null, $batchSize = 100, $test
17171727
*/
17181728
protected function _insertBatch($table, $keys, $values)
17191729
{
1720-
return 'INSERT INTO ' . $table . ' (' . implode(', ', $keys) . ') VALUES ' . implode(', ', $values);
1730+
return 'INSERT ' . $this->compileIgnore('insert') . 'INTO ' . $table . ' (' . implode(', ', $keys) . ') VALUES ' . implode(', ', $values);
17211731
}
17221732

17231733
//--------------------------------------------------------------------
@@ -1844,7 +1854,7 @@ public function insert($set = null, $escape = null, $test = false)
18441854
), array_keys($this->QBSet), array_values($this->QBSet)
18451855
);
18461856

1847-
$this->insertIgnore = false;
1857+
$this->ignore = false;
18481858

18491859
if ($test === false)
18501860
{
@@ -1901,7 +1911,7 @@ protected function validateInsert()
19011911
*/
19021912
protected function _insert($table, array $keys, array $unescapedKeys)
19031913
{
1904-
return 'INSERT INTO ' . $table . ' (' . implode(', ', $keys) . ') VALUES (' . implode(', ', $unescapedKeys) . ')';
1914+
return 'INSERT ' . $this->compileIgnore('insert') . 'INTO ' . $table . ' (' . implode(', ', $keys) . ') VALUES (' . implode(', ', $unescapedKeys) . ')';
19051915
}
19061916

19071917
//--------------------------------------------------------------------
@@ -2016,6 +2026,8 @@ public function getCompiledUpdate($reset = true)
20162026
* @param mixed $where
20172027
* @param integer $limit
20182028
* @param boolean $test Are we testing the code?
2029+
*
2030+
* @throws DatabaseException
20192031
*
20202032
* @return boolean TRUE on success, FALSE on failure
20212033
*/
@@ -2048,6 +2060,8 @@ public function update($set = null, $where = null, int $limit = null, $test = fa
20482060

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

2063+
$this->ignore = false;
2064+
20512065
if (! $test)
20522066
{
20532067
$this->resetWrite();
@@ -2080,12 +2094,14 @@ public function update($set = null, $where = null, int $limit = null, $test = fa
20802094
*/
20812095
protected function _update($table, $values)
20822096
{
2097+
$valStr = [];
2098+
20832099
foreach ($values as $key => $val)
20842100
{
2085-
$valstr[] = $key . ' = ' . $val;
2101+
$valStr[] = $key . ' = ' . $val;
20862102
}
20872103

2088-
return 'UPDATE ' . $table . ' SET ' . implode(', ', $valstr)
2104+
return 'UPDATE ' . $this->compileIgnore('update') . $table . ' SET ' . implode(', ', $valStr)
20892105
. $this->compileWhereHaving('QBWhere')
20902106
. $this->compileOrderBy()
20912107
. ($this->QBLimit ? $this->_limit(' ') : '');
@@ -2193,6 +2209,8 @@ public function updateBatch($set = null, $index = null, $batchSize = 100, $retur
21932209
$this->QBWhere = [];
21942210
}
21952211

2212+
$this->ignore = false;
2213+
21962214
$this->resetWrite();
21972215

21982216
return $returnSQL ? $savedSQL : $affected_rows;
@@ -2214,6 +2232,8 @@ public function updateBatch($set = null, $index = null, $batchSize = 100, $retur
22142232
protected function _updateBatch($table, $values, $index)
22152233
{
22162234
$ids = [];
2235+
$final = [];
2236+
22172237
foreach ($values as $key => $val)
22182238
{
22192239
$ids[] = $val[$index];
@@ -2237,7 +2257,7 @@ protected function _updateBatch($table, $values, $index)
22372257

22382258
$this->where($index . ' IN(' . implode(',', $ids) . ')', null, false);
22392259

2240-
return 'UPDATE ' . $table . ' SET ' . substr($cases, 0, -2) . $this->compileWhereHaving('QBWhere');
2260+
return 'UPDATE ' . $this->compileIgnore('update') . $table . ' SET ' . substr($cases, 0, -2) . $this->compileWhereHaving('QBWhere');
22412261
}
22422262

22432263
//--------------------------------------------------------------------
@@ -2306,6 +2326,8 @@ public function emptyTable($test = false)
23062326

23072327
$sql = $this->_delete($table);
23082328

2329+
$this->ignore = false;
2330+
23092331
if ($test)
23102332
{
23112333
return $sql;
@@ -2422,6 +2444,8 @@ public function delete($where = '', $limit = null, $reset_data = true, $returnSQ
24222444

24232445
$sql = $this->_delete($table);
24242446

2447+
$this->ignore = false;
2448+
24252449
if (! empty($limit))
24262450
{
24272451
$this->QBLimit = $limit;
@@ -2496,7 +2520,7 @@ public function decrement(string $column, int $value = 1)
24962520
*/
24972521
protected function _delete($table)
24982522
{
2499-
return 'DELETE FROM ' . $table . $this->compileWhereHaving('QBWhere')
2523+
return 'DELETE ' . $this->compileIgnore('delete') . 'FROM ' . $table . $this->compileWhereHaving('QBWhere')
25002524
. ($this->QBLimit ? ' LIMIT ' . $this->QBLimit : '');
25012525
}
25022526

@@ -2611,6 +2635,32 @@ protected function compileSelect($select_override = false)
26112635
return $sql;
26122636
}
26132637

2638+
//--------------------------------------------------------------------
2639+
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 = '';
2653+
2654+
if(
2655+
$this->ignore &&
2656+
isset($this->supportedIgnoreStatements[$statement])
2657+
) {
2658+
$sql = trim($this->supportedIgnoreStatements[$statement]).' ';
2659+
}
2660+
2661+
return $sql;
2662+
}
2663+
26142664
//--------------------------------------------------------------------
26152665

26162666
/**

system/Database/MySQLi/Builder.php

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -51,40 +51,16 @@ class Builder extends BaseBuilder
5151
*/
5252
protected $escapeChar = '`';
5353

54-
//--------------------------------------------------------------------
55-
56-
/**
57-
* Insert batch statement
58-
*
59-
* Generates a platform-specific insert string from the supplied data.
60-
*
61-
* @param string $table Table name
62-
* @param array $keys INSERT keys
63-
* @param array $values INSERT values
64-
*
65-
* @return string
66-
*/
67-
protected function _insertBatch($table, $keys, $values)
68-
{
69-
return 'INSERT ' . ($this->insertIgnore ? 'IGNORE ' : '') . 'INTO ' . $table . ' (' . implode(', ', $keys) . ') VALUES ' . implode(', ', $values);
70-
}
71-
72-
//--------------------------------------------------------------------
73-
7454
/**
75-
* Insert statement
76-
*
77-
* Generates a platform-specific insert string from the supplied data
78-
*
79-
* @param string $table The table name
80-
* @param array $keys The insert keys
81-
* @param array $unescapedKeys The insert values
55+
* Specifies which sql statements
56+
* support the ignore option.
8257
*
83-
* @return string
58+
* @var array
8459
*/
85-
protected function _insert($table, array $keys, array $unescapedKeys)
86-
{
87-
return 'INSERT ' . ($this->insertIgnore ? 'IGNORE ' : '') . 'INTO ' . $table . ' (' . implode(', ', $keys) . ') VALUES (' . implode(', ', $unescapedKeys) . ')';
88-
}
60+
protected $supportedIgnoreStatements = [
61+
'update' => 'IGNORE',
62+
'insert' => 'IGNORE',
63+
'delete' => 'IGNORE'
64+
];
8965

9066
}

system/Database/Postgre/Builder.php

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,41 @@ class Builder extends BaseBuilder
5555
'RANDOM()',
5656
];
5757

58+
/**
59+
* Specifies which sql statements
60+
* support the ignore option.
61+
*
62+
* @var array
63+
*/
64+
protected $supportedIgnoreStatements = [
65+
'insert' => 'ON CONFLICT DO NOTHING'
66+
];
67+
5868
//--------------------------------------------------------------------
5969

60-
/**
70+
/**
71+
* Compile Ignore Statement
72+
*
73+
* Checks if the ignore option is supported by
74+
* the Database Driver for the specific statement.
75+
*
76+
* @param string $statement
77+
*
78+
* @return string
79+
*/
80+
protected function compileIgnore(string $statement) {
81+
$sql = parent::compileIgnore($statement);
82+
83+
if(!empty($sql)) {
84+
$sql = ' '.trim($sql);
85+
}
86+
87+
return $sql;
88+
}
89+
90+
//--------------------------------------------------------------------
91+
92+
/**
6193
* ORDER BY
6294
*
6395
* @param string $orderby
@@ -96,6 +128,8 @@ public function orderBy($orderby, $direction = '', $escape = null)
96128
*
97129
* @param string $column
98130
* @param integer $value
131+
*
132+
* @throws DatabaseException
99133
*
100134
* @return boolean
101135
*/
@@ -115,6 +149,8 @@ public function increment(string $column, int $value = 1)
115149
*
116150
* @param string $column
117151
* @param integer $value
152+
*
153+
* @throws DatabaseException
118154
*
119155
* @return boolean
120156
*/
@@ -127,6 +163,42 @@ public function decrement(string $column, int $value = 1)
127163
return $this->db->query($sql, $this->binds, false);
128164
}
129165

166+
//--------------------------------------------------------------------
167+
168+
/**
169+
* Insert batch statement
170+
*
171+
* Generates a platform-specific insert string from the supplied data.
172+
*
173+
* @param string $table Table name
174+
* @param array $keys INSERT keys
175+
* @param array $values INSERT values
176+
*
177+
* @return string
178+
*/
179+
protected function _insertBatch($table, $keys, $values)
180+
{
181+
return 'INSERT INTO ' . $table . ' (' . implode(', ', $keys) . ') VALUES ' . implode(', ', $values) . $this->compileIgnore('insert');
182+
}
183+
184+
//--------------------------------------------------------------------
185+
186+
/**
187+
* Insert statement
188+
*
189+
* Generates a platform-specific insert string from the supplied data
190+
*
191+
* @param string $table The table name
192+
* @param array $keys The insert keys
193+
* @param array $unescapedKeys The insert values
194+
*
195+
* @return string
196+
*/
197+
protected function _insert($table, array $keys, array $unescapedKeys)
198+
{
199+
return 'INSERT INTO ' . $table . ' (' . implode(', ', $keys) . ') VALUES (' . implode(', ', $unescapedKeys) . ')' . $this->compileIgnore('insert');
200+
}
201+
130202
//--------------------------------------------------------------------
131203

132204
/**

0 commit comments

Comments
 (0)