diff --git a/src/main/java/com/alibaba/druid/common/jdbc/ResultSetMetaDataBase.java b/src/main/java/com/alibaba/druid/common/jdbc/ResultSetMetaDataBase.java new file mode 100644 index 0000000000..9e0b77df3b --- /dev/null +++ b/src/main/java/com/alibaba/druid/common/jdbc/ResultSetMetaDataBase.java @@ -0,0 +1,346 @@ +/* + * Copyright 1999-2011 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.common.jdbc; + +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class ResultSetMetaDataBase implements ResultSetMetaData { + + public ResultSetMetaDataBase(){ + + } + + private final List columns = new ArrayList(); + + public List getColumns() { + return columns; + } + + @SuppressWarnings("unchecked") + @Override + public T unwrap(Class iface) throws SQLException { + if (iface == null) { + return null; + } + + if (iface.isAssignableFrom(ResultSetMetaDataBase.class)) { + return (T) this; + } + return null; + } + + @Override + public boolean isWrapperFor(Class iface) throws SQLException { + if (iface == null) { + return false; + } + + return ResultSetMetaDataBase.class.isAssignableFrom(iface); + } + + @Override + public int getColumnCount() throws SQLException { + return columns.size(); + } + + @Override + public boolean isAutoIncrement(int column) throws SQLException { + return columns.get(column - 1).isAutoIncrement(); + } + + @Override + public boolean isCaseSensitive(int column) throws SQLException { + return columns.get(column - 1).isCaseSensitive(); + } + + @Override + public boolean isSearchable(int column) throws SQLException { + return columns.get(column - 1).isSearchable(); + } + + @Override + public boolean isCurrency(int column) throws SQLException { + return columns.get(column - 1).isCurrency(); + } + + @Override + public int isNullable(int column) throws SQLException { + return columns.get(column - 1).getNullable(); + } + + @Override + public boolean isSigned(int column) throws SQLException { + return columns.get(column - 1).isSigned(); + } + + @Override + public int getColumnDisplaySize(int column) throws SQLException { + return columns.get(column - 1).getColumnDisplaySize(); + } + + @Override + public String getColumnLabel(int column) throws SQLException { + return columns.get(column - 1).getColumnLabel(); + } + + @Override + public String getColumnName(int column) throws SQLException { + return columns.get(column - 1).getColumnName(); + } + + @Override + public String getSchemaName(int column) throws SQLException { + return columns.get(column - 1).getSchemaName(); + } + + @Override + public int getPrecision(int column) throws SQLException { + return columns.get(column - 1).getPrecision(); + } + + @Override + public int getScale(int column) throws SQLException { + return columns.get(column - 1).getScale(); + } + + @Override + public String getTableName(int column) throws SQLException { + return columns.get(column - 1).getTableName(); + } + + @Override + public String getCatalogName(int column) throws SQLException { + return columns.get(column - 1).getCatalogName(); + } + + @Override + public int getColumnType(int column) throws SQLException { + return columns.get(column - 1).getColumnType(); + } + + @Override + public String getColumnTypeName(int column) throws SQLException { + return columns.get(column - 1).getColumnTypeName(); + } + + @Override + public boolean isReadOnly(int column) throws SQLException { + return columns.get(column - 1).isReadOnly(); + } + + @Override + public boolean isWritable(int column) throws SQLException { + return columns.get(column - 1).isWritable(); + } + + @Override + public boolean isDefinitelyWritable(int column) throws SQLException { + return columns.get(column - 1).isDefinitelyWritable(); + } + + @Override + public String getColumnClassName(int column) throws SQLException { + return columns.get(column - 1).getColumnClassName(); + } + + public static class ColumnMetaData { + + private boolean autoIncrement = false; + private boolean caseSensitive; + private boolean searchable; + private boolean currency; + private int nullable = 0; + private boolean signed; + private int columnDisplaySize; + private String columnLabel; + private String columnName; + private String schemaName; + private int precision; + private int scale; + private String tableName; + private String catalogName; + private int columnType; + private String columnTypeName; + private boolean readOnly; + private boolean writable; + private boolean definitelyWritable; + private String columnClassName; + + public boolean isAutoIncrement() { + return autoIncrement; + } + + public void setAutoIncrement(boolean autoIncrement) { + this.autoIncrement = autoIncrement; + } + + public boolean isCaseSensitive() { + return caseSensitive; + } + + public void setCaseSensitive(boolean caseSensitive) { + this.caseSensitive = caseSensitive; + } + + public boolean isSearchable() { + return searchable; + } + + public void setSearchable(boolean searchable) { + this.searchable = searchable; + } + + public boolean isCurrency() { + return currency; + } + + public void setCurrency(boolean currency) { + this.currency = currency; + } + + public int getNullable() { + return nullable; + } + + public void setNullable(int nullable) { + this.nullable = nullable; + } + + public boolean isSigned() { + return signed; + } + + public void setSigned(boolean signed) { + this.signed = signed; + } + + public int getColumnDisplaySize() { + return columnDisplaySize; + } + + public void setColumnDisplaySize(int columnDisplaySize) { + this.columnDisplaySize = columnDisplaySize; + } + + public String getColumnLabel() { + return columnLabel; + } + + public void setColumnLabel(String columnLabel) { + this.columnLabel = columnLabel; + } + + public String getColumnName() { + return columnName; + } + + public void setColumnName(String columnName) { + this.columnName = columnName; + } + + public String getSchemaName() { + return schemaName; + } + + public void setSchemaName(String schemaName) { + this.schemaName = schemaName; + } + + public int getPrecision() { + return precision; + } + + public void setPrecision(int precision) { + this.precision = precision; + } + + public int getScale() { + return scale; + } + + public void setScale(int scale) { + this.scale = scale; + } + + public String getTableName() { + return tableName; + } + + public void setTableName(String tableName) { + this.tableName = tableName; + } + + public String getCatalogName() { + return catalogName; + } + + public void setCatalogName(String catalogName) { + this.catalogName = catalogName; + } + + public int getColumnType() { + return columnType; + } + + public void setColumnType(int columnType) { + this.columnType = columnType; + } + + public String getColumnTypeName() { + return columnTypeName; + } + + public void setColumnTypeName(String columnTypeName) { + this.columnTypeName = columnTypeName; + } + + public boolean isReadOnly() { + return readOnly; + } + + public void setReadOnly(boolean readOnly) { + this.readOnly = readOnly; + } + + public boolean isWritable() { + return writable; + } + + public void setWritable(boolean writable) { + this.writable = writable; + } + + public boolean isDefinitelyWritable() { + return definitelyWritable; + } + + public void setDefinitelyWritable(boolean definitelyWritable) { + this.definitelyWritable = definitelyWritable; + } + + public String getColumnClassName() { + return columnClassName; + } + + public void setColumnClassName(String columnClassName) { + this.columnClassName = columnClassName; + } + + } +} diff --git a/src/main/java/com/alibaba/druid/mock/MockResultSetMetaData.java b/src/main/java/com/alibaba/druid/mock/MockResultSetMetaData.java index 836c554970..9930fa1b2f 100644 --- a/src/main/java/com/alibaba/druid/mock/MockResultSetMetaData.java +++ b/src/main/java/com/alibaba/druid/mock/MockResultSetMetaData.java @@ -16,331 +16,13 @@ package com.alibaba.druid.mock; import java.sql.ResultSetMetaData; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; -public class MockResultSetMetaData implements ResultSetMetaData { +import com.alibaba.druid.common.jdbc.ResultSetMetaDataBase; + +public class MockResultSetMetaData extends ResultSetMetaDataBase implements ResultSetMetaData { public MockResultSetMetaData(){ } - private final List columns = new ArrayList(); - - public List getColumns() { - return columns; - } - - @SuppressWarnings("unchecked") - @Override - public T unwrap(Class iface) throws SQLException { - if (iface == null) { - return null; - } - - if (iface.isAssignableFrom(MockResultSetMetaData.class)) { - return (T) this; - } - return null; - } - - @Override - public boolean isWrapperFor(Class iface) throws SQLException { - if (iface == null) { - return false; - } - - return MockResultSetMetaData.class.isAssignableFrom(iface); - } - - @Override - public int getColumnCount() throws SQLException { - return columns.size(); - } - - @Override - public boolean isAutoIncrement(int column) throws SQLException { - return columns.get(column - 1).isAutoIncrement(); - } - - @Override - public boolean isCaseSensitive(int column) throws SQLException { - return columns.get(column - 1).isCaseSensitive(); - } - - @Override - public boolean isSearchable(int column) throws SQLException { - return columns.get(column - 1).isSearchable(); - } - - @Override - public boolean isCurrency(int column) throws SQLException { - return columns.get(column - 1).isCurrency(); - } - - @Override - public int isNullable(int column) throws SQLException { - return columns.get(column - 1).getNullable(); - } - - @Override - public boolean isSigned(int column) throws SQLException { - return columns.get(column - 1).isSigned(); - } - - @Override - public int getColumnDisplaySize(int column) throws SQLException { - return columns.get(column - 1).getColumnDisplaySize(); - } - - @Override - public String getColumnLabel(int column) throws SQLException { - return columns.get(column - 1).getColumnLabel(); - } - - @Override - public String getColumnName(int column) throws SQLException { - return columns.get(column - 1).getColumnName(); - } - - @Override - public String getSchemaName(int column) throws SQLException { - return columns.get(column - 1).getSchemaName(); - } - - @Override - public int getPrecision(int column) throws SQLException { - return columns.get(column - 1).getPrecision(); - } - - @Override - public int getScale(int column) throws SQLException { - return columns.get(column - 1).getScale(); - } - - @Override - public String getTableName(int column) throws SQLException { - return columns.get(column - 1).getTableName(); - } - - @Override - public String getCatalogName(int column) throws SQLException { - return columns.get(column - 1).getCatalogName(); - } - - @Override - public int getColumnType(int column) throws SQLException { - return columns.get(column - 1).getColumnType(); - } - - @Override - public String getColumnTypeName(int column) throws SQLException { - return columns.get(column - 1).getColumnTypeName(); - } - - @Override - public boolean isReadOnly(int column) throws SQLException { - return columns.get(column - 1).isReadOnly(); - } - - @Override - public boolean isWritable(int column) throws SQLException { - return columns.get(column - 1).isWritable(); - } - - @Override - public boolean isDefinitelyWritable(int column) throws SQLException { - return columns.get(column - 1).isDefinitelyWritable(); - } - - @Override - public String getColumnClassName(int column) throws SQLException { - return columns.get(column - 1).getColumnClassName(); - } - - public static class ColumnMetaData { - - private boolean autoIncrement = false; - private boolean caseSensitive; - private boolean searchable; - private boolean currency; - private int nullable = 0; - private boolean signed; - private int columnDisplaySize; - private String columnLabel; - private String columnName; - private String schemaName; - private int precision; - private int scale; - private String tableName; - private String catalogName; - private int columnType; - private String columnTypeName; - private boolean readOnly; - private boolean writable; - private boolean definitelyWritable; - private String columnClassName; - - public boolean isAutoIncrement() { - return autoIncrement; - } - - public void setAutoIncrement(boolean autoIncrement) { - this.autoIncrement = autoIncrement; - } - - public boolean isCaseSensitive() { - return caseSensitive; - } - - public void setCaseSensitive(boolean caseSensitive) { - this.caseSensitive = caseSensitive; - } - - public boolean isSearchable() { - return searchable; - } - - public void setSearchable(boolean searchable) { - this.searchable = searchable; - } - - public boolean isCurrency() { - return currency; - } - - public void setCurrency(boolean currency) { - this.currency = currency; - } - - public int getNullable() { - return nullable; - } - - public void setNullable(int nullable) { - this.nullable = nullable; - } - - public boolean isSigned() { - return signed; - } - - public void setSigned(boolean signed) { - this.signed = signed; - } - - public int getColumnDisplaySize() { - return columnDisplaySize; - } - - public void setColumnDisplaySize(int columnDisplaySize) { - this.columnDisplaySize = columnDisplaySize; - } - - public String getColumnLabel() { - return columnLabel; - } - - public void setColumnLabel(String columnLabel) { - this.columnLabel = columnLabel; - } - - public String getColumnName() { - return columnName; - } - - public void setColumnName(String columnName) { - this.columnName = columnName; - } - - public String getSchemaName() { - return schemaName; - } - - public void setSchemaName(String schemaName) { - this.schemaName = schemaName; - } - - public int getPrecision() { - return precision; - } - - public void setPrecision(int precision) { - this.precision = precision; - } - - public int getScale() { - return scale; - } - - public void setScale(int scale) { - this.scale = scale; - } - - public String getTableName() { - return tableName; - } - - public void setTableName(String tableName) { - this.tableName = tableName; - } - - public String getCatalogName() { - return catalogName; - } - - public void setCatalogName(String catalogName) { - this.catalogName = catalogName; - } - - public int getColumnType() { - return columnType; - } - - public void setColumnType(int columnType) { - this.columnType = columnType; - } - - public String getColumnTypeName() { - return columnTypeName; - } - - public void setColumnTypeName(String columnTypeName) { - this.columnTypeName = columnTypeName; - } - - public boolean isReadOnly() { - return readOnly; - } - - public void setReadOnly(boolean readOnly) { - this.readOnly = readOnly; - } - - public boolean isWritable() { - return writable; - } - - public void setWritable(boolean writable) { - this.writable = writable; - } - - public boolean isDefinitelyWritable() { - return definitelyWritable; - } - - public void setDefinitelyWritable(boolean definitelyWritable) { - this.definitelyWritable = definitelyWritable; - } - - public String getColumnClassName() { - return columnClassName; - } - - public void setColumnClassName(String columnClassName) { - this.columnClassName = columnClassName; - } - - } } diff --git a/src/main/java/com/alibaba/druid/mock/handler/MySqlMockExecuteHandlerImpl.java b/src/main/java/com/alibaba/druid/mock/handler/MySqlMockExecuteHandlerImpl.java index 074a3653e3..1bd67316a5 100644 --- a/src/main/java/com/alibaba/druid/mock/handler/MySqlMockExecuteHandlerImpl.java +++ b/src/main/java/com/alibaba/druid/mock/handler/MySqlMockExecuteHandlerImpl.java @@ -6,9 +6,9 @@ import java.sql.Timestamp; import java.sql.Types; import java.util.List; +import com.alibaba.druid.common.jdbc.ResultSetMetaDataBase.ColumnMetaData; import com.alibaba.druid.mock.MockResultSet; import com.alibaba.druid.mock.MockResultSetMetaData; -import com.alibaba.druid.mock.MockResultSetMetaData.ColumnMetaData; import com.alibaba.druid.mock.MockStatement; import com.alibaba.druid.sql.ast.SQLExpr; import com.alibaba.druid.sql.ast.SQLStatement; diff --git a/src/test/java/com/alibaba/druid/hbase/HBaseDriverTest_getIndex.java b/src/test/java/com/alibaba/druid/hbase/HBaseDriverTest_getIndex.java new file mode 100644 index 0000000000..388e5a0da8 --- /dev/null +++ b/src/test/java/com/alibaba/druid/hbase/HBaseDriverTest_getIndex.java @@ -0,0 +1,35 @@ +package com.alibaba.druid.hbase; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.util.Properties; + +import junit.framework.TestCase; + + +public class HBaseDriverTest_getIndex extends TestCase { + public void test_select_0 () throws Exception { + HBaseDriver driver = new HBaseDriver(); + Connection conn = driver.connect("jdbc:druid-hbase:10.20.153.63", new Properties()); + + PreparedStatement stmt = conn.prepareStatement("SELECT id, name, gender, salary FROM test_user where id >= 3 and id <= ?"); + stmt.setInt(1, 6); + + ResultSet rs = stmt.executeQuery(); + while (rs.next()) { + System.out.print(rs.getInt(1)); + System.out.print(' '); + System.out.print(rs.getString(2)); + System.out.print(' '); + System.out.print(rs.getString(3)); + System.out.print(' '); + System.out.print(rs.getBigDecimal(4)); + System.out.println(); + } + rs.close(); + stmt.close(); + + conn.close(); + } +}