1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//! Rust pg_query   [![Build Status]][actions] [![Latest Version]][crates.io] [![Docs Badge]][docs]
//! ===========
//!
//! [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
//! [actions]: https://actions-badge.atrox.dev/pganalyze/pg_query.rs/goto?ref=main
//! [Latest Version]: https://img.shields.io/crates/v/pg_query.svg
//! [crates.io]: https://crates.io/crates/pg_query
//! [Docs Badge]: https://docs.rs/pg_query/badge.svg
//! [docs]: https://docs.rs/pg_query
//!
//! 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](https://github.com/pganalyze/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](https://pganalyze.com/blog/parse-postgresql-queries-in-ruby.html). The Rust version tries to have a very similar API.
//!
//! ## Getting started
//!
//! Add the following to your `Cargo.toml`
//!
//! ```toml
//! [dependencies]
//! pg_query = "5.0"
//! ```
//!
//! # Example: Parsing a query
//!
//! ```rust
//! 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(_)));
//! ```
//!

mod bindings;
mod error;
mod node_enum;
mod node_mut;
mod node_ref;
mod node_structs;
mod parse_result;
mod query;
mod truncate;

pub use error::*;
pub use node_enum::*;
pub use node_mut::*;
pub use node_ref::*;
pub use node_structs::*;
pub use query::*;
pub use truncate::*;

pub mod protobuf {
    include!(concat!(env!("OUT_DIR"), "/pg_query.rs"));
}

pub use protobuf::Node;

// From Postgres source: src/include/storage/lockdefs.h
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
#[repr(i32)]
pub enum LockMode {
    NoLock = 0,                   // NoLock is not a lock mode, but a flag value meaning "don't get a lock"
    AccessShareLock = 1,          // SELECT
    RowShareLock = 2,             // SELECT FOR UPDATE/FOR SHARE
    RowExclusiveLock = 3,         // INSERT, UPDATE, DELETE
    ShareUpdateExclusiveLock = 4, // VACUUM (non-FULL), ANALYZE, CREATE INDEX CONCURRENTLY
    ShareLock = 5,                // CREATE INDEX (WITHOUT CONCURRENTLY)
    ShareRowExclusiveLock = 6,    // like EXCLUSIVE MODE, but allows ROW SHARE
    ExclusiveLock = 7,            // blocks ROW SHARE/SELECT...FOR UPDATE
    AccessExclusiveLock = 8,      // ALTER TABLE, DROP TABLE, VACUUM FULL, and unqualified LOCK TABLE
}

// From Postgres source: src/include/catalog/pg_trigger.h
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
#[repr(i32)]
pub enum TriggerType {
    Row = 1,
    Before = 2,
    Insert = 4,
    Delete = 8,
    Update = 16,
    Truncate = 32,
    Instead = 64,
}