diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLColumnDescription.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLColumnDescription.java index 31514ae5ef7f10580766fdf57cb147a307fa89a7..00f9cf55c8939b2c6dc545590c77aeea340c2959 100644 --- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLColumnDescription.java +++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLColumnDescription.java @@ -22,8 +22,6 @@ import lombok.extern.slf4j.Slf4j; import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLArrayColumnType; import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLColumnType; -import java.sql.ResultSetMetaData; -import java.sql.SQLException; import java.sql.Types; /** @@ -39,28 +37,18 @@ public final class PostgreSQLColumnDescription { private final int columnIndex; - private final int typeOID; - private final int columnLength; + + private final int typeOID; private final int typeModifier = -1; private final int dataFormat = 0; - public PostgreSQLColumnDescription(final String columnName, final int columnIndex, final int columnType, final int columnLength, final ResultSetMetaData resultSetMetaData) { + public PostgreSQLColumnDescription(final String columnName, final int columnIndex, final int columnType, final int columnLength, final String columnTypeName) { this.columnName = columnName; this.columnIndex = columnIndex; - if (Types.ARRAY == columnType && null != resultSetMetaData) { - String columnTypeName = null; - try { - columnTypeName = resultSetMetaData.getColumnTypeName(columnIndex); - } catch (final SQLException ex) { - log.error("getColumnTypeName failed, columnName={}, columnIndex={}", columnName, columnIndex, ex); - } - typeOID = PostgreSQLArrayColumnType.getTypeOidByColumnTypeName(columnTypeName); - } else { - typeOID = PostgreSQLColumnType.valueOfJDBCType(columnType).getValue(); - } this.columnLength = columnLength; + typeOID = Types.ARRAY == columnType ? PostgreSQLArrayColumnType.getTypeOidByColumnTypeName(columnTypeName) : PostgreSQLColumnType.valueOfJDBCType(columnType).getValue(); } } diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLRowDescriptionPacket.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLRowDescriptionPacket.java index 8d4d3dcda0d5c866d7ff727afc945fceec1d9912..bf50ef8990daaba3c4dd3ae88edd7a6e1f0dd69b 100644 --- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLRowDescriptionPacket.java +++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLRowDescriptionPacket.java @@ -23,7 +23,7 @@ import org.apache.shardingsphere.db.protocol.postgresql.packet.PostgreSQLPacket; import org.apache.shardingsphere.db.protocol.postgresql.packet.command.PostgreSQLCommandPacketType; import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload; -import java.util.List; +import java.util.Collection; /** * Row description packet for PostgreSQL. @@ -37,7 +37,7 @@ public final class PostgreSQLRowDescriptionPacket implements PostgreSQLPacket { @Getter private final int fieldCount; - private final List columnDescriptions; + private final Collection columnDescriptions; @Override public void write(final PostgreSQLPacketPayload payload) { diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLColumnDescriptionTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLColumnDescriptionTest.java index 99d7951ead52a2e5ea9b996da88b26d839f4edbc..ec1d734464836f9ec35131cdda503c3d39a9c663 100644 --- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLColumnDescriptionTest.java +++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLColumnDescriptionTest.java @@ -19,14 +19,10 @@ package org.apache.shardingsphere.db.protocol.postgresql.packet.command.query; import org.junit.Test; -import java.sql.ResultSetMetaData; -import java.sql.SQLException; import java.sql.Types; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; public final class PostgreSQLColumnDescriptionTest { @@ -43,10 +39,8 @@ public final class PostgreSQLColumnDescriptionTest { } @Test - public void assertIntegerArrayTypeOid() throws SQLException { - ResultSetMetaData resultSetMetaData = mock(ResultSetMetaData.class); - when(resultSetMetaData.getColumnTypeName(2)).thenReturn("_int4"); - PostgreSQLColumnDescription description = new PostgreSQLColumnDescription("ages", 2, Types.ARRAY, 12, resultSetMetaData); + public void assertIntegerArrayTypeOid() { + PostgreSQLColumnDescription description = new PostgreSQLColumnDescription("ages", 2, Types.ARRAY, 12, "_int4"); assertThat(description.getTypeOID(), is(1007)); } } diff --git a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/QueryResult.java b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/QueryResult.java index ca8cc56d4419aeaa5f48bf489717edb5d7c1b74c..68bd011dfe4d3d9967e91b343945b03b0c9bf472 100644 --- a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/QueryResult.java +++ b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/QueryResult.java @@ -18,7 +18,6 @@ package org.apache.shardingsphere.infra.executor.sql; import java.io.InputStream; -import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.Calendar; @@ -27,13 +26,6 @@ import java.util.Calendar; */ public interface QueryResult { - /** - * Get JDBC ResultSetMetaData. - * - * @return JDBC ResultSetMetaData - */ - ResultSetMetaData getResultSetMetaData(); - /** * iterate next data. * @@ -106,4 +98,13 @@ public interface QueryResult { * @throws SQLException SQL Exception */ String getColumnLabel(int columnIndex) throws SQLException; + + /** + * Get column type name. + * + * @param columnIndex column index + * @return column type name + * @throws SQLException SQL Exception + */ + String getColumnTypeName(int columnIndex) throws SQLException; } diff --git a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/resourced/jdbc/queryresult/MemoryQueryResult.java b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/resourced/jdbc/queryresult/MemoryQueryResult.java index 0c478e08f19e9705769af44117728ffcffcdbd7f..2180fb13d1c11d0ca63f0a2bffa968d560b1ecc5 100644 --- a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/resourced/jdbc/queryresult/MemoryQueryResult.java +++ b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/resourced/jdbc/queryresult/MemoryQueryResult.java @@ -114,12 +114,7 @@ public final class MemoryQueryResult implements QueryResult { return resultSet.getObject(columnIndex); } } - - @Override - public ResultSetMetaData getResultSetMetaData() { - return resultSetMetaData; - } - + @Override public boolean next() { if (rows.hasNext()) { @@ -174,4 +169,9 @@ public final class MemoryQueryResult implements QueryResult { public String getColumnLabel(final int columnIndex) throws SQLException { return resultSetMetaData.getColumnLabel(columnIndex); } + + @Override + public String getColumnTypeName(final int columnIndex) throws SQLException { + return resultSetMetaData.getColumnTypeName(columnIndex); + } } diff --git a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/resourced/jdbc/queryresult/StreamQueryResult.java b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/resourced/jdbc/queryresult/StreamQueryResult.java index f8bd296289a47449fa45b1f829d0c1924bbe0181..d30e20e5b78e2af177d436edf4f944205f5a4915 100644 --- a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/resourced/jdbc/queryresult/StreamQueryResult.java +++ b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/resourced/jdbc/queryresult/StreamQueryResult.java @@ -17,11 +17,11 @@ package org.apache.shardingsphere.infra.executor.sql.resourced.jdbc.queryresult; -import java.sql.Array; import org.apache.shardingsphere.infra.executor.sql.QueryResult; import java.io.InputStream; import java.math.BigDecimal; +import java.sql.Array; import java.sql.Blob; import java.sql.Clob; import java.sql.Date; @@ -45,12 +45,7 @@ public final class StreamQueryResult implements QueryResult { resultSetMetaData = resultSet.getMetaData(); this.resultSet = resultSet; } - - @Override - public ResultSetMetaData getResultSetMetaData() { - return resultSetMetaData; - } - + @Override public boolean next() throws SQLException { return resultSet.next(); @@ -143,4 +138,9 @@ public final class StreamQueryResult implements QueryResult { public String getColumnLabel(final int columnIndex) throws SQLException { return resultSetMetaData.getColumnLabel(columnIndex); } + + @Override + public String getColumnTypeName(final int columnIndex) throws SQLException { + return resultSetMetaData.getColumnTypeName(columnIndex); + } } diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/ShowTablesBackendHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/ShowTablesBackendHandler.java index 6043376a47fa2d7663461112b891a5f45669c80c..70f961a39bde1ef3c3d95cf2c3c3b5e2ee8b0638 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/ShowTablesBackendHandler.java +++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/ShowTablesBackendHandler.java @@ -51,15 +51,15 @@ public final class ShowTablesBackendHandler implements TextProtocolBackendHandle @Override public BackendResponse execute() throws SQLException { + QueryResponse result = createQueryResponse(backendConnection.getSchemaName()); if (!ProxyContext.getInstance().getMetaData(backendConnection.getSchemaName()).isComplete()) { - return getDefaultQueryResponse(backendConnection.getSchemaName()); + return result; } - // TODO Get all tables from meta data databaseCommunicationEngine = databaseCommunicationEngineFactory.newTextProtocolInstance(sqlStatement, sql, backendConnection); return databaseCommunicationEngine.execute(); } - private QueryResponse getDefaultQueryResponse(final String schemaName) { + private QueryResponse createQueryResponse(final String schemaName) { String column = String.format("Tables_in_%s", schemaName); return new QueryResponse(Collections.singletonList(new QueryHeader(schemaName, "", column, column, 64, Types.VARCHAR, 0, false, false, false, false))); } diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/binary/bind/PostgreSQLComBindExecutor.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/binary/bind/PostgreSQLComBindExecutor.java index 0801e0d71513e49c594a91ee080e3c481bb427e6..c5955f6869abce99bf2b02dc16cf33417a5e16a2 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/binary/bind/PostgreSQLComBindExecutor.java +++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/binary/bind/PostgreSQLComBindExecutor.java @@ -45,7 +45,6 @@ import org.apache.shardingsphere.proxy.frontend.command.executor.QueryCommandExe import org.apache.shardingsphere.proxy.frontend.command.executor.ResponseType; import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement; -import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; @@ -100,8 +99,8 @@ public final class PostgreSQLComBindExecutor implements QueryCommandExecutor { return result; } - private Optional createQueryPacket(final QueryResponse queryResponse) { - List columnDescriptions = getPostgreSQLColumnDescriptions(queryResponse); + private Optional createQueryPacket(final QueryResponse queryResponse) throws SQLException { + Collection columnDescriptions = createColumnDescriptions(queryResponse); if (columnDescriptions.isEmpty()) { responseType = ResponseType.QUERY; } @@ -111,13 +110,13 @@ public final class PostgreSQLComBindExecutor implements QueryCommandExecutor { return Optional.of(new PostgreSQLRowDescriptionPacket(columnDescriptions.size(), columnDescriptions)); } - private List getPostgreSQLColumnDescriptions(final QueryResponse queryResponse) { - List result = new LinkedList<>(); + private Collection createColumnDescriptions(final QueryResponse queryResponse) throws SQLException { + Collection result = new LinkedList<>(); List queryResults = queryResponse.getQueryResults(); - ResultSetMetaData resultSetMetaData = queryResults.isEmpty() ? null : queryResults.get(0).getResultSetMetaData(); int columnIndex = 0; for (QueryHeader each : queryResponse.getQueryHeaders()) { - result.add(new PostgreSQLColumnDescription(each.getColumnName(), ++columnIndex, each.getColumnType(), each.getColumnLength(), resultSetMetaData)); + String columnTypeName = queryResults.isEmpty() ? null : queryResults.get(0).getColumnTypeName(columnIndex + 1); + result.add(new PostgreSQLColumnDescription(each.getColumnName(), ++columnIndex, each.getColumnType(), each.getColumnLength(), columnTypeName)); } return result; } diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/text/PostgreSQLComQueryExecutor.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/text/PostgreSQLComQueryExecutor.java index 947a6c6dd22761cbf74788f96afd63acef1f417c..7c2e52cdef77ebbb20096c3497345a365e1f7502 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/text/PostgreSQLComQueryExecutor.java +++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/text/PostgreSQLComQueryExecutor.java @@ -39,7 +39,6 @@ import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandlerFa import org.apache.shardingsphere.proxy.frontend.command.executor.QueryCommandExecutor; import org.apache.shardingsphere.proxy.frontend.command.executor.ResponseType; -import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.Collection; import java.util.Collections; @@ -75,8 +74,8 @@ public final class PostgreSQLComQueryExecutor implements QueryCommandExecutor { return Collections.singletonList(createUpdatePacket((UpdateResponse) backendResponse)); } - private Optional createQueryPacket(final QueryResponse queryResponse) { - List columnDescriptions = getPostgreSQLColumnDescriptions(queryResponse); + private Optional createQueryPacket(final QueryResponse queryResponse) throws SQLException { + Collection columnDescriptions = createColumnDescriptions(queryResponse); if (columnDescriptions.isEmpty()) { responseType = ResponseType.QUERY; } @@ -86,13 +85,13 @@ public final class PostgreSQLComQueryExecutor implements QueryCommandExecutor { return Optional.of(new PostgreSQLRowDescriptionPacket(columnDescriptions.size(), columnDescriptions)); } - private List getPostgreSQLColumnDescriptions(final QueryResponse queryResponse) { - List result = new LinkedList<>(); + private Collection createColumnDescriptions(final QueryResponse queryResponse) throws SQLException { + Collection result = new LinkedList<>(); List queryResults = queryResponse.getQueryResults(); - ResultSetMetaData resultSetMetaData = queryResults.isEmpty() ? null : queryResults.get(0).getResultSetMetaData(); int columnIndex = 0; for (QueryHeader each : queryResponse.getQueryHeaders()) { - result.add(new PostgreSQLColumnDescription(each.getColumnName(), ++columnIndex, each.getColumnType(), each.getColumnLength(), resultSetMetaData)); + String columnTypeName = queryResults.isEmpty() ? null : queryResults.get(0).getColumnTypeName(columnIndex + 1); + result.add(new PostgreSQLColumnDescription(each.getColumnName(), ++columnIndex, each.getColumnType(), each.getColumnLength(), columnTypeName)); } return result; }