镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* incorrect algorithm extracted from PEM. [#1274](https://github.com/ClickHouse/clickhouse-java/issues/1274)
* transaction failure introduced 0.4.0.
* respect node-specific credentials. [#1114](https://github.com/ClickHouse/clickhouse-java/issues/1114)
* USE statement does nothing. [#1160](https://github.com/ClickHouse/clickhouse-java/issues/1160)

## 0.4.1, 2023-02-19
### Breaking Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ default PreparedStatement prepareStatement(String sql, int resultSetType, int re
*/
String getCurrentDatabase();

/**
* Sets current database.
*
* @param database non-empty database name
* @throws SQLException when failed to change current database
*/
void setCurrentDatabase(String database) throws SQLException;

/**
* Gets current user.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,17 +261,6 @@ protected List<ClickHouseColumn> getTableColumns(String dbName, String tableName
return list;
}

protected void setDatabase(String db) throws SQLException {
ensureOpen();

if (db == null || db.isEmpty()) {
throw new SQLException("Non-empty database name is required", SqlExceptionUtils.SQL_STATE_INVALID_SCHEMA);
} else if (!db.equals(database)) {
this.database = db;
clientRequest.use(db);
}
}

protected String getDatabase() throws SQLException {
ensureOpen();

Expand Down Expand Up @@ -570,7 +559,7 @@ public boolean isReadOnly() throws SQLException {
@Override
public void setCatalog(String catalog) throws SQLException {
if (jdbcConf.useCatalog()) {
setDatabase(catalog);
setCurrentDatabase(catalog);
} else {
log.warn(
"setCatalog method is no-op. Please either change databaseTerm to catalog or use setSchema method instead");
Expand Down Expand Up @@ -964,7 +953,7 @@ public Properties getClientInfo() throws SQLException {
@Override
public void setSchema(String schema) throws SQLException {
if (jdbcConf.useSchema()) {
setDatabase(schema);
setCurrentDatabase(schema);
} else {
log.warn(
"setSchema method is no-op. Please either change databaseTerm to schema or use setCatalog method instead");
Expand Down Expand Up @@ -1033,6 +1022,19 @@ public String getCurrentDatabase() {
return database;
}

@Override
public void setCurrentDatabase(String db) throws SQLException {
ensureOpen();

if (db == null || db.isEmpty()) {
throw new SQLException("Non-empty database name is required", SqlExceptionUtils.SQL_STATE_INVALID_SCHEMA);
} else if (!db.equals(database)) {
this.database = db;
// TODO execute a simple query to validate if the database exists or not
clientRequest.use(db);
}
}

@Override
public String getCurrentUser() {
return user;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ protected void ensureOpen() throws SQLException {
}

protected ClickHouseResponse processSqlStatement(ClickHouseSqlStatement stmt) throws SQLException {
if (stmt.isTCL()) {
if (stmt.getStatementType() == StatementType.USE) {
connection.setCurrentDatabase(stmt.getDatabaseOrDefault(connection.getCurrentDatabase()));
return ClickHouseResponse.EMPTY;
} else if (stmt.isTCL()) {
if (stmt.containsKeyword(ClickHouseTransaction.COMMAND_BEGIN)) {
connection.begin();
} else if (stmt.containsKeyword(ClickHouseTransaction.COMMAND_COMMIT)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,20 @@ public void testSwitchCatalog() throws SQLException {
Assert.assertTrue(rs.next());
Assert.assertEquals(rs.getString(1), dbName);
Assert.assertFalse(rs.next());

conn.createStatement().execute("use system");
Assert.assertEquals(conn.getCurrentDatabase(), "system");
rs = conn.createStatement().executeQuery("select currentDatabase()");
Assert.assertTrue(rs.next());
Assert.assertEquals(rs.getString(1), "system");
Assert.assertFalse(rs.next());

conn.createStatement().execute("use `" + dbName + "`");
Assert.assertEquals(conn.getCurrentDatabase(), dbName);
rs = conn.createStatement().executeQuery("select currentDatabase()");
Assert.assertTrue(rs.next());
Assert.assertEquals(rs.getString(1), dbName);
Assert.assertFalse(rs.next());
}
}

Expand Down Expand Up @@ -243,6 +257,20 @@ public void testSwitchSchema() throws SQLException {
Assert.assertTrue(rs.next());
Assert.assertEquals(rs.getString(1), dbName);
Assert.assertFalse(rs.next());

conn.createStatement().execute("use system");
Assert.assertEquals(conn.getCurrentDatabase(), "system");
rs = conn.createStatement().executeQuery("select currentDatabase()");
Assert.assertTrue(rs.next());
Assert.assertEquals(rs.getString(1), "system");
Assert.assertFalse(rs.next());

conn.createStatement().execute("use `" + dbName + "`");
Assert.assertEquals(conn.getCurrentDatabase(), dbName);
rs = conn.createStatement().executeQuery("select currentDatabase()");
Assert.assertTrue(rs.next());
Assert.assertEquals(rs.getString(1), dbName);
Assert.assertFalse(rs.next());
}
}

Expand Down