Java Code Examples for java.sql.ResultSet#unwrap()

The following examples show how to use java.sql.ResultSet#unwrap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PhoenixMetricsIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 2
Source File: ResultSetTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 3
Source File: ResultSetTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 4
Source File: ResultSetTest.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 5
Source File: Jdbc4NativeJdbcExtractor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public ResultSet getNativeResultSet(ResultSet rs) throws SQLException {
	return rs.unwrap(this.resultSetType);
}
 
Example 6
Source File: Jdbc4NativeJdbcExtractor.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public ResultSet getNativeResultSet(ResultSet rs) throws SQLException {
	return rs.unwrap(this.resultSetType);
}
 
Example 7
Source File: CollectionsUnitTest.java    From cassandra-jdbc-wrapper with Apache License 2.0 4 votes vote down vote up
private CassandraResultSetExtras extras(ResultSet result) throws Exception
{
    Class crse = Class.forName("com.github.adejanovski.cassandra.jdbc.CassandraResultSetExtras");
    return (CassandraResultSetExtras) result.unwrap(crse);
}
 
Example 8
Source File: Jdbc4NativeJdbcExtractor.java    From effectivejava with Apache License 2.0 4 votes vote down vote up
@Override
public ResultSet getNativeResultSet(ResultSet rs) throws SQLException {
	return rs.unwrap(this.resultSetType);
}
 
Example 9
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 2 votes vote down vote up
/**
 * 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();
}
 
Example 10
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 2 votes vote down vote up
/**
 * 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();
}
 
Example 11
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 2 votes vote down vote up
/**
 * 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();
}