Skip to content
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
2 changes: 1 addition & 1 deletion documentation/components/libs/postgresql.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ echo $query->toSQL();
<?php

use function Flow\PostgreSql\DSL\{
insert, col, param, conflict_columns, returning_all
insert, col, param, conflict_columns
};

// Build an upsert query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,30 @@ echo $query->toSql();
// DELETE FROM users WHERE id = ANY (SELECT user_id FROM inactive_users)
```

## Delete with CTE (WITH Clause)

```php
<?php

use function Flow\PostgreSql\DSL\{with, cte, delete, select, col, table, eq, literal, in_subquery};

$query = with(
cte('removed_parents', select(col('id'))->from(table('parents'))->where(eq(col('archived'), literal(true)))),
cte(
'removed_children',
delete()
->from('children')
->where(in_subquery(col('parent_id'), select(col('id'))->from(table('removed_parents'))))
)
)
->delete()
->from('parents')
->where(in_subquery(col('id'), select(col('id'))->from(table('removed_parents'))));

echo $query->toSql();
// WITH removed_parents AS (SELECT id FROM parents WHERE archived = true), removed_children AS (DELETE FROM children WHERE parent_id IN (SELECT id FROM removed_parents)) DELETE FROM parents WHERE id IN (SELECT id FROM removed_parents)
```

## RETURNING Clause

```php
Expand All @@ -118,6 +142,16 @@ $query = delete()

echo $query->toSql();
// DELETE FROM users WHERE id = 1 RETURNING *

// Rename the OLD row alias
$query = delete()
->from('children')
->where(eq(col('id'), literal(10)))
->returningAliases(old: 'o')
->returning(col('id', 'o'));

echo $query->toSql();
// DELETE FROM children WHERE id = 10 RETURNING WITH (OLD AS o) o.id
```

## Complex WHERE Conditions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,25 @@ echo $query->toSql();
// INSERT INTO users (name, email) SELECT name, email FROM archived_users
```

## Insert with CTE

```php
<?php

use function Flow\PostgreSql\DSL\{with, cte, delete, select, col, table, eq, literal};

$query = with(
cte('removed', delete()->from('children')->where(eq(col('parent_id'), literal(1)))->returning(col('id')))
)
->insert()
->into('audit')
->columns('id')
->select(select(col('id'))->from(table('removed')));

echo $query->toSql();
// WITH removed AS (DELETE FROM children WHERE parent_id = 1 RETURNING id) INSERT INTO audit (id) SELECT id FROM removed
```

## Upsert (ON CONFLICT)

### ON CONFLICT DO NOTHING
Expand Down Expand Up @@ -270,6 +289,17 @@ $query = insert()

echo $query->toSql();
// INSERT INTO users (name) VALUES ('John') RETURNING *

// Rename the NEW row alias
$query = insert()
->into('children')
->columns('id', 'parent_id')
->values(literal(12), literal(1))
->returningAliases(new: 'n')
->returning(col('id', 'n'));

echo $query->toSql();
// INSERT INTO children (id, parent_id) VALUES (12, 1) RETURNING WITH (NEW AS n) n.id
```

## Schema-Qualified Tables
Expand Down
20 changes: 20 additions & 0 deletions documentation/components/libs/postgresql/merge-query-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,26 @@ echo $query->toSql();
// MERGE INTO target t USING source s ON t.id = s.id WHEN NOT MATCHED BY SOURCE THEN DELETE
```

## Merge with RETURNING

`merge_action()` returns `INSERT`, `UPDATE` or `DELETE` for each row. `returningAll()` writes `RETURNING *`.

```php
<?php

use function Flow\PostgreSql\DSL\{merge, merge_action, col, eq};

$query = merge('children', 'c')
->using('parents', 'p')
->on(eq(col('parent_id', 'c'), col('id', 'p')))
->whenMatched()
->thenDelete()
->returning(merge_action(), col('id', 'c'));

echo $query->toSql();
// MERGE INTO children c USING parents p ON c.parent_id = p.id WHEN MATCHED THEN DELETE RETURNING merge_action() , c.id
```

## Schema-Qualified Tables

