tags:

views:

140

answers:

2

In our system we have separate API to build query where we need to pass each column and table name separately like below:

dw.setCOLUMN("Column1");
dw.setCOLUMN("Column2");
dw.setTABLE("TableName");

If I would get chance I will remove this API and go with simple SQL query execution (like many Java application does) but due to the existing code and design I am helpless to make any change.

If I have a query like "SELECT function('test'), column1 FROM table" I want to parse the column names separately like function('test') and column1. Are there any API to parse SQL query and return column names and table names separately?

+4  A: 

ANTLR has several different SQL grammars available; one of them may work for your depending on your SQL dialect.

ChssPly76
A: 

You might want to try Squiggle, which is a library for building SQL statements in Java, for example:

Table people = new Table("people");

SelectQuery select = new SelectQuery(people);

select.addColumn(people, "firstname");
select.addColumn(people, "lastname");

select.addOrder(people, "age", Order.DESCENDING);

System.out.println(select);
Supertux