Skip to content

Commit 8b62e2d

Browse files
committed
IGNITE-26314 Use utils class in test.
1 parent c139ea0 commit 8b62e2d

File tree

7 files changed

+55
-360
lines changed

7 files changed

+55
-360
lines changed

modules/jdbc/src/integrationTest/java/org/apache/ignite/internal/jdbc/ItJdbcMetadataSelfTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,14 @@ public void testResultSetMetaDataColumns() throws Exception {
176176
ResultSet rs = stmt.executeQuery("SELECT * FROM META.TEST t");
177177

178178
assertNotNull(rs);
179+
assertFalse(rs.isClosed());
179180

180181
ResultSetMetaData meta = rs.getMetaData();
181182

182183
checkMeta(meta);
184+
185+
rs.close();
186+
assertTrue(rs.isClosed());
183187
} finally {
184188
stmt.execute("DROP TABLE META.TEST;");
185189
}

modules/jdbc/src/main/java/org/apache/ignite/internal/jdbc/JdbcConnection.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,8 @@ public DatabaseMetaData getMetaData() throws SQLException {
477477
ensureNotClosed();
478478

479479
if (metadata == null) {
480-
metadata = new JdbcDatabaseMetadata(this, handler, connProps.getUrl(), connProps.getUsername());
480+
metadata = new JdbcDatabaseMetadata(this, handler, connProps.getUrl(), connProps.getUsername(),
481+
connProps::getConnectionTimeZone);
481482
}
482483

483484
return metadata;

modules/jdbc/src/main/java/org/apache/ignite/internal/jdbc/JdbcDatabaseMetadata.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import static java.sql.RowIdLifetime.ROWID_UNSUPPORTED;
2525
import static java.util.Arrays.asList;
2626
import static java.util.Collections.singletonList;
27-
import static org.apache.ignite.internal.jdbc.JdbcDatabaseMetadataUtils.createObjectListResultSet;
27+
import static org.apache.ignite.internal.jdbc.JdbcUtils.createObjectListResultSet;
2828
import static org.apache.ignite.internal.jdbc.proto.SqlStateCode.CONNECTION_CLOSED;
2929

3030
import java.sql.Connection;
@@ -33,11 +33,13 @@
3333
import java.sql.RowIdLifetime;
3434
import java.sql.SQLException;
3535
import java.sql.Types;
36+
import java.time.ZoneId;
3637
import java.util.ArrayList;
3738
import java.util.LinkedList;
3839
import java.util.List;
3940
import java.util.concurrent.CancellationException;
4041
import java.util.concurrent.ExecutionException;
42+
import java.util.function.Supplier;
4143
import org.apache.ignite.internal.client.proto.ProtocolVersion;
4244
import org.apache.ignite.internal.jdbc.proto.IgniteQueryErrorCode;
4345
import org.apache.ignite.internal.jdbc.proto.JdbcQueryEventHandler;
@@ -85,24 +87,29 @@ public class JdbcDatabaseMetadata implements DatabaseMetaData {
8587

8688
private final Connection connection;
8789

90+
private final Supplier<ZoneId> timeZoneSupplier;
91+
8892
/**
8993
* Constructor.
9094
*
9195
* @param connection Connection
9296
* @param handler Handler.
9397
* @param url URL
9498
* @param userName User name,
99+
* @param timeZoneSupplier Time zone supplier.
95100
*/
96101
public JdbcDatabaseMetadata(
97102
Connection connection,
98103
JdbcQueryEventHandler handler,
99104
String url,
100-
String userName
105+
String userName,
106+
Supplier<ZoneId> timeZoneSupplier
101107
) {
102108
this.handler = handler;
103109
this.connection = connection;
104110
this.url = url;
105111
this.userName = userName;
112+
this.timeZoneSupplier = timeZoneSupplier;
106113
}
107114

108115
/** {@inheritDoc} */
@@ -913,7 +920,7 @@ public ResultSet getTables(String catalog, String schemaPtrn, String tblNamePtrn
913920
rows.add(tableRow(tblMeta));
914921
}
915922

916-
return createObjectListResultSet(rows, meta);
923+
return createObjectListResultSet(rows, meta, timeZoneSupplier);
917924
} catch (InterruptedException e) {
918925
throw new SQLException("Thread was interrupted.", e);
919926
} catch (ExecutionException e) {
@@ -961,7 +968,7 @@ public ResultSet getSchemas(String catalog, String schemaPtrn) throws SQLExcepti
961968
rows.add(row);
962969
}
963970

964-
return createObjectListResultSet(rows, meta);
971+
return createObjectListResultSet(rows, meta, timeZoneSupplier);
965972
} catch (InterruptedException e) {
966973
throw new SQLException("Thread was interrupted.", e);
967974
} catch (ExecutionException e) {
@@ -978,7 +985,7 @@ public ResultSet getCatalogs() throws SQLException {
978985
ensureNotClosed();
979986

980987
return createObjectListResultSet(singletonList(singletonList(CATALOG_NAME)),
981-
asList(columnMeta("TABLE_CAT", ColumnType.STRING)));
988+
asList(columnMeta("TABLE_CAT", ColumnType.STRING)), timeZoneSupplier);
982989
}
983990

