Skip to content

Commit

Permalink
final version?
Browse files Browse the repository at this point in the history
  • Loading branch information
forrest79 committed Nov 15, 2024
1 parent ce744ca commit 7209172
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 77 deletions.
8 changes: 4 additions & 4 deletions docs/db.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ table($rowsAny);
To pass another query, we need to prepare one and then use it:

```php
$query = Forrest79\PhPgSql\Db\Sql\DefaultQuery::create('SELECT id FROM users WHERE inserted_datetime::date > ?', '2020-01-02');
$queryArgs = Forrest79\PhPgSql\Db\Sql\DefaultQuery::createArgs('SELECT id FROM users WHERE inserted_datetime::date > ?', ['2020-01-02']);
$query = Forrest79\PhPgSql\Db\Sql\Query::create('SELECT id FROM users WHERE inserted_datetime::date > ?', '2020-01-02');
$queryArgs = Forrest79\PhPgSql\Db\Sql\Query::createArgs('SELECT id FROM users WHERE inserted_datetime::date > ?', ['2020-01-02']);

$result = $connection->query('SELECT d.id, d.name FROM user_departments ud JOIN departments d ON d.id = ud.department_id WHERE ud.user_id IN (?) AND d.active ORDER BY ?', $query, Forrest79\PhPgSql\Db\Sql\Literal::create('d.id'));

Expand Down Expand Up @@ -474,7 +474,7 @@ $connection->query('INSERT INTO users (nick) VALUES(?)', Forrest79\PhPgSql\Db\Sq
Query example:

```php
$activeDepartmentsQuery = Forrest79\PhPgSql\Db\Sql\DefaultQuery::create('SELECT id FROM departments WHERE active = ?', TRUE);
$activeDepartmentsQuery = Forrest79\PhPgSql\Db\Sql\Query::create('SELECT id FROM departments WHERE active = ?', TRUE);

$cnt = $connection->query('SELECT COUNT(*) FROM user_departments WHERE department_id IN (?)', $activeDepartmentsQuery)->fetchSingle();
dump($cnt); // (integer) 7
Expand All @@ -483,7 +483,7 @@ dump($cnt); // (integer) 7
`Query` has also method `createQuery()` that prepare query in the format, that can be used in `pg_*` functions.

