java.sql.ResultSet#unwrap ( )源码实例Demo

下面列出了java.sql.ResultSet#unwrap ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: phoenix   文件: PhoenixMetricsIT.java
@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);
}
 
源代码2 项目: gemfirexd-oss   文件: ResultSetTest.java
/**
 * 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);
    }
}
 
源代码3 项目: gemfirexd-oss   文件: ResultSetTest.java
/**
 * 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);
    }
}
 
源代码4 项目: spliceengine   文件: ResultSetTest.java
/**
 * 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);
    }
}
 
源代码5 项目: lams   文件: Jdbc4NativeJdbcExtractor.java
@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);
}
 
源代码8 项目: effectivejava   文件: Jdbc4NativeJdbcExtractor.java
@Override
public ResultSet getNativeResultSet(ResultSet rs) throws SQLException {
	return rs.unwrap(this.resultSetType);
}
 
源代码9 项目: phoenix   文件: PhoenixRuntime.java
/**
 * 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();
}
 
源代码10 项目: phoenix   文件: PhoenixRuntime.java
/**
 * 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();
}
 
源代码11 项目: phoenix   文件: PhoenixRuntime.java
/**
 * 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();
}