I am using JSQLParser to select a set of SQL queries. These queries and generated by hibernate.
I have an issue when Hibernate generates an insert query followed by select scope_identity(). Because JSQLParser is parsing the insert part, and then generates an exception
insert into MYTABLE(LOG_ACTION, LOG_APPLICATION, LOG_CATEGORY) values (?, ?, ?) select scope_identity().
The following exception is thrown: Encountered unexpected token: "select" <K_SELECT> at line 1, column 338.
Was expecting one of:
","
";"
"ON"
"RETURNING"
<EOF>
How could I parse such a query without getting this error?
I have the following code:
if (sqlStatement.toLowerCase().startsWith(INSERT)) {
return new InsertParser(sqlStatement);
And then in InsertParser:
private void preprocessInsert() {
try {
Insert insert = (Insert) CCJSqlParserUtil.parse(sqlStatement);
// Set the table
Table table = new Table();
table.setName(insert.getTable().getName());
// Now get the parameters
List<net.sf.jsqlparser.schema.Column> columns = insert.getColumns();
for (net.sf.jsqlparser.schema.Column column : columns) {
Parameter parameter = new Parameter();
mycompany.jdbc.parser.Column pColumn = new mycompany.jdbc.parser.Column();
pColumn.setName(column.getColumnName());
pColumn.setTable(table);
parameter.setColumn(pColumn);
parameters.add(parameter);
}
} catch (JSQLParserException e) {
LOGGER.error("Preprocesing error: " + e.getMessage());
}
}