Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.shardingsphere.proxy.frontend.postgresql.command.query;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.database.protocol.postgresql.constant.PostgreSQLValueFormat;
import org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.PostgreSQLColumnDescription;
import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
import org.apache.shardingsphere.proxy.backend.postgresql.response.header.query.PostgreSQLQueryHeaderBuilder;
import org.apache.shardingsphere.proxy.backend.response.header.query.QueryHeader;

/**
* PostgreSQL column description builder.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class PostgreSQLColumnDescriptionBuilder {

/**
* Build a column description.
*
* @param queryHeader query header
* @param columnIndex column index
* @param valueFormat value format
* @return column description
*/
@HighFrequencyInvocation
public static PostgreSQLColumnDescription build(final QueryHeader queryHeader, final int columnIndex, final PostgreSQLValueFormat valueFormat) {
Integer typeOID = (Integer) queryHeader.getProtocolAttributes().get(PostgreSQLQueryHeaderBuilder.TYPE_OID);
return PostgreSQLValueFormat.TEXT == valueFormat && null != typeOID
? new PostgreSQLColumnDescription(queryHeader.getColumnLabel(), columnIndex, typeOID, queryHeader.getColumnLength(), valueFormat.getCode())
: new PostgreSQLColumnDescription(queryHeader.getColumnLabel(), columnIndex, queryHeader.getColumnType(), queryHeader.getColumnLength(), queryHeader.getColumnTypeName(),
valueFormat.getCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandler;
import org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandlerFactory;
import org.apache.shardingsphere.proxy.backend.postgresql.response.header.query.PostgreSQLQueryHeaderBuilder;
import org.apache.shardingsphere.proxy.backend.response.data.QueryResponseCell;
import org.apache.shardingsphere.proxy.backend.response.data.QueryResponseRow;
import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
import org.apache.shardingsphere.proxy.backend.response.header.query.QueryHeader;
import org.apache.shardingsphere.proxy.backend.response.header.query.QueryResponseHeader;
import org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
import org.apache.shardingsphere.proxy.frontend.postgresql.command.query.PostgreSQLColumnDescriptionBuilder;
import org.apache.shardingsphere.proxy.frontend.postgresql.command.query.PostgreSQLCommand;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dal.VariableAssignSegment;
import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
Expand Down Expand Up @@ -137,11 +137,7 @@ private Collection<PostgreSQLColumnDescription> createColumnDescriptions(final Q
int columnIndex = 0;
for (QueryHeader each : queryResponseHeader.getQueryHeaders()) {
PostgreSQLValueFormat valueFormat = determineValueFormat(columnIndex);
int currentColumnIndex = ++columnIndex;
Integer typeOID = (Integer) each.getProtocolAttributes().get(PostgreSQLQueryHeaderBuilder.TYPE_OID);
result.add(PostgreSQLValueFormat.TEXT == valueFormat && null != typeOID
? new PostgreSQLColumnDescription(each.getColumnLabel(), currentColumnIndex, typeOID, each.getColumnLength(), valueFormat.getCode())
: new PostgreSQLColumnDescription(each.getColumnLabel(), currentColumnIndex, each.getColumnType(), each.getColumnLength(), each.getColumnTypeName(), valueFormat.getCode()));
result.add(PostgreSQLColumnDescriptionBuilder.build(each, ++columnIndex, valueFormat));
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandler;
import org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandlerFactory;
import org.apache.shardingsphere.proxy.backend.handler.ProxySQLComQueryParser;
import org.apache.shardingsphere.proxy.backend.postgresql.response.header.query.PostgreSQLQueryHeaderBuilder;
import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
import org.apache.shardingsphere.proxy.backend.response.header.query.QueryHeader;
import org.apache.shardingsphere.proxy.backend.response.header.query.QueryResponseHeader;
Expand All @@ -43,6 +42,7 @@
import org.apache.shardingsphere.proxy.frontend.command.executor.QueryCommandExecutor;
import org.apache.shardingsphere.proxy.frontend.command.executor.ResponseType;
import org.apache.shardingsphere.proxy.frontend.postgresql.command.PortalContext;
import org.apache.shardingsphere.proxy.frontend.postgresql.command.query.PostgreSQLColumnDescriptionBuilder;
import org.apache.shardingsphere.proxy.frontend.postgresql.command.query.PostgreSQLCommand;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dal.VariableAssignSegment;
import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
Expand Down Expand Up @@ -100,11 +100,7 @@ private Collection<PostgreSQLColumnDescription> createColumnDescriptions(final Q
Collection<PostgreSQLColumnDescription> result = new LinkedList<>();
int columnIndex = 0;
for (QueryHeader each : queryResponseHeader.getQueryHeaders()) {
int currentColumnIndex = ++columnIndex;
Integer typeOID = (Integer) each.getProtocolAttributes().get(PostgreSQLQueryHeaderBuilder.TYPE_OID);
result.add(null == typeOID
? new PostgreSQLColumnDescription(each.getColumnLabel(), currentColumnIndex, each.getColumnType(), each.getColumnLength(), each.getColumnTypeName())
: new PostgreSQLColumnDescription(each.getColumnLabel(), currentColumnIndex, typeOID, each.getColumnLength(), PostgreSQLValueFormat.TEXT.getCode()));
result.add(PostgreSQLColumnDescriptionBuilder.build(each, ++columnIndex, PostgreSQLValueFormat.TEXT));
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ void assertExecuteSelectStatementAndReturnAllRows() throws SQLException, Reflect
QueryHeader queryHeader = new QueryHeader("schema", "table", "columnLabel", "columnName", Types.STRUCT, "record_type", 0, 0, false, false, false, false);
queryHeader.getProtocolAttributes().put(PostgreSQLQueryHeaderBuilder.TYPE_OID, 2249);
QueryHeader binaryQueryHeader = new QueryHeader("schema", "table", "columnLabel", "columnName", Types.STRUCT, "record_type", 0, 0, false, false, false, false);
binaryQueryHeader.getProtocolAttributes().put(PostgreSQLQueryHeaderBuilder.TYPE_OID, 2249);
when(responseHeader.getQueryHeaders()).thenReturn(Arrays.asList(queryHeader, binaryQueryHeader));
when(proxyBackendHandler.execute()).thenReturn(responseHeader);
when(proxyBackendHandler.next()).thenReturn(true, true, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
import org.apache.shardingsphere.database.exception.core.exception.data.InvalidParameterValueException;
import org.apache.shardingsphere.database.protocol.packet.DatabasePacket;
import org.apache.shardingsphere.database.protocol.postgresql.constant.PostgreSQLValueFormat;
import org.apache.shardingsphere.database.protocol.postgresql.packet.PostgreSQLPacket;
import org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.PostgreSQLColumnDescription;
import org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.PostgreSQLDataRowPacket;
Expand Down Expand Up @@ -131,9 +132,25 @@ void assertExecuteQueryWithColumnDescription() throws SQLException, ReflectiveOp
assertThat(columnDescription.getColumnIndex(), is(1));
assertThat(columnDescription.getColumnLength(), is(2));
assertThat(columnDescription.getTypeOID(), is(2249));
assertThat(columnDescription.getDataFormat(), is(PostgreSQLValueFormat.TEXT.getCode()));
assertThat(queryExecutor.getResponseType(), is(ResponseType.QUERY));
}

@Test
void assertExecuteQueryWithArrayColumnDescription() throws SQLException, ReflectiveOperationException {
QueryResponseHeader queryResponseHeader = mock(QueryResponseHeader.class);
QueryHeader queryHeader = new QueryHeader("schema", "table", "array_label", "column", Types.ARRAY, "_int4", 42, 0, false, false, false, false);
when(queryResponseHeader.getQueryHeaders()).thenReturn(Collections.singletonList(queryHeader));
when(proxyBackendHandler.execute()).thenReturn(queryResponseHeader);
PostgreSQLRowDescriptionPacket rowDescriptionPacket = (PostgreSQLRowDescriptionPacket) queryExecutor.execute().iterator().next();
PostgreSQLColumnDescription actual = getColumnDescriptions(rowDescriptionPacket).iterator().next();
assertThat(actual.getColumnName(), is("array_label"));
assertThat(actual.getColumnIndex(), is(1));
assertThat(actual.getColumnLength(), is(42));
assertThat(actual.getTypeOID(), is(1007));
assertThat(actual.getDataFormat(), is(PostgreSQLValueFormat.TEXT.getCode()));
}

@Test
void assertExecuteQueryWithEmptyColumns() throws SQLException, ReflectiveOperationException {
QueryResponseHeader queryResponseHeader = mock(QueryResponseHeader.class);
Expand Down
Loading