Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/skalt/psql_splitter
a nom parser combinator that matches a psql statement.
https://github.com/skalt/psql_splitter
parser parsing postgres postgresql psql sql
Last synced: 15 days ago
JSON representation
a nom parser combinator that matches a psql statement.
- Host: GitHub
- URL: https://github.com/skalt/psql_splitter
- Owner: SKalt
- License: bsd-3-clause
- Created: 2021-12-04T15:00:44.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-02-27T15:02:57.000Z (almost 3 years ago)
- Last Synced: 2024-10-27T07:52:24.831Z (2 months ago)
- Topics: parser, parsing, postgres, postgresql, psql, sql
- Language: Rust
- Homepage:
- Size: 20.5 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# psql_splitter
a [`nom`](https://github.com/Geal/nom) parser combinator that matches a psql statement.
Postgres has a dialect of SQL that I'm going to call pgsql.
Postgres also has a client, `psql`, which recognizes meta-commands and inline-copy syntax that normal pgsql does not.
This parser recognizes psql meta-commands and pgsql tokens, allowing the `statement` combinator to know when a statement has ended.
The `statement` combinator also attaches color commentary on the end of a line to the previous statement, so `select 1; -- take a number` is matched as one statement.## Status
This is a hack with some tests, a README, and a license.
This parser doesn't understand all the tokens in pgsql and psql, which has its pros:
- `psql_splitter::statement` is more likely to split statements out of invalid psql
- a simpler parser is easier to maintainbut also its cons:
- a simpler parser is more likely to be missing something about the enormous pgsql language
## Scope/features
- the splitter should match invalid psql, always splitting the input into one or more statements
- the splitter should treat psql meta-commands as statement terminators, e.g `select 1 as foo \gset` and `\d my_table` would both be complete statements.
- the splitter should ignore statement-terminators inside pgsql strings and parentheses and exotic psql such as `\if ... \endif` and `COPY table_name FROM STDIN; ... \.`
- the splitter should somewhat-reliably split psql and pgsql statements as defined by postgres, but not neccessarily other sql-like dialects derived from postgres's psql and pgsql.
- the splitter should be correct. Speed, a small binary, and memory efficiency aren't goals, but they're nice to have.## Contributing
If you find a bug, please submit an issue with a reproduction case.
PRs for tooling would also be welcome.## License
While this library is offered under the attached BSD 3-clause at ./LICENSE, `./src/*.sql` are excerpts from the postgres codebase, and so are covered by [the postgres license](https://www.postgresql.org/about/licence/).
## Prior art
[`libpg_query`'s `split_with_scanner`](https://github.com/pganalyze/libpg_query)
[`codeschool/sqlite-parser`'s test-suite-extraction tools](https://github.com/codeschool/sqlite-parser/blob/master/test/misc/test-grammar.pegjs)