mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-21 04:44:25 +02:00
DbConnection: Support raw sql expressions in methods insert() and update()
This commit is contained in:
parent
693a893cad
commit
fdb31e8d1c
@ -319,6 +319,7 @@ class DbConnection implements Selectable, Extensible, Updatable, Reducible, Insp
|
|||||||
/**
|
/**
|
||||||
* Insert a table row with the given data
|
* Insert a table row with the given data
|
||||||
*
|
*
|
||||||
|
* Note that the base implementation does not perform any quoting on the $table argument.
|
||||||
* Pass an array with a column name (the same as in $bind) and a PDO::PARAM_* constant as value
|
* Pass an array with a column name (the same as in $bind) and a PDO::PARAM_* constant as value
|
||||||
* as third parameter $types to define a different type than string for a particular column.
|
* as third parameter $types to define a different type than string for a particular column.
|
||||||
*
|
*
|
||||||
@ -330,13 +331,19 @@ class DbConnection implements Selectable, Extensible, Updatable, Reducible, Insp
|
|||||||
*/
|
*/
|
||||||
public function insert($table, array $bind, array $types = array())
|
public function insert($table, array $bind, array $types = array())
|
||||||
{
|
{
|
||||||
$values = array();
|
$columns = $values = array();
|
||||||
foreach ($bind as $column => $_) {
|
foreach ($bind as $column => $value) {
|
||||||
|
$columns[] = $column;
|
||||||
|
if ($value instanceof Zend_Db_Expr) {
|
||||||
|
$values[] = (string) $value;
|
||||||
|
unset($bind[$column]);
|
||||||
|
} else {
|
||||||
$values[] = ':' . $column;
|
$values[] = ':' . $column;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$sql = 'INSERT INTO ' . $table
|
$sql = 'INSERT INTO ' . $table
|
||||||
. ' (' . join(', ', array_keys($bind)) . ') '
|
. ' (' . join(', ', $columns) . ') '
|
||||||
. 'VALUES (' . join(', ', $values) . ')';
|
. 'VALUES (' . join(', ', $values) . ')';
|
||||||
$statement = $this->dbAdapter->prepare($sql);
|
$statement = $this->dbAdapter->prepare($sql);
|
||||||
|
|
||||||
@ -352,6 +359,7 @@ class DbConnection implements Selectable, Extensible, Updatable, Reducible, Insp
|
|||||||
/**
|
/**
|
||||||
* Update table rows with the given data, optionally limited by using a filter
|
* Update table rows with the given data, optionally limited by using a filter
|
||||||
*
|
*
|
||||||
|
* Note that the base implementation does not perform any quoting on the $table argument.
|
||||||
* Pass an array with a column name (the same as in $bind) and a PDO::PARAM_* constant as value
|
* Pass an array with a column name (the same as in $bind) and a PDO::PARAM_* constant as value
|
||||||
* as fourth parameter $types to define a different type than string for a particular column.
|
* as fourth parameter $types to define a different type than string for a particular column.
|
||||||
*
|
*
|
||||||
@ -365,9 +373,14 @@ class DbConnection implements Selectable, Extensible, Updatable, Reducible, Insp
|
|||||||
public function update($table, array $bind, Filter $filter = null, array $types = array())
|
public function update($table, array $bind, Filter $filter = null, array $types = array())
|
||||||
{
|
{
|
||||||
$set = array();
|
$set = array();
|
||||||
foreach ($bind as $column => $_) {
|
foreach ($bind as $column => $value) {
|
||||||
|
if ($value instanceof Zend_Db_Expr) {
|
||||||
|
$set[] = $column . ' = ' . $value;
|
||||||
|
unset($bind[$column]);
|
||||||
|
} else {
|
||||||
$set[] = $column . ' = :' . $column;
|
$set[] = $column . ' = :' . $column;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$sql = 'UPDATE ' . $table
|
$sql = 'UPDATE ' . $table
|
||||||
. ' SET ' . join(', ', $set)
|
. ' SET ' . join(', ', $set)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user