Skip to content

Commit e37c105

Browse files
authored
Merge pull request #2138 from gfunc/main
change connection schema after USE is executed
2 parents 275c754 + dbcfedf commit e37c105

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

jdbc-v2/src/main/java/com/clickhouse/jdbc/StatementImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,13 @@ public boolean execute(String sql, QuerySettings settings) throws SQLException {
367367
}
368368
}
369369
return false;
370+
} else if (type == StatementType.USE) {
371+
executeUpdate(sql, settings);
372+
//USE Database
373+
List<String> tokens = JdbcUtils.tokenizeSQL(sql);
374+
this.schema = tokens.get(1).replace("\"", "");
375+
LOG.info("Changed statement schema " + schema);
376+
return false;
370377
} else {
371378
executeUpdate(sql, settings);
372379
return false;

jdbc-v2/src/test/java/com/clickhouse/jdbc/StatementTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,4 +559,17 @@ void testWithClause() throws Exception {
559559
}
560560
assertEquals(count, 100);
561561
}
562+
563+
@Test(groups = { "integration" })
564+
public void testSwitchDatabase() throws Exception {
565+
String createSql = "CREATE TABLE switchDatabaseWithUse (id UInt8, words String) ENGINE = MergeTree ORDER BY ()";
566+
try (Connection conn = getJdbcConnection()) {
567+
try (Statement stmt = conn.createStatement()) {
568+
assertEquals(stmt.executeUpdate(createSql), 0);
569+
assertEquals(stmt.executeUpdate("CREATE DATABASE \"newDatabase\" ENGINE=Atomic"), 0);
570+
assertFalse(stmt.execute("USE \"newDatabase\""));
571+
assertEquals(stmt.executeUpdate(createSql), 0);
572+
}
573+
}
574+
}
562575
}

0 commit comments

Comments
 (0)