Skip to content

Commit 2cbdc92

Browse files
committed
fix binary protocol transmission issue in postgresql
1 parent 8bab753 commit 2cbdc92

File tree

69 files changed

+2645
-1109
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+2645
-1109
lines changed

database/protocol/type/postgresql/src/main/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/PostgreSQLDataRowPacket.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ private void writeBinaryValue(final PostgreSQLPacketPayload payload, final Binar
5959
return;
6060
}
6161
PostgreSQLBinaryProtocolValue binaryProtocolValue = PostgreSQLBinaryProtocolValueFactory.getBinaryProtocolValue(each.getColumnType());
62-
payload.writeInt4(binaryProtocolValue.getColumnLength(payload, value));
62+
int columnLength = binaryProtocolValue.getColumnLength(payload, value);
63+
if (columnLength > 0) {
64+
payload.writeInt4(columnLength);
65+
}
6366
binaryProtocolValue.write(payload, value);
6467
}
6568

database/protocol/type/postgresql/src/main/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/extended/PostgreSQLColumnType.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ public enum PostgreSQLColumnType implements BinaryColumnType {
197197
JDBC_TYPE_AND_COLUMN_TYPE_MAP.put(Types.BOOLEAN, BOOL);
198198
// TODO Temporary solution for https://github.com/apache/shardingsphere/issues/22522
199199
JDBC_TYPE_AND_COLUMN_TYPE_MAP.put(Types.STRUCT, VARCHAR);
200-
JDBC_TYPE_AND_COLUMN_TYPE_MAP.put(Types.ARRAY, TEXT_ARRAY);
201200
}
202201

203202
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.extended.bind.protocol;
19+
20+
import org.apache.shardingsphere.database.protocol.postgresql.payload.PostgreSQLPacketPayload;
21+
import org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.extended.bind.protocol.util.codec.decoder.PgBinaryObj;
22+
import org.postgresql.core.Oid;
23+
import org.postgresql.jdbc.ShardingSpherePgArrayUtils;
24+
25+
import java.nio.ByteBuffer;
26+
import java.util.HashMap;
27+
import java.util.Map;
28+
29+
public class PostgreSQLArrayBinaryProtocolValue implements PostgreSQLBinaryProtocolValue {
30+
31+
private static final Map<Integer, String> oidTypeName = new HashMap<>();
32+
33+
static {
34+
oidTypeName.put(Oid.BOOL, "bool[]");
35+
oidTypeName.put(Oid.BYTEA, "bytea[]");
36+
// oidTypeName.put(Oid.CHAR_ARRAY, "char[]");
37+
// oidTypeName.put(Oid.NAME_ARRAY, "name[]");
38+
oidTypeName.put(Oid.INT2, "int2[]");
39+
oidTypeName.put(Oid.INT4, "int4[]");
40+
oidTypeName.put(Oid.INT8, "int8[]");
41+
oidTypeName.put(Oid.FLOAT4, "float4[]");
42+
oidTypeName.put(Oid.FLOAT8, "float8[]");
43+
oidTypeName.put(Oid.TEXT, "text[]");
44+
oidTypeName.put(Oid.VARCHAR, "varchar[]");
45+
oidTypeName.put(Oid.DATE, "date[]");
46+
oidTypeName.put(Oid.TIMESTAMP, "timestamp[]");
47+
// oidTypeName.put(Oid.TIMESTAMPTZ_ARRAY, "timestamptz[]");
48+
oidTypeName.put(Oid.TIME, "time[]");
49+
// oidTypeName.put(Oid.TIMETZ_ARRAY, "timetz[]");
50+
oidTypeName.put(Oid.NUMERIC, "numeric[]");
51+
// oidTypeName.put(Oid.UUID_ARRAY, "uuid[]");
52+
}
53+
54+
private PostgreSQLArrayBinaryProtocolValue() {
55+
56+
}
57+
public static final PostgreSQLArrayBinaryProtocolValue instance = new PostgreSQLArrayBinaryProtocolValue();
58+
59+
@Override
60+
public int getColumnLength(PostgreSQLPacketPayload payload, Object value) {
61+
return -1;
62+
}
63+
64+
@Override
65+
public Object read(PostgreSQLPacketPayload payload, int parameterValueLength) {
66+
byte[] bytes = new byte[parameterValueLength];
67+
payload.getByteBuf().readBytes(bytes);
68+
ByteBuffer buf = ByteBuffer.wrap(bytes);
69+
int oid = buf.getInt(8);
70+
String typeName = oidTypeName.get(oid);
71+
PgBinaryObj pgBinaryObj = new PgBinaryObj(bytes);
72+
pgBinaryObj.setType(typeName);
73+
return pgBinaryObj;
74+
}
75+
76+
@Override
77+
public void write(PostgreSQLPacketPayload payload, Object value) {
78+
byte[] result = ShardingSpherePgArrayUtils.getBinaryBytes(value, payload.getCharset());
79+
payload.writeInt4(result.length);
80+
payload.writeBytes(result);
81+
}
82+
}

0 commit comments

Comments
 (0)