lelit/pglast

Expose libpg_query deparse

Closed this issue · 5 comments

libpg_query now supports deparse/unparse, with very good coverage.

pganalyze/libpg_query@aa05c85#diff-a4d461fa40ad82727605dcf20cbb7b43f9a2023fbc1ecae058cddf5ba934899c

It would be nice to have that as an option.

lelit commented

It's already there, in the v3 branch. You must use the parse_sql_protobuf() function.

@lelit when we check out v3 branch seem parse_sql_protobuf/deparse not defined yet. so how can we use this feature ? or is it not mature for production use ?

In [1]: import pglast

In [2]: dir(pglast)
Out[2]: 
['Error',
 'Missing',
 'Node',
 '__all__',
 '__author__',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__path__',
 '__spec__',
 '__version__',
 'ast',
 'enums',
 'error',
 'fingerprint',
 'get_postgresql_version',
 'keywords',
 'node',
 'parse_plpgsql',
 'parse_sql',
 'parser',
 'prettify',
 'printer',
 'printers',
 'split']

Thanks
hong

lelit commented

The functions you need are implemented by pglast.parser, so for example:

>>> x = pglast.parser.parse_sql_protobuf('select 1')
>>> pglast.parser.deparse(x)
'SELECT 1'
lelit commented

There's currently no higher level support to that though, in other words, there's no way to map the protobuf stream content to the classes in pglast.ast.

@lelit thanks for info.