improved sql parser phoenix support.

This commit is contained in:
wenshao
2016-09-15 16:24:58 +08:00
parent 53c3f98bb2
commit 67f0945bcd
6 changed files with 179 additions and 2 deletions
@@ -1,3 +1,18 @@
/*
* Copyright 1999-2101 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.ast.statement;
import com.alibaba.druid.sql.ast.SQLExpr;
@@ -15,7 +15,10 @@
*/
package com.alibaba.druid.sql.dialect.phoenix.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.statement.SQLInsertInto;
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
import com.alibaba.druid.sql.parser.Lexer;
import com.alibaba.druid.sql.parser.ParserException;
@@ -33,4 +36,45 @@ public class PhoenixStatementParser extends SQLStatementParser {
public PhoenixStatementParser(Lexer lexer){
super(new PhoenixExprParser(lexer));
}
@Override
protected void parseInsertColumns(SQLInsertInto insert) {
if (lexer.token() == Token.RPAREN ) {
return;
}
for (;;) {
SQLName expr = this.exprParser.name();
expr.setParent(insert);
insert.getColumns().add(expr);
if (lexer.token() == Token.IDENTIFIER) {
String text = lexer.stringVal();
if (text.equalsIgnoreCase("TINYINT")
|| text.equalsIgnoreCase("BIGINT")
|| text.equalsIgnoreCase("INTEGER")
|| text.equalsIgnoreCase("DOUBLE")
|| text.equalsIgnoreCase("DATE")
|| text.equalsIgnoreCase("VARCHAR")) {
expr.getAttributes().put("dataType", text);
lexer.nextToken();
} else if (text.equalsIgnoreCase("CHAR")) {
String dataType = text;
lexer.nextToken();
accept(Token.LPAREN);
SQLExpr char_len = this.exprParser.primary();
accept(Token.RPAREN);
dataType += ("(" + char_len.toString() + ")");
expr.getAttributes().put("dataType", dataType);
}
}
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
}
}
@@ -1547,7 +1547,7 @@ public class SQLStatementParser extends SQLParser {
if (lexer.token() == (Token.LPAREN)) {
lexer.nextToken();
this.exprParser.exprList(insertStatement.getColumns(), insertStatement);
parseInsertColumns(insertStatement);
accept(Token.RPAREN);
}
@@ -1573,6 +1573,10 @@ public class SQLStatementParser extends SQLParser {
}
}
protected void parseInsertColumns(SQLInsertInto insert) {
this.exprParser.exprList(insert.getColumns(), insert);
}
public boolean parseStatementListDialect(List<SQLStatement> statementList) {
return false;
}
@@ -1077,7 +1077,15 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
}
print0(", ");
}
x.getColumns().get(i).accept(this);
SQLExpr column = x.getColumns().get(i);
column.accept(this);
String dataType = (String) column.getAttribute("dataType");
if (dataType != null) {
print(' ');
print(dataType);
}
}
print(')');
decrementIndent();