@@ -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
0 commit comments