use of net.sf.jsqlparser.statement.create.index.CreateIndex in project dbeaver by dbeaver.
the class SQLQuery method parseQuery.
private void parseQuery() {
if (parsed) {
return;
}
parsed = true;
try {
if (CommonUtils.isEmpty(text)) {
this.statement = null;
this.parseError = new DBException("Empty query");
return;
}
statement = SQLSemanticProcessor.parseQuery(dataSource == null ? null : dataSource.getSQLDialect(), text);
if (statement instanceof Select) {
type = SQLQueryType.SELECT;
// Detect single source table (no joins, no group by, no sub-selects)
SelectBody selectBody = ((Select) statement).getSelectBody();
if (selectBody instanceof PlainSelect) {
PlainSelect plainSelect = (PlainSelect) selectBody;
if (plainSelect.getFromItem() instanceof Table && CommonUtils.isEmpty(plainSelect.getJoins()) && (plainSelect.getGroupBy() == null || CommonUtils.isEmpty(plainSelect.getGroupBy().getGroupByExpressions())) && CommonUtils.isEmpty(plainSelect.getIntoTables())) {
boolean hasSubSelects = false;
for (SelectItem si : plainSelect.getSelectItems()) {
if (si instanceof SelectExpressionItem && ((SelectExpressionItem) si).getExpression() instanceof SubSelect) {
hasSubSelects = true;
break;
}
}
if (!hasSubSelects) {
fillSingleSource((Table) plainSelect.getFromItem());
}
}
// Extract select items info
final List<SelectItem> items = plainSelect.getSelectItems();
if (items != null && !items.isEmpty()) {
selectItems = new ArrayList<>();
for (SelectItem item : items) {
selectItems.add(new SQLSelectItem(item));
}
}
}
} else if (statement instanceof Insert) {
type = SQLQueryType.INSERT;
fillSingleSource(((Insert) statement).getTable());
} else if (statement instanceof Update) {
type = SQLQueryType.UPDATE;
Table table = ((Update) statement).getTable();
if (table != null) {
fillSingleSource(table);
}
} else if (statement instanceof Delete) {
type = SQLQueryType.DELETE;
if (((Delete) statement).getTable() != null) {
fillSingleSource(((Delete) statement).getTable());
} else {
List<Table> tables = ((Delete) statement).getTables();
if (tables != null && tables.size() == 1) {
fillSingleSource(tables.get(0));
}
}
} else if (statement instanceof Alter || statement instanceof CreateTable || statement instanceof CreateView || statement instanceof Drop || statement instanceof CreateIndex) {
type = SQLQueryType.DDL;
} else {
type = SQLQueryType.UNKNOWN;
}
} catch (Throwable e) {
this.type = SQLQueryType.UNKNOWN;
this.parseError = e;
// log.debug("Error parsing SQL query [" + query + "]:" + CommonUtils.getRootCause(e).getMessage());
}
}
Aggregations