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] Allow integer values for whereYear() in SQLite #24115

Merged
merged 2 commits into from
May 7, 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
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ protected function dateBasedWhere($type, Builder $query, $where)
{
$value = $this->parameter($where['value']);

return "strftime('{$type}', {$this->wrap($where['column'])}) {$where['operator']} {$value}";
return "strftime('{$type}', {$this->wrap($where['column'])}) {$where['operator']} cast({$value} as text)";
}

/**
Expand Down
14 changes: 7 additions & 7 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,51 +451,51 @@ 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('select * from "users" where strftime(\'%Y-%m-%d\', "created_at") = cast(? as text)', $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());
$this->assertEquals('select * from "users" where strftime(\'%Y-%m-%d\', "created_at") = cast(NOW() as text)', $builder->toSql());
}

public function testWhereDaySqlite()
{
$builder = $this->getSQLiteBuilder();
$builder->select('*')->from('users')->whereDay('created_at', '=', 1);
$this->assertEquals('select * from "users" where strftime(\'%d\', "created_at") = ?', $builder->toSql());
$this->assertEquals('select * from "users" where strftime(\'%d\', "created_at") = cast(? as text)', $builder->toSql());
$this->assertEquals([0 => 1], $builder->getBindings());
}

public function testWhereMonthSqlite()
{
$builder = $this->getSQLiteBuilder();
$builder->select('*')->from('users')->whereMonth('created_at', '=', 5);
$this->assertEquals('select * from "users" where strftime(\'%m\', "created_at") = ?', $builder->toSql());
$this->assertEquals('select * from "users" where strftime(\'%m\', "created_at") = cast(? as text)', $builder->toSql());
$this->assertEquals([0 => 5], $builder->getBindings());
}

public function testWhereYearSqlite()
{
$builder = $this->getSQLiteBuilder();
$builder->select('*')->from('users')->whereYear('created_at', '=', 2014);
$this->assertEquals('select * from "users" where strftime(\'%Y\', "created_at") = ?', $builder->toSql());
$this->assertEquals('select * from "users" where strftime(\'%Y\', "created_at") = cast(? as text)', $builder->toSql());
$this->assertEquals([0 => 2014], $builder->getBindings());
}

public function testWhereTimeSqlite()
{
$builder = $this->getSQLiteBuilder();
$builder->select('*')->from('users')->whereTime('created_at', '>=', '22:00');
$this->assertEquals('select * from "users" where strftime(\'%H:%M:%S\', "created_at") >= ?', $builder->toSql());
$this->assertEquals('select * from "users" where strftime(\'%H:%M:%S\', "created_at") >= cast(? as text)', $builder->toSql());
$this->assertEquals([0 => '22:00'], $builder->getBindings());
}

public function testWhereTimeOperatorOptionalSqlite()
{
$builder = $this->getSQLiteBuilder();
$builder->select('*')->from('users')->whereTime('created_at', '22:00');
$this->assertEquals('select * from "users" where strftime(\'%H:%M:%S\', "created_at") = ?', $builder->toSql());
$this->assertEquals('select * from "users" where strftime(\'%H:%M:%S\', "created_at") = cast(? as text)', $builder->toSql());
$this->assertEquals([0 => '22:00'], $builder->getBindings());
}

Expand Down
54 changes: 54 additions & 0 deletions tests/Integration/Database/QueryBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Illuminate\Tests\Integration\Database\EloquentBelongsToManyTest;

use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Tests\Integration\Database\DatabaseTestCase;

/**
* @group integration
*/
class QueryBuilderTest extends DatabaseTestCase
{
public function setUp()
{
parent::setUp();

Schema::create('posts', function ($table) {
$table->timestamp('created_at');
});

DB::table('posts')->insert([
['created_at' => new Carbon('2017-11-12 13:14:15')],
['created_at' => new Carbon('2018-01-02 03:04:05')],
]);
}

public function testWhereDate()
{
$this->assertSame(1, DB::table('posts')->whereDate('created_at', '2018-01-02')->count());
}

public function testWhereDay()
{
$this->assertSame(1, DB::table('posts')->whereDay('created_at', '02')->count());
}

public function testWhereMonth()
{
$this->assertSame(1, DB::table('posts')->whereMonth('created_at', '01')->count());
}

public function testWhereYear()
{
$this->assertSame(1, DB::table('posts')->whereYear('created_at', '2018')->count());
$this->assertSame(1, DB::table('posts')->whereYear('created_at', 2018)->count());
}

public function testWhereTime()
{
$this->assertSame(1, DB::table('posts')->whereTime('created_at', '03:04:05')->count());
}
}