refactor sql parser.

This commit is contained in:
wenshao
2017-09-18 11:25:40 +08:00
parent 3c98a1eee5
commit 8343ca08c4
29 changed files with 201 additions and 499 deletions
@@ -1,52 +0,0 @@
/*
* Copyright 1999-2017 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.postgresql.ast.stmt;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.statement.SQLSetStatement;
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGASTVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import com.alibaba.druid.util.JdbcConstants;
import java.util.List;
public class PGSetStatement extends SQLSetStatement implements PGSQLStatement {
public final String range;
public final String param;
public final List<SQLExpr> values;
// SET [ SESSION | LOCAL ] TIME ZONE { timezone | LOCAL | DEFAULT }
// SET [ SESSION | LOCAL ] configuration_parameter { TO | = } { value | 'value' | DEFAULT }
public PGSetStatement(String range, String param, List<SQLExpr> values) {
super(JdbcConstants.POSTGRESQL);
this.range = range;
this.param = param;
this.values = values;
}
@Override
protected void accept0(SQLASTVisitor visitor) {
if (visitor instanceof PGASTVisitor) {
accept0((PGASTVisitor) visitor);
}
}
@Override
public void accept0(PGASTVisitor visitor) {
visitor.visit(this);
visitor.endVisit(this);
}
}
@@ -18,10 +18,7 @@ package com.alibaba.druid.sql.dialect.postgresql.parser;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
import com.alibaba.druid.sql.ast.expr.SQLCurrentOfCursorExpr;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLQueryExpr;
import com.alibaba.druid.sql.ast.expr.*;
import com.alibaba.druid.sql.ast.statement.*;
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGDeleteStatement;
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGInsertStatement;
@@ -327,58 +324,73 @@ public class PGSQLStatementParser extends SQLStatementParser {
lexer.nextToken();
return stmt;
}
private SQLStatement handleTimeZoneSet(String range) {
// ZONE
lexer.nextToken();
// VALUE
lexer.nextToken();
String value = lexer.stringVal();
List<SQLExpr> exprs = new ArrayList<SQLExpr>();
if (lexer.token() == Token.IDENTIFIER) {
exprs.add(new SQLIdentifierExpr(value.toUpperCase()));
} else {
exprs.add(new SQLCharExpr(value));
}
lexer.nextToken();
return new PGSetStatement(range, TIME_ZONE, exprs);
}
@Override
public SQLStatement parseSet() {
accept(Token.SET);
Token token = lexer.token();
String range = "";
SQLSetStatement.Option option = null;
if (token == Token.SESSION) {
lexer.nextToken();
range = Token.SESSION.name();
option = SQLSetStatement.Option.SESSION;
} else if (token == Token.IDENTIFIER && LOCAL.equalsIgnoreCase(lexer.stringVal())) {
range = LOCAL;
option = SQLSetStatement.Option.LOCAL;
lexer.nextToken();
}
String parameter = lexer.stringVal();
SQLExpr paramExpr;
List<SQLExpr> values = new ArrayList<SQLExpr>();
if (TIME.equalsIgnoreCase(parameter)) {
// SET [ SESSION | LOCAL ] TIME ZONE { timezone | LOCAL | DEFAULT }
return handleTimeZoneSet(range);
lexer.nextToken();
acceptIdentifier("ZONE");
paramExpr = new SQLIdentifierExpr("TIME ZONE");
String value = lexer.stringVal();
if (lexer.token() == Token.IDENTIFIER) {
values.add(new SQLIdentifierExpr(value.toUpperCase()));
} else {
values.add(new SQLCharExpr(value));
}
lexer.nextToken();
// return new PGSetStatement(range, TIME_ZONE, exprs);
} else {
paramExpr = new SQLIdentifierExpr(parameter);
lexer.nextToken();
while (!lexer.isEOF()) {
lexer.nextToken();
if (lexer.token() == Token.LITERAL_CHARS) {
values.add(new SQLCharExpr(lexer.stringVal()));
} else if (lexer.token() == Token.LITERAL_INT) {
values.add(new SQLIdentifierExpr(lexer.numberString()));
} else {
values.add(new SQLIdentifierExpr(lexer.stringVal()));
}
// skip comma
lexer.nextToken();
}
}
// TO | =
lexer.nextToken();
// value | 'value' | DEFAULT
List<SQLExpr> values = new ArrayList<SQLExpr>();
while (!lexer.isEOF()) {
lexer.nextToken();
if (lexer.token() == Token.LITERAL_CHARS) {
values.add(new SQLCharExpr(lexer.stringVal()));
} else if (lexer.token() == Token.LITERAL_INT) {
values.add(new SQLIdentifierExpr(lexer.numberString()));
} else {
values.add(new SQLIdentifierExpr(lexer.stringVal()));
SQLExpr valueExpr;
if (values.size() == 1) {
valueExpr = values.get(0);
} else {
SQLListExpr listExpr = new SQLListExpr();
for (SQLExpr value : values) {
listExpr.addItem(value);
}
// skip comma
lexer.nextToken();
valueExpr = listExpr;
}
return new PGSetStatement(range, parameter, values);
SQLSetStatement stmt = new SQLSetStatement(paramExpr, valueExpr, dbType);
stmt.setOption(option);
return stmt;
}
@@ -113,8 +113,4 @@ public interface PGASTVisitor extends SQLASTVisitor {
boolean visit(PGStartTransactionStatement x);
void endVisit(PGSetStatement x);
boolean visit(PGSetStatement x);
}
@@ -268,14 +268,4 @@ public class PGASTVisitorAdapter extends SQLASTVisitorAdapter implements PGASTVi
return true;
}
@Override
public void endVisit(PGSetStatement x) {
}
@Override
public boolean visit(PGSetStatement x) {
return true;
}
}
@@ -20,6 +20,8 @@ import com.alibaba.druid.sql.ast.SQLLimit;
import com.alibaba.druid.sql.ast.SQLSetQuantifier;
import com.alibaba.druid.sql.ast.expr.SQLBinaryExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLListExpr;
import com.alibaba.druid.sql.ast.statement.*;
import com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGBoxExpr;
import com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGCidrExpr;
@@ -39,6 +41,7 @@ import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.Wind
import com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser;
import com.alibaba.druid.sql.parser.Token;
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
import com.alibaba.druid.util.FnvHash;
import com.alibaba.druid.util.StringUtils;
import java.util.List;
@@ -606,33 +609,41 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
}
@Override
public void endVisit(PGSetStatement x) {
}
@Override
public boolean visit(PGSetStatement x) {
public boolean visit(SQLSetStatement x) {
print0(ucase ? "SET " : "set ");
if (!StringUtils.isEmpty(x.range)) {
print0(x.range);
print0(" ");
SQLSetStatement.Option option = x.getOption();
if (option != null) {
print(option.name());
print(' ');
}
if (PGSQLStatementParser.TIME_ZONE.equalsIgnoreCase(x.param)) {
print0(PGSQLStatementParser.TIME_ZONE);
print0(" ");
x.values.get(0).accept(this);
return false;
}
print0(x.param);
print0(" ");
print0(Token.TO.name());
print0(" ");
for (int i = 0; i < x.values.size(); i++) {
List<SQLAssignItem> items = x.getItems();
for (int i = 0; i < items.size(); i++) {
if (i != 0) {
print0(", ");
}
x.values.get(i).accept(this);
SQLAssignItem item = x.getItems().get(i);
SQLExpr target = item.getTarget();
target.accept(this);
if (target instanceof SQLIdentifierExpr && ((SQLIdentifierExpr) target).getName().equalsIgnoreCase("TIME ZONE")) {
print(' ');
} else {
print0(" TO ");
}
SQLExpr value = item.getValue();
if (value instanceof SQLListExpr) {
SQLListExpr listExpr = (SQLListExpr) value;
printAndAccept(listExpr.getItems(), ", ");
} else {
value.accept(this);
}
}
return false;
}
}
@@ -351,13 +351,4 @@ public class PGSchemaStatVisitor extends SchemaStatVisitor implements PGASTVisit
return false;
}
@Override
public void endVisit(PGSetStatement x) {
}
@Override
public boolean visit(PGSetStatement x) {
return false;
}
}