PhoenixPreparedStatement stmt = null; try { stmt = new PhoenixPreparedStatement(this, parser); ParameterMetaData paramMetaData = stmt .getParameterMetaData(); for (int i = 0; i < paramMetaData.getParameterCount(); i++) { stmt.setObject(i + 1, binds.get(bindsOffset + i)); boolean isQuery = stmt.execute(); if (isQuery) { ResultSet rs = stmt.getResultSet(); if (!rs.next()) { if (out != null) { int updateCount = stmt.getUpdateCount(); if (updateCount >= 0) { out.println((updateCount == 0 ? "no" : updateCount) + (updateCount == 1 ? " row " : " rows ") + stmt.getUpdateOperation().toString()); } finally { if (stmt != null) { stmt.close();
private static QueryPlan compileStatement(String query, List<Object> binds) throws SQLException { PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class); PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query); TestUtil.bindParams(pstmt, binds); return pstmt.compileQuery(); }
/** * Returns the opitmized query plan used by phoenix for executing the sql. * @param stmt to return the plan for * @throws SQLException */ public static QueryPlan getOptimizedQueryPlan(PreparedStatement stmt) throws SQLException { checkNotNull(stmt); QueryPlan plan = stmt.unwrap(PhoenixPreparedStatement.class).optimizeQuery(); return plan; }
public QueryPlan optimizeQuery() throws SQLException { throwIfUnboundParameters(); return optimizeQuery(statement); }
@Override public void addBatch() throws SQLException { throwIfUnboundParameters(); batch.add(new PhoenixPreparedStatement(this)); }
@Override public ResultSet executeQuery() throws SQLException { throwIfUnboundParameters(); if (statement.getOperation().isMutation()) { throw new ExecuteQueryNotApplicableException(statement.getOperation()); } return executeQuery(statement,createQueryLogger(statement,query)); }
boolean execute(boolean batched) throws SQLException { throwIfUnboundParameters(); if (!batched && statement.getOperation().isMutation() && !batch.isEmpty()) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.EXECUTE_UPDATE_WITH_NON_EMPTY_BATCH) .build().buildException(); } if (statement.getOperation().isMutation()) { executeMutation(statement); return false; } executeQuery(statement, createQueryLogger(statement,query)); return true; }
/** * Execute the current batch of statements. If any exception occurs * during execution, a {@link org.apache.phoenix.exception.BatchUpdateException} * is thrown which includes the index of the statement within the * batch when the exception occurred. */ @Override public int[] executeBatch() throws SQLException { int i = 0; try { int[] returnCodes = new int [batch.size()]; for (i = 0; i < returnCodes.length; i++) { PhoenixPreparedStatement statement = batch.get(i); returnCodes[i] = statement.execute(true) ? Statement.SUCCESS_NO_INFO : statement.getUpdateCount(); } // Flush all changes in batch if auto flush is true flushIfNecessary(); // If we make it all the way through, clear the batch clearBatch(); return returnCodes; } catch (Throwable t) { throw new BatchUpdateExecution(t,i); } }
@Override public void addBatch(String sql) throws SQLException { batch.add(new PhoenixPreparedStatement(connection, sql)); }
public static List<KeyRange> getSplits(Connection conn, String tableName, String pkCol, byte[] lowerRange, byte[] upperRange, String whereClauseSuffix, String selectClause) throws SQLException { String whereClauseStart = (lowerRange == null && upperRange == null ? "" : " WHERE " + ((lowerRange != null ? (pkCol + " >= ? " + (upperRange != null ? " AND " : "")) : "") + (upperRange != null ? (pkCol + " < ?") : "" ))); String whereClause = whereClauseSuffix == null ? whereClauseStart : whereClauseStart.length() == 0 ? (" WHERE " + whereClauseSuffix) : (" AND " + whereClauseSuffix); String query = "SELECT /*+ NO_INDEX */ "+selectClause+" FROM " + tableName + whereClause; PhoenixPreparedStatement pstmt = conn.prepareStatement(query).unwrap(PhoenixPreparedStatement.class); if (lowerRange != null) { pstmt.setBytes(1, lowerRange); } if (upperRange != null) { pstmt.setBytes(lowerRange != null ? 2 : 1, upperRange); } pstmt.execute(); List<KeyRange> keyRanges = pstmt.getQueryPlan().getSplits(); return keyRanges; }
@Override public int executeUpdate() throws SQLException { throwIfUnboundParameters(); if (!statement.getOperation().isMutation()) { throw new ExecuteUpdateNotApplicableException(statement.getOperation()); } if (!batch.isEmpty()) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.EXECUTE_UPDATE_WITH_NON_EMPTY_BATCH) .build().buildException(); } return executeMutation(statement); }
private QueryPlan getQueryPlan(String query, List<Object> binds) throws SQLException { Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES); Connection conn = DriverManager.getConnection(getUrl(), props); try { PhoenixPreparedStatement statement = conn.prepareStatement(query).unwrap(PhoenixPreparedStatement.class); for (Object bind : binds) { statement.setObject(1, bind); } QueryPlan plan = statement.compileQuery(query); return plan; } finally { conn.close(); } }
private QueryPlan getOptimizedQueryPlan(String query, List<Object> binds) throws SQLException { Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES); Connection conn = DriverManager.getConnection(getUrl(), props); try { PhoenixPreparedStatement statement = conn.prepareStatement(query).unwrap(PhoenixPreparedStatement.class); for (Object bind : binds) { statement.setObject(1, bind); } QueryPlan plan = statement.optimizeQuery(query); return plan; } finally { conn.close(); } }
@Override public void setObject(int parameterIndex, Object x, int targetSqlType, int scaleOrLength) throws SQLException { setObject(parameterIndex, x, targetSqlType); }
public MutationPlan compileMutation() throws SQLException { return compileMutation(statement, query); }
@Test public void testScanCaching_CustomFetchSizeOnStatement() throws SQLException { String query = "select * from atable where a_integer=0"; PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class); PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query); final int FETCH_SIZE = 25; pstmt.setFetchSize(FETCH_SIZE); QueryPlan plan = pstmt.optimizeQuery(); Scan scan = plan.getContext().getScan(); assertEquals(FETCH_SIZE, pstmt.getFetchSize()); assertEquals(FETCH_SIZE, scan.getCaching()); } }
@Override public boolean execute() throws SQLException { return execute(false); }
public QueryPlan compileQuery() throws SQLException { return compileQuery(statement, query); }
@Test public void testScanCaching_Default() throws SQLException { String query = "select * from atable where a_integer=0"; PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class); PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query); QueryPlan plan = pstmt.optimizeQuery(); Scan scan = plan.getContext().getScan(); Configuration config = HBaseConfiguration.create(); int defaultScannerCacheSize = config.getInt(HConstants.HBASE_CLIENT_SCANNER_CACHING, HConstants.DEFAULT_HBASE_CLIENT_SCANNER_CACHING); assertEquals(defaultScannerCacheSize, pstmt.getFetchSize()); assertEquals(defaultScannerCacheSize, scan.getCaching()); }
boolean execute(boolean batched) throws SQLException { throwIfUnboundParameters(); if (!batched && statement.getOperation().isMutation() && !batch.isEmpty()) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.EXECUTE_UPDATE_WITH_NON_EMPTY_BATCH) .build().buildException(); } if (statement.getOperation().isMutation()) { executeMutation(statement); return false; } executeQuery(statement, createQueryLogger(statement,query)); return true; }