Skip to content

Commit

Permalink
Add support for order by and limit on MySQL update queries.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Oct 28, 2013
1 parent 885efdc commit d9d61e1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
26 changes: 26 additions & 0 deletions src/Illuminate/Database/Query/Grammars/MySqlGrammar.php
@@ -1,5 +1,7 @@
<?php namespace Illuminate\Database\Query\Grammars;

use Illuminate\Database\Query\Builder;

class MySqlGrammar extends Grammar {

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

}
8 changes: 4 additions & 4 deletions src/Illuminate/Encryption/Encrypter.php
Expand Up @@ -151,7 +151,7 @@ protected function validMac(array $payload)
*
* @param string $iv
* @param string $value
* @return string
* @return string
*/
protected function hash($iv, $value)
{
Expand Down Expand Up @@ -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']);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Illuminate/Foundation/changes.json
Expand Up @@ -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}
]
}
5 changes: 5 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Expand Up @@ -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);
}


Expand Down

3 comments on commit d9d61e1

@bryantebeek
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member Author

@taylorotwell taylorotwell commented on d9d61e1 Nov 2, 2013 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bryantebeek
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.