From 7d08dd27651dfea564b21f0ebccf65e5952728b2 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 13 May 2015 09:15:18 +0200 Subject: [PATCH] DbConnection: Adjust insert and update to support custom type definitions This strips the custom insert and update implementataions in DbUserBackend down so that it does not need to do such low level stuff... refs #8826 --- .../Authentication/User/DbUserBackend.php | 78 ++++++------------- library/Icinga/Data/Db/DbConnection.php | 48 +++++++++++- 2 files changed, 67 insertions(+), 59 deletions(-) diff --git a/library/Icinga/Authentication/User/DbUserBackend.php b/library/Icinga/Authentication/User/DbUserBackend.php index 441e6cdf3..36402d862 100644 --- a/library/Icinga/Authentication/User/DbUserBackend.php +++ b/library/Icinga/Authentication/User/DbUserBackend.php @@ -96,76 +96,44 @@ class DbUserBackend extends DbRepository implements UserBackendInterface * Insert a table row with the given data * * @param string $table - * @param array $data + * @param array $bind */ - public function insert($table, array $data) + public function insert($table, array $bind) { - $newData['created_at'] = date('Y-m-d H:i:s'); - $newData = $this->requireStatementColumns($table, $data); - - $values = array(); - foreach ($newData as $column => $_) { - $values[] = ':' . $column; - } - - $sql = 'INSERT INTO ' - . $this->prependTablePrefix($table) - . ' (' . join(', ', array_keys($newData)) . ') ' - . 'VALUES (' . join(', ', $values) . ')'; - $statement = $this->ds->getDbAdapter()->prepare($sql); - - foreach ($newData as $column => $value) { - $type = PDO::PARAM_STR; - if ($column === 'password_hash') { - $type = PDO::PARAM_LOB; - } elseif ($column === 'active') { - $type = PDO::PARAM_INT; - } - - $statement->bindValue(':' . $column, $value, $type); - } - - $statement->execute(); + $bind['created_at'] = date('Y-m-d H:i:s'); + $this->ds->insert( + $this->prependTablePrefix($table), + $this->requireStatementColumns($table, $bind), + array( + 'active' => PDO::PARAM_INT, + 'password_hash' => PDO::PARAM_LOB + ) + ); } /** * Update table rows with the given data, optionally limited by using a filter * * @param string $table - * @param array $data + * @param array $bind * @param Filter $filter */ - public function update($table, array $data, Filter $filter = null) + public function update($table, array $bind, Filter $filter = null) { - $newData['last_modified'] = date('Y-m-d H:i:s'); - $newData = $this->requireStatementColumns($table, $data); + $bind['last_modified'] = date('Y-m-d H:i:s'); if ($filter) { $this->requireFilter($table, $filter); } - $set = array(); - foreach ($newData as $column => $_) { - $set[] = $column . ' = :' . $column; - } - - $sql = 'UPDATE ' - . $this->prependTablePrefix($table) - . ' SET ' . join(', ', $set) - . ($filter ? ' WHERE ' . $this->ds->renderFilter($filter) : ''); - $statement = $this->ds->getDbAdapter()->prepare($sql); - - foreach ($newData as $column => $value) { - $type = PDO::PARAM_STR; - if ($column === 'password_hash') { - $type = PDO::PARAM_LOB; - } elseif ($column === 'active') { - $type = PDO::PARAM_INT; - } - - $statement->bindValue(':' . $column, $value, $type); - } - - $statement->execute(); + $this->ds->update( + $this->prependTablePrefix($table), + $this->requireStatementColumns($table, $bind), + $filter, + array( + 'active' => PDO::PARAM_INT, + 'password_hash' => PDO::PARAM_LOB + ) + ); } /** diff --git a/library/Icinga/Data/Db/DbConnection.php b/library/Icinga/Data/Db/DbConnection.php index d0d2ddf98..ebe6e5743 100644 --- a/library/Icinga/Data/Db/DbConnection.php +++ b/library/Icinga/Data/Db/DbConnection.php @@ -271,28 +271,68 @@ class DbConnection implements Selectable, Extensible, Updatable, Reducible /** * Insert a table row with the given data * + * 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. + * * @param string $table * @param array $bind + * @param array $types * * @return int The number of affected rows */ - public function insert($table, array $bind) + public function insert($table, array $bind, array $types = array()) { - return $this->dbAdapter->insert($table, $bind); + $values = array(); + foreach ($bind as $column => $_) { + $values[] = ':' . $column; + } + + $sql = 'INSERT INTO ' . $table + . ' (' . join(', ', array_keys($bind)) . ') ' + . 'VALUES (' . join(', ', $values) . ')'; + $statement = $this->dbAdapter->prepare($sql); + + foreach ($bind as $column => $value) { + $type = isset($types[$column]) ? $types[$column] : PDO::PARAM_STR; + $statement->bindValue(':' . $column, $value, $type); + } + + $statement->execute(); + return $statement->rowCount(); } /** * Update table rows with the given data, optionally limited by using a filter * + * 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. + * * @param string $table * @param array $bind * @param Filter $filter + * @param array $types * * @return int The number of affected rows */ - public function update($table, array $bind, Filter $filter = null) + public function update($table, array $bind, Filter $filter = null, array $types = array()) { - return $this->dbAdapter->update($table, $bind, $filter ? $this->renderFilter($filter) : ''); + $set = array(); + foreach ($bind as $column => $_) { + $set[] = $column . ' = :' . $column; + } + + $sql = 'UPDATE ' . $table + . ' SET ' . join(', ', $set) + . ($filter ? ' WHERE ' . $this->renderFilter($filter) : ''); + $statement = $this->dbAdapter->prepare($sql); + + foreach ($bind as $column => $value) { + $type = isset($types[$column]) ? $types[$column] : PDO::PARAM_STR; + $statement->bindValue(':' . $column, $value, $type); + } + + $statement->execute(); + return $statement->rowCount(); } /**