|
|
@@ -91,7 +91,7 @@ public class SQLQuery implements SQLScriptElement {
|
|
|
private List<SQLSelectItem> selectItems;
|
|
|
private String queryTitle;
|
|
|
private String extraErrorMessage;
|
|
|
- private List<String> allSelectEntitiesNames = new ArrayList<>();
|
|
|
+ private final List<String> allSelectEntitiesNames = new ArrayList<>();
|
|
|
|
|
|
public SQLQuery(@Nullable DBPDataSource dataSource, @NotNull String text) {
|
|
|
this(dataSource, text, 0, text.length());
|
|
|
@@ -129,6 +129,7 @@ public class SQLQuery implements SQLScriptElement {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Nullable
|
|
|
public DBPDataSource getDataSource() {
|
|
|
return dataSource;
|
|
|
}
|
|
|
@@ -145,31 +146,27 @@ public class SQLQuery implements SQLScriptElement {
|
|
|
return;
|
|
|
}
|
|
|
statement = SQLSemanticProcessor.parseQuery(dataSource == null ? null : dataSource.getSQLDialect(), text);
|
|
|
- if (statement instanceof Select) {
|
|
|
+ if (statement instanceof PlainSelect plainSelect) {
|
|
|
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;
|
|
|
+ {
|
|
|
FromItem fromItem = plainSelect.getFromItem();
|
|
|
-
|
|
|
- if (fromItem instanceof SubSelect &&
|
|
|
+ if (fromItem instanceof ParenthesedSelect ps &&
|
|
|
isPotentiallySingleSourceSelect(plainSelect) &&
|
|
|
- ((SubSelect) fromItem).getSelectBody() instanceof PlainSelect &&
|
|
|
- isPotentiallySingleSourceSelect((PlainSelect) ((SubSelect) fromItem).getSelectBody()))
|
|
|
- {
|
|
|
+ ps.getPlainSelect() != null &&
|
|
|
+ isPotentiallySingleSourceSelect(ps.getPlainSelect())
|
|
|
+ ) {
|
|
|
// Real select is in sub-select
|
|
|
- plainSelect = (PlainSelect) ((SubSelect) fromItem).getSelectBody();
|
|
|
+ plainSelect = ps.getPlainSelect();
|
|
|
fromItem = plainSelect.getFromItem();
|
|
|
}
|
|
|
- if (fromItem instanceof Table fromTable &&
|
|
|
- isPotentiallySingleSourceSelect(plainSelect))
|
|
|
- {
|
|
|
- boolean hasSubSelects = false, hasDirectSelects = false;
|
|
|
- for (SelectItem si : plainSelect.getSelectItems()) {
|
|
|
- if (si instanceof SelectExpressionItem && ((SelectExpressionItem) si).getExpression() instanceof SubSelect) {
|
|
|
+ if (fromItem instanceof Table fromTable && isPotentiallySingleSourceSelect(plainSelect)) {
|
|
|
+ boolean hasSubSelects = false;
|
|
|
+ boolean hasDirectSelects = false;
|
|
|
+ for (SelectItem<?> si : plainSelect.getSelectItems()) {
|
|
|
+ if (si.getExpression() instanceof ParenthesedSelect) {
|
|
|
hasSubSelects = true;
|
|
|
- } else if (si instanceof SelectExpressionItem && ((SelectExpressionItem) si).getExpression() instanceof Column) {
|
|
|
+ } else if (si.getExpression() instanceof Column) {
|
|
|
hasDirectSelects = true;
|
|
|
}
|
|
|
}
|
|
|
@@ -230,10 +227,10 @@ public class SQLQuery implements SQLScriptElement {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private boolean isValidSelectItem(@NotNull SelectItem item) {
|
|
|
+ private boolean isValidSelectItem(@NotNull SelectItem<?> item) {
|
|
|
// Workaround for JSQLParser not respecting the `#` comment in MySQL and treating them as valid values
|
|
|
- if (item instanceof SelectExpressionItem && dataSource != null) {
|
|
|
- final Expression expr = ((SelectExpressionItem) item).getExpression();
|
|
|
+ if (dataSource != null) {
|
|
|
+ final Expression expr = item.getExpression();
|
|
|
if (expr instanceof Column) {
|
|
|
final String name = CommonUtils.trim(((Column) expr).getColumnName());
|
|
|
if (CommonUtils.isNotEmpty(name)) {
|
|
|
@@ -251,7 +248,7 @@ public class SQLQuery implements SQLScriptElement {
|
|
|
|
|
|
private boolean isPotentiallySingleSourceSelect(PlainSelect plainSelect) {
|
|
|
return CommonUtils.isEmpty(plainSelect.getJoins()) &&
|
|
|
- (plainSelect.getGroupBy() == null || CommonUtils.isEmpty(plainSelect.getGroupBy().getGroupByExpressionList().getExpressions())) &&
|
|
|
+ (plainSelect.getGroupBy() == null || CommonUtils.isEmpty(plainSelect.getGroupBy().getGroupByExpressionList())) &&
|
|
|
CommonUtils.isEmpty(plainSelect.getIntoTables());
|
|
|
}
|
|
|
|
|
|
@@ -295,12 +292,11 @@ public class SQLQuery implements SQLScriptElement {
|
|
|
*/
|
|
|
public boolean isPlainSelect() {
|
|
|
parseQuery();
|
|
|
- if (statement instanceof Select && ((Select) statement).getSelectBody() instanceof PlainSelect) {
|
|
|
- PlainSelect selectBody = (PlainSelect) ((Select) statement).getSelectBody();
|
|
|
- return CommonUtils.isEmpty(selectBody.getIntoTables()) &&
|
|
|
- selectBody.getLimit() == null &&
|
|
|
- selectBody.getTop() == null &&
|
|
|
- !selectBody.isForUpdate();
|
|
|
+ if (statement instanceof PlainSelect plainSelect) {
|
|
|
+ return CommonUtils.isEmpty(plainSelect.getIntoTables()) &&
|
|
|
+ plainSelect.getLimit() == null &&
|
|
|
+ plainSelect.getTop() == null &&
|
|
|
+ plainSelect.getForUpdateTable() == null;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
@@ -398,6 +394,7 @@ public class SQLQuery implements SQLScriptElement {
|
|
|
return parseError;
|
|
|
}
|
|
|
|
|
|
+ @Nullable
|
|
|
public List<SQLQueryParameter> getParameters() {
|
|
|
return parameters;
|
|
|
}
|
|
|
@@ -447,10 +444,10 @@ public class SQLQuery implements SQLScriptElement {
|
|
|
|
|
|
public DBCEntityMetaData getEntityMetadata(boolean raw) {
|
|
|
parseQuery();
|
|
|
- return raw? rawSingleTableMetadata : singleTableMeta;
|
|
|
+ return raw ? rawSingleTableMetadata : singleTableMeta;
|
|
|
}
|
|
|
|
|
|
- public void setParameters(List<SQLQueryParameter> parameters) {
|
|
|
+ public void setParameters(@Nullable List<SQLQueryParameter> parameters) {
|
|
|
this.parameters = parameters;
|
|
|
}
|
|
|
|
|
|
@@ -474,7 +471,7 @@ public class SQLQuery implements SQLScriptElement {
|
|
|
if (CommonUtils.isEmpty(this.extraErrorMessage)) {
|
|
|
this.extraErrorMessage = extraErrorMessage;
|
|
|
} else {
|
|
|
- this.extraErrorMessage = this.extraErrorMessage + System.getProperty(StandardConstants.ENV_LINE_SEPARATOR) + extraErrorMessage;
|
|
|
+ this.extraErrorMessage = this.extraErrorMessage + System.lineSeparator() + extraErrorMessage;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -499,14 +496,10 @@ public class SQLQuery implements SQLScriptElement {
|
|
|
if (statement == null) {
|
|
|
return false;
|
|
|
}
|
|
|
- if (statement instanceof Delete) {
|
|
|
- if (((Delete) statement).getWhere() == null) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- } else if (statement instanceof Update) {
|
|
|
- if (((Update) statement).getWhere() == null) {
|
|
|
- return true;
|
|
|
- }
|
|
|
+ if (statement instanceof Delete delete) {
|
|
|
+ return delete.getWhere() == null;
|
|
|
+ } else if (statement instanceof Update update) {
|
|
|
+ return update.getWhere() == null;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
@@ -521,12 +514,8 @@ public class SQLQuery implements SQLScriptElement {
|
|
|
if (getType() == SQLQueryType.UNKNOWN) {
|
|
|
return false;
|
|
|
}
|
|
|
- if (statement instanceof Select) {
|
|
|
- SelectBody selectBody = ((Select) statement).getSelectBody();
|
|
|
- if (selectBody instanceof PlainSelect plainSelectBody) {
|
|
|
- return plainSelectBody.isForUpdate() || plainSelectBody.getIntoTables() != null;
|
|
|
- }
|
|
|
- return false;
|
|
|
+ if (statement instanceof PlainSelect plainSelect) {
|
|
|
+ return plainSelect.getForUpdateTable() != null || plainSelect.getIntoTables() != null;
|
|
|
} else {
|
|
|
return true;
|
|
|
}
|
|
|
@@ -591,10 +580,9 @@ public class SQLQuery implements SQLScriptElement {
|
|
|
|
|
|
@Override
|
|
|
public boolean equals(Object obj) {
|
|
|
- if (!(obj instanceof SingleTableMeta)) {
|
|
|
+ if (!(obj instanceof SingleTableMeta md2)) {
|
|
|
return false;
|
|
|
}
|
|
|
- SingleTableMeta md2 = (SingleTableMeta) obj;
|
|
|
return CommonUtils.equalObjects(catalogName, md2.catalogName) &&
|
|
|
CommonUtils.equalObjects(schemaName, md2.schemaName) &&
|
|
|
CommonUtils.equalObjects(tableName, md2.tableName);
|