Skip to content

Commit f472b44

Browse files
committed
added more tests
1 parent 5294643 commit f472b44

File tree

2 files changed

+153
-79
lines changed

2 files changed

+153
-79
lines changed

jdbc-v2/src/main/java/com/clickhouse/jdbc/types/ArrayResultSet.java

Lines changed: 92 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
import com.clickhouse.client.api.data_formats.internal.ValueConverters;
44
import com.clickhouse.data.ClickHouseColumn;
55
import com.clickhouse.data.ClickHouseDataType;
6+
import com.clickhouse.jdbc.internal.ExceptionUtils;
67
import com.clickhouse.jdbc.internal.JdbcUtils;
78
import com.clickhouse.jdbc.metadata.ResultSetMetaDataImpl;
89

910
import java.io.InputStream;
1011
import java.io.Reader;
1112
import java.math.BigDecimal;
13+
import java.math.RoundingMode;
14+
import java.net.MalformedURLException;
1215
import java.net.URL;
1316
import java.sql.Array;
1417
import java.sql.Blob;
@@ -123,6 +126,16 @@ private Object getValueAsObject(int columnIndex, Class<?> type, Object defaultVa
123126
return value == null ? defaultValue : value;
124127
}
125128

129+
private void throwReadOnlyException() throws SQLException {
130+
throw new SQLException("ResultSet is read-only");
131+
}
132+
133+
private void throwUnsupportedIndexOperation(int columnIndex, String operation) throws SQLException {
134+
if (columnIndex == 1) {
135+
throw new SQLFeatureNotSupportedException("operation " + operation + " is not supported on INDEX column");
136+
}
137+
}
138+
126139
@Override
127140
public void close() throws SQLException {
128141
this.closed = true;
@@ -143,9 +156,7 @@ public String getString(int columnIndex) throws SQLException {
143156

144157
@Override
145158
public boolean getBoolean(int columnIndex) throws SQLException {
146-
if (columnIndex == 1) {
147-
throw new SQLException("INDEX column cannot be get as boolean");
148-
}
159+
throwUnsupportedIndexOperation(columnIndex, "getBoolean");
149160
return (Boolean) getValueAsObject(columnIndex, Boolean.class, false);
150161
}
151162

@@ -215,67 +226,53 @@ public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException
215226

216227
@Override
217228
public byte[] getBytes(int columnIndex) throws SQLException {
218-
if (columnIndex == 1) {
219-
throw new SQLException("INDEX column cannot be get as bytes");
220-
}
229+
throwUnsupportedIndexOperation(columnIndex, "getBytes");
221230
return (byte[]) getValueAsObject(columnIndex, byte[].class, null);
222231
}
223232

224233
@Override
225234
public Date getDate(int columnIndex) throws SQLException {
226-
if (columnIndex == 1) {
227-
throw new SQLException("INDEX column cannot be get as date");
228-
}
235+
throwUnsupportedIndexOperation(columnIndex, "getDate");
229236
return (Date) getValueAsObject(columnIndex, Date.class, null);
230237
}
231238

232239
@Override
233240
public Time getTime(int columnIndex) throws SQLException {
234241
checkColumnIndex(columnIndex);
235242
checkRowPosition();
236-
if (columnIndex == 1) {
237-
throw new SQLException("INDEX column cannot be get as time");
238-
}
243+
throwUnsupportedIndexOperation(columnIndex, "getTime");
239244
return (Time) getValueAsObject(columnIndex, Time.class, null);
240245
}
241246

242247
@Override
243248
public Timestamp getTimestamp(int columnIndex) throws SQLException {
244249
checkColumnIndex(columnIndex);
245250
checkRowPosition();
246-
if (columnIndex == 1) {
247-
throw new SQLException("INDEX column cannot be get as timestamp");
248-
}
251+
throwUnsupportedIndexOperation(columnIndex, "getTimestamp");
249252
return (Timestamp) getValueAsObject(columnIndex, Timestamp.class, null);
250253
}
251254

252255
@Override
253256
public InputStream getAsciiStream(int columnIndex) throws SQLException {
254257
checkColumnIndex(columnIndex);
255258
checkRowPosition();
256-
if (columnIndex == 1) {
257-
throw new SQLException("INDEX column cannot be get as ascii stream");
258-
}
259+
throwUnsupportedIndexOperation(columnIndex, "getAsciiStream");
259260
throw new SQLFeatureNotSupportedException("getAsciiStream is not implemented");
260261
}
261262

262263
@Override
263264
public InputStream getUnicodeStream(int columnIndex) throws SQLException {
264265
checkColumnIndex(columnIndex);
265266
checkRowPosition();
266-
if (columnIndex == 1) {
267-
throw new SQLException("INDEX column cannot be get as unicode stream");
268-
}
267+
throwUnsupportedIndexOperation(columnIndex, "getUnicodeStream");
269268
throw new SQLFeatureNotSupportedException("getUnicodeStream is not implemented");
270269
}
271270

272271
@Override
273272
public InputStream getBinaryStream(int columnIndex) throws SQLException {
274273
checkColumnIndex(columnIndex);
275274
checkRowPosition();
276-
if (columnIndex == 1) {
277-
throw new SQLException("INDEX column cannot be get as binary stream");
278-
}
275+
throwUnsupportedIndexOperation(columnIndex, "getBinaryStream");
279276
throw new SQLFeatureNotSupportedException("getBinaryStream is not implemented");
280277
}
281278

@@ -357,17 +354,17 @@ public Timestamp getTimestamp(String columnLabel) throws SQLException {
357354

358355
@Override
359356
public InputStream getAsciiStream(String columnLabel) throws SQLException {
360-
return null;
357+
return getAsciiStream(getColumnIndex(columnLabel));
361358
}
362359

363360
@Override
364361
public InputStream getUnicodeStream(String columnLabel) throws SQLException {
365-
return null;
362+
return getUnicodeStream(getColumnIndex(columnLabel));
366363
}
367364

368365
@Override
369366
public InputStream getBinaryStream(String columnLabel) throws SQLException {
370-
return null;
367+
return getBinaryStream(getColumnIndex(columnLabel));
371368
}
372369

373370
@Override
@@ -407,19 +404,22 @@ public int findColumn(String columnLabel) throws SQLException {
407404

408405
@Override
409406
public Reader getCharacterStream(int columnIndex) throws SQLException {
410-
return null;
407+
checkColumnIndex(columnIndex);
408+
checkRowPosition();
409+
throwUnsupportedIndexOperation(columnIndex, "getCharacterStream");
410+
throw new SQLFeatureNotSupportedException("getCharacterStream is not implemented");
411411
}
412412

413413
@Override
414414
public Reader getCharacterStream(String columnLabel) throws SQLException {
415-
return null;
415+
return getCharacterStream(getColumnIndex(columnLabel));
416416
}
417417

418418
@Override
419419
public BigDecimal getBigDecimal(int columnIndex) throws SQLException {
420-
if (columnIndex == 1) {
421-
throw new SQLException("INDEX column cannot be get as big decimal");
422-
}
420+
checkColumnIndex(columnIndex);
421+
checkRowPosition();
422+
throwUnsupportedIndexOperation(columnIndex, "getBigDecimal");
423423
return (BigDecimal) getValueAsObject(columnIndex, BigDecimal.class, null);
424424
}
425425

@@ -739,10 +739,6 @@ public void updateObject(String columnLabel, Object x) throws SQLException {
739739
throwReadOnlyException();
740740
}
741741

742-
private void throwReadOnlyException() throws SQLException {
743-
throw new SQLException("ResultSet is read-only");
744-
}
745-
746742
@Override
747743
public void insertRow() throws SQLException {
748744
throwReadOnlyException();
@@ -812,22 +808,34 @@ public Object getObject(int columnIndex, Map<String, Class<?>> map) throws SQLEx
812808

813809
@Override
814810
public Ref getRef(int columnIndex) throws SQLException {
815-
return null;
811+
checkRowPosition();
812+
checkColumnIndex(columnIndex);
813+
throwUnsupportedIndexOperation(columnIndex, "getRef");
814+
throw new SQLFeatureNotSupportedException("getRef is not implemented");
816815
}
817816

818817
@Override
819818
public Blob getBlob(int columnIndex) throws SQLException {
820-
return null;
819+
checkColumnIndex(columnIndex);
820+
checkRowPosition();
821+
throwUnsupportedIndexOperation(columnIndex, "getBlob");
822+
throw new SQLFeatureNotSupportedException("getBlob is not implemented");
821823
}
822824

823825
@Override
824826
public Clob getClob(int columnIndex) throws SQLException {
825-
return null;
827+
checkColumnIndex(columnIndex);
828+
checkRowPosition();
829+
throwUnsupportedIndexOperation(columnIndex, "getClob");
830+
throw new SQLFeatureNotSupportedException("getClob is not implemented");
826831
}
827832

828833
@Override
829834
public Array getArray(int columnIndex) throws SQLException {
830-
return null;
835+
checkColumnIndex(columnIndex);
836+
checkRowPosition();
837+
throwUnsupportedIndexOperation(columnIndex, "getArray");
838+
return (Array) getValueAsObject(columnIndex, Array.class, null);
831839
}
832840

833841
@Override
@@ -837,29 +845,29 @@ public Object getObject(String columnLabel, Map<String, Class<?>> map) throws SQ
837845

838846
@Override
839847
public Ref getRef(String columnLabel) throws SQLException {
840-
return null;
848+
return getRef(getColumnIndex(columnLabel));
841849
}
842850

843851
@Override
844852
public Blob getBlob(String columnLabel) throws SQLException {
845-
return null;
853+
return getBlob(getColumnIndex(columnLabel));
846854
}
847855

848856
@Override
849857
public Clob getClob(String columnLabel) throws SQLException {
850-
return null;
858+
return getClob(getColumnIndex(columnLabel));
851859
}
852860

853861
@Override
854862
public Array getArray(String columnLabel) throws SQLException {
855-
return null;
863+
return getArray(getColumnIndex(columnLabel));
856864
}
857865

858866
@Override
859867
public Date getDate(int columnIndex, Calendar cal) throws SQLException {
860-
if (columnIndex == 1) {
861-
throw new SQLException("INDEX column cannot be get as date");
862-
}
868+
checkColumnIndex(columnIndex);
869+
checkRowPosition();
870+
throwUnsupportedIndexOperation(columnIndex, "getDate");
863871
return null;
864872
}
865873

@@ -870,9 +878,9 @@ public Date getDate(String columnLabel, Calendar cal) throws SQLException {
870878

871879
@Override
872880
public Time getTime(int columnIndex, Calendar cal) throws SQLException {
873-
if (columnIndex == 1) {
874-
throw new SQLException("INDEX column cannot be get as time");
875-
}
881+
checkColumnIndex(columnIndex);
882+
checkRowPosition();
883+
throwUnsupportedIndexOperation(columnIndex, "getTime");
876884
return null;
877885
}
878886

@@ -883,6 +891,9 @@ public Time getTime(String columnLabel, Calendar cal) throws SQLException {
883891

884892
@Override
885893
public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException {
894+
checkColumnIndex(columnIndex);
895+
checkRowPosition();
896+
throwUnsupportedIndexOperation(columnIndex, "getTimestamp");
886897
return (Timestamp) getValueAsObject(columnIndex, Timestamp.class, null);
887898
}
888899

@@ -893,7 +904,15 @@ public Timestamp getTimestamp(String columnLabel, Calendar cal) throws SQLExcept
893904

894905
@Override
895906
public URL getURL(int columnIndex) throws SQLException {
896-
return (URL) getValueAsObject(columnIndex, URL.class, null);
907+
checkColumnIndex(columnIndex);
908+
checkRowPosition();
909+
throwUnsupportedIndexOperation(columnIndex, "getURL");
910+
String value = getString(columnIndex);
911+
try {
912+
return new URL(value);
913+
} catch (MalformedURLException e) {
914+
throw new SQLException("Invalid URL value", ExceptionUtils.SQL_STATE_DATA_EXCEPTION, e);
915+
}
897916
}
898917

899918
@Override
@@ -943,12 +962,15 @@ public void updateArray(String columnLabel, Array x) throws SQLException {
943962

944963
@Override
945964
public RowId getRowId(int columnIndex) throws SQLException {
946-
return null;
965+
checkColumnIndex(columnIndex);
966+
checkRowPosition();
967+
throwUnsupportedIndexOperation(columnIndex, "getRowId");
968+
throw new SQLFeatureNotSupportedException("getRowId is not implemented");
947969
}
948970

949971
@Override
950972
public RowId getRowId(String columnLabel) throws SQLException {
951-
return null;
973+
return getRowId(getColumnIndex(columnLabel));
952974
}
953975

954976
@Override
@@ -993,22 +1015,28 @@ public void updateNClob(String columnLabel, NClob nClob) throws SQLException {
9931015

9941016
@Override
9951017
public NClob getNClob(int columnIndex) throws SQLException {
996-
return null;
1018+
checkColumnIndex(columnIndex);
1019+
checkRowPosition();
1020+
throwUnsupportedIndexOperation(columnIndex, "getNClob");
1021+
throw new SQLFeatureNotSupportedException("getNClob is not implemented");
9971022
}
9981023

9991024
@Override
10001025
public NClob getNClob(String columnLabel) throws SQLException {
1001-
return null;
1026+
return getNClob(getColumnIndex(columnLabel));
10021027
}
10031028

10041029
@Override
10051030
public SQLXML getSQLXML(int columnIndex) throws SQLException {
1006-
return null;
1031+
checkColumnIndex(columnIndex);
1032+
checkRowPosition();
1033+
throwUnsupportedIndexOperation(columnIndex, "getSQLXML");
1034+
throw new SQLFeatureNotSupportedException("getSQLXML is not implemented");
10071035
}
10081036

10091037
@Override
10101038
public SQLXML getSQLXML(String columnLabel) throws SQLException {
1011-
return null;
1039+
return getSQLXML(getColumnIndex(columnLabel));
10121040
}
10131041

10141042
@Override
@@ -1023,22 +1051,25 @@ public void updateSQLXML(String columnLabel, SQLXML xmlObject) throws SQLExcepti
10231051

10241052
@Override
10251053
public String getNString(int columnIndex) throws SQLException {
1026-
return "";
1054+
return getString(columnIndex);
10271055
}
10281056

10291057
@Override
10301058
public String getNString(String columnLabel) throws SQLException {
1031-
return "";
1059+
return getString(getColumnIndex(columnLabel));
10321060
}
10331061

10341062
@Override
10351063
public Reader getNCharacterStream(int columnIndex) throws SQLException {
1036-
return null;
1064+
checkColumnIndex(columnIndex);
1065+
checkRowPosition();
1066+
throwUnsupportedIndexOperation(columnIndex, "getNCharacterStream");
1067+
throw new SQLFeatureNotSupportedException("getNCharacterStream is not implemented");
10371068
}
10381069

10391070
@Override
10401071
public Reader getNCharacterStream(String columnLabel) throws SQLException {
1041-
return null;
1072+
return getNCharacterStream(getColumnIndex(columnLabel));
10421073
}
10431074

10441075
@Override

0 commit comments

Comments
 (0)