Skip to content

Commit 50bfa00

Browse files
authored
Merge pull request #2154 from Am-phi/feat/v2_add_execute_with_query_param
feat(ClientV2): Add execute(String, Map<String,Object>)
2 parents 8b6226a + 290f1d2 commit 50bfa00

File tree

2 files changed

+72
-6
lines changed

2 files changed

+72
-6
lines changed

client-v2/src/main/java/com/clickhouse/client/api/Client.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,6 +1916,45 @@ public CompletableFuture<CommandResponse> execute(String sql, CommandSettings se
19161916
});
19171917
}
19181918

1919+
/**
1920+
* <p>Executes a SQL command and doesn't care response. Useful for DDL statements, like `CREATE`, `DROP`, `ALTER`.
1921+
* Method however returns execution errors from a server or summary in case of successful execution. </p>
1922+
*
1923+
* @param sql - SQL command
1924+
* @param params - query parameters
1925+
* @return {@code CompletableFuture<CommandResponse>} - a promise to command response
1926+
*/
1927+
public CompletableFuture<CommandResponse> execute(String sql, Map<String, Object> params){
1928+
return query(sql, params)
1929+
.thenApplyAsync(response -> {
1930+
try {
1931+
return new CommandResponse(response);
1932+
} catch (Exception e) {
1933+
throw new ClientException("Failed to get command response", e);
1934+
}
1935+
});
1936+
}
1937+
1938+
/**
1939+
* <p>Executes a SQL command and doesn't care response. Useful for DDL statements, like `CREATE`, `DROP`, `ALTER`.
1940+
* Method however returns execution errors from a server or summary in case of successful execution. </p>
1941+
*
1942+
* @param sql - SQL command
1943+
* @param params - query parameters
1944+
* @param settings - execution settings
1945+
* @return {@code CompletableFuture<CommandResponse>} - a promise to command response
1946+
*/
1947+
public CompletableFuture<CommandResponse> execute(String sql, Map<String, Object> params, CommandSettings settings){
1948+
return query(sql, params, settings)
1949+
.thenApplyAsync(response -> {
1950+
try {
1951+
return new CommandResponse(response);
1952+
} catch (Exception e) {
1953+
throw new ClientException("Failed to get command response", e);
1954+
}
1955+
});
1956+
}
1957+
19191958
/**
19201959
* <p>Executes a SQL command and doesn't care response. Useful for DDL statements, like `CREATE`, `DROP`, `ALTER`.
19211960
* Method however returns execution errors from a server or summary in case of successful execution. </p>

client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,7 @@
7474
import java.util.Random;
7575
import java.util.Set;
7676
import java.util.UUID;
77-
import java.util.concurrent.CountDownLatch;
78-
import java.util.concurrent.ExecutionException;
79-
import java.util.concurrent.ExecutorService;
80-
import java.util.concurrent.Executors;
81-
import java.util.concurrent.Future;
82-
import java.util.concurrent.TimeUnit;
77+
import java.util.concurrent.*;
8378
import java.util.concurrent.atomic.AtomicInteger;
8479
import java.util.function.Consumer;
8580
import java.util.function.Function;
@@ -1562,6 +1557,38 @@ public void testQueryParams() throws Exception {
15621557
Assert.assertEquals(allRecords.size(), 2);
15631558
}
15641559

1560+
@Test(groups = {"integration"})
1561+
public void testExecuteQueryParam() throws ExecutionException, InterruptedException, TimeoutException {
1562+
1563+
final String table = "execute_query_test";
1564+
Map<String, Object> query_param = new HashMap<>();
1565+
query_param.put("table_name",table);
1566+
query_param.put("engine","MergeTree");
1567+
client.execute("DROP TABLE IF EXISTS " + table).get(10, TimeUnit.SECONDS);
1568+
client.execute("CREATE TABLE {table_name:Identifier} ( id UInt32, name String, created_at DateTime) ENGINE = MergeTree ORDER BY tuple()", query_param)
1569+
.get(10, TimeUnit.SECONDS);
1570+
1571+
TableSchema schema = client.getTableSchema(table);
1572+
Assert.assertNotNull(schema);
1573+
}
1574+
1575+
@Test(groups = {"integration"})
1576+
public void testExecuteQueryParamCommandSettings() throws ExecutionException, InterruptedException, TimeoutException {
1577+
1578+
final String table = "execute_query_test";
1579+
String q1Id = UUID.randomUUID().toString();
1580+
Map<String, Object> query_param = new HashMap<>();
1581+
query_param.put("table_name",table);
1582+
query_param.put("engine","MergeTree");
1583+
client.execute("DROP TABLE IF EXISTS " + table).get(10, TimeUnit.SECONDS);
1584+
client.execute("CREATE TABLE {table_name:Identifier} ( id UInt32, name String, created_at DateTime) ENGINE = MergeTree ORDER BY tuple()",
1585+
query_param, (CommandSettings) new CommandSettings().setQueryId(q1Id))
1586+
.get(10, TimeUnit.SECONDS);
1587+
1588+
TableSchema schema = client.getTableSchema(table);
1589+
Assert.assertNotNull(schema);
1590+
}
1591+
15651592
@Test(groups = {"integration"})
15661593
public void testGetTableSchema() throws Exception {
15671594

0 commit comments

Comments
 (0)