fix(jdbc): improve goldendb metadata handling
This commit is contained in:
@@ -973,7 +973,7 @@ public final class DbxJdbcPlugin {
|
||||
appendColumns(result, meta, null, schemaPattern, table, primaryKeys);
|
||||
}
|
||||
if (quirks.useCatalogFallbackSql()) {
|
||||
mergeShowFullColumnComments(conn, result, schemaPattern, table);
|
||||
mergeShowFullColumnMetadata(conn, result, schemaPattern, table);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -1148,19 +1148,29 @@ public final class DbxJdbcPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
private static void mergeShowFullColumnComments(Connection conn, ArrayNode result, String schema, String table) {
|
||||
private static void mergeShowFullColumnMetadata(Connection conn, ArrayNode result, String schema, String table) {
|
||||
String target = qualifiedJdbcTableName(schema, table);
|
||||
try (Statement statement = conn.createStatement(); ResultSet rs = statement.executeQuery("SHOW FULL COLUMNS FROM " + target)) {
|
||||
int fieldIndex = resultSetColumnIndex(rs, "Field");
|
||||
int typeIndex = resultSetColumnIndex(rs, "Type");
|
||||
int extraIndex = resultSetColumnIndex(rs, "Extra");
|
||||
int commentIndex = resultSetColumnIndex(rs, "Comment");
|
||||
if (fieldIndex <= 0 || commentIndex <= 0) {
|
||||
if (fieldIndex <= 0) {
|
||||
return;
|
||||
}
|
||||
while (rs.next()) {
|
||||
String name = rs.getString(fieldIndex);
|
||||
String comment = rs.getString(commentIndex);
|
||||
if (name != null) {
|
||||
putNullablePreferValue(columnNode(result, name), "comment", comment);
|
||||
ObjectNode item = columnNode(result, name);
|
||||
if (typeIndex > 0) {
|
||||
putNullablePreferValue(item, "data_type", rs.getString(typeIndex));
|
||||
}
|
||||
if (extraIndex > 0) {
|
||||
putNullablePreferValue(item, "extra", rs.getString(extraIndex));
|
||||
}
|
||||
if (commentIndex > 0) {
|
||||
putNullablePreferValue(item, "comment", rs.getString(commentIndex));
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (SQLException | AbstractMethodError | UnsupportedOperationException ignored) {
|
||||
|
||||
@@ -2,6 +2,8 @@ package app.dbx.jdbc;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -557,6 +559,30 @@ final class DbxJdbcPluginTest {
|
||||
assertEquals(true, response.path("result").path(0).path("is_primary_key").asBoolean());
|
||||
}
|
||||
|
||||
@Test
|
||||
void showFullColumnsMetadataCompletesMysqlCompatibleTypesAndComments() throws Exception {
|
||||
Method method = DbxJdbcPlugin.class.getDeclaredMethod(
|
||||
"mergeShowFullColumnMetadata",
|
||||
Connection.class,
|
||||
ArrayNode.class,
|
||||
String.class,
|
||||
String.class
|
||||
);
|
||||
method.setAccessible(true);
|
||||
ArrayNode columns = MAPPER.createArrayNode();
|
||||
ObjectNode column = columns.addObject();
|
||||
column.put("name", "name");
|
||||
column.put("data_type", "varchar");
|
||||
column.putNull("extra");
|
||||
column.putNull("comment");
|
||||
|
||||
method.invoke(null, showFullColumnsConnection(), columns, "app", "people");
|
||||
|
||||
assertEquals("varchar(32)", columns.path(0).path("data_type").asText());
|
||||
assertEquals("auto_increment", columns.path(0).path("extra").asText());
|
||||
assertEquals("姓名", columns.path(0).path("comment").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void oracleMetadataObjectTypeAcceptsPackageBodyAliases() throws Exception {
|
||||
Method method = DbxJdbcPlugin.class.getDeclaredMethod("oracleMetadataObjectType", String.class);
|
||||
@@ -744,6 +770,66 @@ final class DbxJdbcPluginTest {
|
||||
);
|
||||
}
|
||||
|
||||
private static Connection showFullColumnsConnection() {
|
||||
return (Connection) Proxy.newProxyInstance(
|
||||
DbxJdbcPluginTest.class.getClassLoader(),
|
||||
new Class<?>[] { Connection.class },
|
||||
(proxy, method, args) -> switch (method.getName()) {
|
||||
case "createStatement" -> showFullColumnsStatement();
|
||||
case "isClosed" -> false;
|
||||
case "close" -> null;
|
||||
default -> defaultValue(method.getReturnType());
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private static Statement showFullColumnsStatement() {
|
||||
return (Statement) Proxy.newProxyInstance(
|
||||
DbxJdbcPluginTest.class.getClassLoader(),
|
||||
new Class<?>[] { Statement.class },
|
||||
(proxy, method, args) -> switch (method.getName()) {
|
||||
case "executeQuery" -> showFullColumnsResultSet();
|
||||
case "close" -> null;
|
||||
default -> defaultValue(method.getReturnType());
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private static ResultSet showFullColumnsResultSet() {
|
||||
String[] labels = { "Field", "Type", "Extra", "Comment" };
|
||||
String[][] rows = { { "name", "varchar(32)", "auto_increment", "姓名" } };
|
||||
return (ResultSet) Proxy.newProxyInstance(
|
||||
DbxJdbcPluginTest.class.getClassLoader(),
|
||||
new Class<?>[] { ResultSet.class },
|
||||
new java.lang.reflect.InvocationHandler() {
|
||||
private int index = -1;
|
||||
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args) {
|
||||
return switch (method.getName()) {
|
||||
case "next" -> ++index < rows.length;
|
||||
case "getMetaData" -> resultSetMeta(labels);
|
||||
case "getString" -> rows[index][((Integer) args[0]) - 1];
|
||||
case "close" -> null;
|
||||
default -> defaultValue(method.getReturnType());
|
||||
};
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private static ResultSetMetaData resultSetMeta(String[] labels) {
|
||||
return (ResultSetMetaData) Proxy.newProxyInstance(
|
||||
DbxJdbcPluginTest.class.getClassLoader(),
|
||||
new Class<?>[] { ResultSetMetaData.class },
|
||||
(proxy, method, args) -> switch (method.getName()) {
|
||||
case "getColumnCount" -> labels.length;
|
||||
case "getColumnLabel", "getColumnName" -> labels[((Integer) args[0]) - 1];
|
||||
default -> defaultValue(method.getReturnType());
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private static final class BrokenResultSetDriver implements Driver {
|
||||
private final String urlPrefix;
|
||||
private final boolean executeReturnsResultSet;
|
||||
|
||||
Reference in New Issue
Block a user