Skip to content

Commit

Permalink
Fix whereTime() on SQL Server (#25316)
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir authored and taylorotwell committed Aug 24, 2018
1 parent c3a438c commit 947a4f8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ protected function whereDate(Builder $query, $where)
return 'cast('.$this->wrap($where['column']).' as date) '.$where['operator'].' '.$value;
}

/**
* Compile a "where time" clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereTime(Builder $query, $where)
{
$value = $this->parameter($where['value']);

return 'cast('.$this->wrap($where['column']).' as time) '.$where['operator'].' '.$value;
}

/**
* Compile a "JSON contains" statement into SQL.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,19 @@ public function testWhereTimeOperatorOptionalPostgres()
$this->assertEquals([0 => '22:00'], $builder->getBindings());
}

public function testWhereTimeSqlServer()
{
$builder = $this->getSqlServerBuilder();
$builder->select('*')->from('users')->whereTime('created_at', '22:00');
$this->assertEquals('select * from [users] where cast([created_at] as time) = ?', $builder->toSql());
$this->assertEquals([0 => '22:00'], $builder->getBindings());

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

public function testWhereDatePostgres()
{
$builder = $this->getPostgresBuilder();
Expand Down

0 comments on commit 947a4f8

Please sign in to comment.