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

The following examples show how to use java.sql.PreparedStatement#unwrap() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: phoenix   File: PhoenixQueryTimeoutIT.java    License: Apache License 2.0 5 votes vote down vote up
private PreparedStatement loadDataAndPrepareQuery(int timeoutMs, int timeoutSecs) throws Exception, SQLException {
    Properties props = new Properties();
    props.setProperty(QueryServices.THREAD_TIMEOUT_MS_ATTRIB, String.valueOf(timeoutMs));
    Connection conn = DriverManager.getConnection(getUrl(), props);
    PreparedStatement ps = conn.prepareStatement("SELECT * FROM " + tableName);
    PhoenixStatement phoenixStmt = ps.unwrap(PhoenixStatement.class);
    assertEquals(timeoutMs, phoenixStmt.getQueryTimeoutInMillis());
    assertEquals(timeoutSecs, phoenixStmt.getQueryTimeout());
    return ps;
}
 
Example 2
@Override
public PreparedStatement getNativePreparedStatement(PreparedStatement ps) throws SQLException {
	return ps.unwrap(this.preparedStatementType);
}
 
Example 3
Source Project: lams   File: Loader.java    License: GNU General Public License v2.0 4 votes vote down vote up
protected SqlStatementWrapper executeQueryStatement(
		String sqlStatement,
		QueryParameters queryParameters,
		boolean scroll,
		List<AfterLoadAction> afterLoadActions,
		SharedSessionContractImplementor session) throws SQLException {

	// Processing query filters.
	queryParameters.processFilters( sqlStatement, session );

	// Applying LIMIT clause.
	final LimitHandler limitHandler = getLimitHandler(
			queryParameters.getRowSelection()
	);
	String sql = limitHandler.processSql( queryParameters.getFilteredSQL(), queryParameters.getRowSelection() );

	// Adding locks and comments.
	sql = preprocessSQL( sql, queryParameters, getFactory(), afterLoadActions );

	final PreparedStatement st = prepareQueryStatement( sql, queryParameters, limitHandler, scroll, session );

	final ResultSet rs;

	if( queryParameters.isCallable() && isTypeOf( st, CallableStatement.class ) ) {
		final CallableStatement cs = st.unwrap( CallableStatement.class );

		rs = getResultSet(
				cs,
				queryParameters.getRowSelection(),
				limitHandler,
				queryParameters.hasAutoDiscoverScalarTypes(),
				session
		);
	}
	else {
		rs = getResultSet(
			st,
			queryParameters.getRowSelection(),
			limitHandler,
			queryParameters.hasAutoDiscoverScalarTypes(),
			session
		);
	}

	return new SqlStatementWrapper(
		st,
		rs
	);

}
 
Example 4
@Override
public PreparedStatement getNativePreparedStatement(PreparedStatement ps) throws SQLException {
	return ps.unwrap(this.preparedStatementType);
}
 
Example 5
@Override
public PreparedStatement getNativePreparedStatement(PreparedStatement ps) throws SQLException {
	return ps.unwrap(this.preparedStatementType);
}
 
