提交 e862609d authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Simplify execution flow in Query.initExpression()

上级 cbb4d2ac
...@@ -451,83 +451,67 @@ public abstract class Query extends Prepared { ...@@ -451,83 +451,67 @@ public abstract class Query extends Prepared {
// (oracle supports it, but only in order by, not in group by and // (oracle supports it, but only in order by, not in group by and
// not in having): // not in having):
// SELECT 1 AS A FROM DUAL ORDER BY -A // SELECT 1 AS A FROM DUAL ORDER BY -A
boolean isAlias = false;
int idx = expressions.size();
if (e instanceof ExpressionColumn) { if (e instanceof ExpressionColumn) {
// order by expression // order by expression
ExpressionColumn exprCol = (ExpressionColumn) e; ExpressionColumn exprCol = (ExpressionColumn) e;
String tableAlias = exprCol.getOriginalTableAliasName(); String tableAlias = exprCol.getOriginalTableAliasName();
String col = exprCol.getOriginalColumnName(); String col = exprCol.getOriginalColumnName();
for (int j = 0; j < visible; j++) { for (int j = 0; j < visible; j++) {
boolean found = false;
Expression ec = expressions.get(j); Expression ec = expressions.get(j);
if (ec instanceof ExpressionColumn) { if (ec instanceof ExpressionColumn) {
// select expression // select expression
ExpressionColumn c = (ExpressionColumn) ec; ExpressionColumn c = (ExpressionColumn) ec;
found = db.equalsIdentifiers(col, c.getColumnName()); if (!db.equalsIdentifiers(col, c.getColumnName())) {
if (found && tableAlias != null) { continue;
}
if (tableAlias == null) {
return j;
}
String ca = c.getOriginalTableAliasName(); String ca = c.getOriginalTableAliasName();
if (ca == null) { if (ca != null) {
found = false; if (db.equalsIdentifiers(ca, tableAlias)) {
if (filters != null) { return j;
}
} else if (filters != null) {
// select id from test order by test.id // select id from test order by test.id
for (TableFilter f : filters) { for (TableFilter f : filters) {
if (db.equalsIdentifiers(f.getTableAlias(), tableAlias)) { if (db.equalsIdentifiers(f.getTableAlias(), tableAlias)) {
found = true; return j;
break;
}
} }
} }
} else {
found = db.equalsIdentifiers(ca, tableAlias);
} }
} else if (ec instanceof Alias) {
if (tableAlias == null && db.equalsIdentifiers(col, ec.getAlias())) {
return j;
} }
} else if (!(ec instanceof Alias)) {
continue;
} else if (tableAlias == null && db.equalsIdentifiers(col, ec.getAlias())) {
found = true;
} else {
Expression ec2 = ec.getNonAliasExpression(); Expression ec2 = ec.getNonAliasExpression();
if (ec2 instanceof ExpressionColumn) { if (ec2 instanceof ExpressionColumn) {
ExpressionColumn c2 = (ExpressionColumn) ec2; ExpressionColumn c2 = (ExpressionColumn) ec2;
String ta = exprCol.getSQL(); String ta = exprCol.getSQL();
String tb = c2.getSQL(); String tb = c2.getSQL();
String s2 = c2.getColumnName(); String s2 = c2.getColumnName();
found = db.equalsIdentifiers(col, s2); if (db.equalsIdentifiers(col, s2) && db.equalsIdentifiers(ta, tb)) {
if (!db.equalsIdentifiers(ta, tb)) { return j;
found = false;
} }
} }
} }
if (found) {
idx = j;
isAlias = true;
break;
} }
} } else if (expressionSQL != null) {
} else {
String s = e.getSQL(); String s = e.getSQL();
if (expressionSQL != null) {
for (int j = 0, size = expressionSQL.size(); j < size; j++) { for (int j = 0, size = expressionSQL.size(); j < size; j++) {
String s2 = expressionSQL.get(j); if (db.equalsIdentifiers(expressionSQL.get(j), s)) {
if (db.equalsIdentifiers(s2, s)) { return j;
idx = j;
isAlias = true;
break;
} }
} }
} }
}
if (!isAlias) {
if (expressionSQL == null if (expressionSQL == null
|| mustBeInResult && session.getDatabase().getMode().getEnum() != ModeEnum.MySQL || mustBeInResult && session.getDatabase().getMode().getEnum() != ModeEnum.MySQL
&& !checkOrderOther(session, e, expressionSQL)) { && !checkOrderOther(session, e, expressionSQL)) {
throw DbException.get(ErrorCode.ORDER_BY_NOT_IN_RESULT, e.getSQL()); throw DbException.get(ErrorCode.ORDER_BY_NOT_IN_RESULT, e.getSQL());
} }
int idx = expressions.size();
expressions.add(e); expressions.add(e);
String sql = e.getSQL(); expressionSQL.add(e.getSQL());
expressionSQL.add(sql);
}
return idx; return idx;
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论