Skip to content

flow-php/pg-query

Repository files navigation

PostgreSQL Query Parser

Flow PHP's PostgreSQL Query Parser library provides strongly-typed AST (Abstract Syntax Tree) parsing for PostgreSQL SQL queries using the libpg_query library through a PHP extension.

Installation

This library requires the pg_query PHP extension. Install it via PIE:

pie install flow-php/pg-query-ext

Then install the library via Composer:

composer require flow-php/pg-query

Usage

<?php

use Flow\PgQuery\Parser;
use function Flow\PgQuery\DSL\pg_parse;

// Using the Parser class directly
$parser = new Parser();
$result = $parser->parse('SELECT id, name FROM users WHERE active = true');

// Access the AST
foreach ($result->getStmts() as $stmt) {
    $node = $stmt->getStmt();
    // Work with the parsed statement...
}

// Or use DSL functions for convenience
$result = pg_parse('SELECT 1');

// Other utilities
$fingerprint = $parser->fingerprint('SELECT id FROM users WHERE id = 1');
$normalized = $parser->normalize('SELECT * FROM users WHERE id = 1'); // Returns: SELECT * FROM users WHERE id = $1
$statements = $parser->split('SELECT 1; SELECT 2;'); // Returns: ['SELECT 1', 'SELECT 2']

Features

  • Full PostgreSQL SQL parsing - Uses the actual PostgreSQL parser
  • Strongly-typed AST nodes - Generated from protobuf definitions
  • Query fingerprinting - Generate unique fingerprints for queries
  • Query normalization - Replace literals with parameter placeholders
  • Statement splitting - Split multiple SQL statements

Documentation

About

No description, website, or topics provided.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

  •  

Packages

No packages published

Languages