Skip to content

Commit 7820c8c

Browse files
committed
feature: update documentation
1 parent a09f0f7 commit 7820c8c

File tree

18 files changed

+1589
-25
lines changed

18 files changed

+1589
-25
lines changed

documentation/components/libs/postgresql.md

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# PG Query
1+
# PostgreSQL
22

33
- [⬅️️ Back](/documentation/introduction.md)
44
- [📚API Reference](/documentation/api/lib/postgresql)
@@ -9,28 +9,36 @@
99

1010
## Overview
1111

12-
PG Query is a PostgreSQL query library with two main capabilities:
12+
PostgreSQL library provides three main capabilities:
1313

1414
1. **SQL Parser** - Parse, analyze, and modify existing PostgreSQL queries using the real PostgreSQL
1515
parser ([libpg_query](https://github.com/pganalyze/libpg_query))
1616
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
1718

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.
2021

2122
## Use Case Navigator
2223

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` |
2931

3032
## Requirements
3133

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.
3442

3543
## Installation
3644

@@ -245,6 +253,50 @@ echo $query->toSQL();
245253

246254
---
247255

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+
248300
## Query Modification
249301

250302
Modify existing SQL queries programmatically - useful for adding pagination to queries.
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# Client Connection
2+
3+
- [⬅️ Back](/documentation/components/libs/postgresql.md)
4+
5+
[TOC]
6+
7+
The PostgreSQL client uses the `ext-pgsql` extension for database connectivity. Connection parameters can be specified
8+
using connection strings, DSN URLs, or individual parameters.
9+
10+
## Connection String
11+
12+
The most common approach using libpq-style connection strings:
13+
14+
```php
15+
<?php
16+
17+
use function Flow\PostgreSql\DSL\{pgsql_client, pgsql_connection};
18+
19+
// Basic connection
20+
$client = pgsql_client(
21+
pgsql_connection('host=localhost dbname=mydb user=postgres password=secret')
22+
);
23+
24+
// With additional options
25+
$client = pgsql_client(
26+
pgsql_connection('host=localhost port=5432 dbname=mydb user=postgres sslmode=require')
27+
);
28+
29+
// Unix socket connection
30+
$client = pgsql_client(
31+
pgsql_connection('host=/var/run/postgresql dbname=mydb user=postgres')
32+
);
33+
```
34+
35+
## DSN URL
36+
37+
Parse standard PostgreSQL DSN format commonly used in environment variables:
38+
39+
```php
40+
<?php
41+
42+
use function Flow\PostgreSql\DSL\{pgsql_client, pgsql_connection_dsn};
43+
44+
// Standard PostgreSQL DSN
45+
$client = pgsql_client(
46+
pgsql_connection_dsn('postgres://myuser:secret@localhost:5432/mydb')
47+
);
48+
49+
// With SSL mode
50+
$client = pgsql_client(
51+
pgsql_connection_dsn('postgresql://user:[email protected]/app?sslmode=require')
52+
);
53+
54+
// Symfony/Doctrine format
55+
$client = pgsql_client(
56+
pgsql_connection_dsn('pgsql://user:pass@localhost/mydb')
57+
);
58+
59+
// From environment variable
60+
$client = pgsql_client(
61+
pgsql_connection_dsn(getenv('DATABASE_URL'))
62+
);
63+
```
64+
65+
Supported schemes: `postgres://`, `postgresql://`, `pgsql://`
66+
67+
## Explicit Parameters
68+
69+
For better type safety and IDE support:
70+
71+
```php
72+
<?php
73+
74+
use function Flow\PostgreSql\DSL\{pgsql_client, pgsql_connection_params};
75+
76+
$client = pgsql_client(
77+
pgsql_connection_params(
78+
database: 'mydb',
79+
host: 'localhost',
80+
port: 5432,
81+
user: 'myuser',
82+
password: 'secret',
83+
)
84+
);
85+
86+
// With additional libpq options
87+
$client = pgsql_client(
88+
pgsql_connection_params(
89+
database: 'mydb',
90+
host: 'db.example.com',
91+
user: 'myuser',
92+
password: 'secret',
93+
options: [
94+
'sslmode' => 'require',
95+
'connect_timeout' => '10',
96+
],
97+
)
98+
);
99+
```
100+
101+
## Connection Lifecycle
102+
103+
### Checking Connection Status
104+
105+
```php
106+
<?php
107+
108+
if ($client->isConnected()) {
109+
// Connection is alive
110+
}
111+
```
112+
113+
### Closing Connections
114+
115+
Always close connections when done:
116+
117+
```php
118+
<?php
119+
120+
$client->close();
121+
```
122+
123+
For long-running applications, consider connection pooling at the infrastructure level (e.g., PgBouncer).
124+
125+
## Connection Parameters Reference
126+
127+
| Parameter | Description | Default |
128+
|-----------------------|--------------------------------------------------------------------|------------------|
129+
| `host` | Server hostname or IP address | `localhost` |
130+
| `port` | Server port | `5432` |
131+
| `dbname` / `database` | Database name | (required) |
132+
| `user` | Username | (optional) |
133+
| `password` | Password | (optional) |
134+
| `sslmode` | SSL mode (disable, allow, prefer, require, verify-ca, verify-full) | `prefer` |
135+
| `connect_timeout` | Connection timeout in seconds | (system default) |
136+
| `application_name` | Application name for pg_stat_activity | (none) |
137+
138+
For the full list of libpq connection parameters, see
139+
the [PostgreSQL documentation](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS).

0 commit comments

Comments
 (0)