Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.6] Fix dateBasedWhere() with raw expressions in SQLite #24102

Merged
merged 2 commits into from
May 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 All @@ -300,6 +300,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 @@ -378,6 +390,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 @@ -412,6 +428,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 @@ -452,6 +480,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