Does anyone know about a SQL statements parser, in Java, that would allow having an Object representation of a SQL query, allow modifying this representation and generate back the updated SQL statement?
Regards, Christophe
Does anyone know about a SQL statements parser, in Java, that would allow having an Object representation of a SQL query, allow modifying this representation and generate back the updated SQL statement?
Regards, Christophe
I would think that ANTLR would be able to do this.
Maybe you could look at JSqlParser.
This is a demo use Java SQL Parser do something like this:
Input SQL:
SELECT A as A_Alias, B AS B_Alias FROM TABLE_X
If you need to remove the second column “B AS B_Alias” from the select list, just do something like this:
columns.removeResultColumn(1); // 0 is the first column
then you will get this new SQL(the , was removed automatically):
SELECT A as A_Alias FROM TABLE_X
this demo also illustrates how to replace a column, Add criteria (where clause), Add Order by clause and etc.