diff --git a/src/main/java/com/alibaba/druid/common/jdbc/StatementBase.java b/src/main/java/com/alibaba/druid/common/jdbc/StatementBase.java new file mode 100644 index 0000000000..94042fa4c0 --- /dev/null +++ b/src/main/java/com/alibaba/druid/common/jdbc/StatementBase.java @@ -0,0 +1,87 @@ +package com.alibaba.druid.common.jdbc; + +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; + +public abstract class StatementBase implements Statement { + + private Connection connection; + private int fetchDirection; + private int fetchSize; + private int resultSetType; + private int resultSetConcurrency; + private int resultSetHoldability; + + public StatementBase(Connection connection){ + super(); + this.connection = connection; + } + + public Connection getConnection() throws SQLException { + return connection; + } + + public void setConnection(Connection connection) { + this.connection = connection; + } + + protected void checkOpen() throws SQLException { + + } + + @Override + public void setFetchDirection(int direction) throws SQLException { + checkOpen(); + + this.fetchDirection = direction; + } + + @Override + public int getFetchDirection() throws SQLException { + checkOpen(); + return fetchDirection; + } + + @Override + public void setFetchSize(int rows) throws SQLException { + checkOpen(); + this.fetchSize = rows; + } + + @Override + public int getFetchSize() throws SQLException { + checkOpen(); + return fetchSize; + } + + public int getResultSetType() throws SQLException { + checkOpen(); + + return resultSetType; + } + + public void setResultSetType(int resultType) { + this.resultSetType = resultType; + } + + public void setResultSetConcurrency(int resultSetConcurrency) { + this.resultSetConcurrency = resultSetConcurrency; + } + + @Override + public int getResultSetConcurrency() throws SQLException { + checkOpen(); + return resultSetConcurrency; + } + + @Override + public int getResultSetHoldability() throws SQLException { + checkOpen(); + return resultSetHoldability; + } + + public void setResultSetHoldability(int resultSetHoldability) { + this.resultSetHoldability = resultSetHoldability; + } +} diff --git a/src/main/java/com/alibaba/druid/hbase/HBaseConnection.java b/src/main/java/com/alibaba/druid/hbase/HBaseConnection.java index a25d495ea1..ab3430f1a8 100644 --- a/src/main/java/com/alibaba/druid/hbase/HBaseConnection.java +++ b/src/main/java/com/alibaba/druid/hbase/HBaseConnection.java @@ -25,12 +25,18 @@ import com.alibaba.druid.common.jdbc.ConnectionBase; public class HBaseConnection extends ConnectionBase implements Connection { private Configuration config; + private final String url; - public HBaseConnection(Properties info){ + public HBaseConnection(String url, Properties info){ + this.url = url; config = new Configuration(); this.setClientInfo(info); } + public String getUrl() { + return url; + } + @SuppressWarnings("unchecked") @Override public T unwrap(Class iface) throws SQLException { diff --git a/src/main/java/com/alibaba/druid/hbase/HBaseDriver.java b/src/main/java/com/alibaba/druid/hbase/HBaseDriver.java index a3afb85fae..430a936b0d 100644 --- a/src/main/java/com/alibaba/druid/hbase/HBaseDriver.java +++ b/src/main/java/com/alibaba/druid/hbase/HBaseDriver.java @@ -6,17 +6,22 @@ import java.sql.DriverPropertyInfo; import java.sql.SQLException; import java.util.Properties; - public class HBaseDriver implements Driver { + private String prefix = "jdbc:druid-hbase:"; + @Override public Connection connect(String url, Properties info) throws SQLException { - return null; + return new HBaseConnection(url, info); } @Override public boolean acceptsURL(String url) throws SQLException { - return false; + if (url == null) { + return false; + } + + return url.startsWith(prefix); } @Override diff --git a/src/main/java/com/alibaba/druid/mock/MockStatement.java b/src/main/java/com/alibaba/druid/mock/MockStatement.java index 6e06db780f..5afde063aa 100644 --- a/src/main/java/com/alibaba/druid/mock/MockStatement.java +++ b/src/main/java/com/alibaba/druid/mock/MockStatement.java @@ -22,13 +22,14 @@ import java.sql.SQLFeatureNotSupportedException; import java.sql.SQLWarning; import java.sql.Statement; -public class MockStatement implements Statement { +import com.alibaba.druid.common.jdbc.StatementBase; + +public class MockStatement extends StatementBase implements Statement { public final static String ERROR_SQL = "THROW ERROR"; protected boolean closed = false; - private Connection connection; protected MockConnection mockConnection; private int maxFieldSize; private int maxRows; @@ -37,53 +38,32 @@ public class MockStatement implements Statement { private SQLWarning warnings; private String cursorName; private int updateCount; - private int fetchDirection; - private int fetchSize; - - private int resultSetType; - private int resultSetConcurrency; - private int resultSetHoldability; public MockStatement(Connection connection){ - super(); - this.connection = connection; + super(connection); if (connection instanceof MockConnection) { mockConnection = (MockConnection) connection; } } - + protected void checkOpen() throws SQLException, MockConnectionClosedException { if (closed) { throw new SQLException(); } - + if (this.mockConnection != null && this.mockConnection.isClosed()) { throw new MockConnectionClosedException(); } } - public int getResultSetType() throws SQLException { - checkOpen(); - - return resultSetType; - } - - public void setResultSetType(int resultType) { - this.resultSetType = resultType; - } - - public void setResultSetConcurrency(int resultSetConcurrency) { - this.resultSetConcurrency = resultSetConcurrency; - } - public MockConnection getMockConnection() { return mockConnection; } public void setFakeConnection(MockConnection fakeConnection) { this.mockConnection = fakeConnection; - this.connection = fakeConnection; + this.setConnection(fakeConnection); } @SuppressWarnings("unchecked") @@ -127,11 +107,11 @@ public class MockStatement implements Statement { @Override public int executeUpdate(String sql) throws SQLException { checkOpen(); - + if (mockConnection != null) { mockConnection.handleSleep(); } - + return 0; } @@ -231,7 +211,7 @@ public class MockStatement implements Statement { if (ERROR_SQL.equals(sql)) { throw new SQLException(); } - + if (mockConnection != null) { mockConnection.handleSleep(); } @@ -264,37 +244,6 @@ public class MockStatement implements Statement { return false; } - @Override - public void setFetchDirection(int direction) throws SQLException { - checkOpen(); - - this.fetchDirection = direction; - } - - @Override - public int getFetchDirection() throws SQLException { - checkOpen(); - return fetchDirection; - } - - @Override - public void setFetchSize(int rows) throws SQLException { - checkOpen(); - this.fetchSize = rows; - } - - @Override - public int getFetchSize() throws SQLException { - checkOpen(); - return fetchSize; - } - - @Override - public int getResultSetConcurrency() throws SQLException { - checkOpen(); - return resultSetConcurrency; - } - @Override public void addBatch(String sql) throws SQLException { checkOpen(); @@ -308,18 +257,12 @@ public class MockStatement implements Statement { @Override public int[] executeBatch() throws SQLException { checkOpen(); - + if (mockConnection != null) { mockConnection.handleSleep(); } - - return new int[0]; - } - @Override - public Connection getConnection() throws SQLException { - checkOpen(); - return connection; + return new int[0]; } @Override @@ -370,16 +313,6 @@ public class MockStatement implements Statement { return false; } - @Override - public int getResultSetHoldability() throws SQLException { - checkOpen(); - return resultSetHoldability; - } - - public void setResultSetHoldability(int resultSetHoldability) { - this.resultSetHoldability = resultSetHoldability; - } - @Override public boolean isClosed() throws SQLException { return closed;