Skip to content

Commit 8b72cf2

Browse files
committed
Support isWrapperFor() and unwrap() methods
Add missed implementation of Wrapper interface for SQLDatabaseMetadata, SQLResultSet, and SQL ResultSetMetaData classes. Closes: #73
1 parent 7cdb58c commit 8b72cf2

8 files changed

+106
-13
lines changed

src/main/java/org/tarantool/jdbc/SQLDatabaseMetadata.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import java.sql.ResultSet;
99
import java.sql.RowIdLifetime;
1010
import java.sql.SQLException;
11-
import java.sql.SQLFeatureNotSupportedException;
11+
import java.sql.SQLNonTransientException;
1212
import java.sql.Types;
1313
import java.util.ArrayList;
1414
import java.util.Arrays;
@@ -1117,13 +1117,16 @@ public boolean generatedKeyAlwaysReturned() throws SQLException {
11171117
}
11181118

11191119
@Override
1120-
public <T> T unwrap(Class<T> iface) throws SQLException {
1121-
throw new SQLFeatureNotSupportedException();
1120+
public <T> T unwrap(Class<T> type) throws SQLException {
1121+
if (isWrapperFor(type)) {
1122+
return type.cast(this);
1123+
}
1124+
throw new SQLNonTransientException("SQLDatabaseMetadata does not wrap " + type.getName());
11221125
}
11231126

11241127
@Override
1125-
public boolean isWrapperFor(Class<?> iface) throws SQLException {
1126-
throw new SQLFeatureNotSupportedException();
1128+
public boolean isWrapperFor(Class<?> type) throws SQLException {
1129+
return type.isAssignableFrom(this.getClass());
11271130
}
11281131

11291132
private ResultSet asMetadataResultSet(JDBCBridge jdbcBridge) throws SQLException {

src/main/java/org/tarantool/jdbc/SQLResultSet.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -1081,13 +1081,16 @@ public void updateNCharacterStream(String columnLabel, Reader reader) throws SQL
10811081
}
10821082

10831083
@Override
1084-
public <T> T unwrap(Class<T> iface) throws SQLException {
1085-
throw new SQLFeatureNotSupportedException();
1084+
public <T> T unwrap(Class<T> type) throws SQLException {
1085+
if (isWrapperFor(type)) {
1086+
return type.cast(this);
1087+
}
1088+
throw new SQLNonTransientException("ResultSet does not wrap " + type.getName());
10861089
}
10871090

10881091
@Override
1089-
public boolean isWrapperFor(Class<?> iface) throws SQLException {
1090-
throw new SQLFeatureNotSupportedException();
1092+
public boolean isWrapperFor(Class<?> type) throws SQLException {
1093+
return type.isAssignableFrom(this.getClass());
10911094
}
10921095

10931096
@Override

src/main/java/org/tarantool/jdbc/SQLResultSetMetaData.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.sql.ResultSetMetaData;
66
import java.sql.SQLException;
77
import java.sql.SQLFeatureNotSupportedException;
8+
import java.sql.SQLNonTransientException;
89
import java.sql.Types;
910

1011
public class SQLResultSetMetaData implements ResultSetMetaData {
@@ -120,13 +121,16 @@ public String getColumnClassName(int column) throws SQLException {
120121
}
121122

122123
@Override
123-
public <T> T unwrap(Class<T> iface) throws SQLException {
124-
throw new SQLFeatureNotSupportedException();
124+
public <T> T unwrap(Class<T> type) throws SQLException {
125+
if (isWrapperFor(type)) {
126+
return type.cast(this);
127+
}
128+
throw new SQLNonTransientException("ResultSetMetadata does not wrap " + type.getName());
125129
}
126130

127131
@Override
128-
public boolean isWrapperFor(Class<?> iface) throws SQLException {
129-
throw new SQLFeatureNotSupportedException();
132+
public boolean isWrapperFor(Class<?> type) throws SQLException {
133+
return type.isAssignableFrom(this.getClass());
130134
}
131135

132136
@Override

src/test/java/org/tarantool/jdbc/JdbcDatabaseMetaDataIT.java

+12
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,16 @@ public void testSupportsResultSetHoldability() throws SQLException {
255255
assertFalse(meta.supportsResultSetHoldability(42));
256256
}
257257

258+
@Test
259+
public void testUnwrap() throws SQLException {
260+
assertEquals(meta, meta.unwrap(SQLDatabaseMetadata.class));
261+
assertThrows(SQLException.class, () -> meta.unwrap(Integer.class));
262+
}
263+
264+
@Test
265+
public void testIsWrapperFor() throws SQLException {
266+
assertTrue(meta.isWrapperFor(SQLDatabaseMetadata.class));
267+
assertFalse(meta.isWrapperFor(Integer.class));
268+
}
269+
258270
}

src/test/java/org/tarantool/jdbc/JdbcPreparedStatementIT.java

+16
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,22 @@ public void execute() throws Throwable {
169169
assertEquals(3, i);
170170
}
171171

172+
@Test
173+
public void testUnwrap() throws SQLException {
174+
prep = conn.prepareStatement("SELECT val FROM test");
175+
assertEquals(prep, prep.unwrap(SQLPreparedStatement.class));
176+
assertEquals(prep, prep.unwrap(SQLStatement.class));
177+
assertThrows(SQLException.class, () -> prep.unwrap(Integer.class));
178+
}
179+
180+
@Test
181+
public void testIsWrapperFor() throws SQLException {
182+
prep = conn.prepareStatement("SELECT val FROM test");
183+
assertTrue(prep.isWrapperFor(SQLPreparedStatement.class));
184+
assertTrue(prep.isWrapperFor(SQLStatement.class));
185+
assertFalse(prep.isWrapperFor(Integer.class));
186+
}
187+
172188
@Test
173189
public void testSetByte() throws SQLException {
174190
makeHelper(Byte.class)

src/test/java/org/tarantool/jdbc/JdbcResultSetIT.java

+15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertFalse;
55
import static org.junit.jupiter.api.Assertions.assertNotNull;
6+
import static org.junit.jupiter.api.Assertions.assertThrows;
67
import static org.junit.jupiter.api.Assertions.assertTrue;
78

89
import org.junit.jupiter.api.AfterEach;
@@ -133,4 +134,18 @@ public void testHoldability() throws SQLException {
133134
assertEquals(metaData.getResultSetHoldability(), resultSet.getHoldability());
134135
}
135136

137+
@Test
138+
public void testUnwrap() throws SQLException {
139+
ResultSet resultSet = stmt.executeQuery("SELECT * FROM test WHERE id < 0");
140+
assertEquals(resultSet, resultSet.unwrap(SQLResultSet.class));
141+
assertThrows(SQLException.class, () -> resultSet.unwrap(Integer.class));
142+
}
143+
144+
@Test
145+
public void testIsWrapperFor() throws SQLException {
146+
ResultSet resultSet = stmt.executeQuery("SELECT * FROM test WHERE id < 0");
147+
assertTrue(resultSet.isWrapperFor(SQLResultSet.class));
148+
assertFalse(resultSet.isWrapperFor(Integer.class));
149+
}
150+
136151
}

src/test/java/org/tarantool/jdbc/JdbcResultSetMetaDataIT.java

+27
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package org.tarantool.jdbc;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
45
import static org.junit.jupiter.api.Assertions.assertNotNull;
6+
import static org.junit.jupiter.api.Assertions.assertThrows;
7+
import static org.junit.jupiter.api.Assertions.assertTrue;
58

69
import org.junit.jupiter.api.Test;
710

@@ -31,4 +34,28 @@ public void testColumnNames() throws SQLException {
3134
rs.close();
3235
stmt.close();
3336
}
37+
38+
@Test
39+
public void testUnwrap() throws SQLException {
40+
try (
41+
Statement statement = conn.createStatement();
42+
ResultSet resultSet = statement.executeQuery("SELECT * FROM test")
43+
) {
44+
ResultSetMetaData metaData = resultSet.getMetaData();
45+
assertEquals(metaData, metaData.unwrap(SQLResultSetMetaData.class));
46+
assertThrows(SQLException.class, () -> metaData.unwrap(Integer.class));
47+
}
48+
}
49+
50+
@Test
51+
public void testIsWrapperFor() throws SQLException {
52+
try (
53+
Statement statement = conn.createStatement();
54+
ResultSet resultSet = statement.executeQuery("SELECT * FROM test")
55+
) {
56+
ResultSetMetaData metaData = resultSet.getMetaData();
57+
assertTrue(metaData.isWrapperFor(SQLResultSetMetaData.class));
58+
assertFalse(metaData.isWrapperFor(Integer.class));
59+
}
60+
}
3461
}

src/test/java/org/tarantool/jdbc/JdbcStatementIT.java

+13
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,17 @@ public void execute() throws Throwable {
9494
}
9595
assertEquals(3, i);
9696
}
97+
98+
@Test
99+
public void testUnwrap() throws SQLException {
100+
assertEquals(stmt, stmt.unwrap(SQLStatement.class));
101+
assertThrows(SQLException.class, () -> stmt.unwrap(Integer.class));
102+
}
103+
104+
@Test
105+
public void testIsWrapperFor() throws SQLException {
106+
assertTrue(stmt.isWrapperFor(SQLStatement.class));
107+
assertFalse(stmt.isWrapperFor(Integer.class));
108+
}
109+
97110
}

0 commit comments

Comments
 (0)