fix(kingbase): quote MySQL-compatible identifiers
This commit is contained in:
39 files changed
+373
-23
No files matched your search
@@ -58,12 +58,24 @@ public final class KingbaseAgent extends PostgresLikeAgent {
|
||||
@Override
|
||||
protected void afterConnect(ConnectParams params, Connection connection) {
|
||||
postgresCatalogMode = false;
|
||||
setMysqlCompatMode(params.isMysql_compat_mode());
|
||||
if (params.isMysql_compat_mode()) {
|
||||
setMysqlCompatMode(true);
|
||||
return;
|
||||
}
|
||||
postgresCatalogMode = !catalogExists(connection, "sys_catalog.sys_namespace")
|
||||
&& catalogExists(connection, "pg_catalog.pg_namespace");
|
||||
if (!postgresCatalogMode && mysqlSqlModeExists(connection)) {
|
||||
setMysqlCompatMode(true);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean mysqlSqlModeExists(Connection connection) {
|
||||
try (Statement stmt = connection.createStatement();
|
||||
ResultSet rs = stmt.executeQuery("SELECT 1 FROM sys_settings WHERE LOWER(name) = 'sql_mode'")) {
|
||||
return rs.next();
|
||||
} catch (Exception ignored) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean catalogExists(Connection connection, String catalog) {
|
||||
|
||||
@@ -137,6 +137,22 @@ class KingbaseAgentTest extends JdbcFakeExecutionBehaviorTest {
|
||||
Assertions.assertEquals("SET search_path TO \"app\"", agent.setSchemaSQL("app"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void detectsMysqlCompatModeFromServerSqlMode() throws Exception {
|
||||
List<String> sql = new ArrayList<>();
|
||||
KingbaseAgent agent = new KingbaseAgent();
|
||||
Connection connection = mysqlCompatConnection(sql);
|
||||
|
||||
Method afterConnect = KingbaseAgent.class.getDeclaredMethod("afterConnect", ConnectParams.class, Connection.class);
|
||||
afterConnect.setAccessible(true);
|
||||
afterConnect.invoke(agent, new ConnectParams(), connection);
|
||||
|
||||
Assertions.assertTrue(agent.isMysqlCompatMode());
|
||||
Assertions.assertEquals("`", agent.getIdentifierQuote());
|
||||
Assertions.assertEquals("SELECT 1 FROM sys_catalog.sys_namespace WHERE 1 = 0", sql.get(0));
|
||||
Assertions.assertEquals("SELECT 1 FROM sys_settings WHERE LOWER(name) = 'sql_mode'", sql.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mysqlCompatListTablesUsesInformationSchema() {
|
||||
List<String> sql = new ArrayList<>();
|
||||
@@ -512,6 +528,26 @@ class KingbaseAgentTest extends JdbcFakeExecutionBehaviorTest {
|
||||
});
|
||||
}
|
||||
|
||||
private static Connection mysqlCompatConnection(List<String> sql) {
|
||||
return proxy(Connection.class, (method, args) -> {
|
||||
if ("createStatement".equals(method.getName())) {
|
||||
return proxy(Statement.class, (statementMethod, statementArgs) -> {
|
||||
if ("executeQuery".equals(statementMethod.getName())) {
|
||||
String query = String.valueOf(statementArgs[0]);
|
||||
sql.add(query);
|
||||
if (query.contains("sys_settings")) {
|
||||
return resultSet(new String[]{"probe"}, new Object[][]{{1}});
|
||||
}
|
||||
return resultSet(new String[]{"probe"}, new Object[][]{});
|
||||
}
|
||||
return defaultValue(statementMethod.getReturnType());
|
||||
});
|
||||
}
|
||||
if ("isClosed".equals(method.getName())) return false;
|
||||
return defaultValue(method.getReturnType());
|
||||
});
|
||||
}
|
||||
|
||||
private static ResultSet resultSet(String[] columns, Object[][] rows) {
|
||||
int[] index = {-1};
|
||||
return proxy(ResultSet.class, (method, args) -> {
|
||||
|
||||
Reference in new issue
Block a user