fix(kingbase): restore bounded string lengths
This commit is contained in:
2 files changed
+55
-4
No files matched your search
@@ -29,6 +29,8 @@ import java.util.List;
|
|||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public final class KingbaseAgent extends PostgresLikeAgent {
|
public final class KingbaseAgent extends PostgresLikeAgent {
|
||||||
private static final int TRIGGER_TYPE_BEFORE = 1 << 1;
|
private static final int TRIGGER_TYPE_BEFORE = 1 << 1;
|
||||||
@@ -48,6 +50,10 @@ public final class KingbaseAgent extends PostgresLikeAgent {
|
|||||||
private static final String KINGBASE_VIEW_SCHEMA = "CAST(v.schemaname AS varchar(256))";
|
private static final String KINGBASE_VIEW_SCHEMA = "CAST(v.schemaname AS varchar(256))";
|
||||||
private static final String KINGBASE_MATVIEW_NAME = "CAST(mv.matviewname AS varchar(256))";
|
private static final String KINGBASE_MATVIEW_NAME = "CAST(mv.matviewname AS varchar(256))";
|
||||||
private static final String KINGBASE_MATVIEW_SCHEMA = "CAST(mv.schemaname AS varchar(256))";
|
private static final String KINGBASE_MATVIEW_SCHEMA = "CAST(mv.schemaname AS varchar(256))";
|
||||||
|
private static final Pattern BOUNDED_VARCHAR_TYPE = Pattern.compile(
|
||||||
|
"^(?:varchar|character\\s+varying)\\s*\\(\\s*(\\d+)\\s*\\)$",
|
||||||
|
Pattern.CASE_INSENSITIVE
|
||||||
|
);
|
||||||
private boolean postgresCatalogMode;
|
private boolean postgresCatalogMode;
|
||||||
private boolean sqlServerIdentityCatalogMode;
|
private boolean sqlServerIdentityCatalogMode;
|
||||||
|
|
||||||
@@ -572,6 +578,7 @@ public final class KingbaseAgent extends PostgresLikeAgent {
|
|||||||
List<ColumnInfo> result = new ArrayList<>();
|
List<ColumnInfo> result = new ArrayList<>();
|
||||||
String sql = "SELECT ic.column_name, ic.data_type, ic.is_nullable, ic.column_default, " +
|
String sql = "SELECT ic.column_name, ic.data_type, ic.is_nullable, ic.column_default, " +
|
||||||
"ic.numeric_precision, ic.numeric_scale, ic.character_maximum_length, " +
|
"ic.numeric_precision, ic.numeric_scale, ic.character_maximum_length, " +
|
||||||
|
"format_type(a.atttypid, a.atttypmod) AS catalog_data_type, " +
|
||||||
"d.description AS column_comment " +
|
"d.description AS column_comment " +
|
||||||
"FROM information_schema.columns ic " +
|
"FROM information_schema.columns ic " +
|
||||||
// information_schema preserves MySQL-compatible type metadata but does not expose comments.
|
// information_schema preserves MySQL-compatible type metadata but does not expose comments.
|
||||||
@@ -587,9 +594,19 @@ public final class KingbaseAgent extends PostgresLikeAgent {
|
|||||||
try (ResultSet rs = stmt.executeQuery(sql)) {
|
try (ResultSet rs = stmt.executeQuery(sql)) {
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
String columnName = rs.getString("column_name");
|
String columnName = rs.getString("column_name");
|
||||||
|
String dataType = rs.getString("data_type");
|
||||||
|
Integer characterLength = intObject(rs, "character_maximum_length");
|
||||||
|
String catalogDataType = rs.getString("catalog_data_type");
|
||||||
|
Integer catalogCharacterLength = boundedCharacterLength(catalogDataType);
|
||||||
|
if ("varchar".equalsIgnoreCase(dataType)
|
||||||
|
&& (characterLength == null || characterLength <= 0)
|
||||||
|
&& catalogCharacterLength != null) {
|
||||||
|
dataType = catalogDataType;
|
||||||
|
characterLength = catalogCharacterLength;
|
||||||
|
}
|
||||||
result.add(new ColumnInfo(
|
result.add(new ColumnInfo(
|
||||||
columnName,
|
columnName,
|
||||||
rs.getString("data_type"),
|
dataType,
|
||||||
"YES".equalsIgnoreCase(coalesce(rs.getString("is_nullable"))),
|
"YES".equalsIgnoreCase(coalesce(rs.getString("is_nullable"))),
|
||||||
rs.getString("column_default"),
|
rs.getString("column_default"),
|
||||||
primaryKeys.contains(columnName),
|
primaryKeys.contains(columnName),
|
||||||
@@ -597,7 +614,7 @@ public final class KingbaseAgent extends PostgresLikeAgent {
|
|||||||
rs.getString("column_comment"),
|
rs.getString("column_comment"),
|
||||||
intObject(rs, "numeric_precision"),
|
intObject(rs, "numeric_precision"),
|
||||||
intObject(rs, "numeric_scale"),
|
intObject(rs, "numeric_scale"),
|
||||||
intObject(rs, "character_maximum_length")
|
characterLength
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -606,6 +623,18 @@ public final class KingbaseAgent extends PostgresLikeAgent {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Integer boundedCharacterLength(String dataType) {
|
||||||
|
if (dataType == null) return null;
|
||||||
|
Matcher match = BOUNDED_VARCHAR_TYPE.matcher(dataType.trim());
|
||||||
|
if (!match.matches()) return null;
|
||||||
|
try {
|
||||||
|
int length = Integer.parseInt(match.group(1));
|
||||||
|
return length > 0 ? length : null;
|
||||||
|
} catch (NumberFormatException ignored) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<IndexInfo> listIndexes(String schema, String table) {
|
public List<IndexInfo> listIndexes(String schema, String table) {
|
||||||
if (postgresCatalogMode) return super.listIndexes(schema, table);
|
if (postgresCatalogMode) return super.listIndexes(schema, table);
|
||||||
|
|||||||
+24
-2
@@ -556,7 +556,7 @@ class KingbaseAgentTest extends JdbcFakeExecutionBehaviorTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void mysqlCompatGetColumnsKeepsInformationSchemaPath() {
|
void mysqlCompatGetColumnsRestoresBoundedCatalogCharacterTypes() {
|
||||||
List<String> sql = new ArrayList<>();
|
List<String> sql = new ArrayList<>();
|
||||||
KingbaseAgent agent = new KingbaseAgent();
|
KingbaseAgent agent = new KingbaseAgent();
|
||||||
agent.setMysqlCompatMode(true);
|
agent.setMysqlCompatMode(true);
|
||||||
@@ -574,9 +574,14 @@ class KingbaseAgentTest extends JdbcFakeExecutionBehaviorTest {
|
|||||||
"numeric_precision",
|
"numeric_precision",
|
||||||
"numeric_scale",
|
"numeric_scale",
|
||||||
"character_maximum_length",
|
"character_maximum_length",
|
||||||
|
"catalog_data_type",
|
||||||
"column_comment"
|
"column_comment"
|
||||||
},
|
},
|
||||||
new Object[][]{{"id", "int", "NO", null, 32, 0, null, "identifier"}}
|
new Object[][]{
|
||||||
|
{"id", "int", "NO", null, 32, 0, null, "integer", "identifier"},
|
||||||
|
{"name", "varchar", "YES", null, null, null, -1, "character varying(64)", null},
|
||||||
|
{"notes", "varchar", "YES", null, null, null, -1, "varchar", null}
|
||||||
|
}
|
||||||
)
|
)
|
||||||
));
|
));
|
||||||
|
|
||||||
@@ -584,9 +589,26 @@ class KingbaseAgentTest extends JdbcFakeExecutionBehaviorTest {
|
|||||||
|
|
||||||
Assertions.assertEquals("int", columns.get(0).getData_type());
|
Assertions.assertEquals("int", columns.get(0).getData_type());
|
||||||
Assertions.assertEquals("identifier", columns.get(0).getComment());
|
Assertions.assertEquals("identifier", columns.get(0).getComment());
|
||||||
|
Assertions.assertEquals("character varying(64)", columns.get(1).getData_type());
|
||||||
|
Assertions.assertEquals(Integer.valueOf(64), columns.get(1).getCharacter_maximum_length());
|
||||||
|
Assertions.assertEquals("varchar", columns.get(2).getData_type());
|
||||||
|
Assertions.assertEquals(Integer.valueOf(-1), columns.get(2).getCharacter_maximum_length());
|
||||||
Assertions.assertTrue(sql.get(1).contains("FROM information_schema.columns"), sql.get(1));
|
Assertions.assertTrue(sql.get(1).contains("FROM information_schema.columns"), sql.get(1));
|
||||||
|
Assertions.assertTrue(sql.get(1).contains("format_type(a.atttypid, a.atttypmod) AS catalog_data_type"), sql.get(1));
|
||||||
Assertions.assertTrue(sql.get(1).contains("LEFT JOIN sys_catalog.sys_description"), sql.get(1));
|
Assertions.assertTrue(sql.get(1).contains("LEFT JOIN sys_catalog.sys_description"), sql.get(1));
|
||||||
Assertions.assertNull(columns.get(0).getExtra());
|
Assertions.assertNull(columns.get(0).getExtra());
|
||||||
|
|
||||||
|
String ddl = DdlBuilder.buildTableDdl(
|
||||||
|
"PUBLIC",
|
||||||
|
"orders",
|
||||||
|
columns,
|
||||||
|
Collections.emptyList(),
|
||||||
|
Collections.emptyList(),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
Assertions.assertTrue(ddl.contains("`name` character varying(64)"), ddl);
|
||||||
|
Assertions.assertTrue(ddl.contains("`notes` varchar"), ddl);
|
||||||
|
Assertions.assertFalse(ddl.contains("varchar(-1)"), ddl);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in new issue
Block a user