pg_query.rs   ![Build Status] ![Latest Version] ![Docs Badge]

This Rust library uses the actual PostgreSQL server source to parse SQL queries and return the internal PostgreSQL parse tree.

It also allows you to normalize queries (replacing constant values with ?) and parse these normalized queries into a parse tree again.

When you build this library, it builds parts of the PostgreSQL server source (see libpg_query), and then statically links it into this library.

This is slightly crazy, but is the only reliable way of parsing all valid PostgreSQL queries.

You can find further examples and a longer rationale for the original Ruby implementation here. The Rust version tries to have a very similar API.

Getting started

Add the following to your Cargo.toml

toml [dependencies] pg_query = "0.7"

Examples

Parsing a query

```rust use pg_query::ast::Node;

let result = pgquery::parse("SELECT * FROM contacts"); assert!(result.isok()); let result = result.unwrap(); assert!(matches!(*&result[0], Node::SelectStmt(_))); ```

Normalizing a query

rust let result = pg_query::normalize("SELECT 1 FROM x WHERE y = (SELECT 123 FROM a WHERE z = 'bla')").unwrap(); assert_eq!(result, "SELECT $1 FROM x WHERE y = (SELECT $2 FROM a WHERE z = $3)");

Fingerprinting a query

rust let result = pg_query::fingerprint("SELECT * FROM contacts.person WHERE id IN (1, 2, 3, 4);").unwrap(); assert_eq!(result.hex, "643d2a3c294ab8a7");

Truncating a query

rust let query = "INSERT INTO \"x\" (a, b, c, d, e, f) VALUES (?)"; let result = pg_query::parse(query).unwrap(); assert_eq!(result.truncate(32).unwrap(), "INSERT INTO x (...) VALUES (?)");

Credits

Thanks to Paul Mason for his work on pg_parse that this crate is based on.

After version 0.6.0, Paul donated the pgquery crate to the pganalyze team. pgparse is a lighter alternative that focuses on query parsing, while pg_query aims for feaure parity with the Ruby gem.