[CONJ-1256] returns server real catalog name

This commit is contained in:
Diego Dupin
2025-05-23 11:00:50 +02:00
parent 9df73f7d24
commit f6724cc98f
6 changed files with 63 additions and 14 deletions

View File

@ -110,22 +110,23 @@ public interface ColumnDecoder extends Column {
* @param flags column flags
* @return Column
*/
static ColumnDecoder create(String name, DataType type, int flags) {
static ColumnDecoder create(String database, String name, DataType type, int flags) {
byte[] databaseBytes = (database == null ? "def" : database).getBytes(StandardCharsets.UTF_8);
byte[] nameBytes = name.getBytes(StandardCharsets.UTF_8);
byte[] arr = new byte[9 + 2 * nameBytes.length];
arr[0] = 3;
arr[1] = 'D';
arr[2] = 'E';
arr[3] = 'F';
byte[] arr = new byte[databaseBytes.length + 6 + 2 * nameBytes.length];
// write catalog
arr[0] = (byte) databaseBytes.length;
System.arraycopy(databaseBytes, 0, arr, 1, databaseBytes.length);
int[] stringPos = new int[5];
stringPos[0] = 4; // schema pos
stringPos[1] = 5; // table alias pos
stringPos[2] = 6; // table pos
stringPos[0] = databaseBytes.length + 1; // schema pos
stringPos[1] = databaseBytes.length + 2; // table alias pos
stringPos[2] = databaseBytes.length + 3; // table pos
// lenenc_str name
// lenenc_str org_name
int pos = 7;
int pos = databaseBytes.length + 4;
for (int i = 0; i < 2; i++) {
stringPos[i + 3] = pos;
arr[pos++] = (byte) nameBytes.length;

View File

@ -179,7 +179,7 @@ public class CompleteResult extends Result {
ColumnDecoder[] columns = new ColumnDecoder[columnNameLength];
for (int i = 0; i < columnNameLength; i++) {
columns[i] = ColumnDecoder.create(columnNames[i], columnTypes[i], flags);
columns[i] = ColumnDecoder.create(context.getDatabase(), columnNames[i], columnTypes[i], flags);
}
List<byte[]> rows = new ArrayList<>();

View File

@ -98,7 +98,8 @@ public class ColumnDefinitionPacket implements Column, ServerMessage {
}
public String getCatalog() {
return "def";
buf.pos(0);
return buf.readString(buf.readIntLengthEncodedNotNull());
}
public String getSchema() {

View File

@ -90,7 +90,7 @@ public class FloatArray implements Array {
}
return new CompleteResult(
new ColumnDecoder[] {ColumnDecoder.create("Array", DataType.FLOAT, ColumnFlags.NOT_NULL)},
new ColumnDecoder[] {ColumnDecoder.create(context.getDatabase(), "Array", DataType.FLOAT, ColumnFlags.NOT_NULL)},
rows,
context,
ResultSet.TYPE_SCROLL_INSENSITIVE);

View File

@ -0,0 +1,47 @@
package org.mariadb.jdbc.unit.client;
import org.junit.jupiter.api.Test;
import org.mariadb.jdbc.client.ColumnDecoder;
import org.mariadb.jdbc.client.DataType;
import org.mariadb.jdbc.client.ReadableByteBuf;
import org.mariadb.jdbc.client.impl.StandardReadableByteBuf;
import org.mariadb.jdbc.unit.type.GeometryTest;
import java.sql.SQLException;
import static org.junit.jupiter.api.Assertions.*;
public class ColumnDecoderTest {
@Test
public void testWrongEncoding() throws SQLException {
/*
+--------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+------+--------------------------------------------------+------------------+
|000000| 35 00 00 02 03 64 65 66 05 74 65 73 74 6A 08 42 | 5....defg.testj.B |
|000010| 69 74 43 6F 64 65 63 08 42 69 74 43 6F 64 65 63 | itCodec.BitCodec |
|000020| 07 74 31 61 6C 69 61 73 02 74 31 00 0C 3F 00 01 | .t1alias.t1..?.. |
|000030| 00 00 00 10 20 00 00 00 00 | .... .... |
+------+--------------------------------------------------+------------------+
*/
byte[] ptBytes = GeometryTest.hexStringToByteArray("04 64 65 66 67 05 74 65 73 74 6A 08 42" +
"69 74 43 6F 64 65 63 08 42 69 74 43 6F 64 65 63" +
"07 74 31 61 6C 69 61 73 02 74 31 00 0C 3F 00 01" +
"00 00 00 10 20 00 00 00 00");
ReadableByteBuf readBuf = new StandardReadableByteBuf(ptBytes, ptBytes.length);
ColumnDecoder columnDecoder = ColumnDecoder.decodeStd(readBuf);
assertEquals("t1", columnDecoder.getColumnName());
assertEquals("t1alias", columnDecoder.getColumnAlias());
assertEquals("BitCodec", columnDecoder.getTable());
assertEquals("BitCodec", columnDecoder.getTableAlias());
assertEquals("defg", columnDecoder.getCatalog());
assertEquals("testj", columnDecoder.getSchema());
columnDecoder = ColumnDecoder.create("db", "test", DataType.GEOMETRY, 0);
assertEquals("test", columnDecoder.getColumnName());
assertEquals("test", columnDecoder.getColumnAlias());
assertEquals("db", columnDecoder.getCatalog());
}
}

View File

@ -301,7 +301,7 @@ public class GeometryTest {
SQLException.class,
() ->
Geometry.getGeometry(
readBuf, ptBytes.length, ColumnDecoder.create("test", DataType.GEOMETRY, 0)));
readBuf, ptBytes.length, ColumnDecoder.create("db", "test", DataType.GEOMETRY, 0)));
assertNull(Geometry.getGeometry(new StandardReadableByteBuf(new byte[0], 0), 0, null));
}
}