I am looking for an open source API in Java to parse an SQL / HQL query so that it gives me the column names and the table names used in it. I tried using JSQLParser which gives me the table names used in the query. But I don't see a support for getting the column names used in the query. Is there any other API which can help me with this? I am aware of General SQL parser but it seems to be a paid one.
Here is the code snippet using JSQLParser which gives me the table names in the query:
Statement statement;
try
{
statement = CCJSqlParserUtil.parse( " SELECT * FROM ( ( SELECT TBL.ID AS rRowId, TBL.NAME AS name, TBL.DESCRIPTION as description, TBL.TYPE AS type, TBL1.SHORT_NAME AS shortName FROM ROLE_TBL TBL WHERE ( TBL.TYPE = 'CORE' OR TBL1.SHORT_NAME = 'TNG' AND TBL.IS_DELETED <> 1 ) ) MINUS ( SELECT TBL.ID AS rRowId, TBL.NAME AS name, TBL.DESCRIPTION as description, TBL.TYPE AS type, TBL3.SHORT_NAME AS shortName,TBL3.NAME AS tenantName FROM ROLE_TBL TBL INNER JOIN TYPE_ROLE_TBL TBL1 ON TBL.ID=TBL1.ROLE_FK LEFT OUTER JOIN TNT_TBL TBL3 ON TBL3.ID = TBL.TENANT_FK LEFT OUTER JOIN USER_TBL TBL4 ON TBL4.ID = TBL1.USER_FK WHERE ( TBL4.ID =771100 AND TBL.IS_DELETED <> 1 ) ) ) ORDER BY name ASC" );
Select selectStatement = (Select) statement;
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList( selectStatement );
System.out.println( tableList.size() );
for( String s : tableList )
System.out.println( s );
}
catch( JSQLParserException e )
{
e.printStackTrace();
}
Thanks in advance for any help in this regard.