|
1 | | -# PG Query |
| 1 | +# PostgreSQL |
2 | 2 |
|
3 | 3 | - [⬅️️ Back](/documentation/introduction.md) |
4 | 4 | - [📚API Reference](/documentation/api/lib/postgresql) |
|
9 | 9 |
|
10 | 10 | ## Overview |
11 | 11 |
|
12 | | -PG Query is a PostgreSQL query library with two main capabilities: |
| 12 | +PostgreSQL library provides three main capabilities: |
13 | 13 |
|
14 | 14 | 1. **SQL Parser** - Parse, analyze, and modify existing PostgreSQL queries using the real PostgreSQL |
15 | 15 | parser ([libpg_query](https://github.com/pganalyze/libpg_query)) |
16 | 16 | 2. **Query Builder** - Build new queries programmatically with a fluent, type-safe API |
| 17 | +3. **Client** - Execute queries against PostgreSQL with automatic type conversion and object mapping |
17 | 18 |
|
18 | | -Both features produce valid PostgreSQL syntax and can be combined - for example, parse an existing query, modify it, and |
19 | | -convert it back to SQL. |
| 19 | +All features produce valid PostgreSQL syntax and can be combined - for example, build a query with the Query Builder, |
| 20 | +execute it with the Client, and map results to objects. |
20 | 21 |
|
21 | 22 | ## Use Case Navigator |
22 | 23 |
|
23 | | -| I want to... | Go to | |
24 | | -|------------------------------------|-------------------------------------------| |
25 | | -| Build SQL queries with type safety | [Query Builder](#query-builder) | |
26 | | -| Parse and analyze existing SQL | [SQL Parser](#sql-parser) | |
27 | | -| Add pagination to existing queries | [Query Modification](#query-modification) | |
28 | | -| Traverse or modify AST directly | [Advanced Features](#advanced-features) | |
| 24 | +| I want to... | Go to | Extension needed | |
| 25 | +|------------------------------------|-------------------------------------------|------------------| |
| 26 | +| Execute queries and fetch results | [Client](#client) | `ext-pgsql` | |
| 27 | +| Build SQL queries with type safety | [Query Builder](#query-builder) | `ext-pg_query` | |
| 28 | +| Parse and analyze existing SQL | [SQL Parser](#sql-parser) | `ext-pg_query` | |
| 29 | +| Add pagination to existing queries | [Query Modification](#query-modification) | `ext-pg_query` | |
| 30 | +| Traverse or modify AST directly | [Advanced Features](#advanced-features) | `ext-pg_query` | |
29 | 31 |
|
30 | 32 | ## Requirements |
31 | 33 |
|
32 | | -This library requires the `pg_query` PHP extension. |
33 | | -See [postgresql-ext documentation](/documentation/components/extensions/postgresql-ext.md) for installation instructions. |
| 34 | +This library has two optional PHP extensions depending on which features you use: |
| 35 | + |
| 36 | +| Extension | Required for | Installation | |
| 37 | +|----------------|-------------------------------------------------------|-----------------------------------------------------------------------------------------| |
| 38 | +| `ext-pgsql` | Client (database connections, query execution) | Usually bundled with PHP, or `apt install php-pgsql` | |
| 39 | +| `ext-pg_query` | Query Builder, SQL Parser (AST parsing, manipulation) | [postgresql-ext documentation](/documentation/components/extensions/postgresql-ext.md) | |
| 40 | + |
| 41 | +Both extensions are optional - you can use the Client without installing `ext-pg_query`, and vice versa. |
34 | 42 |
|
35 | 43 | ## Installation |
36 | 44 |
|
@@ -245,6 +253,50 @@ echo $query->toSQL(); |
245 | 253 |
|
246 | 254 | --- |
247 | 255 |
|
| 256 | +## Client |
| 257 | + |
| 258 | +Execute queries against PostgreSQL with automatic type conversion and object mapping. |
| 259 | + |
| 260 | +### Quick Start |
| 261 | + |
| 262 | +```php |
| 263 | +<?php |
| 264 | + |
| 265 | +use function Flow\PostgreSql\DSL\{pgsql_client, pgsql_connection}; |
| 266 | + |
| 267 | +// Connect |
| 268 | +$client = pgsql_client(pgsql_connection('host=localhost dbname=mydb user=postgres')); |
| 269 | + |
| 270 | +// Fetch single row |
| 271 | +$user = $client->fetch('SELECT * FROM users WHERE id = $1', [1]); |
| 272 | + |
| 273 | +// Fetch all rows |
| 274 | +$users = $client->fetchAll('SELECT * FROM users WHERE active = $1', [true]); |
| 275 | + |
| 276 | +// Execute INSERT/UPDATE/DELETE |
| 277 | +$affected = $client->execute('UPDATE users SET active = $1 WHERE id = $2', [false, 1]); |
| 278 | + |
| 279 | +// Transaction with automatic commit/rollback |
| 280 | +$result = $client->transaction(function ($client) { |
| 281 | + $client->execute('INSERT INTO users (name) VALUES ($1)', ['John']); |
| 282 | + return $client->fetchScalar('SELECT lastval()'); |
| 283 | +}); |
| 284 | + |
| 285 | +// Close when done |
| 286 | +$client->close(); |
| 287 | +``` |
| 288 | + |
| 289 | +### Detailed Documentation |
| 290 | + |
| 291 | +- [Connection](/documentation/components/libs/postgresql/client-connection.md) - Connection parameters, DSN parsing, lifecycle |
| 292 | +- [Fetching Data](/documentation/components/libs/postgresql/client-fetching.md) - fetch, fetchOne, fetchAll, fetchScalar |
| 293 | +- [Object Mapping](/documentation/components/libs/postgresql/client-object-mapping.md) - Map rows to objects with RowMapper |
| 294 | +- [Cursors](/documentation/components/libs/postgresql/client-cursor.md) - Memory-efficient streaming for large result sets |
| 295 | +- [Transactions](/documentation/components/libs/postgresql/client-transactions.md) - Transaction callback pattern, nesting |
| 296 | +- [Type System](/documentation/components/libs/postgresql/client-types.md) - Value converters, TypedValue, custom types |
| 297 | + |
| 298 | +--- |
| 299 | + |
248 | 300 | ## Query Modification |
249 | 301 |
|
250 | 302 | Modify existing SQL queries programmatically - useful for adding pagination to queries. |
|
0 commit comments