Skip to content

Commit

Permalink
DB: use boolean parameters as a query parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
forrest79 committed Nov 18, 2024
1 parent 7209172 commit 9d82800
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 15 deletions.
4 changes: 2 additions & 2 deletions docs/fluent.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ $query = $connection
'phones' => Forrest79\PhPgSql\Db\Helper::createStringPgArray(['732123456', '736987654']),
]);

dump($query); // (Query) INSERT INTO users (nick, inserted_datetime, active, age, height_cm, phones) VALUES($1, now(), TRUE, $2, $3, $4) [Params: (array) ['James', 37, (NULL), '{\"732123456\",\"736987654\"}']]
dump($query); // (Query) INSERT INTO users (nick, inserted_datetime, active, age, height_cm, phones) VALUES($1, now(), $2, $3, $4, $5) [Params: (array) ['James', 't', 37, (NULL), '{\"732123456\",\"736987654\"}']]

$result = $query->execute();

Expand Down Expand Up @@ -830,7 +830,7 @@ $query = $connection
->join('user_departments', 'ud', 'ud.department_id = au.id')
->where('ud.department_id IN (?)', Forrest79\PhPgSql\Db\Sql\Query::create('SELECT id FROM active_departments'));

dump($query); // (Query) WITH active_users AS (SELECT id, nick, age, height_cm FROM users WHERE active = TRUE), active_departments AS (SELECT id FROM departments WHERE active = TRUE) SELECT au.id, au.nick, au.age, au.height_cm FROM active_users AS au INNER JOIN user_departments AS ud ON ud.department_id = au.id WHERE ud.department_id IN (SELECT id FROM active_departments)
dump($query); // (Query) WITH active_users AS (SELECT id, nick, age, height_cm FROM users WHERE active = TRUE), active_departments AS (SELECT id FROM departments WHERE active = $1) SELECT au.id, au.nick, au.age, au.height_cm FROM active_users AS au INNER JOIN user_departments AS ud ON ud.department_id = au.id WHERE ud.department_id IN (SELECT id FROM active_departments) [Params: (array) ['t']]

$query->execute();
```
Expand Down
10 changes: 8 additions & 2 deletions phpcs-ignores.neon
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,12 @@ ignoreErrors:
count: 1
path: tests/Integration/FetchTest.php

-
sniff: PSR1.Files.SideEffects.FoundWithSymbols
message: 'A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line 15 and the first side effect is on line 9.'
count: 1
path: tests/Integration/FluentQueryExecuteFetchTest.php

-
sniff: PSR1.Files.SideEffects.FoundWithSymbols
message: 'A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line 15 and the first side effect is on line 9.'
Expand Down Expand Up @@ -517,9 +523,9 @@ ignoreErrors:

-
sniff: PSR1.Files.SideEffects.FoundWithSymbols
message: 'A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line 15 and the first side effect is on line 9.'
message: 'A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line 12 and the first side effect is on line 7.'
count: 1
path: tests/Integration/QueryExecuteFetchTest.php
path: tests/Integration/QueryTest.php

-
sniff: PSR1.Files.SideEffects.FoundWithSymbols
Expand Down
10 changes: 7 additions & 3 deletions src/Db/SqlDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,20 @@ static function ($matches) use (&$params, &$parsedParams, &$origParamIndex, &$pa
$parsedParams[] = ($value instanceof \BackedEnum) ? $value->value : $value;
}
return \implode(', ', $keys);
} else if (\is_bool($param)) {
return $param === TRUE ? 'TRUE' : 'FALSE'; // @todo as param
} else if ($param instanceof Sql) {
$subquerySql = self::processSqlDefinition($param->getSqlDefinition(), $paramIndex);
$paramIndex += \count($subquerySql->params);
$parsedParams = \array_merge($parsedParams, $subquerySql->params);
return $subquerySql->sql;
}

$parsedParams[] = ($param instanceof \BackedEnum) ? $param->value : $param;
if (\is_bool($param)) {
$parsedParams[] = $param ? 't' : 'f';
} else if ($param instanceof \BackedEnum) {
$parsedParams[] = $param->value;
} else {
$parsedParams[] = $param;
}

return '$' . ++$paramIndex;
},
Expand Down
7 changes: 1 addition & 6 deletions tests/Integration/FetchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -817,12 +817,7 @@ public function testRowValues(): void

private function fetch(Db\Result $result): Db\Row
{
$row = $result->fetch();
if ($row === NULL) {
throw new \RuntimeException('No data from database were returned');
}

return $row;
return $result->fetch() ?? throw new \RuntimeException('No data from database were returned');
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @testCase
* @property-read Fluent\Connection $connection
*/
final class QueryExecuteFetchTest extends TestCase
final class FluentQueryExecuteFetchTest extends TestCase
{

public function testFetch(): void
Expand Down Expand Up @@ -284,4 +284,4 @@ protected function createConnection(): Db\Connection

}

(new QueryExecuteFetchTest())->run();
(new FluentQueryExecuteFetchTest())->run();
50 changes: 50 additions & 0 deletions tests/Integration/QueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php declare(strict_types=1);

namespace Forrest79\PhPgSql\Tests\Integration;

use Tester;

require_once __DIR__ . '/TestCase.php';

/**
* @testCase
*/
final class QueryTest extends TestCase
{

public function testBoolParameters(): void
{
$this->connection->query('
CREATE TABLE test(
id integer,
bool_column bool
);
');

$this->connection->query('INSERT INTO test(id, bool_column) VALUES(?, ?)', 1, TRUE);
$this->connection->query('INSERT INTO test(id, bool_column) VALUES(?, ?)', 2, FALSE);

$resultTrue = $this->connection->query('SELECT id, bool_column FROM test WHERE bool_column = ?', TRUE);

$rowTrue = $resultTrue->fetch() ?? throw new \RuntimeException('No data from database were returned');

Tester\Assert::same(1, $rowTrue->id);
Tester\Assert::true($rowTrue->bool_column);

$resultTrue->free();

// ---

$resultFalse = $this->connection->query('SELECT id, bool_column FROM test WHERE bool_column = ?', FALSE);

$rowFalse = $resultFalse->fetch() ?? throw new \RuntimeException('No data from database were returned');

Tester\Assert::same(2, $rowFalse->id);
Tester\Assert::false($rowFalse->bool_column);

$resultFalse->free();
}

}

(new QueryTest())->run();
8 changes: 8 additions & 0 deletions tests/Unit/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ public function testPrepareQueryWithQuery(): void
}


public function testPrepareQueryWithBool(): void
{
$query = Db\Sql\Query::create('SELECT * FROM table WHERE column1 = ? AND column2 = ?', TRUE, FALSE)->toDbQuery();
Tester\Assert::same('SELECT * FROM table WHERE column1 = $1 AND column2 = $2', $query->sql);
Tester\Assert::same(['t', 'f'], $query->params);
}


public function testPrepareQueryWithEnum(): void
{
$query = Db\Sql\Query::create('SELECT * FROM table WHERE column = ?', Tests\TestEnum::One)->toDbQuery();
Expand Down

0 comments on commit 9d82800

Please sign in to comment.