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
This commit is contained in:
Johannes Meyer 2015-05-13 09:15:18 +02:00
parent aa466ae721
commit 7d08dd2765
2 changed files with 67 additions and 59 deletions

View File

@ -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
)
);
}
/**

View File

@ -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();
}
/**