Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deconstruct an SQL Statement in Java

Tags:

java

sql

jdbc

I have some legacy SQL and I want to split it up into it's constituent parts & then add more criteria, an order by clause, etc.

Are there any existing Java libraries out there that interpret SQL like this?

So I want to do something like

deconstructedSQL.getCriteria().add("price > 100");

or

deconstructedSQL.getOrderBy().add("price");

or

String select = deconstructedSQL.getSelect();
if ("*".equals(select))
{
    deconstructedSQL.setSelect("my_table.*");
}

I can use Dialects from Hibernate to add info specific to my database for paging (setFirstResult & setMaxResult), but I'd like to take it a step further.

like image 427
Harry Lime Avatar asked Sep 21 '26 14:09

Harry Lime


1 Answers

I would use a sql parser (javacc, jparsec, antlr) to break up the string into an AST. Once you have it in this kind of structure you can play with it as you describe and emit the modified version as a string in the end.

like image 135
Ron Avatar answered Sep 24 '26 04:09

Ron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!