```php
$departmentsQuery = Forrest79\PhPgSql\Db\Sql\DefaultQuery::createArgs('SELECT id FROM departments WHERE id = ?', [1]);
$departmentsQuery = Forrest79\PhPgSql\Db\Sql\Query::createArgs('SELECT id FROM departments WHERE id = ?', [1]);

$query = $departmentsQuery->toDbQuery();

Expand Down
6 changes: 3 additions & 3 deletions docs/fluent.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ $query = $connection
->add('column2', [2, 3]) // this is also add to OR
->addAndBranch() // this is also add to OR and can contains more ANDs
->add('column', $connection->createQuery()->select([1])) // this is add to AND
->add('column2 = ANY(?)', Forrest79\PhPgSql\Db\Sql\DefaultQuery::create('SELECT 2')) // this is add to AND
->add('column2 = ANY(?)', Forrest79\PhPgSql\Db\Sql\Query::create('SELECT 2')) // this is add to AND
->parent() // get original OR
->add('column3 IS NOT NULL') // and add to OR new condition
->query() // back to original query object
Expand Down Expand Up @@ -824,11 +824,11 @@ You can use `WITH` with a simple string query, or defined it with `Db\Sql\Query`
$query = $connection
->createQuery()
->with('active_users', 'SELECT id, nick, age, height_cm FROM users WHERE active = TRUE')
->with('active_departments', Forrest79\PhPgSql\Db\Sql\DefaultQuery::create('SELECT id FROM departments WHERE active = ?', TRUE))
->with('active_departments', Forrest79\PhPgSql\Db\Sql\Query::create('SELECT id FROM departments WHERE active = ?', TRUE))
->select(['au.id', 'au.nick', 'au.age', 'au.height_cm'])
->from('active_users', 'au')
->join('user_departments', 'ud', 'ud.department_id = au.id')
->where('ud.department_id IN (?)', Forrest79\PhPgSql\Db\Sql\DefaultQuery::create('SELECT id FROM active_departments'));
->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)

Expand Down
10 changes: 2 additions & 8 deletions phpcs-ignores.neon
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,6 @@ ignoreErrors:
count: 1
path: src/Db/Sql.php

-
sniff: SlevomatCodingStandard.Classes.RequireAbstractOrFinal.ClassNeitherAbstractNorFinal
message: All classes should be declared using either the "abstract" or "final" keyword.
count: 1
path: src/Db/Sql/DefaultQuery.php

-
sniff: SlevomatCodingStandard.Classes.RequireAbstractOrFinal.ClassNeitherAbstractNorFinal
message: All classes should be declared using either the "abstract" or "final" keyword.
Expand All @@ -300,8 +294,8 @@ ignoreErrors:
path: src/Db/Sql/Literal.php

-
sniff: Squiz.Scope.MethodScope.Missing
message: Visibility must be declared on method "toDbQuery"
sniff: SlevomatCodingStandard.Classes.RequireAbstractOrFinal.ClassNeitherAbstractNorFinal
message: All classes should be declared using either the "abstract" or "final" keyword.
count: 1
path: src/Db/Sql/Query.php

Expand Down
2 changes: 1 addition & 1 deletion src/Db/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(string $sql, array $params)
public static function from(string|self|Sql $query, array $params = []): self
{
if (is_string($query)) {
$query = new Sql\DefaultQuery($query, $params);
$query = new Sql\Query($query, $params);
} else if ($params !== []) {
throw Exceptions\QueryException::cantPassParams();
}
Expand Down
21 changes: 0 additions & 21 deletions src/Db/Sql/DefaultQuery.php

This file was deleted.

13 changes: 11 additions & 2 deletions src/Db/Sql/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@

use Forrest79\PhPgSql\Db;

interface Query extends Db\Sql
class Query extends Expression
{
private Db\Query|NULL $dbQuery = NULL;

function toDbQuery(): Db\Query;

public function toDbQuery(): Db\Query
{
if ($this->dbQuery === NULL) {
$this->dbQuery = Db\SqlDefinition::createQuery($this->getSqlDefinition());
}

return $this->dbQuery;
}

}
2 changes: 1 addition & 1 deletion src/Fluent/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* @phpstan-import-type QueryParams from QueryBuilder
*/
class Query implements Db\Sql\Query
class Query implements Db\Sql
{
public const QUERY_SELECT = 'select';
public const QUERY_INSERT = 'insert';
Expand Down
4 changes: 2 additions & 2 deletions src/Fluent/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -813,9 +813,9 @@ private function processCondition(Condition $condition, array &$params): string
}


private static function areParenthesisNeeded(Db\Sql $sql): bool
protected static function areParenthesisNeeded(Db\Sql $sql): bool
{
return $sql instanceof Db\Sql\Query;
return $sql instanceof Db\Sql\Query || $sql instanceof Query;
}

}
2 changes: 1 addition & 1 deletion tests/Integration/BasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public function testFailedQuery(): void
public function testPassParamToQuery(): void
{
Tester\Assert::exception(function (): void {
$query = Db\Sql\DefaultQuery::create('SELECT 1');
$query = Db\Sql\Query::create('SELECT 1');
$this->connection->query($query, 1);
}, Db\Exceptions\QueryException::class, NULL, Db\Exceptions\QueryException::CANT_PASS_PARAMS);
}
Expand Down
28 changes: 14 additions & 14 deletions tests/Unit/FluentQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function testSelectWithFluentQuery(): void
public function testSelectWithQuery(): void
{
$query = $this->query()
->select(['column' => Db\Sql\DefaultQuery::create('SELECT 1')])
->select(['column' => Db\Sql\Query::create('SELECT 1')])
->toDbQuery();

Tester\Assert::same('SELECT (SELECT 1) AS "column"', $query->sql);
Expand Down Expand Up @@ -131,7 +131,7 @@ public function testFromWithQuery(): void
{
$query = $this->query()
->select(['x.column'])
->from(Db\Sql\DefaultQuery::create('SELECT 1 AS column'), 'x')
->from(Db\Sql\Query::create('SELECT 1 AS column'), 'x')
->toDbQuery();

Tester\Assert::same('SELECT x.column FROM (SELECT 1 AS column) AS x', $query->sql);
Expand Down Expand Up @@ -469,7 +469,7 @@ public function testJoinWithQuery(): void
$query = $this->query()
->select(['x.column'])
->from('table', 't')
->join(Db\Sql\DefaultQuery::create('SELECT 1 AS column'), 'x', 'x.column = t.id')
->join(Db\Sql\Query::create('SELECT 1 AS column'), 'x', 'x.column = t.id')
->toDbQuery();

Tester\Assert::same('SELECT x.column FROM table AS t INNER JOIN (SELECT 1 AS column) AS x ON x.column = t.id', $query->sql);
Expand Down Expand Up @@ -580,7 +580,7 @@ public function testLateralJoinWithSqlQuery(): void
$query = $this->query()
->select(['column1' => 't1.column', 'column2' => 't2.column'])
->from('table1', 't1')
->join(Db\Sql\DefaultQuery::create('SELECT column FROM table2'), 't2', 't2.column = t1.column')
->join(Db\Sql\Query::create('SELECT column FROM table2'), 't2', 't2.column = t1.column')
->lateral('t2')
->toDbQuery();

Expand Down Expand Up @@ -638,7 +638,7 @@ public function testSelectCombineQuery(): void
$query = $this->query()
->from('table', 't')
->select(['column'])
->union(Db\Sql\DefaultQuery::create('SELECT column FROM table2 AS t2'))
->union(Db\Sql\Query::create('SELECT column FROM table2 AS t2'))
->toDbQuery();

Tester\Assert::same('(SELECT column FROM table AS t) UNION (SELECT column FROM table2 AS t2)', $query->sql);
Expand Down Expand Up @@ -689,7 +689,7 @@ public function testInsertRow(): void
'column' => 1,
'column_from' => Db\Sql\Literal::create('3'),
'column_fluent_query' => $this->query()->select(['\'test_fluent\''])->where('4', 4),
'column_query' => Db\Sql\DefaultQuery::create('SELECT \'test\' WHERE 5 = ?', 5),
'column_query' => Db\Sql\Query::create('SELECT \'test\' WHERE 5 = ?', 5),
])
->insert('table')
->returning(['column'])
Expand Down Expand Up @@ -728,7 +728,7 @@ public function testInsertRows(): void
['column' => 3],
['column' => Db\Sql\Literal::create('4')],
['column' => $this->query()->select(['\'test_fluent\''])->where('6', 6)],
['column' => Db\Sql\DefaultQuery::create('SELECT \'test\' WHERE 7 = ?', 7)],
['column' => Db\Sql\Query::create('SELECT \'test\' WHERE 7 = ?', 7)],
])
->insert('table')
->returning(['column'])
Expand Down Expand Up @@ -1093,7 +1093,7 @@ public function testUpdate(): void
'column' => 1,
'column_from' => Db\Sql\Literal::create('t2.id'),
'column_fluent_query' => $this->query()->select(['\'test_fluent\''])->where('2', 2),
'column_query' => Db\Sql\DefaultQuery::create('SELECT \'test\' WHERE 3 = ?', 3),
'column_query' => Db\Sql\Query::create('SELECT \'test\' WHERE 3 = ?', 3),
])
->from('table2', 't2')
->where('t2.column', 100)
Expand Down Expand Up @@ -1229,7 +1229,7 @@ public function testMergeUsingSql(): void
{
$query = $this->query()
->merge('customer_account', 'ca')
->using(Db\Sql\DefaultQuery::create('SELECT customer_id, transaction_value FROM recent_transactions WHERE customer_id > ?', 10), 't', 't.customer_id = ca.customer_id')
->using(Db\Sql\Query::create('SELECT customer_id, transaction_value FROM recent_transactions WHERE customer_id > ?', 10), 't', 't.customer_id = ca.customer_id')
->whenMatched('UPDATE SET balance = balance + transaction_value')
->whenNotMatched('INSERT (customer_id, balance) VALUES (t.customer_id, t.transaction_value)')
->toDbQuery();
Expand Down Expand Up @@ -1288,7 +1288,7 @@ public function testMergeCommonUpsert(): void
$query = $this->query()
->merge('wines', 'w')
->using('(SELECT 1)', 's', 'w.winename = $1')
->whenNotMatched(Db\Sql\DefaultQuery::create('INSERT (winename, balance) VALUES($1, $2)', 'Red wine', 10))
->whenNotMatched(Db\Sql\Query::create('INSERT (winename, balance) VALUES($1, $2)', 'Red wine', 10))
->whenMatched('UPDATE SET balance = $2')
->toDbQuery();

Expand Down Expand Up @@ -1374,10 +1374,10 @@ public function testWith(): void
{
$query = $this->query()
->with('regional_sales', 'SELECT region, SUM(amount) AS total_sales FROM orders GROUP BY region')
->with('top_regions', Db\Sql\DefaultQuery::create('SELECT region FROM regional_sales WHERE total_sales > (?) AND total_sales < ?', Db\Sql\Expression::create('SELECT SUM(total_sales) / 10 FROM regional_sales'), 10000))
->with('top_regions', Db\Sql\Query::create('SELECT region FROM regional_sales WHERE total_sales > (?) AND total_sales < ?', Db\Sql\Expression::create('SELECT SUM(total_sales) / 10 FROM regional_sales'), 10000))
->select(['region', 'product', 'product_units' => 'SUM(quantity)', 'product_sales' => 'SUM(amount)'])
->from('orders')
->where('region', Db\Sql\DefaultQuery::create('SELECT region FROM top_regions'))
->where('region', Db\Sql\Query::create('SELECT region FROM top_regions'))
->where('region != ?', 'Prague')
->groupBy('region', 'product')
->toDbQuery();
Expand Down Expand Up @@ -1590,7 +1590,7 @@ public function testConditionWhere(): void
$conditionOr->add('column2', [2, 3]);
$conditionAnd = $conditionOr->addAndBranch();
$conditionAnd->add('column', $this->query()->select([1]));
$conditionAnd->add('column2 = ANY(?)', Db\Sql\DefaultQuery::create('SELECT 2'));
$conditionAnd->add('column2 = ANY(?)', Db\Sql\Query::create('SELECT 2'));
$conditionOr->add('column3 IS NOT NULL');

$query = $conditionOr->query()
Expand All @@ -1610,7 +1610,7 @@ public function testConditionHaving(): void
$conditionOr->add('column2', [2, 3]);
$conditionAnd = $conditionOr->addAndBranch();
$conditionAnd->add('column', $this->query()->select([1]));
$conditionAnd->add('column2 = ANY(?)', Db\Sql\DefaultQuery::create('SELECT 2'));
$conditionAnd->add('column2 = ANY(?)', Db\Sql\Query::create('SELECT 2'));
$conditionOr->add('column3 IS NOT NULL');

$query = $conditionOr->query()
Expand Down
Loading

0 comments on commit 7209172

Please sign in to comment.