Expand SQL statement in Java

I have legacy SQL, and I want to break it down into its constituent parts, and then add additional criteria, order order, etc.

Are there any existing Java libraries that interpret SQL as follows?

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 the Dialects of Hibernate, to add information specific to my database for the swap ( setFirstResultand setMaxResult), but I would go one step further.

+3
source share
4 answers

sql (javacc, jparsec, antlr), AST. , , , , .

+2

,

+2

This is a demo that illustrates how to Deconstruct, Modify, Rebuild SQL statement using SQL Parser.

source SQL:

SELECT * FROM TABLE_X where f > 0

Java Code:

assertTrue(parser.parse() == 0);
TSelectSqlStatement select = (TSelectSqlStatement)parser.sqlstatements.get(0);
select.addWhereClause("c>1");

what are you getting:

SELECT * FROM TABLE_X where f > 0 and c>1

list selection, grouping, order proposal can also be easily changed.

+1
source

The Zsql structure corresponds to the exact description (not tested).

0
source

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


All Articles