Skip to content

Commit f88883a

Browse files
committed
added more tests for ResultSetMetadataImpl
1 parent f17c5d8 commit f88883a

File tree

3 files changed

+87
-49
lines changed

3 files changed

+87
-49
lines changed

clickhouse-data/src/test/java/com/clickhouse/data/ClickHouseColumnTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,4 +465,10 @@ public Object[][] testJSONBinaryFormat_dp() {
465465
{"JSON(max_dynamic_types=3,max_dynamic_paths=3, SKIP REGEXP '^-.*',SKIP ff, flags Array(Array(Array(Int8))), SKIP alt_count)", 2, Arrays.asList("flags")},
466466
};
467467
}
468+
469+
@Test(groups = {"unit"})
470+
public void testReadColumnWithTableName() {
471+
ClickHouseColumn c = ClickHouseColumn.of("`table`.`field1`", "String");
472+
System.out.println(c);
473+
}
468474
}

jdbc-v2/src/main/java/com/clickhouse/jdbc/metadata/ResultSetMetaDataImpl.java

Lines changed: 25 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ public ResultSetMetaDataImpl(List<ClickHouseColumn> columns, String schema, Stri
3232
this.typeClassMap = typeClassMap;
3333
}
3434

35+
private void checkColumnIndex(int column) throws SQLException {
36+
if (column < 1 || column > columns.size()) {
37+
throw new SQLException("Column index out of range: " + column, ExceptionUtils.SQL_STATE_CLIENT_ERROR);
38+
}
39+
}
40+
3541
private ClickHouseColumn getColumn(int column) throws SQLException {
3642
try {
3743
return columns.get(column - 1);
@@ -47,68 +53,52 @@ public int getColumnCount() throws SQLException {
4753

4854
@Override
4955
public boolean isAutoIncrement(int column) throws SQLException {
56+
checkColumnIndex(column);
5057
return false; // no auto-incremental types
5158
}
5259

5360
@Override
5461
public boolean isCaseSensitive(int column) throws SQLException {
55-
try {
56-
// TODO: should be in sync with DatabaseMetadata
57-
return getColumn(column).getDataType().isCaseSensitive();
58-
} catch (Exception e) {
59-
throw ExceptionUtils.toSqlState(e);
60-
}
62+
// TODO: should be in sync with DatabaseMetadata
63+
return getColumn(column).getDataType().isCaseSensitive();
6164
}
6265

6366
@Override
6467
public boolean isSearchable(int column) throws SQLException {
68+
checkColumnIndex(column);
6569
return true; // all columns are considered as searchable
6670
}
6771

6872
@Override
6973
public boolean isCurrency(int column) throws SQLException {
74+
checkColumnIndex(column);
7075
return false;
7176
}
7277

7378
@Override
7479
public int isNullable(int column) throws SQLException {
75-
try {
76-
return getColumn(column).isNullable() ? columnNullable : columnNoNulls;
77-
} catch (Exception e) {
78-
throw ExceptionUtils.toSqlState(e);
79-
}
80+
return getColumn(column).isNullable() ? columnNullable : columnNoNulls;
8081
}
8182

8283
@Override
8384
public boolean isSigned(int column) throws SQLException {
84-
try {
85-
return getColumn(column).getDataType().isSigned();
86-
} catch (Exception e) {
87-
throw ExceptionUtils.toSqlState(e);
88-
}
85+
return getColumn(column).getDataType().isSigned();
8986
}
9087

9188
@Override
9289
public int getColumnDisplaySize(int column) throws SQLException {
90+
checkColumnIndex(column);
9391
return 80;
9492
}
9593

9694
@Override
9795
public String getColumnLabel(int column) throws SQLException {
98-
try {
99-
return getColumn(column).getColumnName();
100-
} catch (Exception e) {
101-
throw ExceptionUtils.toSqlState(e);
102-
}
96+
return getColumn(column).getColumnName();
10397
}
10498

10599
@Override
106100
public String getColumnName(int column) throws SQLException {
107-
try {
108-
return getColumn(column).getColumnName();
109-
} catch (Exception e) {
110-
throw ExceptionUtils.toSqlState(e);
111-
}
101+
return getColumn(column).getColumnName();
112102
}
113103

114104
@Override
@@ -118,62 +108,51 @@ public String getSchemaName(int column) throws SQLException {
118108

119109
@Override
120110
public int getPrecision(int column) throws SQLException {
121-
try {
122-
return getColumn(column).getPrecision();
123-
} catch (Exception e) {
124-
throw ExceptionUtils.toSqlState(e);
125-
}
111+
return getColumn(column).getPrecision();
126112
}
127113

128114
@Override
129115
public int getScale(int column) throws SQLException {
130-
try {
131-
return getColumn(column).getScale();
132-
} catch (Exception e) {
133-
throw ExceptionUtils.toSqlState(e);
134-
}
116+
return getColumn(column).getScale();
135117
}
136118

137119
@Override
138120
public String getTableName(int column) throws SQLException {
121+
checkColumnIndex(column);
139122
return tableName;
140123
}
141124

142125
@Override
143126
public String getCatalogName(int column) throws SQLException {
127+
checkColumnIndex(column);
144128
return catalog;
145129
}
146130

147131
@Override
148132
public int getColumnType(int column) throws SQLException {
149-
try {
150-
return JdbcUtils.convertToSqlType(getColumn(column).getDataType()).getVendorTypeNumber();
151-
} catch (Exception e) {
152-
throw ExceptionUtils.toSqlState(e);
153-
}
133+
return JdbcUtils.convertToSqlType(getColumn(column).getDataType()).getVendorTypeNumber();
154134
}
155135

156136
@Override
157137
public String getColumnTypeName(int column) throws SQLException {
158-
try {
159-
return getColumn(column).getOriginalTypeName();
160-
} catch (Exception e) {
161-
throw ExceptionUtils.toSqlState(e);
162-
}
138+
return getColumn(column).getOriginalTypeName();
163139
}
164140

165141
@Override
166142
public boolean isReadOnly(int column) throws SQLException {
143+
checkColumnIndex(column);
167144
return true;
168145
}
169146

170147
@Override
171148
public boolean isWritable(int column) throws SQLException {
149+
checkColumnIndex(column);
172150
return false;
173151
}
174152

175153
@Override
176154
public boolean isDefinitelyWritable(int column) throws SQLException {
155+
checkColumnIndex(column);
177156
return false;
178157
}
179158

jdbc-v2/src/test/java/com/clickhouse/jdbc/metadata/ResultSetMetaDataImplTest.java

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,76 @@
11
package com.clickhouse.jdbc.metadata;
22

33
import com.clickhouse.jdbc.JdbcIntegrationTest;
4+
import org.testng.Assert;
45
import org.testng.annotations.Test;
56

6-
import java.math.BigInteger;
77
import java.sql.Connection;
8-
import java.sql.DatabaseMetaData;
98
import java.sql.ResultSet;
109
import java.sql.ResultSetMetaData;
10+
import java.sql.SQLException;
1111
import java.sql.Statement;
1212
import java.sql.Types;
1313

1414
import static org.testng.Assert.assertEquals;
1515
import static org.testng.Assert.assertFalse;
16-
import static org.testng.Assert.assertThrows;
1716
import static org.testng.Assert.assertTrue;
1817

1918
@Test(groups = { "integration" })
2019
public class ResultSetMetaDataImplTest extends JdbcIntegrationTest {
20+
21+
@Test(groups = { "integration" })
22+
public void testConstants() throws Exception {
23+
try (Connection conn = getJdbcConnection()) {
24+
try (Statement stmt = conn.createStatement()) {
25+
ResultSet rs = stmt.executeQuery("SELECT 1 AS a, 2 AS b, 3 AS c");
26+
ResultSetMetaData rsmd = rs.getMetaData();
27+
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
28+
assertFalse(rsmd.isAutoIncrement(i));
29+
assertFalse(rsmd.isWritable(i));
30+
assertTrue(rsmd.isReadOnly(i));
31+
assertFalse(rsmd.isDefinitelyWritable(i));
32+
assertFalse(rsmd.isCurrency(i));
33+
assertTrue(rsmd.isSearchable(i));
34+
assertEquals(rsmd.getTableName(i), "");
35+
assertEquals(rsmd.getCatalogName(i), "");
36+
assertEquals(rsmd.getSchemaName(i), getDatabase());
37+
}
38+
}
39+
}
40+
}
41+
42+
@Test(groups = { "integration" })
43+
public void testColumnIndexOutOfBoundCheck() throws Exception {
44+
try (Connection conn = getJdbcConnection()) {
45+
try (Statement stmt = conn.createStatement()) {
46+
ResultSet rs = stmt.executeQuery("SELECT 1 AS a, 2 AS b, 3 AS c");
47+
ResultSetMetaData rsmd = rs.getMetaData();
48+
int lastColumnIndex = rsmd.getColumnCount() + 1;
49+
for (int i = 0; i <= lastColumnIndex; i += lastColumnIndex ) {
50+
final int outOfBoundIndex = i;
51+
Assert.assertThrows(SQLException.class, () -> rsmd.getColumnClassName(outOfBoundIndex));
52+
Assert.assertThrows(SQLException.class, () -> rsmd.getColumnType(outOfBoundIndex));
53+
Assert.assertThrows(SQLException.class, () -> rsmd.getColumnTypeName(outOfBoundIndex));
54+
Assert.assertThrows(SQLException.class, () -> rsmd.getColumnLabel(outOfBoundIndex));
55+
Assert.assertThrows(SQLException.class, () -> rsmd.getColumnName(outOfBoundIndex));
56+
Assert.assertThrows(SQLException.class, () -> rsmd.getColumnDisplaySize(outOfBoundIndex));
57+
Assert.assertThrows(SQLException.class, () -> rsmd.getScale(outOfBoundIndex));
58+
Assert.assertThrows(SQLException.class, () -> rsmd.getPrecision(outOfBoundIndex));
59+
Assert.assertThrows(SQLException.class, () -> rsmd.isCaseSensitive(outOfBoundIndex));
60+
Assert.assertThrows(SQLException.class, () -> rsmd.isSigned(outOfBoundIndex));
61+
Assert.assertThrows(SQLException.class, () -> rsmd.isSearchable(outOfBoundIndex));
62+
Assert.assertThrows(SQLException.class, () -> rsmd.isCurrency(outOfBoundIndex));
63+
Assert.assertThrows(SQLException.class, () -> rsmd.isNullable(outOfBoundIndex));
64+
Assert.assertThrows(SQLException.class, () -> rsmd.isReadOnly(outOfBoundIndex));
65+
Assert.assertThrows(SQLException.class, () -> rsmd.isWritable(outOfBoundIndex));
66+
Assert.assertThrows(SQLException.class, () -> rsmd.isDefinitelyWritable(outOfBoundIndex));
67+
Assert.assertThrows(SQLException.class, () -> rsmd.getTableName(outOfBoundIndex));
68+
Assert.assertThrows(SQLException.class, () -> rsmd.isAutoIncrement(outOfBoundIndex));
69+
}
70+
}
71+
}
72+
}
73+
2174
@Test(groups = { "integration" })
2275
public void testGetColumnCount() throws Exception {
2376
try (Connection conn = getJdbcConnection()) {

0 commit comments

Comments
 (0)