Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Library to parse SQL statements

I would like to be able to parse an arbitrary SQL SELECT statement and retrieve the various component parts (columns, relations, JOIN conditions, WHERE conditions, ORDER BY columns), ideally using Delphi. A quick google search shows several different freeware products, but it's not clear if they are complete and/or under active development.

My immediate need to just to extract the list of relations used in a series of VIEW definitions in order ensure that required views or tables exist before I try to CREATE the view. So, for instance, for the statement:

SELECT PersonID, LastName, OrderID 
FROM People P INNER JOIN Orders O ON P.PersonID = O.PersonID

I need to get back the values "People" and "Orders". (Obviously, this is a simple example. I want to be able to handle more complex cases where, for instance, the word "FROM" might appear in the column list as part of an expression).

I'm trying to provide this service in a database that allows use of STDCALL functions exported from DLLs, so ideally any candidate library would be callable from Delphi or C.

like image 928
Larry Lustig Avatar asked Dec 04 '09 15:12

Larry Lustig


People also ask

How SQL queries are parsed?

SQL Parsing The parsing stage involves separating the pieces of a SQL statement into a data structure that other routines can process. The database parses a statement when instructed by the application, which means that only the application, and not the database itself, can reduce the number of parses.

How do you parse a SQL statement in Python?

Quick Start. >>> import sqlparse >>> # Split a string containing two SQL statements: >>> raw = 'select * from foo; select * from bar;' >>> statements = sqlparse.

What is an SQL parser?

The SQL Parser parses a SQL query in a string field. When parsing a query, the processor generates fields based on the fields defined in the SQL query and specifies the CRUD operation, table, and schema information in record header attributes.

Can you parse data in SQL?

The SQL PARSE function is a Conversions Function used to convert the String data to the requested data type and returns the result as an expression. It is recommended to use this SQL PARSE function to convert the string data to either Date time, or Number type.


1 Answers

Take a look at Gold Parser. It's got a Delphi version available, and SQL grammar on the download page.

like image 169
Mason Wheeler Avatar answered Oct 01 '22 08:10

Mason Wheeler