Skip to content

Commit 7aa4401

Browse files
author
Paultagoras
committed
Adjusting metadata to use a custom ResultSet instead of SQL logic
1 parent cfb3db5 commit 7aa4401

File tree

4 files changed

+72
-220
lines changed

4 files changed

+72
-220
lines changed

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

Lines changed: 27 additions & 192 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ public ResultSetImpl(StatementImpl parentStatement, QueryResponse response, Clic
4747
this.defaultCalendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
4848
}
4949

50+
protected ResultSetImpl(ResultSetImpl resultSet) {
51+
this.parentStatement = resultSet.parentStatement;
52+
this.response = resultSet.response;
53+
this.reader = resultSet.reader;
54+
this.metaData = resultSet.metaData;
55+
this.closed = false;
56+
this.wasNull = false;
57+
this.defaultCalendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
58+
}
59+
5060
private void checkClosed() throws SQLException {
5161
if (closed) {
5262
throw new SQLException("ResultSet is closed.", ExceptionUtils.SQL_STATE_CONNECTION_EXCEPTION);
@@ -112,246 +122,82 @@ public boolean wasNull() throws SQLException {
112122

113123
@Override
114124
public String getString(int columnIndex) throws SQLException {
115-
checkClosed();
116-
try {
117-
if (reader.hasValue(columnIndex)) {
118-
wasNull = false;
119-
return reader.getString(columnIndex);
120-
} else {
121-
wasNull = true;
122-
return null;
123-
}
124-
} catch (Exception e) {
125-
throw ExceptionUtils.toSqlState(String.format("SQL: [%s]; Method: getString(%s)", parentStatement.getLastSql(), columnIndex), e);
126-
}
125+
return getString(getSchema().columnIndexToName(columnIndex));
127126
}
128127

129128
@Override
130129
public boolean getBoolean(int columnIndex) throws SQLException {
131-
checkClosed();
132-
try {
133-
if (reader.hasValue(columnIndex)) {
134-
wasNull = false;
135-
return reader.getBoolean(columnIndex);
136-
} else {
137-
wasNull = true;
138-
return false;
139-
}
140-
} catch (Exception e) {
141-
throw ExceptionUtils.toSqlState(String.format("SQL: [%s]; Method: getBoolean(%s)", parentStatement.getLastSql(), columnIndex), e);
142-
}
130+
return getBoolean(getSchema().columnIndexToName(columnIndex));
143131
}
144132

145133
@Override
146134
public byte getByte(int columnIndex) throws SQLException {
147-
checkClosed();
148-
try {
149-
if (reader.hasValue(columnIndex)) {
150-
wasNull = false;
151-
return reader.getByte(columnIndex);
152-
} else {
153-
wasNull = true;
154-
return 0;
155-
}
156-
} catch (Exception e) {
157-
throw ExceptionUtils.toSqlState(String.format("SQL: [%s]; Method: getByte(%s)", parentStatement.getLastSql(), columnIndex), e);
158-
}
135+
return getByte(getSchema().columnIndexToName(columnIndex));
159136
}
160137

161138
@Override
162139
public short getShort(int columnIndex) throws SQLException {
163-
checkClosed();
164-
try {
165-
if (reader.hasValue(columnIndex)) {
166-
wasNull = false;
167-
return reader.getShort(columnIndex);
168-
} else {
169-
wasNull = true;
170-
return 0;
171-
}
172-
} catch (Exception e) {
173-
throw ExceptionUtils.toSqlState(String.format("SQL: [%s]; Method: getShort(%s)", parentStatement.getLastSql(), columnIndex), e);
174-
}
140+
return getShort(getSchema().columnIndexToName(columnIndex));
175141
}
176142

177143
@Override
178144
public int getInt(int columnIndex) throws SQLException {
179-
checkClosed();
180-
try {
181-
if (reader.hasValue(columnIndex)) {
182-
wasNull = false;
183-
return reader.getInteger(columnIndex);
184-
} else {
185-
wasNull = true;
186-
return 0;
187-
}
188-
} catch (Exception e) {
189-
throw ExceptionUtils.toSqlState(String.format("SQL: [%s]; Method: getInt(%s)", parentStatement.getLastSql(), columnIndex), e);
190-
}
145+
return getInt(getSchema().columnIndexToName(columnIndex));
191146
}
192147

193148
@Override
194149
public long getLong(int columnIndex) throws SQLException {
195-
checkClosed();
196-
try {
197-
if (reader.hasValue(columnIndex)) {
198-
wasNull = false;
199-
return reader.getLong(columnIndex);
200-
} else {
201-
wasNull = true;
202-
return 0;
203-
}
204-
} catch (Exception e) {
205-
throw ExceptionUtils.toSqlState(String.format("SQL: [%s]; Method: getLong(%s)", parentStatement.getLastSql(), columnIndex), e);
206-
}
150+
return getLong(getSchema().columnIndexToName(columnIndex));
207151
}
208152

209153
@Override
210154
public float getFloat(int columnIndex) throws SQLException {
211-
checkClosed();
212-
try {
213-
if (reader.hasValue(columnIndex)) {
214-
wasNull = false;
215-
return reader.getFloat(columnIndex);
216-
} else {
217-
wasNull = true;
218-
return 0;
219-
}
220-
} catch (Exception e) {
221-
throw ExceptionUtils.toSqlState(String.format("SQL: [%s]; Method: getFloat(%s)", parentStatement.getLastSql(), columnIndex), e);
222-
}
155+
return getFloat(getSchema().columnIndexToName(columnIndex));
223156
}
224157

225158
@Override
226159
public double getDouble(int columnIndex) throws SQLException {
227-
checkClosed();
228-
try {
229-
if (reader.hasValue(columnIndex)) {
230-
wasNull = false;
231-
return reader.getDouble(columnIndex);
232-
} else {
233-
wasNull = true;
234-
return 0;
235-
}
236-
} catch (Exception e) {
237-
throw ExceptionUtils.toSqlState(String.format("SQL: [%s]; Method: getDouble(%s)", parentStatement.getLastSql(), columnIndex), e);
238-
}
160+
return getDouble(getSchema().columnIndexToName(columnIndex));
239161
}
240162

241163
@Override
242164
public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException {
243-
checkClosed();
244-
try {
245-
if (reader.hasValue(columnIndex)) {
246-
wasNull = false;
247-
return reader.getBigDecimal(columnIndex);
248-
} else {
249-
wasNull = true;
250-
return null;
251-
}
252-
} catch (Exception e) {
253-
throw ExceptionUtils.toSqlState(String.format("SQL: [%s]; Method: getBigDecimal(%s)", parentStatement.getLastSql(), columnIndex), e);
254-
}
165+
return getBigDecimal(getSchema().columnIndexToName(columnIndex), scale);
255166
}
256167

257168
@Override
258169
public byte[] getBytes(int columnIndex) throws SQLException {
259-
checkClosed();
260-
try {
261-
if (reader.hasValue(columnIndex)) {
262-
wasNull = false;
263-
return reader.getByteArray(columnIndex);
264-
} else {
265-
wasNull = true;
266-
return null;
267-
}
268-
} catch (Exception e) {
269-
throw ExceptionUtils.toSqlState(String.format("SQL: [%s]; Method: getBytes(%s)", parentStatement.getLastSql(), columnIndex), e);
270-
}
170+
return getBytes(getSchema().columnIndexToName(columnIndex));
271171
}
272172

273173
@Override
274174
public Date getDate(int columnIndex) throws SQLException {
275-
checkClosed();
276-
try {
277-
//TODO: Add this to ClickHouseBinaryFormatReader
278-
LocalDate localDate = reader.getLocalDate(columnIndex);
279-
if (localDate == null) {
280-
wasNull = true;
281-
return null;
282-
}
283-
284-
wasNull = false;
285-
return Date.valueOf(localDate);
286-
} catch (Exception e) {
287-
throw ExceptionUtils.toSqlState(String.format("SQL: [%s]; Method: getDate(%s)", parentStatement.getLastSql(), columnIndex), e);
288-
}
175+
return getDate(getSchema().columnIndexToName(columnIndex));
289176
}
290177

291178
@Override
292179
public Time getTime(int columnIndex) throws SQLException {
293-
checkClosed();
294-
try {
295-
LocalDateTime localDateTime = reader.getLocalDateTime(columnIndex);
296-
if (localDateTime == null) {
297-
wasNull = true;
298-
return null;
299-
}
300-
301-
wasNull = false;
302-
return Time.valueOf(localDateTime.toLocalTime());
303-
} catch (Exception e) {
304-
throw ExceptionUtils.toSqlState(String.format("SQL: [%s]; Method: getTime(%s)", parentStatement.getLastSql(), columnIndex), e);
305-
}
180+
return getTime(getSchema().columnIndexToName(columnIndex));
306181
}
307182

308183
@Override
309184
public Timestamp getTimestamp(int columnIndex) throws SQLException {
310-
checkClosed();
311-
try {
312-
LocalDateTime localDateTime = reader.getLocalDateTime(columnIndex);
313-
if (localDateTime == null) {
314-
wasNull = true;
315-
return null;
316-
}
317-
318-
wasNull = false;
319-
return Timestamp.valueOf(localDateTime);
320-
} catch (Exception e) {
321-
throw ExceptionUtils.toSqlState(String.format("SQL: [%s]; Method: getTimestamp(%s)", parentStatement.getLastSql(), columnIndex), e);
322-
}
185+
return getTimestamp(getSchema().columnIndexToName(columnIndex));
323186
}
324187

325188
@Override
326189
public InputStream getAsciiStream(int columnIndex) throws SQLException {
327-
checkClosed();
328-
//TODO: Add this to ClickHouseBinaryFormatReader
329-
if (!parentStatement.connection.config.isIgnoreUnsupportedRequests()) {
330-
throw new SQLFeatureNotSupportedException("AsciiStream is not yet supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
331-
}
332-
333-
return null;
190+
return getAsciiStream(getSchema().columnIndexToName(columnIndex));
334191
}
335192

336193
@Override
337194
public InputStream getUnicodeStream(int columnIndex) throws SQLException {
338-
checkClosed();
339-
if (!parentStatement.connection.config.isIgnoreUnsupportedRequests()) {
340-
return new ByteArrayInputStream(reader.getString(columnIndex).getBytes(StandardCharsets.UTF_8));
341-
}
342-
343-
return null;
195+
return getUnicodeStream(getSchema().columnIndexToName(columnIndex));
344196
}
345197

346198
@Override
347199
public InputStream getBinaryStream(int columnIndex) throws SQLException {
348-
checkClosed();
349-
//TODO: implement
350-
if (!parentStatement.connection.config.isIgnoreUnsupportedRequests()) {
351-
throw new SQLFeatureNotSupportedException("BinaryStream is not yet supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
352-
}
353-
354-
return null;
200+
return getBinaryStream(getSchema().columnIndexToName(columnIndex));
355201
}
356202

357203
@Override
@@ -659,18 +505,7 @@ public Reader getCharacterStream(String columnLabel) throws SQLException {
659505

660506
@Override
661507
public BigDecimal getBigDecimal(int columnIndex) throws SQLException {
662-
checkClosed();
663-
try {
664-
if (reader.hasValue(columnIndex)) {
665-
wasNull = false;
666-
return reader.getBigDecimal(columnIndex);
667-
} else {
668-
wasNull = true;
669-
return null;
670-
}
671-
} catch (Exception e) {
672-
throw ExceptionUtils.toSqlState(String.format("SQL: [%s]; Method: getBigDecimal(%s)", parentStatement.getLastSql(), columnIndex), e);
673-
}
508+
return getBigDecimal(getSchema().columnIndexToName(columnIndex));
674509
}
675510

676511
@Override

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

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,6 @@ public static Class<?> convertToJavaClass(ClickHouseDataType clickhouseType) {
107107
return SQL_TYPE_TO_CLASS_MAP.get(convertToSqlType(clickhouseType));
108108
}
109109

110-
111-
public static String generateSqlTypeEnum(String columnName) {
112-
StringBuilder sql = new StringBuilder("multiIf(");
113-
for (ClickHouseDataType type : CLICKHOUSE_TO_SQL_TYPE_MAP.keySet()) {
114-
sql.append("position(").append(columnName).append(", '").append(type.name()).append("') > 0, ").append(CLICKHOUSE_TO_SQL_TYPE_MAP.get(type).getVendorTypeNumber()).append(", ");
115-
}
116-
sql.append(JDBCType.OTHER.getVendorTypeNumber()).append(")");
117-
return sql.toString();
118-
}
119-
120110
public static List<String> tokenizeSQL(String sql) {
121111
List<String> tokens = new ArrayList<>();
122112

@@ -184,19 +174,6 @@ public static int indexOfIgnoresCase(List<String> list, String str) {
184174
return -1;
185175
}
186176

187-
public static String generateSqlTypeSizes(String columnName) {
188-
StringBuilder sql = new StringBuilder("multiIf(");
189-
sql.append("character_octet_length IS NOT NULL, character_octet_length, ");
190-
for (ClickHouseDataType type : ClickHouseDataType.values()) {
191-
if (type.getByteLength() > 0) {
192-
sql.append(columnName).append(" == '").append(type.name()).append("', ").append(type.getByteLength()).append(", ");
193-
}
194-
}
195-
sql.append("numeric_precision IS NOT NULL, numeric_precision, ");
196-
sql.append("0)");
197-
return sql.toString();
198-
}
199-
200177

201178
public static Object convert(Object value, Class<?> type) throws SQLException {
202179
if (value == null || type == null) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.clickhouse.jdbc.internal;
2+
3+
import com.clickhouse.data.ClickHouseColumn;
4+
import com.clickhouse.jdbc.ResultSetImpl;
5+
6+
import java.sql.SQLException;
7+
8+
public class MetadataResultSet extends ResultSetImpl {
9+
public MetadataResultSet(ResultSetImpl resultSet) {
10+
super(resultSet);
11+
}
12+
13+
@Override
14+
public String getString(String columnLabel) throws SQLException {
15+
String value = super.getString(columnLabel);
16+
17+
try {
18+
int val = Integer.parseInt(value);
19+
} catch (NumberFormatException e) {
20+
//We only do this if it's not a number, so we can assume it's a type string
21+
if ("DATA_TYPE".equalsIgnoreCase(columnLabel)) {
22+
value = String.valueOf(JdbcUtils.convertToSqlType(ClickHouseColumn.parse("DATA_TYPE " + value).get(0).getDataType()).getVendorTypeNumber());
23+
}
24+
25+
if ("COLUMN_SIZE".equalsIgnoreCase(columnLabel)) {
26+
value = String.valueOf(ClickHouseColumn.parse("COLUMN_SIZE " + value).get(0).getDataType().getByteLength());
27+
}
28+
}
29+
30+
return value;
31+
}
32+
33+
@Override
34+
public int getInt(String columnLabel) throws SQLException {
35+
return getString(columnLabel) == null ? 0 : Integer.parseInt(getString(columnLabel));
36+
}
37+
38+
}

0 commit comments

Comments
 (0)