This commit is contained in:
温少
2012-05-21 09:03:14 +08:00
parent db8743dadc
commit 10a621db91
12 changed files with 212 additions and 206 deletions
@@ -2,12 +2,12 @@ package com.alibaba.druid.hbase.exec;
import java.sql.SQLException;
import com.alibaba.druid.hbase.jdbc.HPreparedStatement;
import com.alibaba.druid.hbase.jdbc.HPreparedStatementImpl;
import com.alibaba.druid.hbase.jdbc.HResultSet;
public interface ExecutePlan {
HResultSet executeQuery(HPreparedStatement statement) throws SQLException;
HResultSet executeQuery(HPreparedStatementImpl statement) throws SQLException;
boolean execute(HPreparedStatement statement) throws SQLException;
boolean execute(HPreparedStatementImpl statement) throws SQLException;
}
@@ -2,18 +2,18 @@ package com.alibaba.druid.hbase.exec;
import java.sql.SQLException;
import com.alibaba.druid.hbase.jdbc.HPreparedStatement;
import com.alibaba.druid.hbase.jdbc.HPreparedStatementImpl;
import com.alibaba.druid.hbase.jdbc.HResultSet;
public class ExecutePlanAdapter implements ExecutePlan {
@Override
public HResultSet executeQuery(HPreparedStatement statement) throws SQLException {
public HResultSet executeQuery(HPreparedStatementImpl statement) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public boolean execute(HPreparedStatement statement) throws SQLException {
public boolean execute(HPreparedStatementImpl statement) throws SQLException {
throw new UnsupportedOperationException();
}
@@ -9,7 +9,7 @@ import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
import com.alibaba.druid.hbase.jdbc.HBaseConnection;
import com.alibaba.druid.hbase.jdbc.HPreparedStatement;
import com.alibaba.druid.hbase.jdbc.HPreparedStatementImpl;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.visitor.SQLEvalVisitorUtils;
@@ -19,7 +19,7 @@ public class InsertExecutePlan extends SingleTableExecutePlan {
private byte[] family = Bytes.toBytes("d");
@Override
public boolean execute(HPreparedStatement statement) throws SQLException {
public boolean execute(HPreparedStatementImpl statement) throws SQLException {
try {
HBaseConnection connection = statement.getConnection();
String dbType = connection.getConnectProperties().getProperty("dbType");
@@ -10,14 +10,14 @@ import org.apache.hadoop.hbase.client.HBaseAdmin;
import com.alibaba.druid.common.jdbc.ResultSetBase;
import com.alibaba.druid.common.jdbc.ResultSetMetaDataBase.ColumnMetaData;
import com.alibaba.druid.hbase.jdbc.HPreparedStatement;
import com.alibaba.druid.hbase.jdbc.HPreparedStatementImpl;
import com.alibaba.druid.hbase.jdbc.HResultSet;
import com.alibaba.druid.hbase.jdbc.HResultSetMetaData;
public class ShowTablesPlan implements ExecutePlan {
@Override
public HResultSet executeQuery(HPreparedStatement statement) throws SQLException {
public HResultSet executeQuery(HPreparedStatementImpl statement) throws SQLException {
try {
HBaseAdmin admin = statement.getConnection().getEngine().getHBaseAdmin();
@@ -32,7 +32,7 @@ public class ShowTablesPlan implements ExecutePlan {
}
@Override
public boolean execute(HPreparedStatement statement) throws SQLException {
public boolean execute(HPreparedStatementImpl statement) throws SQLException {
HResultSet resultSet = executeQuery(statement);
statement.setResultSet(resultSet);
return true;
@@ -17,7 +17,7 @@ import org.apache.hadoop.hbase.util.Bytes;
import com.alibaba.druid.hbase.jdbc.HBaseConnection;
import com.alibaba.druid.hbase.jdbc.HBaseResultSet;
import com.alibaba.druid.hbase.jdbc.HPreparedStatement;
import com.alibaba.druid.hbase.jdbc.HPreparedStatementImpl;
import com.alibaba.druid.hbase.jdbc.HResultSetMetaData;
import com.alibaba.druid.hbase.mapping.HMapping;
import com.alibaba.druid.sql.ast.SQLExpr;
@@ -70,7 +70,7 @@ public class SingleTableQueryExecutePlan extends SingleTableExecutePlan {
}
@Override
public HBaseResultSet executeQuery(HPreparedStatement statement) throws SQLException {
public HBaseResultSet executeQuery(HPreparedStatementImpl statement) throws SQLException {
try {
HBaseConnection connection = statement.getConnection();
String dbType = connection.getConnectProperties().getProperty("dbType");
@@ -77,7 +77,7 @@ public class HBaseConnection extends ConnectionBase implements Connection {
@Override
public Statement createStatement() throws SQLException {
return new HStatement(this);
return new HStatementImpl(this);
}
@Override
@@ -156,8 +156,8 @@ public class HBaseConnection extends ConnectionBase implements Connection {
}
@Override
public HPreparedStatement prepareStatement(String sql) throws SQLException {
return new HPreparedStatement(this, sql);
public HPreparedStatementImpl prepareStatement(String sql) throws SQLException {
return new HPreparedStatementImpl(this, sql);
}
@Override
@@ -184,7 +184,7 @@ public class HBaseConnection extends ConnectionBase implements Connection {
@Override
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
HPreparedStatement stmt = prepareStatement(sql);
HPreparedStatementImpl stmt = prepareStatement(sql);
stmt.setColumnNames(columnNames);
return stmt;
}
@@ -18,7 +18,7 @@ import com.alibaba.druid.util.JdbcUtils;
public class HBaseResultSet extends ResultSetBase implements HResultSet {
private HStatementInterface statement;
private HStatement statement;
private ResultScanner scanner;
private HTableInterface htable;
private Result result;
@@ -26,7 +26,7 @@ public class HBaseResultSet extends ResultSetBase implements HResultSet {
private HMapping mapping;
public HBaseResultSet(HStatementInterface statement, HTableInterface htable, ResultScanner scanner){
public HBaseResultSet(HStatement statement, HTableInterface htable, ResultScanner scanner){
super(statement);
this.statement = statement;
this.htable = htable;
@@ -1,165 +1,6 @@
package com.alibaba.druid.hbase.jdbc;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import com.alibaba.druid.common.jdbc.PreparedStatementBase;
import com.alibaba.druid.common.jdbc.ResultSetMetaDataBase.ColumnMetaData;
import com.alibaba.druid.hbase.exec.ExecutePlan;
import com.alibaba.druid.hbase.exec.InsertExecutePlan;
import com.alibaba.druid.hbase.exec.ShowTablesPlan;
import com.alibaba.druid.hbase.exec.SingleTableQueryExecutePlan;
import com.alibaba.druid.hbase.hbql.ast.HBQLShowStatement;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.visitor.SQLEvalVisitor;
import com.alibaba.druid.sql.visitor.SQLEvalVisitorUtils;
public class HPreparedStatement extends PreparedStatementBase implements PreparedStatement, HStatementInterface {
private final String sql;
private String[] columnNames;
private HBaseConnection hbaseConnection;
private ExecutePlan executePlan;
private String dbType = "hbase";
public HPreparedStatement(HBaseConnection conn, String sql) throws SQLException{
super(conn);
this.sql = sql;
this.hbaseConnection = conn;
init();
}
private void splitCondition(List<SQLExpr> conditions, SQLExpr expr) {
if (expr instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr binaryExpr = (SQLBinaryOpExpr) expr;
if (binaryExpr.getOperator() == SQLBinaryOperator.BooleanAnd) {
splitCondition(conditions, binaryExpr.getLeft());
splitCondition(conditions, binaryExpr.getRight());
return;
}
}
conditions.add(expr);
}
public void init() throws SQLException {
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
if (stmtList.size() != 1) {
throw new SQLException("not support multi-statement");
}
SQLStatement sqlStmt = stmtList.get(0);
if (sqlStmt instanceof SQLSelectStatement) {
SQLSelectStatement selectStmt = (SQLSelectStatement) sqlStmt;
SQLEvalVisitor evalVisitor = SQLEvalVisitorUtils.createEvalVisitor(dbType);
selectStmt.accept(evalVisitor);
SQLSelectQueryBlock selectQueryBlock = (SQLSelectQueryBlock) selectStmt.getSelect().getQuery();
SQLExprTableSource tableSource = (SQLExprTableSource) selectQueryBlock.getFrom();
String tableName = ((SQLIdentifierExpr) tableSource.getExpr()).getName();
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();
if (selectItem.getAlias() != null) {
columnMetaData.setColumnLabel(selectItem.getAlias());
}
columnMetaData.setColumnName(SQLUtils.toSQLString(selectItem.getExpr(), dbType));
columnMetaData.setColumnType(Types.BINARY);
resultMetaData.getColumns().add(columnMetaData);
}
singleTableQueryExecuetePlan.setResultMetaData(resultMetaData);
this.executePlan = singleTableQueryExecuetePlan;
} else if (sqlStmt instanceof SQLInsertStatement) {
SQLInsertStatement insertStmt = (SQLInsertStatement) sqlStmt;
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 if (sqlStmt instanceof HBQLShowStatement) {
ShowTablesPlan showTablePlan = new ShowTablesPlan();
this.executePlan = showTablePlan;
} else {
throw new SQLException("TODO");
}
}
public ExecutePlan getExecutePlan() {
return executePlan;
}
public String[] getColumnNames() {
return columnNames;
}
public void setColumnNames(String[] columnNames) {
this.columnNames = columnNames;
}
public String getSql() {
return sql;
}
@Override
public HBaseConnection getConnection() throws SQLException {
return hbaseConnection;
}
@Override
public ResultSet executeQuery() throws SQLException {
return this.executePlan.executeQuery(this);
}
@Override
public int executeUpdate() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean execute() throws SQLException {
this.executePlan.execute(this);
return false;
}
public interface HPreparedStatement extends HStatement {
}
@@ -0,0 +1,165 @@
package com.alibaba.druid.hbase.jdbc;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import com.alibaba.druid.common.jdbc.PreparedStatementBase;
import com.alibaba.druid.common.jdbc.ResultSetMetaDataBase.ColumnMetaData;
import com.alibaba.druid.hbase.exec.ExecutePlan;
import com.alibaba.druid.hbase.exec.InsertExecutePlan;
import com.alibaba.druid.hbase.exec.ShowTablesPlan;
import com.alibaba.druid.hbase.exec.SingleTableQueryExecutePlan;
import com.alibaba.druid.hbase.hbql.ast.HBQLShowStatement;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.visitor.SQLEvalVisitor;
import com.alibaba.druid.sql.visitor.SQLEvalVisitorUtils;
public class HPreparedStatementImpl extends PreparedStatementBase implements PreparedStatement, HStatement {
private final String sql;
private String[] columnNames;
private HBaseConnection hbaseConnection;
private ExecutePlan executePlan;
private String dbType = "hbase";
public HPreparedStatementImpl(HBaseConnection conn, String sql) throws SQLException{
super(conn);
this.sql = sql;
this.hbaseConnection = conn;
init();
}
private void splitCondition(List<SQLExpr> conditions, SQLExpr expr) {
if (expr instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr binaryExpr = (SQLBinaryOpExpr) expr;
if (binaryExpr.getOperator() == SQLBinaryOperator.BooleanAnd) {
splitCondition(conditions, binaryExpr.getLeft());
splitCondition(conditions, binaryExpr.getRight());
return;
}
}
conditions.add(expr);
}
public void init() throws SQLException {
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
if (stmtList.size() != 1) {
throw new SQLException("not support multi-statement");
}
SQLStatement sqlStmt = stmtList.get(0);
if (sqlStmt instanceof SQLSelectStatement) {
SQLSelectStatement selectStmt = (SQLSelectStatement) sqlStmt;
SQLEvalVisitor evalVisitor = SQLEvalVisitorUtils.createEvalVisitor(dbType);
selectStmt.accept(evalVisitor);
SQLSelectQueryBlock selectQueryBlock = (SQLSelectQueryBlock) selectStmt.getSelect().getQuery();
SQLExprTableSource tableSource = (SQLExprTableSource) selectQueryBlock.getFrom();
String tableName = ((SQLIdentifierExpr) tableSource.getExpr()).getName();
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();
if (selectItem.getAlias() != null) {
columnMetaData.setColumnLabel(selectItem.getAlias());
}
columnMetaData.setColumnName(SQLUtils.toSQLString(selectItem.getExpr(), dbType));
columnMetaData.setColumnType(Types.BINARY);
resultMetaData.getColumns().add(columnMetaData);
}
singleTableQueryExecuetePlan.setResultMetaData(resultMetaData);
this.executePlan = singleTableQueryExecuetePlan;
} else if (sqlStmt instanceof SQLInsertStatement) {
SQLInsertStatement insertStmt = (SQLInsertStatement) sqlStmt;
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 if (sqlStmt instanceof HBQLShowStatement) {
ShowTablesPlan showTablePlan = new ShowTablesPlan();
this.executePlan = showTablePlan;
} else {
throw new SQLException("TODO");
}
}
public ExecutePlan getExecutePlan() {
return executePlan;
}
public String[] getColumnNames() {
return columnNames;
}
public void setColumnNames(String[] columnNames) {
this.columnNames = columnNames;
}
public String getSql() {
return sql;
}
@Override
public HBaseConnection getConnection() throws SQLException {
return hbaseConnection;
}
@Override
public ResultSet executeQuery() throws SQLException {
return this.executePlan.executeQuery(this);
}
@Override
public int executeUpdate() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean execute() throws SQLException {
this.executePlan.execute(this);
return false;
}
}
@@ -3,23 +3,6 @@ package com.alibaba.druid.hbase.jdbc;
import java.sql.SQLException;
import java.sql.Statement;
import com.alibaba.druid.common.jdbc.StatementBase;
public class HStatement extends StatementBase implements Statement, HStatementInterface {
private HBaseConnection conn;
public HStatement(HBaseConnection conn){
super(conn);
this.conn = conn;
}
@Override
public HBaseConnection getConnection() throws SQLException {
return conn;
}
public void checkOpen() {
}
public interface HStatement extends Statement {
HBaseConnection getConnection() throws SQLException;
}
@@ -0,0 +1,25 @@
package com.alibaba.druid.hbase.jdbc;
import java.sql.SQLException;
import java.sql.Statement;
import com.alibaba.druid.common.jdbc.StatementBase;
public class HStatementImpl extends StatementBase implements Statement, HStatement {
private HBaseConnection conn;
public HStatementImpl(HBaseConnection conn){
super(conn);
this.conn = conn;
}
@Override
public HBaseConnection getConnection() throws SQLException {
return conn;
}
public void checkOpen() {
}
}
@@ -1,8 +0,0 @@
package com.alibaba.druid.hbase.jdbc;
import java.sql.SQLException;
import java.sql.Statement;
public interface HStatementInterface extends Statement {
HBaseConnection getConnection() throws SQLException;
}