feat(oceanbase-oracle): include default/comment table metadata
This commit is contained in:
1 parent
dee5bb2fd8
commit
9cc7244dab
2 files changed
+108
-2
No files matched your search
+25
-2
@@ -4,6 +4,7 @@ import com.dbx.agent.ColumnInfo;
|
|||||||
import com.dbx.agent.ConfiguredJdbcAgent;
|
import com.dbx.agent.ConfiguredJdbcAgent;
|
||||||
import com.dbx.agent.ConnectParams;
|
import com.dbx.agent.ConnectParams;
|
||||||
import com.dbx.agent.DatabaseInfo;
|
import com.dbx.agent.DatabaseInfo;
|
||||||
|
import com.dbx.agent.DdlBuilder;
|
||||||
import com.dbx.agent.ForeignKeyInfo;
|
import com.dbx.agent.ForeignKeyInfo;
|
||||||
import com.dbx.agent.IndexInfo;
|
import com.dbx.agent.IndexInfo;
|
||||||
import com.dbx.agent.JdbcAgentProfile;
|
import com.dbx.agent.JdbcAgentProfile;
|
||||||
@@ -17,6 +18,7 @@ import com.dbx.agent.TriggerInfo;
|
|||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
@@ -277,7 +279,7 @@ public final class OceanBaseOracleAgent extends ConfiguredJdbcAgent {
|
|||||||
String owner = normalizeSchema(schema);
|
String owner = normalizeSchema(schema);
|
||||||
String sql = """
|
String sql = """
|
||||||
SELECT c.COLUMN_NAME, c.DATA_TYPE, c.NULLABLE, c.DATA_PRECISION, c.DATA_SCALE,
|
SELECT c.COLUMN_NAME, c.DATA_TYPE, c.NULLABLE, c.DATA_PRECISION, c.DATA_SCALE,
|
||||||
c.DATA_LENGTH, c.CHAR_LENGTH, cc.COMMENTS,
|
c.DATA_LENGTH, c.CHAR_LENGTH, c.DATA_DEFAULT, cc.COMMENTS,
|
||||||
CASE WHEN pk.COLUMN_NAME IS NULL THEN 0 ELSE 1 END AS IS_PK
|
CASE WHEN pk.COLUMN_NAME IS NULL THEN 0 ELSE 1 END AS IS_PK
|
||||||
FROM ALL_TAB_COLUMNS c
|
FROM ALL_TAB_COLUMNS c
|
||||||
LEFT JOIN ALL_COL_COMMENTS cc
|
LEFT JOIN ALL_COL_COMMENTS cc
|
||||||
@@ -301,6 +303,8 @@ public final class OceanBaseOracleAgent extends ConfiguredJdbcAgent {
|
|||||||
stmt.setString(4, table);
|
stmt.setString(4, table);
|
||||||
try (ResultSet rs = stmt.executeQuery()) {
|
try (ResultSet rs = stmt.executeQuery()) {
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
|
// Oracle-compatible DATA_DEFAULT is LONG-like; read it before other metadata fields.
|
||||||
|
String defaultValue = rs.getString("DATA_DEFAULT");
|
||||||
String name = rs.getString("COLUMN_NAME");
|
String name = rs.getString("COLUMN_NAME");
|
||||||
String baseType = rs.getString("DATA_TYPE");
|
String baseType = rs.getString("DATA_TYPE");
|
||||||
Integer numPrec = intOrNull(rs, "DATA_PRECISION");
|
Integer numPrec = intOrNull(rs, "DATA_PRECISION");
|
||||||
@@ -311,7 +315,7 @@ public final class OceanBaseOracleAgent extends ConfiguredJdbcAgent {
|
|||||||
name,
|
name,
|
||||||
formatDataType(baseType, numPrec, numScale, dataLen, charLen),
|
formatDataType(baseType, numPrec, numScale, dataLen, charLen),
|
||||||
"Y".equalsIgnoreCase(rs.getString("NULLABLE")),
|
"Y".equalsIgnoreCase(rs.getString("NULLABLE")),
|
||||||
null,
|
defaultValue,
|
||||||
rs.getInt("IS_PK") == 1,
|
rs.getInt("IS_PK") == 1,
|
||||||
null,
|
null,
|
||||||
rs.getString("COMMENTS"),
|
rs.getString("COMMENTS"),
|
||||||
@@ -326,6 +330,25 @@ public final class OceanBaseOracleAgent extends ConfiguredJdbcAgent {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getTableDdl(String schema, String table) {
|
||||||
|
List<IndexInfo> indexes;
|
||||||
|
try {
|
||||||
|
indexes = listIndexes(schema, table);
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
indexes = Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
List<ForeignKeyInfo> foreignKeys;
|
||||||
|
try {
|
||||||
|
foreignKeys = listForeignKeys(schema, table);
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
foreignKeys = Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
return DdlBuilder.buildTableDdl(schema, table, getColumns(schema, table), indexes, foreignKeys, false, true);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<IndexInfo> listIndexes(String schema, String table) {
|
public List<IndexInfo> listIndexes(String schema, String table) {
|
||||||
return unchecked(() -> {
|
return unchecked(() -> {
|
||||||
|
|||||||
+83
@@ -1,5 +1,6 @@
|
|||||||
package com.dbx.agent.oceanbaseoracle;
|
package com.dbx.agent.oceanbaseoracle;
|
||||||
|
|
||||||
|
import com.dbx.agent.ColumnInfo;
|
||||||
import com.dbx.agent.ConnectParams;
|
import com.dbx.agent.ConnectParams;
|
||||||
import com.dbx.agent.MetadataListConstraints;
|
import com.dbx.agent.MetadataListConstraints;
|
||||||
import com.dbx.agent.ObjectInfo;
|
import com.dbx.agent.ObjectInfo;
|
||||||
@@ -116,6 +117,77 @@ class OceanBaseOracleAgentTest {
|
|||||||
Assertions.assertTrue(sql.get(0).contains("ROWNUM <= ?"), sql.get(0));
|
Assertions.assertTrue(sql.get(0).contains("ROWNUM <= ?"), sql.get(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getColumnsIncludesDefaultAndCommentMetadata() {
|
||||||
|
List<String> sql = new ArrayList<>();
|
||||||
|
OceanBaseOracleAgent agent = new OceanBaseOracleAgent();
|
||||||
|
TestSupport.setPrivateConnection(agent, preparedConnection(sql, columnResultSet(
|
||||||
|
new Object[][]{
|
||||||
|
{"DISPLAY_NAME", "VARCHAR2", "Y", null, null, 64, 64, "'anonymous'", "User's display name", 0}
|
||||||
|
}
|
||||||
|
)));
|
||||||
|
|
||||||
|
List<ColumnInfo> columns = agent.getColumns("APP", "USERS");
|
||||||
|
|
||||||
|
Assertions.assertEquals(1, columns.size());
|
||||||
|
ColumnInfo column = columns.get(0);
|
||||||
|
Assertions.assertEquals("DISPLAY_NAME", column.getName());
|
||||||
|
Assertions.assertEquals("VARCHAR2(64)", column.getData_type());
|
||||||
|
Assertions.assertTrue(column.getIs_nullable());
|
||||||
|
Assertions.assertEquals("'anonymous'", column.getColumn_default());
|
||||||
|
Assertions.assertFalse(column.getIs_primary_key());
|
||||||
|
Assertions.assertEquals("User's display name", column.getComment());
|
||||||
|
Assertions.assertEquals(64, column.getCharacter_maximum_length());
|
||||||
|
Assertions.assertTrue(sql.get(0).contains("c.DATA_DEFAULT"), sql.get(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void tableDdlIncludesDefaultsAndOnlyNonBlankColumnComments() {
|
||||||
|
OceanBaseOracleAgent agent = new OceanBaseOracleAgent();
|
||||||
|
TestSupport.setPrivateConnection(agent, preparedConnection(new ArrayList<>(),
|
||||||
|
resultSet(
|
||||||
|
new String[]{"INDEX_NAME", "COLUMN_NAME", "COLUMN_POSITION", "UNIQUENESS", "CONSTRAINT_TYPE", "INDEX_TYPE"},
|
||||||
|
new Object[][]{}
|
||||||
|
),
|
||||||
|
resultSet(
|
||||||
|
new String[]{"CONSTRAINT_NAME", "COLUMN_NAME", "TABLE_NAME", "REF_COLUMN_NAME"},
|
||||||
|
new Object[][]{}
|
||||||
|
),
|
||||||
|
columnResultSet(new Object[][]{
|
||||||
|
{"CREATED_AT", "TIMESTAMP", "N", null, null, null, null, "SYSDATE", "Created timestamp", 0},
|
||||||
|
{"INTERNAL_NOTE", "VARCHAR2", "Y", null, null, 100, 100, null, " ", 0}
|
||||||
|
})
|
||||||
|
));
|
||||||
|
|
||||||
|
String ddl = agent.getTableDdl("APP", "AUDIT_LOG");
|
||||||
|
|
||||||
|
Assertions.assertTrue(ddl.contains("\"CREATED_AT\" TIMESTAMP NOT NULL DEFAULT SYSDATE"), ddl);
|
||||||
|
Assertions.assertTrue(
|
||||||
|
ddl.contains("COMMENT ON COLUMN \"APP\".\"AUDIT_LOG\".\"CREATED_AT\" IS 'Created timestamp';"),
|
||||||
|
ddl
|
||||||
|
);
|
||||||
|
Assertions.assertTrue(ddl.contains("\"INTERNAL_NOTE\" VARCHAR2(100)"), ddl);
|
||||||
|
Assertions.assertFalse(ddl.contains("\"INTERNAL_NOTE\" IS"), ddl);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ResultSet columnResultSet(Object[][] rows) {
|
||||||
|
return resultSet(
|
||||||
|
new String[]{
|
||||||
|
"COLUMN_NAME",
|
||||||
|
"DATA_TYPE",
|
||||||
|
"NULLABLE",
|
||||||
|
"DATA_PRECISION",
|
||||||
|
"DATA_SCALE",
|
||||||
|
"DATA_LENGTH",
|
||||||
|
"CHAR_LENGTH",
|
||||||
|
"DATA_DEFAULT",
|
||||||
|
"COMMENTS",
|
||||||
|
"IS_PK"
|
||||||
|
},
|
||||||
|
rows
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
private static Connection preparedConnection(List<String> sql, ResultSet... resultSets) {
|
private static Connection preparedConnection(List<String> sql, ResultSet... resultSets) {
|
||||||
int[] resultSetIndex = {0};
|
int[] resultSetIndex = {0};
|
||||||
PreparedStatement statement = proxy(PreparedStatement.class, (method, args) -> {
|
PreparedStatement statement = proxy(PreparedStatement.class, (method, args) -> {
|
||||||
@@ -151,6 +223,17 @@ class OceanBaseOracleAgentTest {
|
|||||||
case "getString":
|
case "getString":
|
||||||
Object value = columnValue(columns, rows[index[0]], args[0]);
|
Object value = columnValue(columns, rows[index[0]], args[0]);
|
||||||
return value == null ? null : String.valueOf(value);
|
return value == null ? null : String.valueOf(value);
|
||||||
|
case "getObject":
|
||||||
|
return columnValue(columns, rows[index[0]], args[0]);
|
||||||
|
case "getInt":
|
||||||
|
Object intValue = columnValue(columns, rows[index[0]], args[0]);
|
||||||
|
if (intValue instanceof Number) {
|
||||||
|
return ((Number) intValue).intValue();
|
||||||
|
}
|
||||||
|
if (intValue == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return Integer.parseInt(String.valueOf(intValue));
|
||||||
case "close":
|
case "close":
|
||||||
return null;
|
return null;
|
||||||
default:
|
default:
|
||||||
|
|||||||
Reference in new issue
Block a user