Skip to content

Support isWrapperFor() and unwrap() methods #154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/main/java/org/tarantool/jdbc/SQLDatabaseMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.sql.ResultSet;
import java.sql.RowIdLifetime;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLNonTransientException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -1117,13 +1117,16 @@ public boolean generatedKeyAlwaysReturned() throws SQLException {
}

@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
throw new SQLFeatureNotSupportedException();
public <T> T unwrap(Class<T> type) throws SQLException {
if (isWrapperFor(type)) {
return type.cast(this);
}
throw new SQLNonTransientException("SQLDatabaseMetadata does not wrap " + type.getName());
}

@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
throw new SQLFeatureNotSupportedException();
public boolean isWrapperFor(Class<?> type) throws SQLException {
return type.isAssignableFrom(this.getClass());
}

private ResultSet asMetadataResultSet(JDBCBridge jdbcBridge) throws SQLException {
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/org/tarantool/jdbc/SQLResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -1081,13 +1081,16 @@ public void updateNCharacterStream(String columnLabel, Reader reader) throws SQL
}

@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
throw new SQLFeatureNotSupportedException();
public <T> T unwrap(Class<T> type) throws SQLException {
if (isWrapperFor(type)) {
return type.cast(this);
}
throw new SQLNonTransientException("ResultSet does not wrap " + type.getName());
}

@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
throw new SQLFeatureNotSupportedException();
public boolean isWrapperFor(Class<?> type) throws SQLException {
return type.isAssignableFrom(this.getClass());
}

@Override
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/org/tarantool/jdbc/SQLResultSetMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLNonTransientException;
import java.sql.Types;

public class SQLResultSetMetaData implements ResultSetMetaData {
Expand Down Expand Up @@ -120,13 +121,16 @@ public String getColumnClassName(int column) throws SQLException {
}

@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
throw new SQLFeatureNotSupportedException();
public <T> T unwrap(Class<T> type) throws SQLException {
if (isWrapperFor(type)) {
return type.cast(this);
}
throw new SQLNonTransientException("ResultSetMetadata does not wrap " + type.getName());
}

@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
throw new SQLFeatureNotSupportedException();
public boolean isWrapperFor(Class<?> type) throws SQLException {
return type.isAssignableFrom(this.getClass());
}

@Override
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/org/tarantool/jdbc/JdbcDatabaseMetaDataIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,16 @@ public void testSupportsResultSetHoldability() throws SQLException {
assertFalse(meta.supportsResultSetHoldability(42));
}

@Test
public void testUnwrap() throws SQLException {
assertEquals(meta, meta.unwrap(SQLDatabaseMetadata.class));
assertThrows(SQLException.class, () -> meta.unwrap(Integer.class));
}

@Test
public void testIsWrapperFor() throws SQLException {
assertTrue(meta.isWrapperFor(SQLDatabaseMetadata.class));
assertFalse(meta.isWrapperFor(Integer.class));
}

}
16 changes: 16 additions & 0 deletions src/test/java/org/tarantool/jdbc/JdbcPreparedStatementIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,22 @@ public void execute() throws Throwable {
assertEquals(3, i);
}

@Test
public void testUnwrap() throws SQLException {
prep = conn.prepareStatement("SELECT val FROM test");
assertEquals(prep, prep.unwrap(SQLPreparedStatement.class));
assertEquals(prep, prep.unwrap(SQLStatement.class));
assertThrows(SQLException.class, () -> prep.unwrap(Integer.class));
}

@Test
public void testIsWrapperFor() throws SQLException {
prep = conn.prepareStatement("SELECT val FROM test");
assertTrue(prep.isWrapperFor(SQLPreparedStatement.class));
assertTrue(prep.isWrapperFor(SQLStatement.class));
assertFalse(prep.isWrapperFor(Integer.class));
}

@Test
public void testSetByte() throws SQLException {
makeHelper(Byte.class)
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/tarantool/jdbc/JdbcResultSetIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

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

@Test
public void testUnwrap() throws SQLException {
ResultSet resultSet = stmt.executeQuery("SELECT * FROM test WHERE id < 0");
assertEquals(resultSet, resultSet.unwrap(SQLResultSet.class));
assertThrows(SQLException.class, () -> resultSet.unwrap(Integer.class));
}

@Test
public void testIsWrapperFor() throws SQLException {
ResultSet resultSet = stmt.executeQuery("SELECT * FROM test WHERE id < 0");
assertTrue(resultSet.isWrapperFor(SQLResultSet.class));
assertFalse(resultSet.isWrapperFor(Integer.class));
}

}
27 changes: 27 additions & 0 deletions src/test/java/org/tarantool/jdbc/JdbcResultSetMetaDataIT.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.tarantool.jdbc;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -31,4 +34,28 @@ public void testColumnNames() throws SQLException {
rs.close();
stmt.close();
}

@Test
public void testUnwrap() throws SQLException {
try (
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM test")
) {
ResultSetMetaData metaData = resultSet.getMetaData();
assertEquals(metaData, metaData.unwrap(SQLResultSetMetaData.class));
assertThrows(SQLException.class, () -> metaData.unwrap(Integer.class));
}
}

@Test
public void testIsWrapperFor() throws SQLException {
try (
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM test")
) {
ResultSetMetaData metaData = resultSet.getMetaData();
assertTrue(metaData.isWrapperFor(SQLResultSetMetaData.class));
assertFalse(metaData.isWrapperFor(Integer.class));
}
}
}
13 changes: 13 additions & 0 deletions src/test/java/org/tarantool/jdbc/JdbcStatementIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,17 @@ public void execute() throws Throwable {
}
assertEquals(3, i);
}

@Test
public void testUnwrap() throws SQLException {
assertEquals(stmt, stmt.unwrap(SQLStatement.class));
assertThrows(SQLException.class, () -> stmt.unwrap(Integer.class));
}

@Test
public void testIsWrapperFor() throws SQLException {
assertTrue(stmt.isWrapperFor(SQLStatement.class));
assertFalse(stmt.isWrapperFor(Integer.class));
}

}