|
| 1 | +# PG Query |
| 2 | + |
| 3 | +- [⬅️️ Back](/documentation/introduction.md) |
| 4 | + |
| 5 | +PostgreSQL Query Parser library provides strongly-typed AST (Abstract Syntax Tree) parsing for PostgreSQL SQL queries using the [libpg_query](https://github.com/pganalyze/libpg_query) library through a PHP extension. |
| 6 | + |
| 7 | +This library wraps the low-level extension functions and provides: |
| 8 | +- Strongly-typed AST nodes generated from protobuf definitions |
| 9 | +- A `Parser` class for object-oriented access |
| 10 | +- DSL helper functions for convenient usage |
| 11 | + |
| 12 | +## Requirements |
| 13 | + |
| 14 | +This library requires the `pg_query` PHP extension. See [pg-query-ext documentation](/documentation/components/extensions/pg-query-ext.md) for installation instructions. |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +``` |
| 19 | +composer require flow-php/pg-query:~--FLOW_PHP_VERSION-- |
| 20 | +``` |
| 21 | + |
| 22 | +## Usage |
| 23 | + |
| 24 | +### Using the Parser Class |
| 25 | + |
| 26 | +```php |
| 27 | +<?php |
| 28 | + |
| 29 | +use Flow\PgQuery\Parser; |
| 30 | + |
| 31 | +$parser = new Parser(); |
| 32 | + |
| 33 | +// Parse SQL into AST |
| 34 | +$result = $parser->parse('SELECT id, name FROM users WHERE active = true'); |
| 35 | + |
| 36 | +// Access the AST |
| 37 | +foreach ($result->getStmts() as $stmt) { |
| 38 | + $node = $stmt->getStmt(); |
| 39 | + $selectStmt = $node->getSelectStmt(); |
| 40 | + // Work with strongly-typed AST nodes... |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +### Using DSL Functions |
| 45 | + |
| 46 | +```php |
| 47 | +<?php |
| 48 | + |
| 49 | +use function Flow\PgQuery\DSL\pg_parse; |
| 50 | +use function Flow\PgQuery\DSL\pg_parser; |
| 51 | +use function Flow\PgQuery\DSL\pg_fingerprint; |
| 52 | +use function Flow\PgQuery\DSL\pg_normalize; |
| 53 | +use function Flow\PgQuery\DSL\pg_split; |
| 54 | + |
| 55 | +// Parse SQL |
| 56 | +$result = pg_parse('SELECT * FROM users'); |
| 57 | + |
| 58 | +// Get a reusable parser instance |
| 59 | +$parser = pg_parser(); |
| 60 | + |
| 61 | +// Generate fingerprint |
| 62 | +$fingerprint = pg_fingerprint('SELECT id FROM users WHERE id = 1'); |
| 63 | + |
| 64 | +// Normalize query |
| 65 | +$normalized = pg_normalize('SELECT * FROM users WHERE id = 1'); |
| 66 | + |
| 67 | +// Split multiple statements |
| 68 | +$statements = pg_split('SELECT 1; SELECT 2;'); |
| 69 | +``` |
| 70 | + |
| 71 | +## Features |
| 72 | + |
| 73 | +### Query Parsing |
| 74 | + |
| 75 | +Parse PostgreSQL SQL into a strongly-typed AST: |
| 76 | + |
| 77 | +```php |
| 78 | +<?php |
| 79 | + |
| 80 | +use Flow\PgQuery\Parser; |
| 81 | + |
| 82 | +$parser = new Parser(); |
| 83 | +$result = $parser->parse('SELECT id, name FROM users WHERE active = true ORDER BY name'); |
| 84 | + |
| 85 | +foreach ($result->getStmts() as $stmt) { |
| 86 | + $selectStmt = $stmt->getStmt()->getSelectStmt(); |
| 87 | + |
| 88 | + // Access FROM clause |
| 89 | + foreach ($selectStmt->getFromClause() as $fromItem) { |
| 90 | + $rangeVar = $fromItem->getRangeVar(); |
| 91 | + echo "Table: " . $rangeVar->getRelname() . "\n"; |
| 92 | + } |
| 93 | + |
| 94 | + // Access target list (SELECT columns) |
| 95 | + foreach ($selectStmt->getTargetList() as $target) { |
| 96 | + $columnRef = $target->getResTarget()->getVal()->getColumnRef(); |
| 97 | + // Process column references... |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +### Query Fingerprinting |
| 103 | + |
| 104 | +Generate unique fingerprints for structurally equivalent queries. This is useful for grouping similar queries regardless of their literal values: |
| 105 | + |
| 106 | +```php |
| 107 | +<?php |
| 108 | + |
| 109 | +use Flow\PgQuery\Parser; |
| 110 | + |
| 111 | +$parser = new Parser(); |
| 112 | + |
| 113 | +// These queries produce the same fingerprint |
| 114 | +$fp1 = $parser->fingerprint('SELECT * FROM users WHERE id = 1'); |
| 115 | +$fp2 = $parser->fingerprint('SELECT * FROM users WHERE id = 999'); |
| 116 | + |
| 117 | +var_dump($fp1 === $fp2); // true |
| 118 | +``` |
| 119 | + |
| 120 | +### Query Normalization |
| 121 | + |
| 122 | +Replace literal values with parameter placeholders: |
| 123 | + |
| 124 | +```php |
| 125 | +<?php |
| 126 | + |
| 127 | +use Flow\PgQuery\Parser; |
| 128 | + |
| 129 | +$parser = new Parser(); |
| 130 | + |
| 131 | +$normalized = $parser->normalize("SELECT * FROM users WHERE name = 'John' AND age = 25"); |
| 132 | +// Returns: SELECT * FROM users WHERE name = $1 AND age = $2 |
| 133 | +``` |
| 134 | + |
| 135 | +### Statement Splitting |
| 136 | + |
| 137 | +Split a string containing multiple SQL statements: |
| 138 | + |
| 139 | +```php |
| 140 | +<?php |
| 141 | + |
| 142 | +use Flow\PgQuery\Parser; |
| 143 | + |
| 144 | +$parser = new Parser(); |
| 145 | + |
| 146 | +$statements = $parser->split('SELECT 1; SELECT 2; SELECT 3'); |
| 147 | +// Returns: ['SELECT 1', ' SELECT 2', ' SELECT 3'] |
| 148 | +``` |
| 149 | + |
| 150 | +## API Reference |
| 151 | + |
| 152 | +### Parser Class |
| 153 | + |
| 154 | +| Method | Description | Returns | |
| 155 | +|--------|-------------|---------| |
| 156 | +| `parse(string $sql)` | Parse SQL into AST | `ParseResult` | |
| 157 | +| `fingerprint(string $sql)` | Generate query fingerprint | `?string` | |
| 158 | +| `normalize(string $sql)` | Normalize query with placeholders | `?string` | |
| 159 | +| `split(string $sql)` | Split multiple statements | `array<string>` | |
| 160 | + |
| 161 | +### DSL Functions |
| 162 | + |
| 163 | +| Function | Description | Returns | |
| 164 | +|----------|-------------|---------| |
| 165 | +| `pg_parser()` | Create a new Parser instance | `Parser` | |
| 166 | +| `pg_parse(string $sql)` | Parse SQL into AST | `ParseResult` | |
| 167 | +| `pg_fingerprint(string $sql)` | Generate query fingerprint | `?string` | |
| 168 | +| `pg_normalize(string $sql)` | Normalize query | `?string` | |
| 169 | +| `pg_split(string $sql)` | Split statements | `array<string>` | |
| 170 | + |
| 171 | +## AST Node Types |
| 172 | + |
| 173 | +The library includes 343 strongly-typed AST node classes generated from PostgreSQL's protobuf definitions. All classes are in the `Flow\PgQuery\Protobuf\AST` namespace. |
| 174 | + |
| 175 | +Common node types include: |
| 176 | +- `SelectStmt` - SELECT statement |
| 177 | +- `InsertStmt` - INSERT statement |
| 178 | +- `UpdateStmt` - UPDATE statement |
| 179 | +- `DeleteStmt` - DELETE statement |
| 180 | +- `ColumnRef` - Column reference |
| 181 | +- `A_Expr` - Expression node |
| 182 | +- `FuncCall` - Function call |
| 183 | +- `JoinExpr` - JOIN expression |
| 184 | +- `RangeVar` - Table/view reference |
| 185 | + |
| 186 | +## Exception Handling |
| 187 | + |
| 188 | +```php |
| 189 | +<?php |
| 190 | + |
| 191 | +use Flow\PgQuery\Parser; |
| 192 | +use Flow\PgQuery\Exception\ParserException; |
| 193 | +use Flow\PgQuery\Exception\ExtensionNotLoadedException; |
| 194 | + |
| 195 | +try { |
| 196 | + $parser = new Parser(); |
| 197 | +} catch (ExtensionNotLoadedException $e) { |
| 198 | + // pg_query extension is not loaded |
| 199 | +} |
| 200 | + |
| 201 | +try { |
| 202 | + $result = $parser->parse('INVALID SQL SYNTAX HERE'); |
| 203 | +} catch (ParserException $e) { |
| 204 | + echo "Parse error: " . $e->getMessage(); |
| 205 | +} |
| 206 | +``` |
| 207 | + |
| 208 | +## Performance |
| 209 | + |
| 210 | +For optimal protobuf parsing performance, install the `ext-protobuf` PHP extension: |
| 211 | + |
| 212 | +```bash |
| 213 | +pecl install protobuf |
| 214 | +``` |
| 215 | + |
| 216 | +The library will work without it using the pure PHP implementation from `google/protobuf`, but the native extension provides significantly better performance for AST deserialization. |
0 commit comments