List of usage examples for java.sql PreparedStatement unwrap
<T> T unwrap(java.lang.Class<T> iface) throws java.sql.SQLException;
From source file:com.alibaba.druid.pool.bonecp.TestPSCache.java
public static MockPreparedStatement unwrap(PreparedStatement stmt) throws Exception { if (stmt instanceof NewProxyPreparedStatement) { Field field = NewProxyPreparedStatement.class.getDeclaredField("inner"); field.setAccessible(true);/*from ww w . j a v a2 s. c o m*/ return (MockPreparedStatement) field.get(stmt); } MockPreparedStatement mockStmt = stmt.unwrap(MockPreparedStatement.class); return mockStmt; }
From source file:com.github.ferstl.spring.jdbc.oracle.ParameterizedBatchingPreparedStatementCallback.java
@Override public int[][] doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException { OraclePreparedStatement ops = ps.unwrap(OraclePreparedStatement.class); List<int[]> rowCounts = new ArrayList<>(); for (int i = 0; i < this.batchArgs.size(); i += this.sendBatchSize) { int remainder = this.batchArgs.size() - i; int batchSize = remainder < this.sendBatchSize ? remainder : this.sendBatchSize; int[] rowCountsCurrentBatch = new int[batchSize]; rowCounts.add(rowCountsCurrentBatch); ops.setExecuteBatch(batchSize);/*from w w w .ja v a 2 s .c o m*/ List<T> batch = this.batchArgs.subList(i, i + batchSize); for (int j = 0; j < batchSize; j++) { this.ppss.setValues(ops, batch.get(j)); rowCountsCurrentBatch[j] = ops.executeUpdate(); } } return rowCounts.toArray(new int[rowCounts.size()][]); }
From source file:com.github.ferstl.spring.jdbc.oracle.BatchingPreparedStatementCallback.java
@Override public int[] doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException { OraclePreparedStatement ops = ps.unwrap(OraclePreparedStatement.class); int batchSize = this.pss.getBatchSize(); // Don't use an int[] array here because instances of InterruptibleBatchPreparedStatementSetter // might return Integer.MAX_VALUE as batch size. List<Integer> rowCounts = new ArrayList<>(); if (this.pss instanceof InterruptibleBatchPreparedStatementSetter) { InterruptibleBatchPreparedStatementSetter ipss = (InterruptibleBatchPreparedStatementSetter) this.pss; executeUpdate(ops, ipss, rowCounts); } else {/* ww w .j a v a2 s. co m*/ int sizeOfCompleteBatches = (batchSize / this.sendBatchSize) * this.sendBatchSize; int sizeOfLastBatch = batchSize % this.sendBatchSize; executeUpdate(ops, rowCounts, 0, sizeOfCompleteBatches); executeUpdate(ops, rowCounts, sizeOfCompleteBatches, sizeOfCompleteBatches + sizeOfLastBatch); } return toIntArray(rowCounts); }
From source file:com.github.ferstl.spring.jdbc.oracle.OracleNamedParameterJdbcTemplateTest.java
@Test public void repetition() throws SQLException { Map<String, Object> map = new HashMap<>(3); map.put("ten", 10); String sql = "SELECT 1 FROM dual WHERE 10 = :ten or 0 < :ten "; PreparedStatementCreator preparedStatementCreator = this.namedJdbcTemplate.getPreparedStatementCreator(sql, new MapSqlParameterSource(map)); Connection connection = mock(Connection.class); PreparedStatement preparedStatement = mock(PreparedStatement.class); OraclePreparedStatement oracleStatement = mock(OraclePreparedStatement.class); when(connection.prepareStatement(sql)).thenReturn(preparedStatement); when(preparedStatement.unwrap(OraclePreparedStatement.class)).thenReturn(oracleStatement); preparedStatementCreator.createPreparedStatement(connection); verify(oracleStatement).setObjectAtName("ten", 10); }
From source file:com.github.ferstl.spring.jdbc.oracle.OracleNamedParameterJdbcTemplateTest.java
@Test public void endingNoSpace() throws SQLException { Map<String, Object> map = new HashMap<>(3); map.put("ten", 10); map.put("twenty", 20); String sql = "SELECT 1 FROM dual WHERE 1 = :ten or 20 = :twenty"; PreparedStatementCreator preparedStatementCreator = this.namedJdbcTemplate.getPreparedStatementCreator(sql, new MapSqlParameterSource(map)); Connection connection = mock(Connection.class); PreparedStatement preparedStatement = mock(PreparedStatement.class); OraclePreparedStatement oracleStatement = mock(OraclePreparedStatement.class); when(connection.prepareStatement(sql)).thenReturn(preparedStatement); when(preparedStatement.unwrap(OraclePreparedStatement.class)).thenReturn(oracleStatement); preparedStatementCreator.createPreparedStatement(connection); verify(oracleStatement).setObjectAtName("ten", 10); verify(oracleStatement).setObjectAtName("twenty", 20); }
From source file:com.github.ferstl.spring.jdbc.oracle.OracleNamedParameterJdbcTemplateTest.java
@Test public void setNullNoType() throws SQLException { Map<String, Object> map = new HashMap<>(2); map.put("ten", 10); map.put("twenty", null); String sql = "SELECT 1 FROM dual WHERE 1 = :ten or 20 = :twenty"; PreparedStatementCreator preparedStatementCreator = this.namedJdbcTemplate.getPreparedStatementCreator(sql, new MapSqlParameterSource(map)); Connection connection = mock(Connection.class); PreparedStatement preparedStatement = mock(PreparedStatement.class); OraclePreparedStatement oracleStatement = mock(OraclePreparedStatement.class); when(connection.prepareStatement(sql)).thenReturn(preparedStatement); when(preparedStatement.unwrap(OraclePreparedStatement.class)).thenReturn(oracleStatement); preparedStatementCreator.createPreparedStatement(connection); verify(oracleStatement).setObjectAtName("ten", 10); verify(oracleStatement).setNullAtName("twenty", Types.NULL); }
From source file:com.github.ferstl.spring.jdbc.oracle.OracleNamedParameterJdbcTemplateTest.java
@Test public void setWithType() throws SQLException { MapSqlParameterSource source = new MapSqlParameterSource(new HashMap<String, Object>(2)); source.addValue("ten", 10, Types.NUMERIC); source.addValue("twenty", null, Types.VARCHAR); String sql = "SELECT 1 FROM dual WHERE 1 = :ten or 20 = :twenty"; PreparedStatementCreator preparedStatementCreator = this.namedJdbcTemplate.getPreparedStatementCreator(sql, source);/*from w w w . j ava2 s. c o m*/ Connection connection = mock(Connection.class); PreparedStatement preparedStatement = mock(PreparedStatement.class); OraclePreparedStatement oracleStatement = mock(OraclePreparedStatement.class); when(connection.prepareStatement(sql)).thenReturn(preparedStatement); when(preparedStatement.unwrap(OraclePreparedStatement.class)).thenReturn(oracleStatement); preparedStatementCreator.createPreparedStatement(connection); verify(oracleStatement).setObjectAtName("ten", 10, Types.NUMERIC); verify(oracleStatement).setNullAtName("twenty", Types.VARCHAR); }
From source file:com.github.ferstl.spring.jdbc.oracle.OracleNamedParameterJdbcTemplateTest.java
@Test public void endingWithSpace() throws SQLException { Map<String, Object> map = new HashMap<>(3); map.put("ten", 10); map.put("twenty", 20); String sql = "SELECT 1 FROM dual WHERE 10 = :ten or 20 = :twenty "; PreparedStatementCreator preparedStatementCreator = this.namedJdbcTemplate.getPreparedStatementCreator(sql, new MapSqlParameterSource(map)); Connection connection = mock(Connection.class); PreparedStatement preparedStatement = mock(PreparedStatement.class); OraclePreparedStatement oracleStatement = mock(OraclePreparedStatement.class); when(connection.prepareStatement(sql)).thenReturn(preparedStatement); when(preparedStatement.unwrap(OraclePreparedStatement.class)).thenReturn(oracleStatement); preparedStatementCreator.createPreparedStatement(connection); verify(oracleStatement).setObjectAtName("ten", 10); verify(oracleStatement).setObjectAtName("twenty", 20); }
From source file:com.github.ferstl.spring.jdbc.oracle.OracleNamedParameterJdbcTemplateTest.java
@Test public void commonPrefix() throws SQLException { Map<String, Object> map = new HashMap<>(3); map.put("arg", 10); map.put("arg2", 20); String sql = "SELECT 1 FROM dual WHERE 10 = :arg or 20 = :arg2"; PreparedStatementCreator preparedStatementCreator = this.namedJdbcTemplate.getPreparedStatementCreator(sql, new MapSqlParameterSource(map)); Connection connection = mock(Connection.class); PreparedStatement preparedStatement = mock(PreparedStatement.class); OraclePreparedStatement oracleStatement = mock(OraclePreparedStatement.class); when(connection.prepareStatement(sql)).thenReturn(preparedStatement); when(preparedStatement.unwrap(OraclePreparedStatement.class)).thenReturn(oracleStatement); preparedStatementCreator.createPreparedStatement(connection); verify(oracleStatement).setObjectAtName("arg", 10); verify(oracleStatement).setObjectAtName("arg2", 20); }
From source file:org.apache.phoenix.util.PhoenixRuntime.java
/** * Returns the opitmized query plan used by phoenix for executing the sql. * @param stmt to return the plan for//from ww w . j a v a 2 s .c o m * @throws SQLException */ public static QueryPlan getOptimizedQueryPlan(PreparedStatement stmt) throws SQLException { checkNotNull(stmt); QueryPlan plan = stmt.unwrap(PhoenixPreparedStatement.class).optimizeQuery(); return plan; }