fix(kingbase): use catalog column metadata
This commit is contained in:
1 parent
def59969e5
commit
d024ac3bf4
4 files changed
+358
No files matched your search
@@ -214,6 +214,62 @@ public final class KingbaseAgent extends PostgresLikeAgent {
|
||||
public List<ColumnInfo> getColumns(String schema, String table) {
|
||||
return unchecked(() -> {
|
||||
Set<String> primaryKeys = primaryKeys(schema, table);
|
||||
if (!isMysqlCompatMode()) {
|
||||
return getRegularColumns(schema, table, primaryKeys);
|
||||
}
|
||||
return getInformationSchemaColumns(schema, table, primaryKeys);
|
||||
});
|
||||
}
|
||||
|
||||
private List<ColumnInfo> getRegularColumns(String schema, String table, Set<String> primaryKeys) {
|
||||
return unchecked(() -> {
|
||||
List<ColumnInfo> result = new ArrayList<>();
|
||||
String sql = "SELECT a.attname AS column_name, " +
|
||||
"format_type(a.atttypid, a.atttypmod) AS data_type, " +
|
||||
"NOT a.attnotnull AS is_nullable, " +
|
||||
"sys_get_expr(ad.adbin, ad.adrelid) AS column_default, " +
|
||||
"d.description AS column_comment, " +
|
||||
"CASE WHEN t.typname = 'numeric' AND a.atttypmod > 0 " +
|
||||
"THEN ((a.atttypmod - 4) >> 16) & 65535 ELSE NULL END AS numeric_precision, " +
|
||||
"CASE WHEN t.typname = 'numeric' AND a.atttypmod > 0 " +
|
||||
"THEN (a.atttypmod - 4) & 65535 ELSE NULL END AS numeric_scale, " +
|
||||
"CASE WHEN t.typname IN ('varchar', 'bpchar') AND a.atttypmod > 0 " +
|
||||
"THEN a.atttypmod - 4 ELSE NULL END AS character_maximum_length " +
|
||||
"FROM sys_catalog.sys_attribute a " +
|
||||
"JOIN sys_catalog.sys_type t ON t.oid = a.atttypid " +
|
||||
"JOIN sys_catalog.sys_class c ON c.oid = a.attrelid " +
|
||||
"JOIN sys_catalog.sys_namespace n ON n.oid = c.relnamespace " +
|
||||
"LEFT JOIN sys_catalog.sys_attrdef ad ON ad.adrelid = a.attrelid AND ad.adnum = a.attnum " +
|
||||
"LEFT JOIN sys_catalog.sys_description d ON d.objoid = a.attrelid AND d.objsubid = a.attnum " +
|
||||
"WHERE n.nspname = " + sqlString(effectiveSchema(schema)) +
|
||||
" AND c.relname = " + sqlString(table) + " " +
|
||||
"AND a.attnum > 0 AND NOT a.attisdropped " +
|
||||
"ORDER BY a.attnum";
|
||||
try (Statement stmt = requireConnected().createStatement()) {
|
||||
try (ResultSet rs = stmt.executeQuery(sql)) {
|
||||
while (rs.next()) {
|
||||
String columnName = rs.getString("column_name");
|
||||
result.add(new ColumnInfo(
|
||||
columnName,
|
||||
rs.getString("data_type"),
|
||||
rs.getBoolean("is_nullable"),
|
||||
rs.getString("column_default"),
|
||||
primaryKeys.contains(columnName),
|
||||
null,
|
||||
rs.getString("column_comment"),
|
||||
intObject(rs, "numeric_precision"),
|
||||
intObject(rs, "numeric_scale"),
|
||||
intObject(rs, "character_maximum_length")
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
private List<ColumnInfo> getInformationSchemaColumns(String schema, String table, Set<String> primaryKeys) {
|
||||
return unchecked(() -> {
|
||||
List<ColumnInfo> result = new ArrayList<>();
|
||||
String sql = "SELECT column_name, data_type, is_nullable, column_default, " +
|
||||
"numeric_precision, numeric_scale, character_maximum_length " +
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.dbx.agent.kingbase;
|
||||
|
||||
import com.dbx.agent.ColumnInfo;
|
||||
import com.dbx.agent.DatabaseAgent;
|
||||
import com.dbx.agent.DatabaseInfo;
|
||||
import com.dbx.agent.ObjectInfo;
|
||||
@@ -167,6 +168,78 @@ class KingbaseAgentTest extends JdbcFakeExecutionBehaviorTest {
|
||||
Assertions.assertTrue(sql.get(0).contains("FROM sys_catalog.sys_proc"), sql.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void regularGetColumnsUsesFormattedCatalogTypes() {
|
||||
List<String> sql = new ArrayList<>();
|
||||
KingbaseAgent agent = new KingbaseAgent();
|
||||
TestSupport.setPrivateConnection(agent, preparedConnection(sql,
|
||||
resultSet(
|
||||
new String[]{"column_name"},
|
||||
new Object[][]{{"id"}}
|
||||
),
|
||||
resultSet(
|
||||
new String[]{
|
||||
"column_name",
|
||||
"data_type",
|
||||
"is_nullable",
|
||||
"column_default",
|
||||
"column_comment",
|
||||
"numeric_precision",
|
||||
"numeric_scale",
|
||||
"character_maximum_length"
|
||||
},
|
||||
new Object[][]{
|
||||
{"id", "integer", false, "nextval('orders_id_seq'::regclass)", "identifier", 32, 0, null},
|
||||
{"create_time", "timestamp with time zone", true, null, null, null, null, null},
|
||||
{"name", "character varying(64)", true, null, "display name", null, null, 64}
|
||||
}
|
||||
)
|
||||
));
|
||||
|
||||
List<ColumnInfo> columns = agent.getColumns("public", "orders");
|
||||
|
||||
Assertions.assertEquals(3, columns.size());
|
||||
Assertions.assertEquals("integer", columns.get(0).getData_type());
|
||||
Assertions.assertTrue(columns.get(0).getIs_primary_key());
|
||||
Assertions.assertFalse(columns.get(0).getIs_nullable());
|
||||
Assertions.assertEquals("timestamp with time zone", columns.get(1).getData_type());
|
||||
Assertions.assertNotEquals("USER-DEFINED", columns.get(1).getData_type());
|
||||
Assertions.assertEquals(Integer.valueOf(64), columns.get(2).getCharacter_maximum_length());
|
||||
Assertions.assertTrue(sql.get(1).contains("format_type(a.atttypid, a.atttypmod) AS data_type"), sql.get(1));
|
||||
Assertions.assertTrue(sql.get(1).contains("FROM sys_catalog.sys_attribute"), sql.get(1));
|
||||
Assertions.assertFalse(sql.get(1).contains("information_schema.columns"), sql.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mysqlCompatGetColumnsKeepsInformationSchemaPath() {
|
||||
List<String> sql = new ArrayList<>();
|
||||
KingbaseAgent agent = new KingbaseAgent();
|
||||
agent.setMysqlCompatMode(true);
|
||||
TestSupport.setPrivateConnection(agent, preparedConnection(sql,
|
||||
resultSet(
|
||||
new String[]{"column_name"},
|
||||
new Object[][]{{"id"}}
|
||||
),
|
||||
resultSet(
|
||||
new String[]{
|
||||
"column_name",
|
||||
"data_type",
|
||||
"is_nullable",
|
||||
"column_default",
|
||||
"numeric_precision",
|
||||
"numeric_scale",
|
||||
"character_maximum_length"
|
||||
},
|
||||
new Object[][]{{"id", "int", "NO", null, 32, 0, null}}
|
||||
)
|
||||
));
|
||||
|
||||
List<ColumnInfo> columns = agent.getColumns("PUBLIC", "orders");
|
||||
|
||||
Assertions.assertEquals("int", columns.get(0).getData_type());
|
||||
Assertions.assertTrue(sql.get(1).contains("FROM information_schema.columns"), sql.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mysqlCompatTimestampTypeNameIsReadAsTimestampText() throws Exception {
|
||||
Timestamp timestamp = Timestamp.valueOf("2026-06-22 11:29:00");
|
||||
@@ -243,6 +316,13 @@ class KingbaseAgentTest extends JdbcFakeExecutionBehaviorTest {
|
||||
}
|
||||
}
|
||||
return null;
|
||||
case "getBoolean":
|
||||
Object booleanValue = columnValue(columns, rows[index[0]], args[0]);
|
||||
if (booleanValue instanceof Boolean) return booleanValue;
|
||||
if (booleanValue instanceof Number) return ((Number) booleanValue).intValue() != 0;
|
||||
return Boolean.parseBoolean(String.valueOf(booleanValue));
|
||||
case "getObject":
|
||||
return columnValue(columns, rows[index[0]], args[0]);
|
||||
case "wasNull":
|
||||
return false;
|
||||
case "close":
|
||||
@@ -253,6 +333,18 @@ class KingbaseAgentTest extends JdbcFakeExecutionBehaviorTest {
|
||||
});
|
||||
}
|
||||
|
||||
private static Object columnValue(String[] columns, Object[] row, Object key) {
|
||||
if (key instanceof Number) {
|
||||
return row[((Number) key).intValue() - 1];
|
||||
}
|
||||
for (int i = 0; i < columns.length; i++) {
|
||||
if (columns[i].equalsIgnoreCase(String.valueOf(key))) {
|
||||
return row[i];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static ResultSet timestampResultSet(Timestamp timestamp) {
|
||||
return proxy(ResultSet.class, (method, args) -> {
|
||||
switch (method.getName()) {
|
||||
|
||||
Reference in new issue
Block a user