Skip to content

Commit 7ccf021

Browse files
committed
chore: update pg-query and pg-query-ext documnetaion
1 parent 087b1d5 commit 7ccf021

File tree

2 files changed

+127
-3
lines changed

2 files changed

+127
-3
lines changed

documentation/components/extensions/pg-query-ext.md

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ This extension provides low-level functions for parsing PostgreSQL SQL queries.
88

99
## Features
1010

11-
- Parse PostgreSQL SQL queries into JSON AST
11+
- Parse PostgreSQL SQL queries into JSON or protobuf AST
1212
- Generate query fingerprints for query grouping
1313
- Normalize SQL queries (replace literals with placeholders)
14+
- Deparse AST back to SQL with optional pretty-printing
1415
- Parse PL/pgSQL functions
1516
- Split multiple SQL statements
1617
- Scan SQL into tokens
18+
- Generate query summaries for logging/monitoring
1719

1820
## Requirements
1921

@@ -70,6 +72,9 @@ php -d extension=/path/to/pg_query.so your_script.php
7072
$json = pg_query_parse('SELECT * FROM users WHERE id = 1');
7173
$ast = json_decode($json, true);
7274

75+
// Parse SQL and return protobuf AST (more efficient for programmatic use)
76+
$protobuf = pg_query_parse_protobuf('SELECT * FROM users WHERE id = 1');
77+
7378
// Generate fingerprint (same for structurally equivalent queries)
7479
$fp = pg_query_fingerprint('SELECT * FROM users WHERE id = 1');
7580
// Returns same fingerprint for: SELECT * FROM users WHERE id = 2
@@ -78,6 +83,9 @@ $fp = pg_query_fingerprint('SELECT * FROM users WHERE id = 1');
7883
$normalized = pg_query_normalize("SELECT * FROM users WHERE name = 'John'");
7984
// Returns: SELECT * FROM users WHERE name = $1
8085

