pg_query/
lib.rs

1//! Rust pg_query   [![Build Status]][actions] [![Latest Version]][crates.io] [![Docs Badge]][docs]
2//! ===========
3//!
4//! [Build Status]: https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fpganalyze%2Fpg_query.rs%2Fbadge%3Fref%3Dmain&style=flat&label=build&logo=none
5//! [actions]: https://actions-badge.atrox.dev/pganalyze/pg_query.rs/goto?ref=main
6//! [Latest Version]: https://img.shields.io/crates/v/pg_query.svg
7//! [crates.io]: https://crates.io/crates/pg_query
8//! [Docs Badge]: https://docs.rs/pg_query/badge.svg
9//! [docs]: https://docs.rs/pg_query
10//!
11//! This Rust library uses the actual PostgreSQL server source to parse SQL queries and return the internal PostgreSQL parse tree.
12//!
13//! It also allows you to normalize queries (replacing constant values with $1, etc.) and parse these normalized queries into a parse tree again.
14//!
15//! When you build this library, it builds parts of the PostgreSQL server source (see [libpg_query](https://github.com/pganalyze/libpg_query)), and then statically links it into this library.
16//!
17//! You can find further examples and a longer rationale for the original Ruby implementation [here](https://pganalyze.com/blog/parse-postgresql-queries-in-ruby.html). The Rust version tries to have a very similar API.
18//!
19//! ## Getting started
20//!
21//! Add the following to your `Cargo.toml`
22//!
23//! ```toml
24//! [dependencies]
25//! pg_query = "5.1"
26//! ```
27//!
28//! # Example: Parsing a query
29//!
30//! ```rust
31//! use pg_query::NodeRef;
32//!
33//! let result = pg_query::parse("SELECT * FROM contacts");
34//! assert!(result.is_ok());
35//! let result = result.unwrap();
36//! assert_eq!(result.tables(), vec!["contacts"]);
37//! assert!(matches!(result.protobuf.nodes()[0].0, NodeRef::SelectStmt(_)));
38//! ```
39//!
40
41mod bindings;
42mod error;
43mod node_enum;
44mod node_mut;
45mod node_ref;
46mod node_structs;
47mod parse_result;
48#[rustfmt::skip]
49pub mod protobuf;
50mod query;
51mod truncate;
52
53pub use error::*;
54pub use node_enum::*;
55pub use node_mut::*;
56pub use node_ref::*;
57pub use parse_result::*;
58pub use query::*;
59pub use truncate::*;
60
61pub use protobuf::Node;
62
63// From Postgres source: src/include/storage/lockdefs.h
64#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
65#[repr(i32)]
66pub enum LockMode {
67    NoLock = 0,                   // NoLock is not a lock mode, but a flag value meaning "don't get a lock"
68    AccessShareLock = 1,          // SELECT
69    RowShareLock = 2,             // SELECT FOR UPDATE/FOR SHARE
70    RowExclusiveLock = 3,         // INSERT, UPDATE, DELETE
71    ShareUpdateExclusiveLock = 4, // VACUUM (non-FULL), ANALYZE, CREATE INDEX CONCURRENTLY
72    ShareLock = 5,                // CREATE INDEX (WITHOUT CONCURRENTLY)
73    ShareRowExclusiveLock = 6,    // like EXCLUSIVE MODE, but allows ROW SHARE
74    ExclusiveLock = 7,            // blocks ROW SHARE/SELECT...FOR UPDATE
75    AccessExclusiveLock = 8,      // ALTER TABLE, DROP TABLE, VACUUM FULL, and unqualified LOCK TABLE
76}
77
78// From Postgres source: src/include/catalog/pg_trigger.h
79#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
80#[repr(i32)]
81pub enum TriggerType {
82    Row = 1,
83    Before = 2,
84    Insert = 4,
85    Delete = 8,
86    Update = 16,
87    Truncate = 32,
88    Instead = 64,
89}