Skip to content

Commit 94f0381

Browse files
author
Paultagoras
committed
Adjusting based on the BasicExample
1 parent e00ef9d commit 94f0381

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

examples/jdbc/src/main/java/com/clickhouse/examples/jdbc/Basic.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,15 @@ public static void main(String[] args) {
234234
// jdbc:ch:https://[email protected]:443
235235
// jdbc:ch:https://demo:[email protected]
236236
String url = System.getProperty("chUrl", "jdbc:ch://localhost");
237+
System.setProperty("clickhouse.jdbc.v2", "true");
237238

238239
try {
239240
usedPooledConnection(url);
240241
} catch (SQLException e) {
241242
e.printStackTrace();
242243
}
243244

245+
System.out.println(url);
244246
try (Connection conn = getConnection(url)) {
245247
connectWithCustomSettings(url);
246248
insertByteArray(conn);

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,26 @@ public void setCursorName(String name) throws SQLException {
210210
@Override
211211
public boolean execute(String sql) throws SQLException {
212212
checkClosed();
213-
StatementType type = parseStatementType(sql);
214-
215-
if (type == StatementType.SELECT) {
216-
executeQuery(sql);
217-
return true;
218-
} else {
219-
executeUpdate(sql);
220-
return false;
213+
List<String> statements = List.of(sql.split(";"));
214+
boolean firstIsResult = false;
215+
216+
int index = 0;
217+
for (String statement : statements) {
218+
StatementType type = parseStatementType(statement);
219+
220+
if (type == StatementType.SELECT) {
221+
executeQuery(statement);
222+
if (index == 0) {
223+
firstIsResult = true;
224+
}
225+
} else {
226+
executeUpdate(statement);
227+
}
228+
229+
index++;
221230
}
231+
232+
return firstIsResult;
222233
}
223234

224235
@Override

jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcConfiguration.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.stream.Collectors;
1414

1515
public class JdbcConfiguration {
16+
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(JdbcConfiguration.class);
1617
public static final String PREFIX_CLICKHOUSE = "jdbc:clickhouse:";
1718
public static final String PREFIX_CLICKHOUSE_SHORT = "jdbc:ch:";
1819

@@ -82,17 +83,26 @@ public DriverPropertyInfo[] getPropertyInfo() {
8283
}
8384

8485
private Map<String, String> parseUrl(String urlString) {
86+
log.debug("Parsing URL: {}", urlString);
8587
URL url;
8688
try {
87-
url = new URL(stripUrlPrefix(urlString));
89+
String urlStripped = stripUrlPrefix(urlString);
90+
int index = urlStripped.indexOf("//");
91+
if (index == 0) {//Add in the HTTP protocol if it is missing
92+
urlStripped = "http:" + urlStripped;
93+
}
94+
95+
url = new URL(urlStripped);
8896
} catch (MalformedURLException e) {
8997
throw new IllegalArgumentException("URL is malformed.");
9098
}
9199

92100
Map<String, String> urlProperties = new HashMap<>();
93101
urlProperties.put("host", url.getHost());
94-
urlProperties.put("port", String.valueOf(url.getPort() == -1 ? 8443 : url.getPort()));
95102
urlProperties.put("protocol", url.getProtocol());
103+
urlProperties.put("port", String.valueOf(url.getPort() == -1 ?
104+
url.getProtocol().equalsIgnoreCase("HTTP") ? 8123 : 8443
105+
: url.getPort()));
96106

97107
try {
98108
urlProperties.put("database", url.getPath().substring(1));

0 commit comments

Comments
 (0)