Unverified 提交 81bb6b2e authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov 提交者: GitHub

Merge pull request #1272 from katzyn/misc

Reduce code duplication in Parser
...@@ -807,13 +807,7 @@ public class Parser { ...@@ -807,13 +807,7 @@ public class Parser {
do { do {
Column column = readTableColumn(filter); Column column = readTableColumn(filter);
read("="); read("=");
Expression expression; command.setAssignment(column, readExpressionOrDefault());
if (readIf("DEFAULT")) {
expression = ValueExpression.getDefault();
} else {
expression = readExpression();
}
command.setAssignment(column, expression);
} while (readIf(",")); } while (readIf(","));
} }
if (readIf("WHERE")) { if (readIf("WHERE")) {
...@@ -1105,18 +1099,8 @@ public class Parser { ...@@ -1105,18 +1099,8 @@ public class Parser {
} }
if (readIf("VALUES")) { if (readIf("VALUES")) {
do { do {
ArrayList<Expression> values = Utils.newSmallArrayList();
read("("); read("(");
if (!readIf(")")) { command.addRow(parseValuesForInsert());
do {
if (readIf("DEFAULT")) {
values.add(null);
} else {
values.add(readExpression());
}
} while (readIfMore(false));
}
command.addRow(values.toArray(new Expression[0]));
} while (readIf(",")); } while (readIf(","));
} else { } else {
command.setQuery(parseSelect()); command.setQuery(parseSelect());
...@@ -1294,13 +1278,7 @@ public class Parser { ...@@ -1294,13 +1278,7 @@ public class Parser {
} }
Column column = table.getColumn(columnName); Column column = table.getColumn(columnName);
read("="); read("=");
Expression expression; command.addAssignmentForDuplicate(column, readExpressionOrDefault());
if (readIf("DEFAULT")) {
expression = ValueExpression.getDefault();
} else {
expression = readExpression();
}
command.addAssignmentForDuplicate(column, expression);
} while (readIf(",")); } while (readIf(","));
} }
} }
...@@ -1334,17 +1312,7 @@ public class Parser { ...@@ -1334,17 +1312,7 @@ public class Parser {
} else if (readIf("VALUES")) { } else if (readIf("VALUES")) {
read("("); read("(");
do { do {
ArrayList<Expression> values = Utils.newSmallArrayList(); command.addRow(parseValuesForInsert());
if (!readIf(")")) {
do {
if (readIf("DEFAULT")) {
values.add(null);
} else {
values.add(readExpression());
}
} while (readIfMore(false));
}
command.addRow(values.toArray(new Expression[0]));
// the following condition will allow (..),; and (..); // the following condition will allow (..),; and (..);
} while (readIf(",") && readIf("(")); } while (readIf(",") && readIf("("));
} else if (readIf("SET")) { } else if (readIf("SET")) {
...@@ -1356,13 +1324,7 @@ public class Parser { ...@@ -1356,13 +1324,7 @@ public class Parser {
do { do {
columnList.add(parseColumn(table)); columnList.add(parseColumn(table));
read("="); read("=");
Expression expression; values.add(readExpressionOrDefault());
if (readIf("DEFAULT")) {
expression = ValueExpression.getDefault();
} else {
expression = readExpression();
}
values.add(expression);
} while (readIf(",")); } while (readIf(","));
command.setColumns(columnList.toArray(new Column[0])); command.setColumns(columnList.toArray(new Column[0]));
command.addRow(values.toArray(new Expression[0])); command.addRow(values.toArray(new Expression[0]));
...@@ -1392,18 +1354,8 @@ public class Parser { ...@@ -1392,18 +1354,8 @@ public class Parser {
} }
if (readIf("VALUES")) { if (readIf("VALUES")) {
do { do {
ArrayList<Expression> values = Utils.newSmallArrayList();
read("("); read("(");
if (!readIf(")")) { command.addRow(parseValuesForInsert());
do {
if (readIf("DEFAULT")) {
values.add(null);
} else {
values.add(readExpression());
}
} while (readIfMore(false));
}
command.addRow(values.toArray(new Expression[0]));
} while (readIf(",")); } while (readIf(","));
} else { } else {
command.setQuery(parseSelect()); command.setQuery(parseSelect());
...@@ -1411,6 +1363,20 @@ public class Parser { ...@@ -1411,6 +1363,20 @@ public class Parser {
return command; return command;
} }
private Expression[] parseValuesForInsert() {
ArrayList<Expression> values = Utils.newSmallArrayList();
if (!readIf(")")) {
do {
if (readIf("DEFAULT")) {
values.add(null);
} else {
values.add(readExpression());
}
} while (readIfMore(false));
}
return values.toArray(new Expression[0]);
}
private TableFilter readTableFilter() { private TableFilter readTableFilter() {
Table table; Table table;
String alias = null; String alias = null;
...@@ -1774,10 +1740,9 @@ public class Parser { ...@@ -1774,10 +1740,9 @@ public class Parser {
command.setSchemaName(readUniqueIdentifier()); command.setSchemaName(readUniqueIdentifier());
ifExists = readIfExists(ifExists); ifExists = readIfExists(ifExists);
command.setIfExists(ifExists); command.setIfExists(ifExists);
if (readIf("CASCADE")) { ConstraintActionType dropAction = parseCascadeOrRestrict();
command.setDropAction(ConstraintActionType.CASCADE); if (dropAction != null) {
} else if (readIf("RESTRICT")) { command.setDropAction(dropAction);
command.setDropAction(ConstraintActionType.RESTRICT);
} }
return command; return command;
} else if (readIf("ALL")) { } else if (readIf("ALL")) {
...@@ -1811,10 +1776,9 @@ public class Parser { ...@@ -1811,10 +1776,9 @@ public class Parser {
command.setTypeName(readUniqueIdentifier()); command.setTypeName(readUniqueIdentifier());
ifExists = readIfExists(ifExists); ifExists = readIfExists(ifExists);
command.setIfExists(ifExists); command.setIfExists(ifExists);
if (readIf("CASCADE")) { ConstraintActionType dropAction = parseCascadeOrRestrict();
command.setDropAction(ConstraintActionType.CASCADE); if (dropAction != null) {
} else if (readIf("RESTRICT")) { command.setDropAction(dropAction);
command.setDropAction(ConstraintActionType.RESTRICT);
} }
return command; return command;
} }
...@@ -1951,10 +1915,9 @@ public class Parser { ...@@ -1951,10 +1915,9 @@ public class Parser {
if (readIf("(")) { if (readIf("(")) {
for (int i = 0;; i++) { for (int i = 0;; i++) {
command.setExpression(i, readExpression()); command.setExpression(i, readExpression());
if (readIf(")")) { if (!readIfMore(true)) {
break; break;
} }
read(",");
} }
} }
return command; return command;
...@@ -2365,6 +2328,13 @@ public class Parser { ...@@ -2365,6 +2328,13 @@ public class Parser {
command.setSQL(sql); command.setSQL(sql);
} }
private Expression readExpressionOrDefault() {
if (readIf("DEFAULT")) {
return ValueExpression.getDefault();
}
return readExpression();
}
private Expression readExpression() { private Expression readExpression() {
Expression r = readAnd(); Expression r = readAnd();
while (readIf("OR")) { while (readIf("OR")) {
...@@ -2697,12 +2667,8 @@ public class Parser { ...@@ -2697,12 +2667,8 @@ public class Parser {
distinct); distinct);
} }
read(")"); read(")");
if (r != null && readIf("FILTER")) { if (r != null) {
read("("); r.setFilterCondition(readFilterCondition());
read("WHERE");
Expression condition = readExpression();
read(")");
r.setFilterCondition(condition);
} }
return r; return r;
} }
...@@ -2735,12 +2701,10 @@ public class Parser { ...@@ -2735,12 +2701,10 @@ public class Parser {
} }
Expression[] args; Expression[] args;
ArrayList<Expression> argList = Utils.newSmallArrayList(); ArrayList<Expression> argList = Utils.newSmallArrayList();
int numArgs = 0; if (!readIf(")")) {
while (!readIf(")")) { do {
if (numArgs++ > 0) { argList.add(readExpression());
read(","); } while (readIfMore(true));
}
argList.add(readExpression());
} }
args = argList.toArray(new Expression[0]); args = argList.toArray(new Expression[0]);
return new JavaFunction(functionAlias, args); return new JavaFunction(functionAlias, args);
...@@ -2752,19 +2716,22 @@ public class Parser { ...@@ -2752,19 +2716,22 @@ public class Parser {
do { do {
params.add(readExpression()); params.add(readExpression());
} while (readIfMore(true)); } while (readIfMore(true));
Expression filterCondition; Expression filterCondition = readFilterCondition();
Expression[] list = params.toArray(new Expression[0]);
JavaAggregate agg = new JavaAggregate(aggregate, list, currentSelect, distinct, filterCondition);
currentSelect.setGroupQuery();
return agg;
}
private Expression readFilterCondition() {
if (readIf("FILTER")) { if (readIf("FILTER")) {
read("("); read("(");
read("WHERE"); read("WHERE");
filterCondition = readExpression(); Expression filterCondition = readExpression();
read(")"); read(")");
} else { return filterCondition;
filterCondition = null;
} }
Expression[] list = params.toArray(new Expression[0]); return null;
JavaAggregate agg = new JavaAggregate(aggregate, list, currentSelect, distinct, filterCondition);
currentSelect.setGroupQuery();
return agg;
} }
private AggregateType getAggregateType(String name) { private AggregateType getAggregateType(String name) {
...@@ -3303,12 +3270,10 @@ public class Parser { ...@@ -3303,12 +3270,10 @@ public class Parser {
if (readIfMore(true)) { if (readIfMore(true)) {
ArrayList<Expression> list = Utils.newSmallArrayList(); ArrayList<Expression> list = Utils.newSmallArrayList();
list.add(r); list.add(r);
while (!readIf(")")) { if (!readIf(")")) {
r = readExpression(); do {
list.add(r); list.add(readExpression());
if (!readIfMore(true)) { } while (readIfMore(false));
break;
}
} }
r = new ExpressionList(list.toArray(new Expression[0])); r = new ExpressionList(list.toArray(new Expression[0]));
} }
...@@ -3584,9 +3549,7 @@ public class Parser { ...@@ -3584,9 +3549,7 @@ public class Parser {
} }
private boolean isToken(String token) { private boolean isToken(String token) {
boolean result = equalsToken(token, currentToken) && if (!currentTokenQuoted && equalsToken(token, currentToken)) {
!currentTokenQuoted;
if (result) {
return true; return true;
} }
addExpected(token); addExpected(token);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论