From 947a4f8e518352698e1291dcd6417e36b7b1c70b Mon Sep 17 00:00:00 2001 From: Jonas Staudenmeir Date: Fri, 24 Aug 2018 15:53:16 +0200 Subject: [PATCH] Fix whereTime() on SQL Server (#25316) --- .../Database/Query/Grammars/SqlServerGrammar.php | 14 ++++++++++++++ tests/Database/DatabaseQueryBuilderTest.php | 13 +++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php b/src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php index bb4809d67291..24e8f4c9ee51 100755 --- a/src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php +++ b/src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php @@ -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. * diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index 1106423701b9..f8bea9f66665 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -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();