Skip to content

Commit 8bd9ae9

Browse files
committed
added ignore flag
1 parent 1e6a0dc commit 8bd9ae9

3 files changed

Lines changed: 103 additions & 1 deletion

File tree

system/Database/BaseBuilder.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,14 @@ class BaseBuilder
208208
*/
209209
protected $canLimitWhereUpdates = true;
210210

211+
/**
212+
* Ignore errors on insert statements
213+
* for example for duplicate keys.
214+
*
215+
* @var bool
216+
*/
217+
protected $insertIgnore = false;
218+
211219
//--------------------------------------------------------------------
212220

213221
/**
@@ -254,6 +262,22 @@ public function getBinds(): array
254262
return $this->binds;
255263
}
256264

265+
//--------------------------------------------------------------------
266+
267+
/**
268+
* Ignore
269+
*
270+
* Set ignore Flag for next insert query.
271+
*
272+
* @return BaseBuilder
273+
*/
274+
public function ignore()
275+
{
276+
$this->insertIgnore = true;
277+
278+
return $this;
279+
}
280+
257281
//--------------------------------------------------------------------
258282

259283
/**
@@ -1666,6 +1690,8 @@ public function insertBatch($set = null, $escape = null, $batchSize = 100, $test
16661690
}
16671691
}
16681692

1693+
$this->insertIgnore = false;
1694+
16691695
if (! $testing)
16701696
{
16711697
$this->resetWrite();
@@ -1758,6 +1784,8 @@ public function setInsertBatch($key, $value = '', $escape = null)
17581784
*
17591785
* @param boolean $reset TRUE: reset QB values; FALSE: leave QB values alone
17601786
*
1787+
* @throws DatabaseException
1788+
*
17611789
* @return string
17621790
*/
17631791
public function getCompiledInsert($reset = true)
@@ -1791,6 +1819,8 @@ public function getCompiledInsert($reset = true)
17911819
* @param array $set An associative array of insert values
17921820
* @param boolean $escape Whether to escape values and identifiers
17931821
* @param boolean $test Used when running tests
1822+
*
1823+
* @throws DatabaseException
17941824
*
17951825
* @return BaseResult|Query|false
17961826
*/
@@ -1812,6 +1842,8 @@ public function insert($set = null, $escape = null, $test = false)
18121842
), array_keys($this->QBSet), array_values($this->QBSet)
18131843
);
18141844

1845+
$this->insertIgnore = false;
1846+
18151847
if ($test === false)
18161848
{
18171849
$this->resetWrite();

system/Database/MySQLi/Builder.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,40 @@ 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+
74+
/**
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
82+
*
83+
* @return string
84+
*/
85+
protected function _insert($table, array $keys, array $unescapedKeys)
86+
{
87+
return 'INSERT ' . ($this->insertIgnore ? 'IGNORE' : '') . ' INTO ' . $table . ' (' . implode(', ', $keys) . ') VALUES (' . implode(', ', $unescapedKeys) . ')';
88+
}
89+
5490
}

system/Database/SQLite3/Builder.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,40 @@ protected function _truncate($table)
103103
return 'DELETE FROM ' . $table;
104104
}
105105

106-
//--------------------------------------------------------------------
106+
//--------------------------------------------------------------------
107+
108+
/**
109+
* Insert batch statement
110+
*
111+
* Generates a platform-specific insert string from the supplied data.
112+
*
113+
* @param string $table Table name
114+
* @param array $keys INSERT keys
115+
* @param array $values INSERT values
116+
*
117+
* @return string
118+
*/
119+
protected function _insertBatch($table, $keys, $values)
120+
{
121+
return 'INSERT ' . ($this->insertIgnore ? 'OR IGNORE' : '') . ' INTO ' . $table . ' (' . implode(', ', $keys) . ') VALUES ' . implode(', ', $values);
122+
}
123+
124+
//--------------------------------------------------------------------
125+
126+
/**
127+
* Insert statement
128+
*
129+
* Generates a platform-specific insert string from the supplied data
130+
*
131+
* @param string $table The table name
132+
* @param array $keys The insert keys
133+
* @param array $unescapedKeys The insert values
134+
*
135+
* @return string
136+
*/
137+
protected function _insert($table, array $keys, array $unescapedKeys)
138+
{
139+
return 'INSERT ' . ($this->insertIgnore ? 'OR IGNORE' : '') . ' INTO ' . $table . ' (' . implode(', ', $keys) . ') VALUES (' . implode(', ', $unescapedKeys) . ')';
140+
}
107141

108142
}

0 commit comments

Comments
 (0)