private static int doCreateTestQueries(DatabaseConnection connection, DatabaseType databaseType, List<String> queriesAfter) throws SQLException { int stmtC = 0; // now execute any test queries which test the newly created table for (String query : queriesAfter) { CompiledStatement compiledStmt = null; try { compiledStmt = connection.compileStatement(query, StatementBuilder.StatementType.SELECT, noFieldTypes); // we don't care about an object cache here DatabaseResults results = compiledStmt.runQuery(null); int rowC = 0; // count the results for (boolean isThereMore = results.first(); isThereMore; isThereMore = results.next()) { rowC++; } logger.info("executing create table after-query got {} results: {}", rowC, query); } catch (SQLException e) { // we do this to make sure that the statement is in the exception throw SqlExceptionUtil.create("executing create table after-query failed: " + query, e); } finally { // result set is closed by the statement being closed if (compiledStmt != null) { compiledStmt.close(); } } stmtC++; } return stmtC; }
try { compiledStmt = connection.compileStatement(statement, StatementBuilder.StatementType.EXECUTE, noFieldTypes); rowC = compiledStmt.runExecute(); logger.info("executed {} table statement changed {} rows: {}", label, rowC, statement); } catch (SQLException e) { compiledStmt.close();
/** * Delete rows that match the prepared statement. */ public int delete(DatabaseConnection databaseConnection, PreparedDelete<T> preparedDelete) throws SQLException { CompiledStatement compiledStatement = preparedDelete.compile(databaseConnection, StatementType.DELETE); try { int result = compiledStatement.runUpdate(); if (dao != null && !localIsInBatchMode.get()) { dao.notifyChanges(); } return result; } finally { IOUtils.closeThrowSqlException(compiledStatement, "compiled statement"); } }
expect(conn.compileStatement(isA(String.class), isA(StatementType.class), isA(FieldType[].class), anyInt(), anyBoolean())).andReturn(stmt); expect(stmt.runExecute()).andReturn(rowN); stmt.close(); if (queryAfter != null) { expect(conn.compileStatement(isA(String.class), isA(StatementType.class), isA(FieldType[].class), results = createMock(DatabaseResults.class); expect(results.first()).andReturn(false); expect(stmt.runQuery(null)).andReturn(results); stmt.close(); replay(results); rowC.incrementAndGet();
/** * If the statement parameter is null then this won't log information */ public SelectIterator(Class<?> dataClass, Dao<T, ID> classDao, GenericRowMapper<T> rowMapper, ConnectionSource connectionSource, DatabaseConnection connection, CompiledStatement compiledStmt, String statement, ObjectCache objectCache) throws SQLException { this.dataClass = dataClass; this.classDao = classDao; this.rowMapper = rowMapper; this.connectionSource = connectionSource; this.connection = connection; this.compiledStmt = compiledStmt; this.results = compiledStmt.runQuery(objectCache); this.statement = statement; if (statement != null) { logger.debug("starting iterator @{} for '{}'", hashCode(), statement); } }
private static <T> int clearTable(ConnectionSource connectionSource, String tableName) throws SQLException { DatabaseType databaseType = connectionSource.getDatabaseType(); StringBuilder sb = new StringBuilder(48); if (databaseType.isTruncateSupported()) { sb.append("TRUNCATE TABLE "); } else { sb.append("DELETE FROM "); } databaseType.appendEscapedEntityName(sb, tableName); String statement = sb.toString(); logger.info("clearing table '{}' with '{}", tableName, statement); CompiledStatement compiledStmt = null; DatabaseConnection connection = connectionSource.getReadWriteConnection(tableName); try { compiledStmt = connection.compileStatement(statement, StatementType.EXECUTE, noFieldTypes, DatabaseConnection.DEFAULT_RESULT_FLAGS, false); return compiledStmt.runExecute(); } finally { IOUtils.closeThrowSqlException(compiledStmt, "compiled statement"); connectionSource.releaseConnection(connection); } }
@Test public void testDeleteThrow() throws Exception { TableInfo<Foo, String> tableInfo = new TableInfo<Foo, String>(databaseType, Foo.class); DatabaseConnection connection = createMock(DatabaseConnection.class); @SuppressWarnings("unchecked") PreparedDelete<Foo> delete = createMock(PreparedDelete.class); CompiledStatement compiledStmt = createMock(CompiledStatement.class); expect(delete.compile(connection, StatementType.DELETE)).andReturn(compiledStmt); expect(compiledStmt.runUpdate()).andThrow(new SQLException("expected")); compiledStmt.close(); StatementExecutor<Foo, String> statementExec = new StatementExecutor<Foo, String>(databaseType, tableInfo, null); replay(connection, compiledStmt, delete); try { statementExec.delete(connection, delete); fail("should have thrown"); } catch (SQLException e) { // expected } verify(connection, compiledStmt, delete); }
/** * Return the first object that matches the {@link PreparedStmt} or null if none. */ public T queryForFirst(DatabaseConnection databaseConnection, PreparedStmt<T> preparedStmt, ObjectCache objectCache) throws SQLException { CompiledStatement compiledStatement = preparedStmt.compile(databaseConnection, StatementType.SELECT); DatabaseResults results = null; try { compiledStatement.setMaxRows(1); results = compiledStatement.runQuery(objectCache); if (results.first()) { logger.debug("query-for-first of '{}' returned at least 1 result", preparedStmt.getStatement()); return preparedStmt.mapRow(results); } else { logger.debug("query-for-first of '{}' returned at 0 results", preparedStmt.getStatement()); return null; } } finally { IOUtils.closeThrowSqlException(results, "results"); IOUtils.closeThrowSqlException(compiledStatement, "compiled statement"); } }
@Override public void close() throws IOException { if (!closed) { compiledStmt.close(); closed = true; last = null; if (statement != null) { logger.debug("closed iterator @{} after {} rows", hashCode(), rowC); } try { connectionSource.releaseConnection(connection); } catch (SQLException e) { throw new IOException("could not release connection", e); } } }
if (limit != null) { stmt.setMaxRows(limit.intValue()); sqlType = fieldType.getSqlType(); stmt.setObject(i, argValue, sqlType); if (argValues != null) { argValues[i] = argValue;
private void assignStatementArguments(CompiledStatement compiledStatement, String[] arguments) throws SQLException { for (int i = 0; i < arguments.length; i++) { compiledStatement.setObject(i, arguments[i], SqlType.STRING); } }
/** * If the statement parameter is null then this won't log information */ public SelectIterator(Class<?> dataClass, Dao<T, ID> classDao, GenericRowMapper<T> rowMapper, ConnectionSource connectionSource, DatabaseConnection connection, CompiledStatement compiledStmt, String statement, ObjectCache objectCache) throws SQLException { this.dataClass = dataClass; this.classDao = classDao; this.rowMapper = rowMapper; this.connectionSource = connectionSource; this.connection = connection; this.compiledStmt = compiledStmt; this.results = compiledStmt.runQuery(objectCache); this.statement = statement; if (statement != null) { logger.debug("starting iterator @{} for '{}'", hashCode(), statement); } }
private static <T> int clearTable(ConnectionSource connectionSource, String tableName) throws SQLException { DatabaseType databaseType = connectionSource.getDatabaseType(); StringBuilder sb = new StringBuilder(48); if (databaseType.isTruncateSupported()) { sb.append("TRUNCATE TABLE "); } else { sb.append("DELETE FROM "); } databaseType.appendEscapedEntityName(sb, tableName); String statement = sb.toString(); logger.info("clearing table '{}' with '{}", tableName, statement); CompiledStatement compiledStmt = null; DatabaseConnection connection = connectionSource.getReadWriteConnection(tableName); try { compiledStmt = connection.compileStatement(statement, StatementType.EXECUTE, noFieldTypes, DatabaseConnection.DEFAULT_RESULT_FLAGS, false); return compiledStmt.runExecute(); } finally { IOUtils.closeThrowSqlException(compiledStmt, "compiled statement"); connectionSource.releaseConnection(connection); } }
@Test public void testUpdateThrow() throws Exception { TableInfo<Foo, String> tableInfo = new TableInfo<Foo, String>(databaseType, Foo.class); DatabaseConnection connection = createMock(DatabaseConnection.class); @SuppressWarnings("unchecked") PreparedUpdate<Foo> update = createMock(PreparedUpdate.class); CompiledStatement compiledStmt = createMock(CompiledStatement.class); expect(update.compile(connection, StatementType.UPDATE)).andReturn(compiledStmt); expect(compiledStmt.runUpdate()).andThrow(new SQLException("expected")); compiledStmt.close(); StatementExecutor<Foo, String> statementExec = new StatementExecutor<Foo, String>(databaseType, tableInfo, null); replay(connection, compiledStmt, update); try { statementExec.update(connection, update); fail("should have thrown"); } catch (SQLException e) { // expected } verify(connection, compiledStmt, update); }
/** * Return the first object that matches the {@link PreparedStmt} or null if none. */ public T queryForFirst(DatabaseConnection databaseConnection, PreparedStmt<T> preparedStmt, ObjectCache objectCache) throws SQLException { CompiledStatement compiledStatement = preparedStmt.compile(databaseConnection, StatementType.SELECT); DatabaseResults results = null; try { compiledStatement.setMaxRows(1); results = compiledStatement.runQuery(objectCache); if (results.first()) { logger.debug("query-for-first of '{}' returned at least 1 result", preparedStmt.getStatement()); return preparedStmt.mapRow(results); } else { logger.debug("query-for-first of '{}' returned 0 results", preparedStmt.getStatement()); return null; } } finally { IOUtils.closeThrowSqlException(results, "results"); IOUtils.closeThrowSqlException(compiledStatement, "compiled statement"); } }
@Override public void close() throws IOException { if (!closed) { compiledStmt.close(); closed = true; last = null; if (statement != null) { logger.debug("closed iterator @{} after {} rows", hashCode(), rowC); } try { connectionSource.releaseConnection(connection); } catch (SQLException e) { throw new IOException("could not release connection", e); } } }
if (limit != null) { stmt.setMaxRows(limit.intValue()); sqlType = fieldType.getSqlType(); stmt.setObject(i, argValue, sqlType); if (argValues != null) { argValues[i] = argValue;
private void assignStatementArguments(CompiledStatement compiledStatement, String[] arguments) throws SQLException { for (int i = 0; i < arguments.length; i++) { compiledStatement.setObject(i, arguments[i], SqlType.STRING); } }
private static int doCreateTestQueries(DatabaseConnection connection, DatabaseType databaseType, List<String> queriesAfter) throws SQLException { int stmtC = 0; // now execute any test queries which test the newly created table for (String query : queriesAfter) { CompiledStatement compiledStmt = null; try { compiledStmt = connection.compileStatement(query, StatementBuilder.StatementType.SELECT, noFieldTypes); // we don't care about an object cache here DatabaseResults results = compiledStmt.runQuery(null); int rowC = 0; // count the results for (boolean isThereMore = results.first(); isThereMore; isThereMore = results.next()) { rowC++; } logger.debug("executing create table after-query got {} results: {}", rowC, query); } catch (SQLException e) { // we do this to make sure that the statement is in the exception throw SqlExceptionUtil.create("executing create table after-query failed: " + query, e); } finally { // result set is closed by the statement being closed if (compiledStmt != null) { compiledStmt.close(); } } stmtC++; } return stmtC; }
try { compiledStmt = connection.compileStatement(statement, StatementBuilder.StatementType.EXECUTE, noFieldTypes); rowC = compiledStmt.runExecute(); logger.debug("executed {} table statement changed {} rows: {}", label, rowC, statement); } catch (SQLException e) { compiledStmt.close();