Skip to content

Commit 8ddaa36

Browse files
committed
change connection schema after USE is executed
1 parent d4336a6 commit 8ddaa36

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
@@ -366,6 +366,13 @@ public boolean execute(String sql, QuerySettings settings) throws SQLException {
366366
}
367367
}
368368
return false;
369+
} else if (type == StatementType.USE) {
370+
executeUpdate(sql, settings);
371+
//USE Database
372+
List<String> tokens = JdbcUtils.tokenizeSQL(sql);
373+
this.schema = tokens.get(1).replace("\"", "");;
374+
LOG.info("Changed statement schema " + schema);
375+
return false;
369376
} else {
370377
executeUpdate(sql, settings);
371378
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
@@ -539,4 +539,17 @@ public void testTextFormatInResponse() throws Exception {
539539
Assert.expectThrows(SQLException.class, () ->stmt.executeQuery("SELECT 1 FORMAT JSON"));
540540
}
541541
}
542+
543+
@Test(groups = { "integration" })
544+
public void testSwitchDatabase() throws Exception {
545+
String createSql = "CREATE TABLE strings (id UInt8, words String) ENGINE = MergeTree ORDER BY ()";
546+
try (Connection conn = getJdbcConnection()) {
547+
try (Statement stmt = conn.createStatement()) {
548+
assertEquals(stmt.executeUpdate(createSql), 0);
549+
assertEquals(stmt.executeUpdate("CREATE DATABASE \"test\" ENGINE=Atomic"), 0);
550+
assertFalse(stmt.execute("USE \"test\""));
551+
assertEquals(stmt.executeUpdate(createSql), 0);
552+
}
553+
}
554+
}
542555
}

0 commit comments

Comments
 (0)