Using the Mozilla SQL Parser — Part 3
In this article, see part three of how to use the Mozilla SQL parser.
Join the DZone community and get the full member experience.Join For Free
In Part 2, of the series on the Mozilla SQL Parser, we covered how we can identify the tables used in the from clause. As is obvious from the select example give below, names of tables can also appear in the where clause.
In addition to the 'select' and 'from' keys created by the moz_sql_parser, it creates the 'where' key for the where clause. By traversing the where clause, we will be able to identify the tables used in it.
It should be noted that a where clause can have additional nesting. Hence, we need to have a recursive mechanism to traverse through all the depths and collect the table references. It is important to note that the where clause also contains a from clause. So we need to include that functionality as well. Oh yes! The where clause can also contain a select clause.
The method to identify the tables mentioned in the where clause is given below.
By using the code mentioned in the three-part series, it is possible to put together a program that allows us to parse Select SQL queries using the Moz SQL Parser. Parsing — for a typical programmer — is tough task made easy by the parser. Once we have the SQL statement parsed into a navigable structure, it is up to us to traverse it and extract the required information.
Opinions expressed by DZone contributors are their own.