Example 6
@Test
public void testAnyAllComparisonSubquery() throws Exception {
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    Connection conn = DriverManager.getConnection(getUrl(), props);
    String tableName1 = getTableName(conn, JOIN_ITEM_TABLE_FULL_NAME);
    String tableName4 = getTableName(conn, JOIN_ORDER_TABLE_FULL_NAME);
    try {
        String query = "SELECT /*+ USE_SORT_MERGE_JOIN*/ \"order_id\", name FROM " + tableName4 + " o JOIN " + tableName1 + " i ON o.\"item_id\" = i.\"item_id\" WHERE quantity = ALL(SELECT quantity FROM " + tableName4 + " q WHERE o.\"item_id\" = q.\"item_id\")";
        PreparedStatement statement = conn.prepareStatement(query);
        ResultSet rs = statement.executeQuery();
        assertTrue (rs.next());
        assertEquals(rs.getString(1), "000000000000001");
        assertEquals(rs.getString(2), "T1");
        assertTrue (rs.next());
        assertEquals(rs.getString(1), "000000000000003");
        assertEquals(rs.getString(2), "T2");
        assertTrue (rs.next());
        assertEquals(rs.getString(1), "000000000000005");
        assertEquals(rs.getString(2), "T3");

        assertFalse(rs.next());
        
        query = "SELECT /*+ USE_SORT_MERGE_JOIN*/ \"order_id\", name FROM " + tableName4 + " o JOIN " + tableName1 + " i ON o.\"item_id\" = i.\"item_id\" WHERE quantity != ALL(SELECT max(quantity) FROM " + tableName4 + " q WHERE o.\"item_id\" = q.\"item_id\")";
        statement = conn.prepareStatement(query);
        rs = statement.executeQuery();
        assertTrue (rs.next());
        assertEquals(rs.getString(1), "000000000000002");
        assertEquals(rs.getString(2), "T6");

        assertFalse(rs.next());
        //add order by to make the query result stable
        query = "SELECT /*+ USE_SORT_MERGE_JOIN*/ \"order_id\", name FROM " + tableName4 + " o JOIN " + tableName1 + " i ON o.\"item_id\" = i.\"item_id\" WHERE quantity != ANY(SELECT quantity FROM " + tableName4 + " q WHERE o.\"item_id\" = q.\"item_id\" GROUP BY quantity) order by \"order_id\"";
        statement = conn.prepareStatement(query);
        rs = statement.executeQuery();
        assertTrue (rs.next());
        assertEquals(rs.getString(1), "000000000000002");
        assertEquals(rs.getString(2), "T6");
        assertTrue (rs.next());
        assertEquals(rs.getString(1), "000000000000004");
        assertEquals(rs.getString(2), "T6");

        assertFalse(rs.next());

        PhoenixPreparedStatement phoenixPreparedStatement = statement.unwrap(PhoenixPreparedStatement.class);
        ClientScanPlan clientScanPlan =(ClientScanPlan)phoenixPreparedStatement.optimizeQuery(query);
        SortMergeJoinPlan sortMergeJoin = (SortMergeJoinPlan)clientScanPlan.getDelegate();
        ClientScanPlan lhsQueryPlan = (ClientScanPlan)sortMergeJoin.getLhsPlan();
        /**
         * test orderBy of lhs of final SortJoinMergePlan is avoid.
         */
        assertTrue(lhsQueryPlan.getOrderBy() == OrderBy.FWD_ROW_KEY_ORDER_BY);
        TupleProjectionPlan rhsQueryPlan = (TupleProjectionPlan)sortMergeJoin.getRhsPlan();
        ClientAggregatePlan clientAggregatePlan = (ClientAggregatePlan)rhsQueryPlan.getDelegate();
        /**
         * test groupBy and orderBy of rhs of final SortJoinMergePlan is avoid.
         */
        assertTrue(clientAggregatePlan.getGroupBy().isOrderPreserving());
        assertTrue(clientAggregatePlan.getOrderBy() == OrderBy.FWD_ROW_KEY_ORDER_BY);
    } finally {
        conn.close();
    }
}
 
Example 7
@Test
public void testRoundRobinBehavior() throws Exception {
    int nRows = 30000;
    try (Connection conn = DriverManager.getConnection(getUrl())) {
        conn.createStatement().execute("CREATE TABLE " + tableName + "(K VARCHAR PRIMARY KEY)");
        PreparedStatement stmt = conn.prepareStatement("UPSERT INTO " + tableName + " VALUES(?)");
        for (int i = 1; i <= nRows; i++) {
            stmt.setString(1, i + "");
            stmt.executeUpdate();
            if ((i % 2000) == 0) {
                conn.commit();
            }
        }
        conn.commit();
        conn.createStatement().execute("UPDATE STATISTICS " + tableName);
        PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
        MockParallelIteratorFactory parallelIteratorFactory = new MockParallelIteratorFactory();
        phxConn.setIteratorFactory(parallelIteratorFactory);
        ResultSet rs = stmt.executeQuery("SELECT * FROM " + tableName);
        StatementContext ctx = rs.unwrap(PhoenixResultSet.class).getContext();
        PTable table = ctx.getResolver().getTables().get(0).getTable();
        parallelIteratorFactory.setTable(table);
        PhoenixStatement pstmt = stmt.unwrap(PhoenixStatement.class);
        int numIterators = pstmt.getQueryPlan().getSplits().size();
        assertTrue(numIterators > 1);
        int numFetches = 2 * numIterators;
        List<String> iteratorOrder = new ArrayList<>(numFetches);
        for (int i = 1; i <= numFetches; i++) {
            rs.next();
            iteratorOrder.add(rs.getString(1));
        }
        /*
         * Because TableResultIterators are created in parallel in multiple threads, their relative order is not
         * deterministic. However, once the iterators are assigned to a RoundRobinResultIterator, the order in which
         * the next iterator is picked is deterministic - i1, i2, .. i7, i8, i1, i2, .. i7, i8, i1, i2, ..
         */
        for (int i = 0; i < numIterators; i++) {
            assertEquals(iteratorOrder.get(i), iteratorOrder.get(i + numIterators));
        }
    }
}