Skip to content

Commit

Permalink
Implemented update by conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 2, 2012
1 parent 9cd7aba commit e3c095f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/Cake/Model/Datasource/Database/Connection.php
Expand Up @@ -174,13 +174,13 @@ public function update($table, array $data, array $conditions = array(), $types
$sql = sprintf(
$sql,
$table,
rtrim(',', implode(' = ?,', $keys)),
implode(', ', array_map(function($k) {return $k . ' = ?';}, $keys)),
$conditions
);
if (!empty($type)) {
$types = $this->_mapTypes($keys, $types);
}
return $this->execute($sql, array_values($data), $types);
return $this->execute($sql, array_merge(array_values($data), $params), $types);
}

/**
Expand Down Expand Up @@ -298,6 +298,13 @@ protected function _mapTypes($columns, $types) {
return $types;
}

/**
* Simple condtions parser joind by AND
*
* @param array conditions key value array or list of conditions to be joined
* to construct a WHERE clause
* @return string
**/
protected function _parseConditions($conditions) {
$params = array();
if (empty($conditions)) {
Expand All @@ -310,8 +317,10 @@ protected function _parseConditions($conditions) {
$conds[] = $value;
continue;
}
$conds[] = $key . ' = ';
$conds[] = $key . ' = ?';
$params[] = $value;
}
return array(sprintf($sql, implode(' AND ', $conds)), $params);
}

}
Expand Up @@ -212,6 +212,11 @@ public function testInsertWithPositionalTypes() {
$this->assertEquals($data, $row);
}

/**
* Auxiliary function to insert a couple rows in a newly creted table
*
* @return void
**/
protected function _insertTwoRecords() {
$table = 'CREATE TEMPORARY TABLE things(id int, title varchar(20), body varchar(50))';
$this->connection->execute($table);
Expand Down Expand Up @@ -250,6 +255,11 @@ public function testStatementReusing() {
$this->assertEquals('another body', $row['body']);
}

/**
* Tests rows can be updated without specifying any coditions nor types
*
* @return void
**/
public function testUpdateWithoutConditionsNorTypes() {
$this->_insertTwoRecords();
$title = 'changed the title!';
Expand All @@ -259,4 +269,32 @@ public function testUpdateWithoutConditionsNorTypes() {
$this->assertCount(2, $result);
}

/**
* Tests it is possible to use key => value conditions for update
*
* @return void
**/
public function testUpdateWithConditionsNoTypes() {
$this->_insertTwoRecords();
$title = 'changed the title!';
$body = 'changed the body!';
$this->connection->update('things', array('title' => $title, 'body' => $body), array('id' => 2));
$result = $this->connection->execute('SELECT * FROM things WHERE title = ? AND body = ?', array($title, $body));
$this->assertCount(1, $result);
}

/**
* Tests it is possible to use key => value and stirng conditions for update
*
* @return void
**/
public function testUpdateWithConditionsCombinedNoTypes() {
$this->_insertTwoRecords();
$title = 'changed the title!';
$body = 'changed the body!';
$this->connection->update('things', array('title' => $title, 'body' => $body), array('id' => 2, 'body is not null'));
$result = $this->connection->execute('SELECT * FROM things WHERE title = ? AND body = ?', array($title, $body));
$this->assertCount(1, $result);
}

}

0 comments on commit e3c095f

Please sign in to comment.