```php
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ echo $query->toSql();

use function Flow\PostgreSql\DSL\{
select, star, table, col,
eq, gt, lt, ge, le, ne, between, in_, like, is_null,
eq, gt, lt, ge, le, ne, between, in_, in_subquery, like, is_null,
literal,
and_, or_, not_
};
Expand Down Expand Up @@ -98,6 +98,14 @@ $query = select(star())
echo $query->toSql();
// SELECT * FROM users WHERE status IN ('active', 'pending', 'verified')

// IN (subquery) condition
$query = select(star())
->from(table('users'))
->where(in_subquery(col('id'), select(col('user_id'))->from(table('orders'))));

echo $query->toSql();
// SELECT * FROM users WHERE id IN (SELECT user_id FROM orders)

// LIKE condition
$query = select(star())
->from(table('users'))
Expand Down Expand Up @@ -244,7 +252,7 @@ echo $query->toSql();
<?php

use function Flow\PostgreSql\DSL\{
select, with, star, table, col, cte, eq, literal
select, with, star, table, col, cte, eq, literal, update, binary_expr, lt
};

use Flow\PostgreSql\QueryBuilder\Clause\CTEMaterialization;
Expand Down Expand Up @@ -284,6 +292,62 @@ $query = with(cte('tree', $recursiveQuery))
echo $query->toSql();
// WITH RECURSIVE tree AS (...) SELECT * FROM tree

// Recursive CTE with SEARCH (searchDepthFirst() writes DEPTH FIRST)
$query = with(
cte(
't',
select(literal(1))->unionAll(
select(binary_expr(col('n'), '+', literal(1)))->from(table('t'))->where(lt(col('n'), literal(3)))
),
['n']
)->searchBreadthFirst(['n'], 'ord')
)
->recursive()
->select(star())
->from(table('t'));

echo $query->toSql();
// WITH RECURSIVE t(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM t WHERE n < 3) SEARCH BREADTH FIRST BY n SET ord SELECT * FROM t

// Recursive CTE with CYCLE (without the mark values, the cycle mark is true / false)
$query = with(
cte(
'graph',
select(col('id'), col('link'))
->from(table('edges'))
->where(eq(col('id'), literal(1)))
->unionAll(
select(col('id', 'e'), col('link', 'e'))
->from(table('edges')->as('e'))
->join(table('graph'), eq(col('id', 'e'), col('link', 'graph')))
),
['id', 'link']
)->cycle(['id'], 'is_cycle', 'path', literal('Y'), literal('N'))
)
->recursive()
->select(star())
->from(table('graph'));

echo $query->toSql();
// WITH RECURSIVE graph(id, link) AS (SELECT id, link FROM edges WHERE id = 1 UNION ALL SELECT e.id, e.link FROM edges e JOIN graph ON e.id = graph.link) CYCLE id SET is_cycle TO 'Y' DEFAULT 'N' USING path SELECT * FROM graph

// Data-modifying CTE: cte() also takes INSERT, UPDATE, DELETE and MERGE queries
$query = with(
cte(
'updated',
update()
->update('children')
->set('parent_id', literal(2))
->where(eq(col('id'), literal(10)))
->returning(col('id'), col('parent_id'))
)
)
->select(col('id'), col('parent_id'))
->from(table('updated'));

echo $query->toSql();
// WITH updated AS (UPDATE children SET parent_id = 2 WHERE id = 10 RETURNING id, parent_id) SELECT id, parent_id FROM updated

// Materialized CTE
$query = with(
cte(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The Update Query Builder provides a fluent, type-safe interface for constructing
```php
<?php

use function Flow\PostgreSql\DSL\{update, literal, col, eq, literal};
use function Flow\PostgreSql\DSL\{update, literal, col, eq};

$query = update()
->update('users')
Expand Down Expand Up @@ -45,7 +45,7 @@ echo $query->toSql();
```php
<?php

use function Flow\PostgreSql\DSL\{update, literal, col, eq, literal};
use function Flow\PostgreSql\DSL\{update, literal, col, eq};

// Chained set() calls
$query = update()
Expand Down Expand Up @@ -75,7 +75,7 @@ echo $query->toSql();
```php
<?php

use function Flow\PostgreSql\DSL\{update, literal, col, eq, literal};
use function Flow\PostgreSql\DSL\{update, literal, col, eq};

$query = update()
->update('users', 'u')
Expand Down Expand Up @@ -144,12 +144,31 @@ echo $query->toSql();
// UPDATE products SET price = original_price WHERE id = 1
```

## Update with CTE

```php
<?php

use function Flow\PostgreSql\DSL\{with, cte, delete, select, col, table, eq, literal, in_subquery};

$query = with(
cte('moved', delete()->from('children')->where(eq(col('parent_id'), literal(1)))->returning(col('id')))
)
->update()
->update('audit')
->set('note', literal('moved'))
->where(in_subquery(col('id'), select(col('id'))->from(table('moved'))));

echo $query->toSql();
// WITH moved AS (DELETE FROM children WHERE parent_id = 1 RETURNING id) UPDATE audit SET note = 'moved' WHERE id IN (SELECT id FROM moved)
```

## RETURNING Clause

```php
<?php

use function Flow\PostgreSql\DSL\{update, literal, col, eq, literal};
use function Flow\PostgreSql\DSL\{update, literal, col, eq};

// Return specific columns
$query = update()
Expand All @@ -170,6 +189,17 @@ $query = update()

echo $query->toSql();
// UPDATE users SET name = 'John' WHERE id = 1 RETURNING *

// Old and new values through renamed aliases
$query = update()
->update('children')
->set('parent_id', literal(2))
->where(eq(col('id'), literal(10)))
->returningAliases(old: 'o', new: 'n')
->returning(col('parent_id', 'o'), col('parent_id', 'n'));

echo $query->toSql();
// UPDATE children SET parent_id = 2 WHERE id = 10 RETURNING WITH (OLD AS o, NEW AS n) o.parent_id, n.parent_id
```

## Complex WHERE Conditions
Expand Down
Loading