IntroductionΒΆ

At the lower level the module exposes several libpg_query functions:

  1. pglast.parser.parse_sql_json()

  2. pglast.parser.parse_sql_protobuf()

  3. pglast.parser.parse_plpgsql_json()

  4. pglast.parser.fingerprint()

  5. pglast.parser.normalize()

  6. pglast.parser.scan()

  7. pglast.parser.split()

  8. pglast.parser.deparse_protobuf()

The first two take an SQL statement and return the correspondent parse tree respectively as a JSON encoded value and a Protobuf encoded value; the third function takes a PLpgSQL statement and returns the parse tree as JSON, the fourth returns an hash of the given statement that can be used to compare different SQLs, the fifth replaces the constants in the statement with $n parameters, the sixth returns a sequence of tokens that compose a SQL statement, the seventh returns a sequence of the single statements and the last one accepts a Protobuf-serialized statement and reproduce the original SQL statement.

One more function, pglast.parser.parse_sql(), is similar to parse_sql_json() but instead of JSON returns the syntax tree represented by a hierarchy of instances of the classes implemented in the pglast.ast module.

On top of that, the module implements two serializations, one that transforms a Node into a raw textual representation and another that returns a prettified <pglast.stream.IndentedStream representation. The latter is exposed by the pgpp CLI tool, see the Command line section of the examples of usage.