86+
// Normalize utility/DDL statements
87+
$normalized = pg_query_normalize_utility('CREATE TABLE users (id INT, name VARCHAR(255))');
88+
8189
// Split multiple statements
8290
$statements = pg_query_split('SELECT 1; SELECT 2; SELECT 3');
8391
// Returns: ['SELECT 1', ' SELECT 2', ' SELECT 3']
@@ -92,19 +100,59 @@ $plpgsql = pg_query_parse_plpgsql('
92100
');
93101

94102
// Scan SQL into tokens (returns protobuf data)
95-
$protobuf = pg_query_scan('SELECT 1');
103+
$tokens = pg_query_scan('SELECT 1');
104+
105+
// Deparse protobuf AST back to SQL
106+
$protobuf = pg_query_parse_protobuf('SELECT id, name FROM users WHERE active = true');
107+
$sql = pg_query_deparse($protobuf);
108+
// Returns: SELECT id, name FROM users WHERE active = true
109+
110+
// Deparse with pretty-printing options
111+
$sql = pg_query_deparse_opts(
112+
$protobuf,
113+
true, // pretty_print
114+
4, // indent_size
115+
80, // max_line_length
116+
false, // trailing_newline
117+
false // commas_start_of_line
118+
);
119+
// Returns:
120+
// SELECT id, name
121+
// FROM users
122+
// WHERE active = true
123+
124+
// Generate query summary (protobuf format, useful for logging)
125+
$summary = pg_query_summary('SELECT * FROM users WHERE id = 1');
96126
```
97127

98128
## Functions Reference
99129

100130
| Function | Description | Returns |
101131
|----------|-------------|---------|
102132
| `pg_query_parse(string $sql)` | Parse SQL to JSON AST | `string` (JSON) |
133+
| `pg_query_parse_protobuf(string $sql)` | Parse SQL to protobuf AST | `string` (protobuf) |
103134
| `pg_query_fingerprint(string $sql)` | Generate query fingerprint | `string\|false` |
104135
| `pg_query_normalize(string $sql)` | Normalize query with placeholders | `string\|false` |
136+
| `pg_query_normalize_utility(string $sql)` | Normalize DDL/utility statements | `string\|false` |
105137
| `pg_query_parse_plpgsql(string $sql)` | Parse PL/pgSQL function | `string` (JSON) |
106138
| `pg_query_split(string $sql)` | Split multiple statements | `array<string>` |
107139
| `pg_query_scan(string $sql)` | Scan SQL into tokens | `string` (protobuf) |
140+
| `pg_query_deparse(string $protobuf)` | Convert protobuf AST back to SQL | `string` |
141+
| `pg_query_deparse_opts(...)` | Deparse with formatting options | `string` |
142+
| `pg_query_summary(string $sql, int $options, int $truncate)` | Generate query summary | `string` (protobuf) |
143+
144+
### pg_query_deparse_opts Parameters
145+
146+
```php
147+
pg_query_deparse_opts(
148+
string $protobuf, // Protobuf AST from pg_query_parse_protobuf()
149+
bool $pretty_print = false, // Enable pretty printing
150+
int $indent_size = 4, // Spaces per indentation level
151+
int $max_line_length = 80, // Maximum line length before wrapping
152+
bool $trailing_newline = false, // Add trailing newline
153+
bool $commas_start_of_line = false // Place commas at line start
154+
): string
155+
```
108156

109157
## Error Handling
110158

documentation/components/libs/pg-query.md

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,53 @@ $normalized = $parser->normalize("SELECT * FROM users WHERE name = 'John'");
6868
$normalized = $parser->normalize('SELECT * FROM users WHERE id = :id');
6969
// Returns: SELECT * FROM users WHERE id = $1
7070

71+
// Normalize utility/DDL statements
72+
$normalized = $parser->normalizeUtility('CREATE TABLE users (id INT, name VARCHAR(255))');
73+
7174
// Split multiple statements
7275
$statements = $parser->split('SELECT 1; SELECT 2;');
7376
// Returns: ['SELECT 1', ' SELECT 2']
77+
78+
// Generate query summary (protobuf format, useful for logging)
79+
$summary = $parser->summary('SELECT * FROM users WHERE id = 1');
7480
```
7581

7682
## DSL Functions
7783

7884
```php
7985
<?php
8086

81-
use function Flow\PgQuery\DSL\{pg_parse, pg_parser, pg_fingerprint, pg_normalize, pg_split};
87+
use function Flow\PgQuery\DSL\{
88+
pg_parse,
89+
pg_parser,
90+
pg_fingerprint,
91+
pg_normalize,
92+
pg_normalize_utility,
93+
pg_split,
94+
pg_deparse,
95+
pg_deparse_options,
96+
pg_format,
97+
pg_summary
98+
};
8299

83100
$query = pg_parse('SELECT * FROM users');
84101
$parser = pg_parser();
85102
$fingerprint = pg_fingerprint('SELECT * FROM users WHERE id = 1');
86103
$normalized = pg_normalize('SELECT * FROM users WHERE id = 1');
104+
$normalizedDdl = pg_normalize_utility('CREATE TABLE users (id INT)');
87105
$statements = pg_split('SELECT 1; SELECT 2;');
106+
$summary = pg_summary('SELECT * FROM users');
107+
108+
// Deparse (convert AST back to SQL)
109+
$sql = pg_deparse($query); // Simple output
110+
$sql = pg_deparse($query, pg_deparse_options()->indentSize(2)); // Pretty printed
111+
112+
// Format SQL (parse + deparse with pretty printing)
113+
$formatted = pg_format('SELECT id,name FROM users WHERE active=true');
114+
// Returns:
115+
// SELECT id, name
116+
// FROM users
117+
// WHERE active = true
88118
```
89119

90120
## ParsedQuery Methods
@@ -94,9 +124,55 @@ $statements = pg_split('SELECT 1; SELECT 2;');
94124
| `tables()` | Get all tables referenced in the query | `array<Table>` |
95125
| `columns(?string $tableName)` | Get columns, optionally filtered by table/alias | `array<Column>` |
96126
| `functions()` | Get all function calls | `array<FunctionCall>` |
127+
| `deparse(?DeparseOptions $options)` | Convert AST back to SQL string | `string` |
97128
| `traverse(NodeVisitor ...$visitors)` | Traverse AST with custom visitors | `void` |
98129
| `raw()` | Access underlying protobuf ParseResult | `ParseResult` |
99130

131+
## Deparsing (AST to SQL)
132+
133+
Convert a parsed query back to SQL, optionally with pretty-printing:
134+
135+
```php
136+
<?php
137+
138+
use Flow\PgQuery\{DeparseOptions, Parser};
139+
140+
$parser = new Parser();
141+
$query = $parser->parse('SELECT u.id, u.name FROM users u JOIN orders o ON u.id = o.user_id WHERE u.active = true');
142+
143+
// Simple deparse (compact output)
144+
$sql = $query->deparse();
145+
// Returns: SELECT u.id, u.name FROM users u JOIN orders o ON u.id = o.user_id WHERE u.active = true
146+
147+
// Pretty-printed output
148+
$sql = $query->deparse(DeparseOptions::new());
149+
// Returns:
150+
// SELECT u.id, u.name
151+
// FROM
152+
// users u
153+
// JOIN orders o ON u.id = o.user_id
154+
// WHERE u.active = true
155+
156+
// Custom formatting options
157+
$sql = $query->deparse(
158+
DeparseOptions::new()
159+
->indentSize(2) // 2 spaces per indent level
160+
->maxLineLength(60) // Wrap at 60 characters
161+
->trailingNewline() // Add newline at end
162+
->commasStartOfLine() // Place commas at line start
163+
);
164+
```
165+
166+
### DeparseOptions
167+
168+
| Method | Description | Default |
169+
|--------|-------------|---------|
170+
| `prettyPrint(bool)` | Enable/disable pretty printing | `true` |
171+
| `indentSize(int)` | Spaces per indentation level | `4` |
172+
| `maxLineLength(int)` | Maximum line length before wrapping | `80` |
173+
| `trailingNewline(bool)` | Add trailing newline at end | `false` |
174+
| `commasStartOfLine(bool)` | Place commas at start of lines | `false` |
175+
100176
## Custom AST Traversal
101177

102178
For advanced use cases, you can traverse the AST with custom visitors:

0 commit comments

Comments
 (0)