Skip to content

Commit 52c6e6b

Browse files
committed
Base implementation of RowBinaryWriter support in JDBC
1 parent b3caed8 commit 52c6e6b

File tree

9 files changed

+786
-33
lines changed

9 files changed

+786
-33
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.clickhouse.client.api.data_formats;
2+
3+
import com.clickhouse.data.ClickHouseFormat;
4+
5+
import java.io.IOException;
6+
import java.io.OutputStream;
7+
import java.math.BigDecimal;
8+
import java.math.BigInteger;
9+
import java.time.LocalDate;
10+
import java.time.LocalDateTime;
11+
import java.time.ZonedDateTime;
12+
import java.util.List;
13+
14+
/**
15+
* Experimental API
16+
*/
17+
public interface ClickHouseBinaryFormatWriter {
18+
19+
/**
20+
* Returns an output stream to which this writer is serializing values.
21+
* Caution: this method is not intended for application usage.
22+
* @return Output stream of the writer
23+
*/
24+
OutputStream getOutputStream();
25+
26+
int getRowCount();
27+
28+
ClickHouseFormat getFormat();
29+
30+
void clearRow();
31+
32+
void setValue(String column, Object value);
33+
34+
void setValue(int colIndex, Object value);
35+
36+
void commitRow() throws IOException;
37+
38+
void setByte(String column, byte value);
39+
40+
void setByte(int colIndex, byte value);
41+
42+
void setShort(String column, short value);
43+
44+
void setShort(int colIndex, short value);
45+
46+
void setInteger(String column, int value);
47+
48+
void setInteger(int colIndex, int value);
49+
50+
void setLong(String column, long value);
51+
52+
void setLong(int colIndex, long value);
53+
54+
void setBigInteger(int colIndex, BigInteger value);
55+
56+
void setBigInteger(String column, BigInteger value);
57+
58+
void setFloat(int colIndex, float value);
59+
60+
void setFloat(String column, float value);
61+
62+
void setDouble(int colIndex, double value);
63+
64+
void setDouble(String column, double value);
65+
66+
void setBigDecimal(int colIndex, BigDecimal value);
67+
68+
void setBigDecimal(String column, BigDecimal value);
69+
70+
void setBoolean(int colIndex, boolean value);
71+
72+
void setBoolean(String column, boolean value);
73+
74+
void setString(String column, String value);
75+
76+
void setString(int colIndex, String value);
77+
78+
void setDate(String column, LocalDate value);
79+
80+
void setDate(int colIndex, LocalDate value);
81+
82+
void setDateTime(String column, LocalDateTime value);
83+
84+
void setDateTime(int colIndex, LocalDateTime value);
85+
86+
void setDateTime(String column, ZonedDateTime value);
87+
88+
void setDateTime(int colIndex, ZonedDateTime value);
89+
90+
void setList(String column, List<?> value);
91+
92+
void setList(int colIndex, List<?> value);
93+
94+
}

client-v2/src/main/java/com/clickhouse/client/api/data_formats/RowBinaryFormatWriter.java

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77

88
import java.io.IOException;
99
import java.io.OutputStream;
10+
import java.math.BigDecimal;
11+
import java.math.BigInteger;
1012
import java.time.LocalDate;
1113
import java.time.LocalDateTime;
1214
import java.time.ZonedDateTime;
15+
import java.util.Arrays;
1316
import java.util.List;
1417

1518

