Skip to content

Commit ab269e0

Browse files
committed
Merge branch 'main' into support_variant_type
2 parents 166ed6e + c0d9634 commit ab269e0

File tree

8 files changed

+182
-161
lines changed

8 files changed

+182
-161
lines changed

clickhouse-data/src/main/java/com/clickhouse/data/ClickHouseFormat.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public enum ClickHouseFormat {
5050
JSONCompactStringsEachRowWithNamesAndTypes(true, true, false, true, true, JSONCompactStringsEachRow), // https://clickhouse.com/docs/en/interfaces/formats/#jsoncompactstringeachrowwithnamesandtypes
5151
JSONCompactStrings(false, true, false, false, false, JSONCompactStringsEachRow), // https://clickhouse.com/docs/en/interfaces/formats/#jsoncompactstrings
5252
JSONEachRow(true, true, false, false, true), // https://clickhouse.com/docs/en/interfaces/formats/#jsoneachrow
53+
PrettyJSONEachRow(false, true, false, false, true), // https://clickhouse.com/docs/en/interfaces/formats/PrettyJSONEachRow
5354
JSONEachRowWithProgress(false, true, false, false, true, JSONEachRow), // https://clickhouse.com/docs/en/interfaces/formats/#jsoneachrowwithprogress
5455
JSONLines(true, true, false, false, true), // alias of JSONEachRow
5556
NDJSON(true, true, true, true, true), // alias of JSONEachRow

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

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,31 +54,37 @@ public class ConnectionImpl implements Connection, JdbcV2Wrapper {
5454
private final com.clickhouse.jdbc.metadata.DatabaseMetaData metadata;
5555

5656
public ConnectionImpl(String url, Properties info) throws SQLException {
57-
log.debug("Creating connection to {}", url);
58-
this.url = url;//Raw URL
59-
this.config = new JdbcConfiguration(url, info);
60-
this.onCluster = false;
61-
this.cluster = null;
62-
String clientName = "ClickHouse JDBC Driver V2/" + Driver.driverVersion;
63-
64-
if (this.config.isDisableFrameworkDetection()) {
65-
log.debug("Framework detection is disabled.");
66-
} else {
67-
String detectedFrameworks = Driver.FrameworksDetection.getFrameworksDetected();
68-
log.debug("Detected frameworks: {}", detectedFrameworks);
69-
clientName += " (" + detectedFrameworks + ")";
70-
}
71-
72-
this.client = this.config.applyClientProperties(new Client.Builder())
73-
.setClientName(clientName)
74-
.build();
75-
this.client.loadServerInfo();
76-
this.schema = client.getDefaultDatabase();
77-
this.defaultQuerySettings = new QuerySettings()
78-
.serverSetting(ServerSettings.ASYNC_INSERT, "0")
79-
.serverSetting(ServerSettings.WAIT_END_OF_QUERY, "0");
57+
try {
58+
log.debug("Creating connection to {}", url);
59+
this.url = url;//Raw URL
60+
this.config = new JdbcConfiguration(url, info);
61+
this.onCluster = false;
62+
this.cluster = null;
63+
String clientName = "ClickHouse JDBC Driver V2/" + Driver.driverVersion;
64+
65+
if (this.config.isDisableFrameworkDetection()) {
66+
log.debug("Framework detection is disabled.");
67+
} else {
68+
String detectedFrameworks = Driver.FrameworksDetection.getFrameworksDetected();
69+
log.debug("Detected frameworks: {}", detectedFrameworks);
70+
clientName += " (" + detectedFrameworks + ")";
71+
}
8072

81-
this.metadata = new com.clickhouse.jdbc.metadata.DatabaseMetaData(this, false, url);
73+
this.client = this.config.applyClientProperties(new Client.Builder())
74+
.setClientName(clientName)
75+
.build();
76+
this.client.loadServerInfo();
77+
this.schema = client.getDefaultDatabase();
78+
this.defaultQuerySettings = new QuerySettings()
79+
.serverSetting(ServerSettings.ASYNC_INSERT, "0")
80+
.serverSetting(ServerSettings.WAIT_END_OF_QUERY, "0");
81+
82+
this.metadata = new com.clickhouse.jdbc.metadata.DatabaseMetaData(this, false, url);
83+
} catch (SQLException e) {
84+
throw e;
85+
} catch (Exception e) {
86+
throw new SQLException("Failed to create connection", ExceptionUtils.SQL_STATE_CONNECTION_EXCEPTION, e);
87+
}
8288
}
8389

8490
public QuerySettings getDefaultQuerySettings() {

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

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,20 @@
2828
import java.sql.Time;
2929
import java.sql.Timestamp;
3030
import java.sql.Types;
31+
import java.time.Instant;
3132
import java.time.LocalDate;
3233
import java.time.LocalDateTime;
3334
import java.time.LocalTime;
3435
import java.time.ZoneId;
36+
import java.time.ZonedDateTime;
3537
import java.time.format.DateTimeFormatter;
3638
import java.time.format.DateTimeFormatterBuilder;
3739
import java.time.temporal.ChronoField;
3840
import java.util.Calendar;
3941
import java.util.Collection;
4042
import java.util.GregorianCalendar;
4143
import java.util.Map;
44+
import java.util.TimeZone;
4245

4346
public class PreparedStatementImpl extends StatementImpl implements PreparedStatement, JdbcV2Wrapper {
4447
private static final Logger LOG = LoggerFactory.getLogger(PreparedStatementImpl.class);
@@ -159,19 +162,16 @@ public void setBytes(int parameterIndex, byte[] x) throws SQLException {
159162

160163
@Override
161164
public void setDate(int parameterIndex, Date x) throws SQLException {
162-
checkClosed();
163165
setDate(parameterIndex, x, null);
164166
}
165167

166168
@Override
167169
public void setTime(int parameterIndex, Time x) throws SQLException {
168-
checkClosed();
169170
setTime(parameterIndex, x, null);
170171
}
171172

172173
@Override
173174
public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException {
174-
checkClosed();
175175
setTimestamp(parameterIndex, x, null);
176176
}
177177

@@ -269,42 +269,42 @@ public ResultSetMetaData getMetaData() throws SQLException {
269269
public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException {
270270
checkClosed();
271271
if (cal == null) {
272-
cal = new GregorianCalendar();
273-
cal.setTime(x);
272+
cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));//This says whatever date is in UTC
274273
}
275274

276-
ZoneId tz = cal.getTimeZone().toZoneId();
275+
LocalDate d = x.toLocalDate();
277276
Calendar c = (Calendar) cal.clone();
278-
c.setTime(x);
279-
parameters[parameterIndex - 1] = encodeObject(c.toInstant().atZone(tz).toLocalDate());
277+
c.clear();
278+
c.set(d.getYear(), d.getMonthValue() - 1, d.getDayOfMonth(), 0, 0, 0);
279+
parameters[parameterIndex - 1] = encodeObject(c.toInstant());
280280
}
281281

282282
@Override
283283
public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException {
284284
checkClosed();
285285
if (cal == null) {
286-
cal = new GregorianCalendar();
287-
cal.setTime(x);
286+
cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
288287
}
289288

290-
ZoneId tz = cal.getTimeZone().toZoneId();
289+
LocalTime t = x.toLocalTime();
291290
Calendar c = (Calendar) cal.clone();
292-
c.setTime(x);
293-
parameters[parameterIndex - 1] = encodeObject(c.toInstant().atZone(tz).toLocalTime());
291+
c.clear();
292+
c.set(1970, Calendar.JANUARY, 1, t.getHour(), t.getMinute(), t.getSecond());
293+
parameters[parameterIndex - 1] = encodeObject(c.toInstant());
294294
}
295295

296296
@Override
297297
public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException {
298298
checkClosed();
299299
if (cal == null) {
300-
cal = new GregorianCalendar();
301-
cal.setTime(x);
300+
cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
302301
}
303302

304-
ZoneId tz = cal.getTimeZone().toZoneId();
303+
LocalDateTime ldt = x.toLocalDateTime();
305304
Calendar c = (Calendar) cal.clone();
306-
c.setTime(x);
307-
parameters[parameterIndex - 1] = encodeObject(c.toInstant().atZone(tz).withNano(x.getNanos()).toLocalDateTime());
305+
c.clear();
306+
c.set(ldt.getYear(), ldt.getMonthValue() - 1, ldt.getDayOfMonth(), ldt.getHour(), ldt.getMinute(), ldt.getSecond());
307+
parameters[parameterIndex - 1] = encodeObject(c.toInstant().atZone(ZoneId.of("UTC")).withNano(x.getNanos()));
308308
}
309309

310310
@Override
@@ -479,6 +479,10 @@ private static String encodeObject(Object x) throws SQLException {
479479
return "'" + DATETIME_FORMATTER.format(((Timestamp) x).toLocalDateTime()) + "'";
480480
} else if (x instanceof LocalDateTime) {
481481
return "'" + DATETIME_FORMATTER.format((LocalDateTime) x) + "'";
482+
} else if (x instanceof ZonedDateTime) {
483+
return encodeObject(((ZonedDateTime) x).toInstant());
484+
} else if (x instanceof Instant) {
485+
return "fromUnixTimestamp64Nano(" + (((Instant) x).getEpochSecond() * 1_000_000_000L + ((Instant) x).getNano())+ ")";
482486
} else if (x instanceof Array) {
483487
StringBuilder listString = new StringBuilder();
484488
listString.append("[");
@@ -571,6 +575,7 @@ private static String encodeObject(Object x) throws SQLException {
571575
}
572576
}
573577

578+
574579
private static String escapeString(String x) {
575580
return x.replace("\\", "\\\\").replace("'", "\\'");//Escape single quotes
576581
}

0 commit comments

Comments
 (0)