984991
/** {@inheritDoc} */
@@ -989,7 +996,7 @@ public ResultSet getTableTypes() throws SQLException {
989996

990997
return createObjectListResultSet(
991998
asList(List.of(TYPE_TABLE, TYPE_VIEW)),
992-
asList(columnMeta("TABLE_TYPE", ColumnType.STRING)));
999+
asList(columnMeta("TABLE_TYPE", ColumnType.STRING)), timeZoneSupplier);
9931000
}
9941001

9951002
/** {@inheritDoc} */
@@ -1042,7 +1049,7 @@ public ResultSet getColumns(String catalog, String schemaPtrn, String tblNamePtr
10421049
rows.add(columnRow(res.meta().get(i), i + 1));
10431050
}
10441051

1045-
return createObjectListResultSet(rows, meta);
1052+
return createObjectListResultSet(rows, meta, timeZoneSupplier);
10461053
} catch (InterruptedException e) {
10471054
throw new SQLException("Thread was interrupted.", e);
10481055
} catch (ExecutionException e) {
@@ -1150,7 +1157,7 @@ public ResultSet getPrimaryKeys(String catalog, String schema, String tbl) throw
11501157
rows.addAll(primaryKeyRows(pkMeta));
11511158
}
11521159

1153-
return createObjectListResultSet(rows, meta);
1160+
return createObjectListResultSet(rows, meta, timeZoneSupplier);
11541161
} catch (InterruptedException e) {
11551162
throw new SQLException("Thread was interrupted.", e);
11561163
} catch (ExecutionException e) {
@@ -1346,7 +1353,7 @@ public ResultSet getTypeInfo() throws SQLException {
13461353
columnMeta("SQL_DATA_TYPE", ColumnType.INT32),
13471354
columnMeta("SQL_DATETIME_SUB", ColumnType.INT32),
13481355
columnMeta("NUM_PREC_RADIX", ColumnType.INT32)
1349-
));
1356+
), timeZoneSupplier);
13501357
}
13511358

