Permalink
Browse files

Add support for order by and limit on MySQL update queries.

  • Loading branch information...
taylorotwell committed Oct 28, 2013
1 parent 885efdc commit d9d61e13fc2f01efc00c9b87ff6f1e91bab1e9b8
@@ -1,5 +1,7 @@
<?php namespace Illuminate\Database\Query\Grammars;
use Illuminate\Database\Query\Builder;
class MySqlGrammar extends Grammar {
/**
@@ -9,4 +11,28 @@ class MySqlGrammar extends Grammar {
*/
protected $wrapper = '`%s`';
/**
* Compile an update statement into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $values
* @return string
*/
public function compileUpdate(Builder $query, $values)
{
$sql = parent::compileUpdate($query, $values);
if (isset($query->orders))
{
$sql .= ' '.$this->compileOrders($query, $query->orders);
}
if (isset($query->limit))
{
$sql .= ' '.$this->compileLimit($query, $query->limit);
}
return rtrim($sql);
}
}
@@ -151,7 +151,7 @@ protected function validMac(array $payload)
*
* @param string $iv
* @param string $value
* @return string
* @return string
*/
protected function hash($iv, $value)
{
@@ -201,12 +201,12 @@ protected function paddingIsValid($pad, $value)
/**
* Verify that the encryption payload is valid.
*
* @param array $data
* @param array|mixed $data
* @return bool
*/
protected function invalidPayload(array $data)
protected function invalidPayload($data)
{
return ! isset($data['iv']) or ! isset($data['value']) or ! isset($data['mac']);
return ! is_array($data) or ! isset($data['iv']) or ! isset($data['value']) or ! isset($data['mac']);
}
/**
@@ -60,6 +60,7 @@
{"message": "Allow custom messages to be registered when using Validator::extend.", "backport": null},
{"message": "Added new auth:clear-reminders command for clearing expired password reminders.", "backport": null},
{"message": "Added Cookie::queue method for creating cookies that are automatically attached to the final response.", "backport": null},
{"message": "Allow environment to be checked via App::environment method.", "backport": null}
{"message": "Allow environment to be checked via App::environment method.", "backport": null},
{"message": "Add support for order by and limit on MySQL update queries.", "backport": null}
]
}
@@ -614,6 +614,11 @@ public function testUpdateMethod()
$builder->getConnection()->shouldReceive('update')->once()->with('update "users" set "email" = ?, "name" = ? where "id" = ?', array('foo', 'bar', 1))->andReturn(1);
$result = $builder->from('users')->where('id', '=', 1)->update(array('email' => 'foo', 'name' => 'bar'));
$this->assertEquals(1, $result);
$builder = $this->getMySqlBuilder();
$builder->getConnection()->shouldReceive('update')->once()->with('update `users` set `email` = ?, `name` = ? where `id` = ? order by `foo` desc limit 5', array('foo', 'bar', 1))->andReturn(1);
$result = $builder->from('users')->where('id', '=', 1)->orderBy('foo', 'desc')->limit(5)->update(array('email' => 'foo', 'name' => 'bar'));
$this->assertEquals(1, $result);
}

3 comments on commit d9d61e1

@bryantebeek

This comment has been minimized.

bryantebeek replied Nov 2, 2013

This does not work in combination with multiple-table syntax.

See https://dev.mysql.com/doc/refman/5.0/en/update.html

For the multiple-table syntax, UPDATE updates rows in each table named in table_references that satisfy the conditions. In this case, ORDER BY and LIMIT cannot be used.

Suggestion to check for available joins:
if (! empty($query->joins)) return rtrim($sql);

@taylorotwell

This comment has been minimized.

Member

taylorotwell replied Nov 2, 2013

@bryantebeek

This comment has been minimized.

bryantebeek replied Nov 2, 2013

Any suggestion how we could adapt the grammar to our requirements?
We are doing a join on every request, so we don't want the mysql grammar to prepend these statements.

Please sign in to comment.