mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser phoenix support.
This commit is contained in:
@@ -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;
|
||||
|
||||
+44
@@ -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();
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.alibaba.druid.bvt.sql.phoenix;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.phoenix.visitor.PhoenixSchemaStatVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 16/9/13.
|
||||
*/
|
||||
public class PhoenixUpsertTest_1 extends TestCase {
|
||||
public void test_0() throws Exception {
|
||||
String sql = "upsert into t_1 (a BIGINT,b,c) values (?,?,?)";
|
||||
|
||||
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.PHOENIX);
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
SchemaStatVisitor visitor = new PhoenixSchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
// System.out.println("Tables : " + visitor.getTables());
|
||||
// System.out.println("fields : " + visitor.getColumns());
|
||||
// System.out.println("coditions : " + visitor.getConditions());
|
||||
// System.out.println("orderBy : " + visitor.getOrderByColumns());
|
||||
|
||||
Assert.assertEquals(1, visitor.getTables().size());
|
||||
Assert.assertEquals(3, visitor.getColumns().size());
|
||||
Assert.assertEquals(0, visitor.getConditions().size());
|
||||
|
||||
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_1")));
|
||||
|
||||
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("t_1", "a")));
|
||||
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("t_1", "b")));
|
||||
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("t_1", "c")));
|
||||
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
|
||||
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "full_name")));
|
||||
|
||||
String output = SQLUtils.toSQLString(stmt, JdbcConstants.PHOENIX);
|
||||
Assert.assertEquals("UPSERT INTO t_1\n" +
|
||||
"\t(a BIGINT, b, c)\n" +
|
||||
"VALUES\n" +
|
||||
"(?, ?, ?)", //
|
||||
output);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.alibaba.druid.bvt.sql.phoenix;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.phoenix.visitor.PhoenixSchemaStatVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 16/9/13.
|
||||
*/
|
||||
public class PhoenixUpsertTest_2 extends TestCase {
|
||||
public void test_0() throws Exception {
|
||||
String sql = "upsert into t_1 (a CHAR(15),b,c) values (?,?,?)";
|
||||
|
||||
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.PHOENIX);
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
SchemaStatVisitor visitor = new PhoenixSchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
// System.out.println("Tables : " + visitor.getTables());
|
||||
// System.out.println("fields : " + visitor.getColumns());
|
||||
// System.out.println("coditions : " + visitor.getConditions());
|
||||
// System.out.println("orderBy : " + visitor.getOrderByColumns());
|
||||
|
||||
Assert.assertEquals(1, visitor.getTables().size());
|
||||
Assert.assertEquals(3, visitor.getColumns().size());
|
||||
Assert.assertEquals(0, visitor.getConditions().size());
|
||||
|
||||
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_1")));
|
||||
|
||||
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("t_1", "a")));
|
||||
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("t_1", "b")));
|
||||
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("t_1", "c")));
|
||||
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
|
||||
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "full_name")));
|
||||
|
||||
String output = SQLUtils.toSQLString(stmt, JdbcConstants.PHOENIX);
|
||||
Assert.assertEquals("UPSERT INTO t_1\n" +
|
||||
"\t(a CHAR(15), b, c)\n" +
|
||||
"VALUES\n" +
|
||||
"(?, ?, ?)", //
|
||||
output);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user