Skip to content

Commit

Permalink
added ModelQueryBuilder->update
Browse files Browse the repository at this point in the history
  • Loading branch information
woru committed Nov 27, 2013
1 parent f713a6b commit 159bcd5
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 9 deletions.
29 changes: 21 additions & 8 deletions lib/Ouzo/Db/Dialect/Dialect.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ public function select()
return $sql;
}

public function update()
{
if ($this->_query->type == QueryType::$UPDATE) {
$attributes = DialectUtil::buildAttributesPartForUpdate($this->_query->updateAttributes);
return " {$this->_query->table} set $attributes";
}
return '';
}

public function join()
{
$join = DialectUtil::buildJoinQuery($this->_query->joinClauses);
Expand Down Expand Up @@ -74,16 +83,20 @@ public function from()
public function buildQuery(Query $query)
{
$this->_query = $query;

$sql = DialectUtil::buildQueryPrefix($query->type);
$sql .= $this->select();
$sql .= $this->from();
$sql .= $this->join();
$sql .= $this->where();
$sql .= $this->order();
$sql .= $this->limit();
$sql .= $this->offset();

if ($query->type == QueryType::$UPDATE) {
$sql .= $this->update();
$sql .= $this->where();
} else {
$sql .= $this->select();
$sql .= $this->from();
$sql .= $this->join();
$sql .= $this->where();
$sql .= $this->order();
$sql .= $this->limit();
$sql .= $this->offset();
}
return rtrim($sql);
}
}
18 changes: 17 additions & 1 deletion lib/Ouzo/Db/Dialect/DialectUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@
use Ouzo\Db\QueryType;
use Ouzo\Db\WhereClause;
use Ouzo\Utilities\FluentArray;
use Ouzo\Utilities\Joiner;

class DialectUtil
{
public static function buildQueryPrefix($type)
{
return $type == QueryType::$DELETE ? 'DELETE' : 'SELECT';
if ($type == QueryType::$DELETE) {
return 'DELETE';
}else if ($type == QueryType::$UPDATE) {
return 'UPDATE';
} else {
return 'SELECT';
}
}

public static function _addAliases()
Expand Down Expand Up @@ -61,6 +68,15 @@ public static function buildJoinQuery($joinClauses)
return implode(" ", $elements);
}

public static function buildAttributesPartForUpdate($updateAttributes)
{
return Joiner::on(', ')->join(FluentArray::from($updateAttributes)
->keys()
->map(function ($column) {
return "$column = ?";
})->toArray());
}

public static function buildJoinQueryPart(JoinClause $joinClause)
{
return 'LEFT JOIN ' . $joinClause->joinTable . ' ON ' . $joinClause->getJoinColumnWithTable() . ' = ' . $joinClause->getJoinedColumnWithTable();
Expand Down
8 changes: 8 additions & 0 deletions lib/Ouzo/Db/ModelQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,14 @@ private function addJoin(Model $model, Relation $relation)
$this->_joinedRelations[] = $relation;
}

/**
* Runs an update query against a set of models
*/
public function update(array $attributes)
{
return QueryExecutor::prepare($this->_db, $this->_query)->update($attributes);
}

/**
* @return ModelQueryBuilder
*/
Expand Down
1 change: 1 addition & 0 deletions lib/Ouzo/Db/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Query
public $order;
public $limit;
public $offset;
public $updateAttributes = array();
public $whereClauses = array();
public $joinClauses = array();
public $type;
Expand Down
11 changes: 11 additions & 0 deletions lib/Ouzo/Db/QueryExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ public function delete()
return $this->_db->query->rowCount();
}

public function update(array $attributes)
{
$this->_query->type = QueryType::$UPDATE;
$this->_query->updateAttributes = $attributes;
$this->_buildQuery();
$this->_db->query($this->_sql, $this->_boundValues);
return $this->_db->query->rowCount();
}

public function count()
{
$this->_query->type = QueryType::$COUNT;
Expand Down Expand Up @@ -140,6 +149,8 @@ private static function isEmptyResult($whereClauses)

private function _addBindValues()
{
$this->_addBindValue(array_values($this->_query->updateAttributes));

foreach ($this->_query->whereClauses as $whereClause) {
$this->_addBindValuesFromWhereClause($whereClause);
}
Expand Down
1 change: 1 addition & 0 deletions lib/Ouzo/Db/QueryType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ class QueryType
static $SELECT = 1;
static $COUNT = 2;
static $DELETE = 3;
static $UPDATE = 4;
}
19 changes: 19 additions & 0 deletions test/lib/Ouzo/Db/Dialect/PostgresDialectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,23 @@ public function shouldReturnSelectWithMultipleJoins()
$expected = 'SELECT * FROM products LEFT JOIN categories ON categories.id_category = products.id_category LEFT JOIN orders ON orders.id = products.id_product WHERE id = ?';
$this->assertEquals($expected, $sql);
}

/**
* @test
*/
public function shouldBuildUpdateQuery()
{
//given
$query = new Query();
$query->table = 'products';
$query->type = QueryType::$UPDATE;
$query->updateAttributes = array('col1' => 'val1', 'col2' => 'val2');
$query->where(array('col1' => 'prev1', 'col2' => 'prev2'));

//when
$sql = $this->dialect->buildQuery($query);

//then
$this->assertEquals("UPDATE products set col1 = ?, col2 = ? WHERE col1 = ? AND col2 = ?", $sql);
}
}
15 changes: 15 additions & 0 deletions test/lib/Ouzo/Db/ModelQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -757,4 +757,19 @@ public function shouldCloneBuilder()
$this->assertEquals($product, $query->fetch());
}

/**
* @test
*/
public function shouldUpdateModel()
{
//given
$product = Product::create(array('name' => 'bob'));

//when
$affectedRows = Product::where(array('name' => 'bob'))->update(array('name' => 'eric'));

//then
$this->assertEquals('eric', $product->reload()->name);
$this->assertEquals(1, $affectedRows);
}
}

0 comments on commit 159bcd5

Please sign in to comment.