Skip to content

Commit

Permalink
added ignore flag
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Mar 25, 2019
1 parent 1e6a0dc commit 8bd9ae9
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
32 changes: 32 additions & 0 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ class BaseBuilder
*/
protected $canLimitWhereUpdates = true;

/**
* Ignore errors on insert statements
* for example for duplicate keys.
*
* @var bool
*/
protected $insertIgnore = false;

//--------------------------------------------------------------------

/**
Expand Down Expand Up @@ -254,6 +262,22 @@ public function getBinds(): array
return $this->binds;
}

//--------------------------------------------------------------------

/**
* Ignore
*
* Set ignore Flag for next insert query.
*
* @return BaseBuilder
*/
public function ignore()
{
$this->insertIgnore = true;

return $this;
}

//--------------------------------------------------------------------

/**
Expand Down Expand Up @@ -1666,6 +1690,8 @@ public function insertBatch($set = null, $escape = null, $batchSize = 100, $test
}
}

$this->insertIgnore = false;

if (! $testing)
{
$this->resetWrite();
Expand Down Expand Up @@ -1758,6 +1784,8 @@ public function setInsertBatch($key, $value = '', $escape = null)
*
* @param boolean $reset TRUE: reset QB values; FALSE: leave QB values alone
*
* @throws DatabaseException
*
* @return string
*/
public function getCompiledInsert($reset = true)
Expand Down Expand Up @@ -1791,6 +1819,8 @@ public function getCompiledInsert($reset = true)
* @param array $set An associative array of insert values
* @param boolean $escape Whether to escape values and identifiers
* @param boolean $test Used when running tests
*
* @throws DatabaseException
*
* @return BaseResult|Query|false
*/
Expand All @@ -1812,6 +1842,8 @@ public function insert($set = null, $escape = null, $test = false)
), array_keys($this->QBSet), array_values($this->QBSet)
);

$this->insertIgnore = false;

if ($test === false)
{
$this->resetWrite();
Expand Down
36 changes: 36 additions & 0 deletions system/Database/MySQLi/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,40 @@ class Builder extends BaseBuilder
*/
protected $escapeChar = '`';

//--------------------------------------------------------------------

/**
* Insert batch statement
*
* Generates a platform-specific insert string from the supplied data.
*
* @param string $table Table name
* @param array $keys INSERT keys
* @param array $values INSERT values
*
* @return string
*/
protected function _insertBatch($table, $keys, $values)
{
return 'INSERT ' . ($this->insertIgnore ? 'IGNORE' : '') . ' INTO ' . $table . ' (' . implode(', ', $keys) . ') VALUES ' . implode(', ', $values);
}

//--------------------------------------------------------------------

/**
* Insert statement
*
* Generates a platform-specific insert string from the supplied data
*
* @param string $table The table name
* @param array $keys The insert keys
* @param array $unescapedKeys The insert values
*
* @return string
*/
protected function _insert($table, array $keys, array $unescapedKeys)
{
return 'INSERT ' . ($this->insertIgnore ? 'IGNORE' : '') . ' INTO ' . $table . ' (' . implode(', ', $keys) . ') VALUES (' . implode(', ', $unescapedKeys) . ')';
}

}
36 changes: 35 additions & 1 deletion system/Database/SQLite3/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,40 @@ protected function _truncate($table)
return 'DELETE FROM ' . $table;
}

//--------------------------------------------------------------------
//--------------------------------------------------------------------

/**
* Insert batch statement
*
* Generates a platform-specific insert string from the supplied data.
*
* @param string $table Table name
* @param array $keys INSERT keys
* @param array $values INSERT values
*
* @return string
*/
protected function _insertBatch($table, $keys, $values)
{
return 'INSERT ' . ($this->insertIgnore ? 'OR IGNORE' : '') . ' INTO ' . $table . ' (' . implode(', ', $keys) . ') VALUES ' . implode(', ', $values);
}

//--------------------------------------------------------------------

/**
* Insert statement
*
* Generates a platform-specific insert string from the supplied data
*
* @param string $table The table name
* @param array $keys The insert keys
* @param array $unescapedKeys The insert values
*
* @return string
*/
protected function _insert($table, array $keys, array $unescapedKeys)
{
return 'INSERT ' . ($this->insertIgnore ? 'OR IGNORE' : '') . ' INTO ' . $table . ' (' . implode(', ', $keys) . ') VALUES (' . implode(', ', $unescapedKeys) . ')';
}

}

0 comments on commit 8bd9ae9

Please sign in to comment.