Skip to content

[12.x] fix(postgres): missing parentheses in whereDate/whereTime for json columns #55159

New issue

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

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

Already on GitHub? # to your account

Merged
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
14 changes: 12 additions & 2 deletions src/Illuminate/Database/Query/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,13 @@ protected function whereLike(Builder $query, $where)
protected function whereDate(Builder $query, $where)
{
$value = $this->parameter($where['value']);
$column = $this->wrap($where['column']);

return $this->wrap($where['column']).'::date '.$where['operator'].' '.$value;
if ($this->isJsonSelector($where['column'])) {
$column = '('.$column.')';
}

return $column.'::date '.$where['operator'].' '.$value;
}

/**
Expand All @@ -123,8 +128,13 @@ protected function whereDate(Builder $query, $where)
protected function whereTime(Builder $query, $where)
{
$value = $this->parameter($where['value']);
$column = $this->wrap($where['column']);

if ($this->isJsonSelector($where['column'])) {
$column = '('.$column.')';
}

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

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,10 @@ public function testWhereDatePostgres()
$builder = $this->getPostgresBuilder();
$builder->select('*')->from('users')->whereDate('created_at', new Raw('NOW()'));
$this->assertSame('select * from "users" where "created_at"::date = NOW()', $builder->toSql());

$builder = $this->getPostgresBuilder();
$builder->select('*')->from('users')->whereDate('result->created_at', new Raw('NOW()'));
$this->assertSame('select * from "users" where ("result"->>\'created_at\')::date = NOW()', $builder->toSql());
}

public function testWhereDayPostgres()
Expand Down Expand Up @@ -625,6 +629,11 @@ public function testWhereTimePostgres()
$builder->select('*')->from('users')->whereTime('created_at', '>=', '22:00');
$this->assertSame('select * from "users" where "created_at"::time >= ?', $builder->toSql());
$this->assertEquals([0 => '22:00'], $builder->getBindings());

$builder = $this->getPostgresBuilder();
$builder->select('*')->from('users')->whereTime('result->created_at', '>=', '22:00');
$this->assertSame('select * from "users" where ("result"->>\'created_at\')::time >= ?', $builder->toSql());
$this->assertEquals([0 => '22:00'], $builder->getBindings());
}

public function testWherePast()
Expand Down
Loading