Skip to content

Commit 48a0b09

Browse files
committed
Merge branch 'main' into v2_support_dynamic_and_json
2 parents de27d86 + e37c105 commit 48a0b09

File tree

6 files changed

+63
-30
lines changed

6 files changed

+63
-30
lines changed

clickhouse-r2dbc/src/main/java/com/clickhouse/r2dbc/ClickHouseStatement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,6 @@ public Flux<? extends Result> execute() {
156156

157157
@Override
158158
public Statement returnGeneratedValues(String... columns) {
159-
throw new UnsupportedOperationException(GENERATED_VALUES_CAN_NOT_BE_RETURNED_FROM_CLICKHOUSE_DATABASE);
159+
return this;
160160
}
161161
}

clickhouse-r2dbc/src/test/java/com/clickhouse/r2dbc/spi/test/R2DBCTestKitImplTest.java

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -207,40 +207,16 @@ public void duplicateColumnNames() {
207207

208208
@Override
209209
@Test
210+
@Disabled
210211
public void returnGeneratedValues() {
211-
getJdbcOperations().execute(expand(TestStatement.DROP_TABLE));
212-
getJdbcOperations().execute(getCreateTableWithAutogeneratedKey());
213-
Flux.usingWhen(getConnectionFactory().create(),
214-
connection -> {
215-
Statement statement = connection.createStatement(getInsertIntoWithAutogeneratedKey());
216-
217-
statement.returnGeneratedValues();
218-
219-
return Flux.from(statement
220-
.execute())
221-
.flatMap(it -> it.map((row, rowMetadata) -> row.get(0)));
222-
},
223-
Connection::close)
224-
.as(StepVerifier::create)
225-
.expectErrorMatches(UnsupportedOperationException.class::isInstance)
226-
.verify();
212+
// not supported
227213
}
228214

229215
@Override
230216
@Test
217+
@Disabled
231218
public void returnGeneratedValuesFails() {
232-
233-
Flux.usingWhen(getConnectionFactory().create(),
234-
connection -> {
235-
Statement statement = connection.createStatement(expand(TestStatement.INSERT_VALUE100));
236-
237-
assertThrows(UnsupportedOperationException.class,
238-
() -> statement.returnGeneratedValues((String[]) null));
239-
return Mono.empty();
240-
},
241-
Connection::close)
242-
.as(StepVerifier::create)
243-
.verifyComplete();
219+
// not supported
244220
}
245221

246222
@Override

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ protected static StatementType parseStatementType(String sql) {
8484

8585
switch (tokens[0].toUpperCase()) {
8686
case "SELECT": return StatementType.SELECT;
87+
case "WITH": return StatementType.SELECT;
8788
case "INSERT": return StatementType.INSERT;
8889
case "DELETE": return StatementType.DELETE;
8990
case "UPDATE": return StatementType.UPDATE;
@@ -366,6 +367,13 @@ public boolean execute(String sql, QuerySettings settings) throws SQLException {
366367
}
367368
}
368369
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;
369377
} else {
370378
executeUpdate(sql, settings);
371379
return false;

jdbc-v2/src/main/java/com/clickhouse/jdbc/metadata/ResultSetMetaData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,6 @@ public boolean isDefinitelyWritable(int column) throws SQLException {
165165

166166
@Override
167167
public String getColumnClassName(int column) throws SQLException {
168-
throw new SQLException("Not implemented", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
168+
return null;
169169
}
170170
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,20 @@ public void testEscapeStrings() throws Exception {
260260
}
261261
}
262262
}
263+
264+
265+
@Test(groups = "integration")
266+
void testWithClause() throws Exception {
267+
int count = 0;
268+
try (Connection conn = getJdbcConnection()) {
269+
try (PreparedStatement stmt = conn.prepareStatement("with data as (SELECT number FROM numbers(100)) select * from data ")) {
270+
stmt.execute();
271+
ResultSet rs = stmt.getResultSet();
272+
while (rs.next()) {
273+
count++;
274+
}
275+
}
276+
}
277+
assertEquals(count, 100);
278+
}
263279
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,11 @@ public void testWithComments() throws Exception {
464464
assertEquals(StatementImpl.parseStatementType(" "), StatementImpl.StatementType.OTHER);
465465
}
466466

467+
@Test(groups = { "integration" })
468+
public void testParseStatementWithClause() throws Exception {
469+
assertEquals(StatementImpl.parseStatementType("with data as (SELECT number FROM numbers(100)) select * from data"), StatementImpl.StatementType.SELECT);
470+
}
471+
467472

468473
@Test(groups = { "integration" })
469474
public void testWithIPs() throws Exception {
@@ -539,4 +544,32 @@ public void testTextFormatInResponse() throws Exception {
539544
Assert.expectThrows(SQLException.class, () ->stmt.executeQuery("SELECT 1 FORMAT JSON"));
540545
}
541546
}
547+
548+
@Test(groups = "integration")
549+
void testWithClause() throws Exception {
550+
int count = 0;
551+
try (Connection conn = getJdbcConnection()) {
552+
try (Statement stmt = conn.createStatement()) {
553+
stmt.execute("with data as (SELECT number FROM numbers(100)) select * from data");
554+
ResultSet rs = stmt.getResultSet();
555+
while (rs.next()) {
556+
count++;
557+
}
558+
}
559+
}
560+
assertEquals(count, 100);
561+
}
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+
}
542575
}

0 commit comments

Comments
 (0)