Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/paupino/pg_parse
PostgreSQL parser for Rust that uses the actual PostgreSQL server source to parse SQL queries and return the internal PostgreSQL parse tree.
https://github.com/paupino/pg_parse
postgresql rust
Last synced: 3 days ago
JSON representation
PostgreSQL parser for Rust that uses the actual PostgreSQL server source to parse SQL queries and return the internal PostgreSQL parse tree.
- Host: GitHub
- URL: https://github.com/paupino/pg_parse
- Owner: paupino
- License: mit
- Created: 2021-02-15T21:03:05.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2024-01-23T20:41:36.000Z (11 months ago)
- Last Synced: 2024-12-04T09:11:53.877Z (25 days ago)
- Topics: postgresql, rust
- Language: Rust
- Homepage:
- Size: 155 KB
- Stars: 76
- Watchers: 2
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
pg_parse [![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%2Fpaupino%2Fpg_parse%2Fbadge&label=build&logo=none
[actions]: https://actions-badge.atrox.dev/paupino/pg_parse/goto
[Latest Version]: https://img.shields.io/crates/v/pg_parse.svg
[crates.io]: https://crates.io/crates/pg_parse
[Docs Badge]: https://docs.rs/pg_parse/badge.svg
[docs]: https://docs.rs/pg_parsePostgreSQL parser for Rust that uses the [actual PostgreSQL server source]((https://github.com/pganalyze/libpg_query)) to parse
SQL queries and return the internal PostgreSQL parse tree.## Getting started
Add the following to your `Cargo.toml`
```toml
[dependencies]
pg_parse = "0.11"
```## Example: Parsing a query
```rust
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`](https://github.com/pganalyze/) organization maintains the official implementation: [`pg_query.rs`](https://github.com/pganalyze/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](https://github.com/lfittl) for all of his amazing work creating [libpg_query](https://github.com/pganalyze/libpg_query).