Skip to content

Commit

Permalink
[5.6] Allow integer values for whereYear() in SQLite (#24115)
Browse files Browse the repository at this point in the history
* Allow integer values for whereYear() in SQLite

* Fix style
  • Loading branch information
staudenmeir authored and taylorotwell committed May 7, 2018
1 parent 951d42f commit a37aa1c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
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());
}
}

0 comments on commit a37aa1c

Please sign in to comment.