Unverified 提交 40c61d60 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov 提交者: GitHub

Merge pull request #984 from katzyn/parser

Minor refactorings in Parser
...@@ -794,8 +794,7 @@ public class Parser { ...@@ -794,8 +794,7 @@ public class Parser {
do { do {
Column column = readTableColumn(filter); Column column = readTableColumn(filter);
columns.add(column); columns.add(column);
} while (readIf(",")); } while (readIfMore(true));
read(")");
read("="); read("=");
Expression expression = readExpression(); Expression expression = readExpression();
if (columns.size() == 1) { if (columns.size() == 1) {
...@@ -906,8 +905,7 @@ public class Parser { ...@@ -906,8 +905,7 @@ public class Parser {
column.sortType |= SortOrder.NULLS_LAST; column.sortType |= SortOrder.NULLS_LAST;
} }
} }
} while (readIf(",")); } while (readIfMore(true));
read(")");
return columns.toArray(new IndexColumn[0]); return columns.toArray(new IndexColumn[0]);
} }
...@@ -916,7 +914,7 @@ public class Parser { ...@@ -916,7 +914,7 @@ public class Parser {
do { do {
String columnName = readColumnIdentifier(); String columnName = readColumnIdentifier();
columns.add(columnName); columns.add(columnName);
} while (readIfMore()); } while (readIfMore(false));
return columns.toArray(new String[0]); return columns.toArray(new String[0]);
} }
...@@ -931,7 +929,7 @@ public class Parser { ...@@ -931,7 +929,7 @@ public class Parser {
column.getSQL()); column.getSQL());
} }
columns.add(column); columns.add(column);
} while (readIfMore()); } while (readIfMore(false));
} }
return columns.toArray(new Column[0]); return columns.toArray(new Column[0]);
} }
...@@ -944,9 +942,16 @@ public class Parser { ...@@ -944,9 +942,16 @@ public class Parser {
return table.getColumn(id); return table.getColumn(id);
} }
private boolean readIfMore() { /**
* Read comma or closing brace.
*
* @param strict
* if {@code false} additional comma before brace is allowed
* @return {@code true} if comma is read, {@code false} if brace is read
*/
private boolean readIfMore(boolean strict) {
if (readIf(",")) { if (readIf(",")) {
return !readIf(")"); return strict || !readIf(")");
} }
read(")"); read(")");
return false; return false;
...@@ -1110,7 +1115,7 @@ public class Parser { ...@@ -1110,7 +1115,7 @@ public class Parser {
} else { } else {
values.add(readExpression()); values.add(readExpression());
} }
} while (readIfMore()); } while (readIfMore(false));
} }
command.addRow(values.toArray(new Expression[0])); command.addRow(values.toArray(new Expression[0]));
} while (readIf(",")); } while (readIf(","));
...@@ -1281,7 +1286,7 @@ public class Parser { ...@@ -1281,7 +1286,7 @@ public class Parser {
} else { } else {
values.add(readExpression()); values.add(readExpression());
} }
} while (readIfMore()); } while (readIfMore(false));
} }
command.addRow(values.toArray(new Expression[0])); command.addRow(values.toArray(new Expression[0]));
// the following condition will allow (..),; and (..); // the following condition will allow (..),; and (..);
...@@ -1340,7 +1345,7 @@ public class Parser { ...@@ -1340,7 +1345,7 @@ public class Parser {
} else { } else {
values.add(readExpression()); values.add(readExpression());
} }
} while (readIfMore()); } while (readIfMore(false));
} }
command.addRow(values.toArray(new Expression[0])); command.addRow(values.toArray(new Expression[0]));
} while (readIf(",")); } while (readIf(","));
...@@ -1476,8 +1481,7 @@ public class Parser { ...@@ -1476,8 +1481,7 @@ public class Parser {
String indexName = readIdentifierWithSchema(); String indexName = readIdentifierWithSchema();
Index index = table.getIndex(indexName); Index index = table.getIndex(indexName);
indexNames.add(index.getName()); indexNames.add(index.getName());
} while (readIf(",")); } while (readIfMore(true));
read(")");
} }
return IndexHints.createUseIndexHints(indexNames); return IndexHints.createUseIndexHints(indexNames);
} }
...@@ -1503,8 +1507,7 @@ public class Parser { ...@@ -1503,8 +1507,7 @@ public class Parser {
ArrayList<String> derivedColumnNames = New.arrayList(); ArrayList<String> derivedColumnNames = New.arrayList();
do { do {
derivedColumnNames.add(readAliasIdentifier()); derivedColumnNames.add(readAliasIdentifier());
} while (readIf(",")); } while (readIfMore(true));
read(")");
return derivedColumnNames; return derivedColumnNames;
} }
return null; return null;
...@@ -2722,8 +2725,7 @@ public class Parser { ...@@ -2722,8 +2725,7 @@ public class Parser {
ArrayList<Expression> params = New.arrayList(); ArrayList<Expression> params = New.arrayList();
do { do {
params.add(readExpression()); params.add(readExpression());
} while (readIf(",")); } while (readIfMore(true));
read(")");
Expression filterCondition; Expression filterCondition;
if (readIf("FILTER")) { if (readIf("FILTER")) {
read("("); read("(");
...@@ -2894,8 +2896,7 @@ public class Parser { ...@@ -2894,8 +2896,7 @@ public class Parser {
read("="); read("=");
function.setParameter(i, readExpression()); function.setParameter(i, readExpression());
i++; i++;
} while (readIf(",")); } while (readIfMore(true));
read(")");
TableFunction tf = (TableFunction) function; TableFunction tf = (TableFunction) function;
tf.setColumns(columns); tf.setColumns(columns);
break; break;
...@@ -2915,8 +2916,7 @@ public class Parser { ...@@ -2915,8 +2916,7 @@ public class Parser {
int i = 0; int i = 0;
do { do {
function.setParameter(i++, readExpression()); function.setParameter(i++, readExpression());
} while (readIf(",")); } while (readIfMore(true));
read(")");
} }
} }
function.doneWithParameters(); function.doneWithParameters();
...@@ -3542,19 +3542,6 @@ public class Parser { ...@@ -3542,19 +3542,6 @@ public class Parser {
return false; return false;
} }
/*
* Reads passed token in list, in order and returns true on first match.
* If none of the token matches returns false
*/
private boolean readIfOr(String... tokens) {
for (String token: tokens) {
if (readIf(token)) {
return true;
}
}
return false;
}
/* /*
* Reads every token in list, in order - returns true if all are found. * Reads every token in list, in order - returns true if all are found.
* If any are not found, returns false - AND resets parsing back to state when called. * If any are not found, returns false - AND resets parsing back to state when called.
...@@ -4154,40 +4141,39 @@ public class Parser { ...@@ -4154,40 +4141,39 @@ public class Parser {
break; break;
} }
} else if (s.length() == 2) { } else if (s.length() == 2) {
char c1 = s.charAt(1);
switch (c0) { switch (c0) {
case ':': case ':':
if ("::".equals(s)) { if (c1 == ':' || c1 == '=') {
return KEYWORD;
} else if (":=".equals(s)) {
return KEYWORD; return KEYWORD;
} }
break; break;
case '>': case '>':
if (">=".equals(s)) { if (c1 == '=') {
return BIGGER_EQUAL; return BIGGER_EQUAL;
} }
break; break;
case '<': case '<':
if ("<=".equals(s)) { if (c1 == '=') {
return SMALLER_EQUAL; return SMALLER_EQUAL;
} else if ("<>".equals(s)) { } else if (c1 == '>') {
return NOT_EQUAL; return NOT_EQUAL;
} }
break; break;
case '!': case '!':
if ("!=".equals(s)) { if (c1 == '=') {
return NOT_EQUAL; return NOT_EQUAL;
} else if ("!~".equals(s)) { } else if (c1 == '~') {
return KEYWORD; return KEYWORD;
} }
break; break;
case '|': case '|':
if ("||".equals(s)) { if (c1 == '|') {
return STRING_CONCAT; return STRING_CONCAT;
} }
break; break;
case '&': case '&':
if ("&&".equals(s)) { if (c1 == '&') {
return SPATIAL_INTERSECTS; return SPATIAL_INTERSECTS;
} }
break; break;
...@@ -4491,7 +4477,9 @@ public class Parser { ...@@ -4491,7 +4477,9 @@ public class Parser {
} }
original += "(" + p; original += "(" + p;
// Oracle syntax // Oracle syntax
readIfOr("CHAR", "BYTE"); if (!readIf("CHAR")) {
readIf("BYTE");
}
if (dataType.supportsScale) { if (dataType.supportsScale) {
if (readIf(",")) { if (readIf(",")) {
scale = readInt(); scale = readInt();
...@@ -4525,13 +4513,12 @@ public class Parser { ...@@ -4525,13 +4513,12 @@ public class Parser {
String enumerator0 = readString(); String enumerator0 = readString();
enumeratorList.add(enumerator0); enumeratorList.add(enumerator0);
original += "'" + enumerator0 + "'"; original += "'" + enumerator0 + "'";
while (readIf(",")) { while (readIfMore(true)) {
original += ','; original += ',';
String enumeratorN = readString(); String enumeratorN = readString();
original += "'" + enumeratorN + "'"; original += "'" + enumeratorN + "'";
enumeratorList.add(enumeratorN); enumeratorList.add(enumeratorN);
} }
read(")");
original += ')'; original += ')';
enumerators = enumeratorList.toArray(new String[0]); enumerators = enumeratorList.toArray(new String[0]);
} }
...@@ -4851,10 +4838,7 @@ public class Parser { ...@@ -4851,10 +4838,7 @@ public class Parser {
columns.set(i, column); columns.set(i, column);
row.add(expr); row.add(expr);
i++; i++;
} while (multiColumn && readIf(",")); } while (multiColumn && readIfMore(true));
if (multiColumn) {
read(")");
}
rows.add(row); rows.add(row);
} while (readIf(",")); } while (readIf(","));
int columnCount = columns.size(); int columnCount = columns.size();
...@@ -6358,8 +6342,7 @@ public class Parser { ...@@ -6358,8 +6342,7 @@ public class Parser {
command.setIfNotExists(false); command.setIfNotExists(false);
do { do {
parseTableColumnDefinition(command, schema, tableName); parseTableColumnDefinition(command, schema, tableName);
} while (readIf(",")); } while (readIfMore(true));
read(")");
} else { } else {
boolean ifNotExists = readIfNotExists(); boolean ifNotExists = readIfNotExists();
command.setIfNotExists(ifNotExists); command.setIfNotExists(ifNotExists);
...@@ -6610,7 +6593,7 @@ public class Parser { ...@@ -6610,7 +6593,7 @@ public class Parser {
if (!readIf(")")) { if (!readIf(")")) {
do { do {
parseTableColumnDefinition(command, schema, tableName); parseTableColumnDefinition(command, schema, tableName);
} while (readIfMore()); } while (readIfMore(false));
} }
} }
// Allows "COMMENT='comment'" in DDL statements (MySQL syntax) // Allows "COMMENT='comment'" in DDL statements (MySQL syntax)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论