@@ -21,7 +24,7 @@
2124
* <p>
2225
* Experimental API
2326
*/
24-
public class RowBinaryFormatWriter {
27+
public class RowBinaryFormatWriter implements ClickHouseBinaryFormatWriter {
2528

2629
private final OutputStream out;
2730

@@ -31,6 +34,8 @@ public class RowBinaryFormatWriter {
3134

3235
private final boolean defaultSupport;
3336

37+
private int rowCount = 0;
38+
3439
public RowBinaryFormatWriter(OutputStream out, TableSchema tableSchema, ClickHouseFormat format) {
3540
if (format != ClickHouseFormat.RowBinary && format != ClickHouseFormat.RowBinaryWithDefaults) {
3641
throw new IllegalArgumentException("Only RowBinary and RowBinaryWithDefaults are supported");
@@ -42,14 +47,37 @@ public RowBinaryFormatWriter(OutputStream out, TableSchema tableSchema, ClickHou
4247
this.defaultSupport = format == ClickHouseFormat.RowBinaryWithDefaults;
4348
}
4449

50+
@Override
51+
public OutputStream getOutputStream() {
52+
return out;
53+
}
54+
55+
@Override
56+
public int getRowCount() {
57+
return rowCount;
58+
}
59+
60+
@Override
61+
public ClickHouseFormat getFormat() {
62+
return defaultSupport ? ClickHouseFormat.RowBinaryWithDefaults : ClickHouseFormat.RowBinary;
63+
}
64+
65+
@Override
66+
public void clearRow() {
67+
Arrays.fill(row, null);
68+
}
69+
70+
@Override
4571
public void setValue(String column, Object value) {
4672
setValue(tableSchema.nameToColumnIndex(column), value);
4773
}
4874

75+
@Override
4976
public void setValue(int colIndex, Object value) {
5077
row[colIndex - 1] = value;
5178
}
5279

80+
@Override
5381
public void commitRow() throws IOException {
5482
List<ClickHouseColumn> columnList = tableSchema.getColumns();
5583
for (int i = 0; i < row.length; i++) {
@@ -59,76 +87,145 @@ public void commitRow() throws IOException {
5987
SerializerUtils.serializeData(out, row[i], column);
6088
}
6189
}
90+
rowCount++;
6291
}
6392

93+
@Override
6494
public void setByte(String column, byte value) {
6595
setValue(column, value);
6696
}
6797

98+
@Override
6899
public void setByte(int colIndex, byte value) {
69100
setValue(colIndex, value);
70101
}
71102

103+
@Override
72104
public void setShort(String column, short value) {
73105
setValue(column, value);
74106
}
75107

108+
@Override
76109
public void setShort(int colIndex, short value) {
77110
setValue(colIndex, value);
78111
}
79112

113+
@Override
80114
public void setInteger(String column, int value) {
81115
setValue(column, value);
82116
}
83117

118+
@Override
84119
public void setInteger(int colIndex, int value) {
85120
setValue(colIndex, value);
86121
}
87122

123+
@Override
88124
public void setLong(String column, long value) {
89125
setValue(column, value);
90126
}
91127

128+
@Override
92129
public void setLong(int colIndex, long value) {
93130
setValue(colIndex, value);
94131
}
95132

133+
@Override
134+
public void setBigInteger(int colIndex, BigInteger value) {
135+
setValue(colIndex, value);
136+
}
137+
138+
@Override
139+
public void setBigInteger(String column, BigInteger value) {
140+
setValue(column, value);
141+
}
142+
143+
@Override
144+
public void setFloat(int colIndex, float value) {
145+
setValue(colIndex, value);
146+
}
147+
148+
@Override
149+
public void setFloat(String column, float value) {
150+
setValue(column, value);
151+
}
152+
153+
@Override
154+
public void setDouble(int colIndex, double value) {
155+
setValue(colIndex, value);
156+
}
157+
158+
@Override
159+
public void setDouble(String column, double value) {
160+
setValue(column, value);
161+
}
162+
163+
@Override
164+
public void setBigDecimal(int colIndex, BigDecimal value) {
165+
setValue(colIndex, value);
166+
}
167+
168+
@Override
169+
public void setBigDecimal(String column, BigDecimal value) {
170+
setValue(column, value);
171+
}
172+
173+
@Override
174+
public void setBoolean(int colIndex, boolean value) {
175+
setValue(colIndex, value);
176+
}
177+
178+
@Override
179+
public void setBoolean(String column, boolean value) {
180+
setValue(column, value);
181+
}
182+
183+
@Override
96184
public void setString(String column, String value) {
97185
setValue(column, value);
98186
}
99187

188+
@Override
100189
public void setString(int colIndex, String value) {
101190
setValue(colIndex, value);
102191
}
103192

193+
@Override
104194
public void setDate(String column, LocalDate value) {
105195
setValue(column, value);
106196
}
107197

198+
@Override
108199
public void setDate(int colIndex, LocalDate value) {
109200
setValue(colIndex, value);
110201
}
111202

203+
@Override
112204
public void setDateTime(String column, LocalDateTime value) {
113205
setValue(column, value);
114206
}
115207

208+
@Override
116209
public void setDateTime(int colIndex, LocalDateTime value) {
117210
setValue(colIndex, value);
118211
}
119212

213+
@Override
120214
public void setDateTime(String column, ZonedDateTime value) {
121215
setValue(column, value);
122216
}
123217

218+
@Override
124219
public void setDateTime(int colIndex, ZonedDateTime value) {
125220
setValue(colIndex, value);
126221
}
127222

223+
@Override
128224
public void setList(String column, List<?> value) {
129225
setValue(column, value);
130226
}
131227

228+
@Override
132229
public void setList(int colIndex, List<?> value) {
133230
setValue(colIndex, value);
134231
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,14 @@ public Statement createStatement(int resultSetType, int resultSetConcurrency, in
359359
@Override
360360
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
361361
checkOpen();
362-
return new PreparedStatementImpl(this, sql);
362+
363+
StatementImpl.StatementType statementType = StatementImpl.parseStatementType(sql);
364+
if (statementType == StatementImpl.StatementType.INSERT) {
365+
if (!PreparedStatementImpl.FUNC_DETECT_REGEXP.matcher(sql).find()) {
366+
return new WriterStatementImpl(this, sql, statementType);
367+
}
368+
}
369+
return new PreparedStatementImpl(this, sql, statementType);
363370
}
364371

365372
@Override

0 commit comments

Comments
 (0)