Crate pg_query

Source
Expand description

§Rust pg_query   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 $1, etc.) 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.

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

[dependencies]
pg_query = "5.1"

§Example: Parsing a query

use pg_query::NodeRef;

let result = pg_query::parse("SELECT * FROM contacts");
assert!(result.is_ok());
let result = result.unwrap();
assert_eq!(result.tables(), vec!["contacts"]);
assert!(matches!(result.protobuf.nodes()[0].0, NodeRef::SelectStmt(_)));

Re-exports§

pub use protobuf::Node;
pub use protobuf::node::Node as NodeEnum;

Modules§

protobuf

Structs§

Fingerprint
Represents the resulting fingerprint containing both the raw integer form as well as the corresponding 16 character hex value.
ParseResult
Result from calling parse

Enums§

Context
Error
Error structure representing the basic error scenarios for pg_query.
LockMode
NodeMut
NodeRef
TriggerType

Functions§

deparse
Converts a parsed tree back into a string.
fingerprint
Fingerprints the given SQL statement. Useful for comparing parse trees across different implementations of libpg_query.
normalize
Normalizes the given SQL statement, returning a parametized version.
parse
Parses the given SQL statement into the given abstract syntax tree.
parse_plpgsql
An experimental API which parses a PLPGSQL function. This currently returns the raw JSON structure.
scan
Scan a sql query into a its component of tokens.
split_with_parser
Split a well-formed query into separate statements.
split_with_scanner
Split a potentially-malformed query into separate statements. Note that invalid tokens will be skipped
truncate

Type Aliases§

Result
Convenient Result alias for returning pg_query::Error.