下面列出了java.sql.ResultSet#unwrap ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testReadMetricsForSelect() throws Exception {
String tableName = generateUniqueName();
long numSaltBuckets = 6;
String ddl = "CREATE TABLE " + tableName + " (K VARCHAR NOT NULL PRIMARY KEY, V VARCHAR)" + " SALT_BUCKETS = "
+ numSaltBuckets;
Connection conn = DriverManager.getConnection(getUrl());
conn.createStatement().execute(ddl);
long numRows = 1000;
long numExpectedTasks = numSaltBuckets;
insertRowsInTable(tableName, numRows);
String query = "SELECT * FROM " + tableName;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
PhoenixResultSet resultSetBeingTested = rs.unwrap(PhoenixResultSet.class);
changeInternalStateForTesting(resultSetBeingTested);
while (resultSetBeingTested.next()) {}
resultSetBeingTested.close();
Set<String> expectedTableNames = Sets.newHashSet(tableName);
assertReadMetricValuesForSelectSql(Lists.newArrayList(numRows), Lists.newArrayList(numExpectedTasks),
resultSetBeingTested, expectedTableNames);
}
/**
* Tests the wrapper methods isWrapperFor and unwrap. There are two cases
* to be tested
* Case 1: isWrapperFor returns true and we call unwrap
* Case 2: isWrapperFor returns false and we call unwrap
*
*
* @throws SQLException Thrown if some unexpected error happens
*/
public void testWrapper() throws SQLException {
PreparedStatement ps = prepareStatement("select count(*) from sys.systables");
ResultSet rs = ps.executeQuery();
Class<ResultSet> wrap_class = ResultSet.class;
//The if succeeds and we call the unwrap method on the conn object
if(rs.isWrapperFor(wrap_class)) {
ResultSet rs1 =
(ResultSet)rs.unwrap(wrap_class);
}
else {
assertFalse("isWrapperFor wrongly returns false", rs.isWrapperFor(wrap_class));
}
//Being Test for Case2
//test for the case when isWrapper returns false
//using some class that will return false when
//passed to isWrapperFor
Class<PreparedStatement> wrap_class1 = PreparedStatement.class;
try {
//returning false is the correct behaviour in this case
//Generate a message if it returns true
if(rs.isWrapperFor(wrap_class1)) {
assertTrue("isWrapperFor wrongly returns true", rs.isWrapperFor(wrap_class1));
}
else {
PreparedStatement ps1 = (PreparedStatement)
rs.unwrap(wrap_class1);
fail("unwrap does not throw the expected exception");
}
}
catch (SQLException sqle) {
//Calling unwrap in this case throws an
//SQLException ensure that this SQLException
//has the correct SQLState
assertSQLState(SQLStateConstants.UNABLE_TO_UNWRAP, sqle);
}
}
/**
* Tests the wrapper methods isWrapperFor and unwrap. There are two cases
* to be tested
* Case 1: isWrapperFor returns true and we call unwrap
* Case 2: isWrapperFor returns false and we call unwrap
*
*
* @throws SQLException Thrown if some unexpected error happens
*/
public void testWrapper() throws SQLException {
PreparedStatement ps = prepareStatement("select count(*) from sys.systables");
ResultSet rs = ps.executeQuery();
Class<ResultSet> wrap_class = ResultSet.class;
//The if succeeds and we call the unwrap method on the conn object
if(rs.isWrapperFor(wrap_class)) {
ResultSet rs1 =
(ResultSet)rs.unwrap(wrap_class);
}
else {
assertFalse("isWrapperFor wrongly returns false", rs.isWrapperFor(wrap_class));
}
//Being Test for Case2
//test for the case when isWrapper returns false
//using some class that will return false when
//passed to isWrapperFor
Class<PreparedStatement> wrap_class1 = PreparedStatement.class;
try {
//returning false is the correct behaviour in this case
//Generate a message if it returns true
if(rs.isWrapperFor(wrap_class1)) {
assertTrue("isWrapperFor wrongly returns true", rs.isWrapperFor(wrap_class1));
}
else {
PreparedStatement ps1 = (PreparedStatement)
rs.unwrap(wrap_class1);
fail("unwrap does not throw the expected exception");
}
}
catch (SQLException sqle) {
//Calling unwrap in this case throws an
//SQLException ensure that this SQLException
//has the correct SQLState
assertSQLState(SQLStateConstants.UNABLE_TO_UNWRAP, sqle);
}
}
/**
* Tests the wrapper methods isWrapperFor and unwrap. There are two cases
* to be tested
* Case 1: isWrapperFor returns true and we call unwrap
* Case 2: isWrapperFor returns false and we call unwrap
*
*
* @throws SQLException Thrown if some unexpected error happens
*/
public void testWrapper() throws SQLException {
PreparedStatement ps = prepareStatement("select count(*) from sys.systables");
ResultSet rs = ps.executeQuery();
Class<ResultSet> wrap_class = ResultSet.class;
//The if succeeds and we call the unwrap method on the conn object
if(rs.isWrapperFor(wrap_class)) {
ResultSet rs1 =
(ResultSet)rs.unwrap(wrap_class);
}
else {
assertFalse("isWrapperFor wrongly returns false", rs.isWrapperFor(wrap_class));
}
//Being Test for Case2
//test for the case when isWrapper returns false
//using some class that will return false when
//passed to isWrapperFor
Class<PreparedStatement> wrap_class1 = PreparedStatement.class;
try {
//returning false is the correct behaviour in this case
//Generate a message if it returns true
if(rs.isWrapperFor(wrap_class1)) {
assertTrue("isWrapperFor wrongly returns true", rs.isWrapperFor(wrap_class1));
}
else {
PreparedStatement ps1 = (PreparedStatement)
rs.unwrap(wrap_class1);
fail("unwrap does not throw the expected exception");
}
}
catch (SQLException sqle) {
//Calling unwrap in this case throws an
//SQLException ensure that this SQLException
//has the correct SQLState
assertSQLState(SQLStateConstants.UNABLE_TO_UNWRAP, sqle);
}
}
@Override
public ResultSet getNativeResultSet(ResultSet rs) throws SQLException {
return rs.unwrap(this.resultSetType);
}
@Override
public ResultSet getNativeResultSet(ResultSet rs) throws SQLException {
return rs.unwrap(this.resultSetType);
}
private CassandraResultSetExtras extras(ResultSet result) throws Exception
{
Class crse = Class.forName("com.github.adejanovski.cassandra.jdbc.CassandraResultSetExtras");
return (CassandraResultSetExtras) result.unwrap(crse);
}
@Override
public ResultSet getNativeResultSet(ResultSet rs) throws SQLException {
return rs.unwrap(this.resultSetType);
}
/**
* Method to expose the metrics associated with performing reads using the passed result set. A typical pattern is:
*
* <pre>
* {@code
* Map<String, Map<MetricType, Long>> overAllQueryMetrics = null;
* Map<String, Map<MetricType, Long>> requestReadMetrics = null;
* try (ResultSet rs = stmt.executeQuery()) {
* while(rs.next()) {
* .....
* }
* overAllQueryMetrics = PhoenixRuntime.getOverAllReadRequestMetrics(rs);
* requestReadMetrics = PhoenixRuntime.getRequestReadMetrics(rs);
* PhoenixRuntime.resetMetrics(rs);
* }
* </pre>
*
* @param rs
* result set to get the metrics for
* @return a map of (table name) -> (map of (metric name) -> (metric value))
* @throws SQLException
*/
public static Map<String, Map<MetricType, Long>> getRequestReadMetricInfo(ResultSet rs) throws SQLException {
PhoenixResultSet resultSet = rs.unwrap(PhoenixResultSet.class);
return resultSet.getReadMetrics();
}
/**
* Method to expose the overall metrics associated with executing a query via phoenix. A typical pattern of
* accessing request level read metrics and overall read query metrics is:
*
* <pre>
* {@code
* Map<String, Map<MetricType, Long>> overAllQueryMetrics = null;
* Map<String, Map<MetricType, Long>> requestReadMetrics = null;
* try (ResultSet rs = stmt.executeQuery()) {
* while(rs.next()) {
* .....
* }
* overAllQueryMetrics = PhoenixRuntime.getOverAllReadRequestMetrics(rs);
* requestReadMetrics = PhoenixRuntime.getRequestReadMetrics(rs);
* PhoenixRuntime.resetMetrics(rs);
* }
* </pre>
*
* @param rs
* result set to get the metrics for
* @return a map of metric name -> metric value
* @throws SQLException
*/
public static Map<MetricType, Long> getOverAllReadRequestMetricInfo(ResultSet rs) throws SQLException {
PhoenixResultSet resultSet = rs.unwrap(PhoenixResultSet.class);
return resultSet.getOverAllRequestReadMetrics();
}
/**
* Reset the read metrics collected in the result set.
*
* @see {@link #getRequestReadMetrics(ResultSet)} {@link #getOverAllReadRequestMetrics(ResultSet)}
* @param rs
* @throws SQLException
*/
public static void resetMetrics(ResultSet rs) throws SQLException {
PhoenixResultSet prs = rs.unwrap(PhoenixResultSet.class);
prs.resetMetrics();
}