13521359
/** {@inheritDoc} */
@@ -1631,7 +1638,7 @@ public boolean autoCommitFailureClosesAllResultSets() {
16311638

16321639
/** {@inheritDoc} */
16331640
@Override
1634-
public ResultSet getClientInfoProperties() throws SQLException {
1641+
public ResultSet getClientInfoProperties() {
16351642
// We do not check whether connection is closed as
16361643
// this operation is not expected to do any server calls.
16371644
return createObjectListResultSet(asList(
Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,17 @@
2323
import java.time.LocalDate;
2424
import java.time.LocalDateTime;
2525
import java.time.LocalTime;
26-
import java.util.EnumSet;
26+
import java.time.ZoneId;
2727
import java.util.Iterator;
2828
import java.util.List;
2929
import java.util.NoSuchElementException;
3030
import java.util.UUID;
31+
import java.util.function.Supplier;
3132
import org.apache.ignite.internal.jdbc2.ClientSyncResultSet;
3233
import org.apache.ignite.internal.jdbc2.JdbcResultSet;
3334
import org.apache.ignite.internal.sql.ResultSetMetadataImpl;
34-
import org.apache.ignite.internal.util.IgniteUtils;
3535
import org.apache.ignite.internal.util.TransformingIterator;
3636
import org.apache.ignite.sql.ColumnMetadata;
37-
import org.apache.ignite.sql.ColumnType;
3837
import org.apache.ignite.sql.ResultSetMetadata;
3938
import org.apache.ignite.sql.SqlRow;
4039
import org.apache.ignite.table.Tuple;
@@ -43,42 +42,42 @@
4342
/**
4443
* Helper methods for creating a {@link ResultSet} using a list of objects.
4544
*/
46-
class JdbcDatabaseMetadataUtils {
47-
/** List of supported metadata column types. */
48-
private static final EnumSet<ColumnType> SUPPORTED_TYPES = EnumSet.of(
49-
ColumnType.NULL,
50-
ColumnType.BOOLEAN,
51-
ColumnType.INT8,
52-
ColumnType.INT16,
53-
ColumnType.INT32,
54-
ColumnType.INT64,
55-
ColumnType.STRING
56-
);
45+
public class JdbcUtils {
46+
/** System time zone supplier. */
47+
private static final Supplier<ZoneId> defaultZoneSupplier = ZoneId::systemDefault;
5748

5849
/** Creates an empty {@link ResultSet} using the provided metadata. */
5950
static ResultSet createObjectListResultSet(List<ColumnMetadata> columnsMeta) {
60-
return createObjectListResultSet(List.of(), columnsMeta);
51+
return createObjectListResultSet(List.of(), columnsMeta, defaultZoneSupplier);
6152
}
6253

6354
/** Creates a {@link ResultSet} using the provided list of objects and metadata. */
64-
static ResultSet createObjectListResultSet(List<List<Object>> rows, List<ColumnMetadata> columnsMeta) {
65-
ResultSetMetadata meta = new ResultSetMetadataImpl(columnsMeta);
55+
public static ResultSet createObjectListResultSet(
56+
List<List<Object>> rows,
57+
List<ColumnMetadata> columnsMeta,
58+
Supplier<ZoneId> timeZoneSupplier
59+
) {
60+
return createObjectListResultSet(rows, columnsMeta, timeZoneSupplier, 0);
61+
}
6662

67-
if (IgniteUtils.assertionsEnabled()) {
68-
for (ColumnMetadata columnMeta : meta.columns()) {
69-
assert SUPPORTED_TYPES.contains(columnMeta.type()) : "Unsupported column type: " + columnMeta.type();
70-
}
71-
}
63+
/** Creates a {@link ResultSet} using the provided list of objects and metadata. */
64+
public static ResultSet createObjectListResultSet(
65+
List<List<Object>> rows,
66+
List<ColumnMetadata> columnsMeta,
67+
Supplier<ZoneId> timeZoneSupplier,
68+
int maxRows
69+
) {
70+
ResultSetMetadata meta = new ResultSetMetadataImpl(columnsMeta);
7271

7372
TransformingIterator<List<Object>, SqlRow> transformer =
7473
new TransformingIterator<>(rows.iterator(), ObjectListToSqlRowAdapter::new);
7574

7675
return new JdbcResultSet(
7776
new IteratorBasedClientSyncResultSet(transformer, meta),
7877
null,
79-
null,
78+
timeZoneSupplier,
8079
false,
81-
0
80+
maxRows
8281
);
8382
}
8483

modules/jdbc/src/main/java/org/apache/ignite/internal/jdbc2/JdbcConnection2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public JdbcConnection2(
161161
properties = props;
162162

163163
//noinspection ThisEscapedInObjectConstruction
164-
metadata = new JdbcDatabaseMetadata(this, eventHandler, props.getUrl(), props.getUsername());
164+
metadata = new JdbcDatabaseMetadata(this, eventHandler, props.getUrl(), props.getUsername(), props::getConnectionTimeZone);
165165
}
166166

167167
/** {@inheritDoc} */

modules/jdbc/src/test/java/org/apache/ignite/internal/jdbc/JdbcDatabaseMetadataSelfTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.sql.ResultSet;
2828
import java.sql.RowIdLifetime;
2929
import java.sql.SQLException;
30+
import java.time.ZoneId;
3031
import java.util.concurrent.ThreadLocalRandom;
3132
import org.apache.ignite.internal.client.proto.ProtocolVersion;
3233
import org.apache.ignite.internal.properties.IgniteProductVersion;
@@ -46,7 +47,7 @@ public void constantMethods() throws SQLException {
4647

4748
JdbcClientQueryEventHandler handler = Mockito.mock(JdbcClientQueryEventHandler.class);
4849
Connection connection = Mockito.mock(Connection.class);
49-
DatabaseMetaData metaData = new JdbcDatabaseMetadata(connection, handler, jdbcUrl, username);
50+
DatabaseMetaData metaData = new JdbcDatabaseMetadata(connection, handler, jdbcUrl, username, ZoneId::systemDefault);
5051

5152
// Basic info
5253
assertTrue(metaData.allProceduresAreCallable());

0 commit comments

Comments
 (0)