Skip to content

Commit

Permalink
[5.6] Fix dateBasedWhere() with raw expressions in SQLite (#24102)
Browse files Browse the repository at this point in the history
* Add whereDate() tests

* Fix dateBasedWhere() with raw expressions in SQLite
  • Loading branch information
staudenmeir authored and taylorotwell committed May 4, 2018
1 parent ff5fd17 commit 682f8af
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
4 changes: 1 addition & 3 deletions src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,7 @@ protected function whereTime(Builder $query, $where)
*/
protected function dateBasedWhere($type, Builder $query, $where)
{
$value = str_pad($where['value'], 2, '0', STR_PAD_LEFT);

$value = $this->parameter($value);
$value = $this->parameter($where['value']);

return "strftime('{$type}', {$this->wrap($where['column'])}) {$where['operator']} {$value}";
}
Expand Down
42 changes: 41 additions & 1 deletion tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public function testDateBasedWheresAcceptsTwoArguments()
$this->assertEquals('select * from `users` where month(`created_at`) = ?', $builder->toSql());

$builder = $this->getMySqlBuilder();
$builder->select('*')->from('users')->whereyear('created_at', 1);
$builder->select('*')->from('users')->whereYear('created_at', 1);
$this->assertEquals('select * from `users` where year(`created_at`) = ?', $builder->toSql());
}

Expand Down Expand Up @@ -319,6 +319,18 @@ public function testDateBasedWheresExpressionIsNotBound()
$this->assertEquals([true], $builder->getBindings());
}

public function testWhereDateMySql()
{
$builder = $this->getMySqlBuilder();
$builder->select('*')->from('users')->whereDate('created_at', '=', '2015-12-21');
$this->assertEquals('select * from `users` where date(`created_at`) = ?', $builder->toSql());
$this->assertEquals([0 => '2015-12-21'], $builder->getBindings());

$builder = $this->getMySqlBuilder();
$builder->select('*')->from('users')->whereDate('created_at', '=', new Raw('NOW()'));
$this->assertEquals('select * from `users` where date(`created_at`) = NOW()', $builder->toSql());
}

public function testWhereDayMySql()
{
$builder = $this->getMySqlBuilder();
Expand Down Expand Up @@ -397,6 +409,10 @@ public function testWhereDatePostgres()
$builder->select('*')->from('users')->whereDate('created_at', '=', '2015-12-21');
$this->assertEquals('select * from "users" where "created_at"::date = ?', $builder->toSql());
$this->assertEquals([0 => '2015-12-21'], $builder->getBindings());

$builder = $this->getPostgresBuilder();
$builder->select('*')->from('users')->whereDate('created_at', new Raw('NOW()'));
$this->assertEquals('select * from "users" where "created_at"::date = NOW()', $builder->toSql());
}

public function testWhereDayPostgres()
Expand Down Expand Up @@ -431,6 +447,18 @@ public function testWhereTimePostgres()
$this->assertEquals([0 => '22:00'], $builder->getBindings());
}

public function testWhereDateSqlite()
{
$builder = $this->getSQLiteBuilder();
$builder->select('*')->from('users')->whereDate('created_at', '=', '2015-12-21');
$this->assertEquals('select * from "users" where strftime(\'%Y-%m-%d\', "created_at") = ?', $builder->toSql());
$this->assertEquals([0 => '2015-12-21'], $builder->getBindings());

$builder = $this->getSQLiteBuilder();
$builder->select('*')->from('users')->whereDate('created_at', new Raw('NOW()'));
$this->assertEquals('select * from "users" where strftime(\'%Y-%m-%d\', "created_at") = NOW()', $builder->toSql());
}

public function testWhereDaySqlite()
{
$builder = $this->getSQLiteBuilder();
Expand Down Expand Up @@ -471,6 +499,18 @@ public function testWhereTimeOperatorOptionalSqlite()
$this->assertEquals([0 => '22:00'], $builder->getBindings());
}

public function testWhereDateSqlServer()
{
$builder = $this->getSqlServerBuilder();
$builder->select('*')->from('users')->whereDate('created_at', '=', '2015-12-21');
$this->assertEquals('select * from [users] where cast([created_at] as date) = ?', $builder->toSql());
$this->assertEquals([0 => '2015-12-21'], $builder->getBindings());

$builder = $this->getSqlServerBuilder();
$builder->select('*')->from('users')->whereDate('created_at', new Raw('NOW()'));
$this->assertEquals('select * from [users] where cast([created_at] as date) = NOW()', $builder->toSql());
}

public function testWhereDaySqlServer()
{
$builder = $this->getSqlServerBuilder();
Expand Down

0 comments on commit 682f8af

Please sign in to comment.