mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
hbase support
This commit is contained in:
@@ -34,6 +34,8 @@ public class HPreparedStatement extends PreparedStatementBase implements Prepare
|
||||
|
||||
private ExecutePlan executePlan;
|
||||
|
||||
private String dbType = "hbase";
|
||||
|
||||
public HPreparedStatement(HBaseConnection conn, String sql) throws SQLException{
|
||||
super(conn);
|
||||
this.sql = sql;
|
||||
@@ -41,7 +43,7 @@ public class HPreparedStatement extends PreparedStatementBase implements Prepare
|
||||
|
||||
init();
|
||||
}
|
||||
|
||||
|
||||
private void splitCondition(List<SQLExpr> conditions, SQLExpr expr) {
|
||||
if (expr instanceof SQLBinaryOpExpr) {
|
||||
SQLBinaryOpExpr binaryExpr = (SQLBinaryOpExpr) expr;
|
||||
@@ -55,7 +57,6 @@ public class HPreparedStatement extends PreparedStatementBase implements Prepare
|
||||
}
|
||||
|
||||
public void init() throws SQLException {
|
||||
String dbType = this.hbaseConnection.getConnectProperties().getProperty("dbType");
|
||||
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
|
||||
|
||||
if (stmtList.size() != 1) {
|
||||
@@ -65,7 +66,7 @@ public class HPreparedStatement extends PreparedStatementBase implements Prepare
|
||||
SQLStatement sqlStmt = stmtList.get(0);
|
||||
if (sqlStmt instanceof SQLSelectStatement) {
|
||||
SQLSelectStatement selectStmt = (SQLSelectStatement) sqlStmt;
|
||||
|
||||
|
||||
SQLEvalVisitor evalVisitor = SQLEvalVisitorUtils.createEvalVisitor(dbType);
|
||||
selectStmt.accept(evalVisitor);
|
||||
|
||||
@@ -76,9 +77,9 @@ public class HPreparedStatement extends PreparedStatementBase implements Prepare
|
||||
|
||||
SingleTableQueryExecutePlan singleTableQueryExecuetePlan = new SingleTableQueryExecutePlan();
|
||||
singleTableQueryExecuetePlan.setTableName(tableName);
|
||||
|
||||
|
||||
splitCondition(singleTableQueryExecuetePlan.getConditions(), selectQueryBlock.getWhere());
|
||||
|
||||
|
||||
HResultSetMetaData resultMetaData = new HResultSetMetaData();
|
||||
for (SQLSelectItem selectItem : selectQueryBlock.getSelectList()) {
|
||||
ColumnMetaData columnMetaData = new ColumnMetaData();
|
||||
@@ -97,20 +98,20 @@ public class HPreparedStatement extends PreparedStatementBase implements Prepare
|
||||
|
||||
SQLEvalVisitor evalVisitor = SQLEvalVisitorUtils.createEvalVisitor(dbType);
|
||||
insertStmt.accept(evalVisitor);
|
||||
|
||||
|
||||
String tableName = ((SQLIdentifierExpr) insertStmt.getTableSource().getExpr()).getName();
|
||||
|
||||
|
||||
InsertExecutePlan insertExecutePlan = new InsertExecutePlan();
|
||||
insertExecutePlan.setTableName(tableName);
|
||||
|
||||
|
||||
for (int i = 0; i < insertStmt.getColumns().size(); ++i) {
|
||||
SQLExpr columnExpr = insertStmt.getColumns().get(i);
|
||||
SQLExpr valueExpr = insertStmt.getValues().getValues().get(i);
|
||||
|
||||
|
||||
String columnName = ((SQLIdentifierExpr) columnExpr).getName();
|
||||
insertExecutePlan.getColumns().put(columnName, valueExpr);
|
||||
}
|
||||
|
||||
|
||||
this.executePlan = insertExecutePlan;
|
||||
} else {
|
||||
throw new SQLException("TODO");
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.alibaba.druid.hbase.hbql.ast;
|
||||
|
||||
import com.alibaba.druid.hbase.hbql.visitor.HBQLVisitor;
|
||||
|
||||
public class HBQLShowStatement extends HBQLStatementImpl {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public void accept0(HBQLVisitor visitor) {
|
||||
visitor.visit(this);
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.alibaba.druid.hbase.hbql.ast;
|
||||
|
||||
import com.alibaba.druid.hbase.hbql.visitor.HBQLVisitor;
|
||||
import com.alibaba.druid.sql.ast.SQLStatementImpl;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class HBQLStatementImpl extends SQLStatementImpl {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
if (visitor instanceof HBQLVisitor) {
|
||||
accept0((HBQLVisitor) visitor);
|
||||
} else {
|
||||
throw new IllegalArgumentException("not support visitor type : " + visitor.getClass().getName());
|
||||
}
|
||||
}
|
||||
|
||||
public void accept0(HBQLVisitor visitor) {
|
||||
throw new UnsupportedOperationException(this.getClass().getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.alibaba.druid.hbase.hbql.parser;
|
||||
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
import com.alibaba.druid.sql.parser.ParserException;
|
||||
import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
|
||||
|
||||
public class HBQLExprParser extends SQLExprParser {
|
||||
public HBQLExprParser(String sql) throws ParserException{
|
||||
super(sql);
|
||||
}
|
||||
|
||||
public HBQLExprParser(Lexer lexer){
|
||||
super(lexer);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.alibaba.druid.hbase.hbql.parser;
|
||||
|
||||
import com.alibaba.druid.hbase.hbql.ast.HBQLShowStatement;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
|
||||
@@ -14,4 +16,11 @@ public class HBQLStatementParser extends SQLStatementParser {
|
||||
public HBQLStatementParser(SQLExprParser exprParser){
|
||||
super(exprParser);
|
||||
}
|
||||
|
||||
public SQLStatement parseShow() {
|
||||
acceptIdentifier("SHOW");
|
||||
acceptIdentifier("TABLES");
|
||||
return new HBQLShowStatement();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.alibaba.druid.hbase.hbql.visitor;
|
||||
|
||||
import com.alibaba.druid.hbase.hbql.ast.HBQLShowStatement;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
|
||||
|
||||
|
||||
public class HBQLOutputVisitor extends SQLASTOutputVisitor implements HBQLVisitor {
|
||||
|
||||
public HBQLOutputVisitor(Appendable appender){
|
||||
super(appender);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(HBQLShowStatement x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(HBQLShowStatement x) {
|
||||
print("SHOW TABLES");
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +1,11 @@
|
||||
package com.alibaba.druid.hbase.hbql.visitor;
|
||||
|
||||
import com.alibaba.druid.hbase.hbql.ast.HBQLShowStatement;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
|
||||
public interface HBQLVisitor extends SQLASTVisitor {
|
||||
|
||||
void endVisit(HBQLShowStatement x);
|
||||
|
||||
boolean visit(HBQLShowStatement x);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.alibaba.druid.sql.parser;
|
||||
|
||||
import com.alibaba.druid.hbase.hbql.parser.HBQLExprParser;
|
||||
import com.alibaba.druid.hbase.hbql.parser.HBQLStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlExprParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.oracle.parser.OracleExprParser;
|
||||
@@ -10,45 +12,53 @@ import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerExprParser;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
|
||||
public class SQLParserUtils {
|
||||
|
||||
public static SQLStatementParser createSQLStatementParser(String sql, String dbType) {
|
||||
if (JdbcUtils.ORACLE.equals(dbType)) {
|
||||
return new OracleStatementParser(sql);
|
||||
}
|
||||
|
||||
|
||||
if (JdbcUtils.MYSQL.equals(dbType)) {
|
||||
return new MySqlStatementParser(sql);
|
||||
}
|
||||
|
||||
|
||||
if (JdbcUtils.POSTGRESQL.equals(dbType)) {
|
||||
return new PGSQLStatementParser(sql);
|
||||
}
|
||||
|
||||
|
||||
if (JdbcUtils.SQL_SERVER.equals(dbType)) {
|
||||
return new SQLServerStatementParser(sql);
|
||||
}
|
||||
|
||||
|
||||
if (JdbcUtils.HBASE.equals(dbType)) {
|
||||
return new HBQLStatementParser(sql);
|
||||
}
|
||||
|
||||
return new SQLStatementParser(sql);
|
||||
}
|
||||
|
||||
|
||||
public static SQLExprParser createExprParser(String sql, String dbType) {
|
||||
if (JdbcUtils.ORACLE.equals(dbType)) {
|
||||
return new OracleExprParser(sql);
|
||||
}
|
||||
|
||||
|
||||
if (JdbcUtils.MYSQL.equals(dbType)) {
|
||||
return new MySqlExprParser(sql);
|
||||
}
|
||||
|
||||
|
||||
if (JdbcUtils.POSTGRESQL.equals(dbType)) {
|
||||
return new PGExprParser(sql);
|
||||
}
|
||||
|
||||
|
||||
if (JdbcUtils.SQL_SERVER.equals(dbType)) {
|
||||
return new SQLServerExprParser(sql);
|
||||
}
|
||||
|
||||
|
||||
if (JdbcUtils.HBASE.equals(dbType)) {
|
||||
return new HBQLExprParser(sql);
|
||||
}
|
||||
|
||||
return new SQLExprParser(sql);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -191,6 +191,13 @@ public class SQLStatementParser extends SQLParser {
|
||||
statementList.add(stmt);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (identifierEquals("SHOW")) {
|
||||
SQLStatement stmt = parseShow();
|
||||
|
||||
statementList.add(stmt);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
char mark_ch = lexer.current();
|
||||
@@ -236,6 +243,10 @@ public class SQLStatementParser extends SQLParser {
|
||||
public SQLStatement parseCommit() {
|
||||
throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
|
||||
}
|
||||
|
||||
public SQLStatement parseShow() {
|
||||
throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
|
||||
}
|
||||
|
||||
public SQLUseStatement parseUse() {
|
||||
accept(Token.USE);
|
||||
|
||||
@@ -62,6 +62,8 @@ public final class JdbcUtils {
|
||||
public static final String MYSQL = "mysql";
|
||||
|
||||
public static final String DERBY = "derby";
|
||||
|
||||
public static final String HBASE = "hbase";
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(JdbcUtils.class);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user