What language does a good SQL parsing library have?

I am looking for a good SQL parser. One of them will work with subqueries, nonselectable queries, CTEs, window functions, and other legal SQL elements.

The result will be some sort of abstract syntax tree that I could subsequently work on.

The language is mostly irrelevant, since I am ready to learn a new language only to use the library, if it exists.

I know that it is technically possible to extract the parser from some open source database, but it is far from easy (at least for the PostgreSQL analyzer that I need).

+4
source share
3 answers

Python has a non-validating SQL parser: python-sqlparse . Tokens are exposed as objects. I doubt that they support "other legal SQL statements", window functions, etc., though, since they are controlled by specific provider grammars and none of the providers are fully compatible with SQL standards.

Um (knowing you're ready to learn a new language), why do you need to work with the syntax tree? If you need the magic of working with a database, you probably don't need to reinvent the wheel: Python got a fantastic database toolkit - SQL ALchemy .

+2
source

You can use "sql parser" for Google. This is the one indicated: Generic SQL Parser Here are some highlighted functions listed on the official website:

  • Checking the syntax of stand-alone SQL
  • Highly customizable SQL formatter
  • In-depth SQL script analysis
  • Full access to the SQL query parsing tree
  • Custom SQL engine for various databases
  • Main programming language support

This is a commercial SQL library.

+1
source

Our DMS Software Reengineering Toolkit contains the complete PL / SQL and ANSI SQL 2011 parsers (for AST) and prettyprinters (AST returns to the actual text). None of them are PostGres SQL, but the DMS has a dialect mechanism that makes it relatively easy to build a dialect. from basic grammar, revising only some grammar rules and keeping the rest. Doing this from SQL 2011 grammar seems like a practical way to solve this problem.

DMS also offers tools for accessing / passing through / modifying ASTs, both procedurally and in terms of syntax structures and transformations. Think of it as "living beyond parsing."

0
source

Source: https://habr.com/ru/post/1379464/


All Articles