Unverified 提交 57945c49 authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #844 from katzyn/wrapper

Add simple implementations of isWrapperFor() and unwrap() to JdbcDataSource
...@@ -23,6 +23,7 @@ import javax.sql.XAConnection; ...@@ -23,6 +23,7 @@ import javax.sql.XAConnection;
import javax.sql.XADataSource; import javax.sql.XADataSource;
import org.h2.Driver; import org.h2.Driver;
import org.h2.jdbc.JdbcConnection; import org.h2.jdbc.JdbcConnection;
import org.h2.message.DbException;
import org.h2.message.TraceObject; import org.h2.message.TraceObject;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
...@@ -401,23 +402,31 @@ public class JdbcDataSource extends TraceObject implements XADataSource, ...@@ -401,23 +402,31 @@ public class JdbcDataSource extends TraceObject implements XADataSource,
} }
/** /**
* [Not supported] Return an object of this class if possible. * Return an object of this class if possible.
* *
* @param iface the class * @param iface the class
*/ */
@Override @Override
@SuppressWarnings("unchecked")
public <T> T unwrap(Class<T> iface) throws SQLException { public <T> T unwrap(Class<T> iface) throws SQLException {
throw unsupported("unwrap"); try {
if (isWrapperFor(iface)) {
return (T) this;
}
throw DbException.getInvalidValueException("iface", iface);
} catch (Exception e) {
throw logAndConvert(e);
}
} }
/** /**
* [Not supported] Checks if unwrap can return an object of this class. * Checks if unwrap can return an object of this class.
* *
* @param iface the class * @param iface the class
*/ */
@Override @Override
public boolean isWrapperFor(Class<?> iface) throws SQLException { public boolean isWrapperFor(Class<?> iface) throws SQLException {
throw unsupported("isWrapperFor"); return iface != null && iface.isAssignableFrom(getClass());
} }
/** /**
......
...@@ -15,9 +15,12 @@ import javax.naming.StringRefAddr; ...@@ -15,9 +15,12 @@ import javax.naming.StringRefAddr;
import javax.naming.spi.ObjectFactory; import javax.naming.spi.ObjectFactory;
import javax.sql.ConnectionEvent; import javax.sql.ConnectionEvent;
import javax.sql.ConnectionEventListener; import javax.sql.ConnectionEventListener;
import javax.sql.DataSource;
import javax.sql.XAConnection; import javax.sql.XAConnection;
import javax.transaction.xa.XAResource; import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid; import javax.transaction.xa.Xid;
import org.h2.api.ErrorCode;
import org.h2.jdbcx.JdbcDataSource; import org.h2.jdbcx.JdbcDataSource;
import org.h2.jdbcx.JdbcDataSourceFactory; import org.h2.jdbcx.JdbcDataSourceFactory;
import org.h2.jdbcx.JdbcXAConnection; import org.h2.jdbcx.JdbcXAConnection;
...@@ -71,6 +74,7 @@ public class TestDataSource extends TestBase { ...@@ -71,6 +74,7 @@ public class TestDataSource extends TestBase {
} }
testDataSourceFactory(); testDataSourceFactory();
testDataSource(); testDataSource();
testUnwrap();
testXAConnection(); testXAConnection();
deleteDb("dataSource"); deleteDb("dataSource");
} }
...@@ -190,4 +194,20 @@ public class TestDataSource extends TestBase { ...@@ -190,4 +194,20 @@ public class TestDataSource extends TestBase {
conn.close(); conn.close();
} }
private void testUnwrap() throws SQLException {
JdbcDataSource ds = new JdbcDataSource();
assertTrue(ds.isWrapperFor(Object.class));
assertTrue(ds.isWrapperFor(DataSource.class));
assertTrue(ds.isWrapperFor(JdbcDataSource.class));
assertFalse(ds.isWrapperFor(String.class));
assertTrue(ds == ds.unwrap(Object.class));
assertTrue(ds == ds.unwrap(DataSource.class));
try {
ds.unwrap(String.class);
fail();
} catch (SQLException ex) {
assertEquals(ErrorCode.INVALID_VALUE_2, ex.getErrorCode());
}
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论