diff --git a/lib/Db/QueryBuilderTrait.php b/lib/Db/QueryBuilderTrait.php index c60eeafbd..6570da6dd 100644 --- a/lib/Db/QueryBuilderTrait.php +++ b/lib/Db/QueryBuilderTrait.php @@ -1056,7 +1056,11 @@ protected function where($column = null, $operator = null, $value = null): self */ protected function whereRaw($expression, $params = null): self { - return $this->where($this->raw($expression), null, $params); + $args = [$this->raw($expression), null]; + if (func_num_args() > 1) { + $args[] = $params; + } + return $this->addWhere(...$args); } /** @@ -1590,7 +1594,8 @@ protected function addWhere( string $condition = 'AND', string $type = null ): self { - if ($column instanceof Closure) { + $isClosure = $column instanceof Closure; + if ($isClosure) { /** @phpstan-ignore-next-line Allow new static */ $query = new static([ 'wei' => $this->wei, @@ -1602,15 +1607,18 @@ protected function addWhere( $this->addQueryParams($query->getQueryParams()); } - if (null === $value) { - $operator = 'NOT NULL' === $operator ? $operator : 'NULL'; - } elseif (is_array($value) && !in_array($operator, ['BETWEEN', 'NOT BETWEEN'], true)) { + if (is_array($value) && !in_array($operator, ['BETWEEN', 'NOT BETWEEN'], true)) { $operator = 'NOT IN' === $operator ? $operator : 'IN'; } $this->addQueryPart('where', compact('column', 'operator', 'value', 'condition', 'type'), true); - if (null !== $value) { - $this->addQueryParam($value); + + // Ignore params when + // 1. query is closure + // 2. operator is NULL or NOT NULL + // 3. params are not passed + if (!$isClosure && !in_array($operator, ['NULL', 'NOT NULL'], true) && func_num_args() > 2) { + $this->addQueryParam(null === $value ? [null] : $value); } return $this; diff --git a/tests/unit/QueryBuilderTest.php b/tests/unit/QueryBuilderTest.php index 4ed208370..2dc82a20f 100644 --- a/tests/unit/QueryBuilderTest.php +++ b/tests/unit/QueryBuilderTest.php @@ -221,6 +221,24 @@ public function testWhereNot() $this->assertEquals('test', $qb->fetch()['name']); } + public function testWhereNotNullValue() + { + $this->initFixtures(); + + $qb = Qb::table('test_users')->whereNot('id', null); + $this->assertEquals('SELECT * FROM `p_test_users` WHERE `id` != NULL', $qb->getRawSql()); + $this->assertNull($qb->fetch()); + } + + public function testWhereGtNullValue() + { + $this->initFixtures(); + + $qb = Qb::table('test_users')->where('id', '>', null); + $this->assertEquals('SELECT * FROM `p_test_users` WHERE `id` > NULL', $qb->getRawSql()); + $this->assertNull($qb->fetch()); + } + public function testOrWhere() { $sql = Qb::table('test_users')