#sql-query #parse-tree #postgresql #sql-parser #source #internal #ast-parser

pg_parse

PostgreSQL parser that uses the actual PostgreSQL server source to parse SQL queries and return the internal PostgreSQL parse tree

6 releases (breaking)

0.11.0 Jul 27, 2023
0.10.0 Nov 24, 2022
0.9.1 Jun 7, 2022
0.8.0 Feb 20, 2022
0.7.0 Jan 20, 2022

#1487 in Database interfaces

MIT license

17MB
333K SLoC

C 164K SLoC // 0.1% comments C++ 92K SLoC // 0.1% comments SQL 68K SLoC // 0.2% comments Rust 8K SLoC // 0.0% comments Ruby 1.5K SLoC // 0.0% comments

pg_parse   Build Status Latest Version Docs Badge

PostgreSQL parser for Rust that uses the actual PostgreSQL server source to parse SQL queries and return the internal PostgreSQL parse tree.

Getting started

Add the following to your Cargo.toml

[dependencies]
pg_parse = "0.11"

Example: Parsing a query

use pg_parse::ast::Node;

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

// We can also convert back to a string, if the `str` feature is enabled (enabled by default).
#[cfg(feature = "str")]
assert_eq!(result[0].to_string(), "SELECT * FROM contacts");

What's the difference between pg_parse and pg_query.rs?

The pganalyze organization maintains the official implementation: pg_query.rs. This closely resembles the name of the C library also published by the team (libpg_query). This implementation uses the protobuf interface introduced with version 13 of libpg_query.

This library similarly consumes libpg_query however utilizes the older JSON interface to manage parsing. The intention of this library is to maintain a dependency "light" implementation with serde and serde_json being the only required runtime dependencies.

So which one should you use? You probably want to use the official pg_query.rs library as that will continue to be kept closely up to date with libpg_query updates. This library will continue to be maintained however may not be as up-to-date as the official implementation.

Credits

A huge thank you to Lukas Fittl for all of his amazing work creating libpg_query.

Dependencies