/** * 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); } }
/** * 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); } }
/** * Return a long value from a prepared query. */ public long queryForLong(DatabaseConnection databaseConnection, PreparedStmt<T> preparedStmt) throws SQLException { CompiledStatement compiledStatement = preparedStmt.compile(databaseConnection, StatementType.SELECT_LONG); DatabaseResults results = null; try { results = compiledStatement.runQuery(null); if (results.first()) { return results.getLong(0); } else { throw new SQLException("No result found in queryForLong: " + preparedStmt.getStatement()); } } finally { IOUtils.closeThrowSqlException(results, "results"); IOUtils.closeThrowSqlException(compiledStatement, "compiled statement"); } }
/** * Return a long value from a prepared query. */ public long queryForLong(DatabaseConnection databaseConnection, PreparedStmt<T> preparedStmt) throws SQLException { CompiledStatement compiledStatement = preparedStmt.compile(databaseConnection, StatementType.SELECT_LONG); DatabaseResults results = null; try { results = compiledStatement.runQuery(null); if (results.first()) { return results.getLong(0); } else { throw new SQLException("No result found in queryForLong: " + preparedStmt.getStatement()); } } finally { IOUtils.closeThrowSqlException(results, "results"); IOUtils.closeThrowSqlException(compiledStatement, "compiled statement"); } }
@Test(expected = IllegalStateException.class) public void testNextThrow() throws Exception { ConnectionSource cs = createMock(ConnectionSource.class); cs.releaseConnection(null); CompiledStatement stmt = createMock(CompiledStatement.class); DatabaseResults results = createMock(DatabaseResults.class); expect(stmt.runQuery(null)).andReturn(results); expect(results.first()).andThrow(new SQLException("some result problem")); @SuppressWarnings("unchecked") GenericRowMapper<Foo> mapper = (GenericRowMapper<Foo>) createMock(GenericRowMapper.class); stmt.close(); replay(stmt, mapper, cs, results); SelectIterator<Foo, Integer> iterator = new SelectIterator<Foo, Integer>(Foo.class, null, mapper, cs, null, stmt, "statement", null); try { iterator.hasNext(); } finally { iterator.close(); } verify(stmt, mapper, cs, results); }
@Test(expected = IllegalStateException.class) public void testHasNextThrow() throws Exception { ConnectionSource cs = createMock(ConnectionSource.class); cs.releaseConnection(null); CompiledStatement stmt = createMock(CompiledStatement.class); DatabaseResults results = createMock(DatabaseResults.class); expect(stmt.runQuery(null)).andReturn(results); expect(results.first()).andThrow(new SQLException("some database problem")); stmt.close(); replay(stmt, results, cs); SelectIterator<Foo, Integer> iterator = new SelectIterator<Foo, Integer>(Foo.class, null, null, cs, null, stmt, "statement", null); try { iterator.hasNext(); } finally { iterator.close(); } }
/** * Return a long from a raw query with String[] arguments. */ public long queryForLong(DatabaseConnection databaseConnection, String query, String[] arguments) throws SQLException { logger.debug("executing raw query for long: {}", query); if (arguments.length > 0) { // need to do the (Object) cast to force args to be a single object logger.trace("query arguments: {}", (Object) arguments); } CompiledStatement compiledStatement = null; DatabaseResults results = null; try { compiledStatement = databaseConnection.compileStatement(query, StatementType.SELECT, noFieldTypes, DatabaseConnection.DEFAULT_RESULT_FLAGS, false); assignStatementArguments(compiledStatement, arguments); results = compiledStatement.runQuery(null); if (results.first()) { return results.getLong(0); } else { throw new SQLException("No result found in queryForLong: " + query); } } finally { IOUtils.closeThrowSqlException(results, "results"); IOUtils.closeThrowSqlException(compiledStatement, "compiled statement"); } }
/** * Return a long from a raw query with String[] arguments. */ public long queryForLong(DatabaseConnection databaseConnection, String query, String[] arguments) throws SQLException { logger.debug("executing raw query for long: {}", query); if (arguments.length > 0) { // need to do the (Object) cast to force args to be a single object logger.trace("query arguments: {}", (Object) arguments); } CompiledStatement compiledStatement = null; DatabaseResults results = null; try { compiledStatement = databaseConnection.compileStatement(query, StatementType.SELECT, noFieldTypes, DatabaseConnection.DEFAULT_RESULT_FLAGS, false); assignStatementArguments(compiledStatement, arguments); results = compiledStatement.runQuery(null); if (results.first()) { return results.getLong(0); } else { throw new SQLException("No result found in queryForLong: " + query); } } finally { IOUtils.closeThrowSqlException(results, "results"); IOUtils.closeThrowSqlException(compiledStatement, "compiled statement"); } }
@Test(expected = IllegalStateException.class) public void testRemoveThrow() throws Exception { ConnectionSource cs = createMock(ConnectionSource.class); cs.releaseConnection(null); CompiledStatement stmt = createMock(CompiledStatement.class); DatabaseResults results = createMock(DatabaseResults.class); expect(results.first()).andReturn(true); expect(stmt.runQuery(null)).andReturn(results); @SuppressWarnings("unchecked") GenericRowMapper<Foo> mapper = (GenericRowMapper<Foo>) createMock(GenericRowMapper.class); Foo foo = new Foo(); expect(mapper.mapRow(results)).andReturn(foo); @SuppressWarnings("unchecked") Dao<Foo, Integer> dao = (Dao<Foo, Integer>) createMock(Dao.class); expect(dao.delete(foo)).andThrow(new SQLException("some dao problem")); stmt.close(); replay(stmt, dao, results, mapper, cs); SelectIterator<Foo, Integer> iterator = new SelectIterator<Foo, Integer>(Foo.class, dao, mapper, cs, null, stmt, "statement", null); try { iterator.hasNext(); iterator.next(); iterator.remove(); } finally { iterator.close(); } }
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, StatementType.SELECT, noFieldTypes, DatabaseConnection.DEFAULT_RESULT_FLAGS, false); // 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 IOUtils.closeThrowSqlException(compiledStmt, "compiled statement"); } stmtC++; } return stmtC; }
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, StatementType.SELECT, noFieldTypes, DatabaseConnection.DEFAULT_RESULT_FLAGS, false); // 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 IOUtils.closeThrowSqlException(compiledStmt, "compiled statement"); } stmtC++; } return stmtC; }
/** * 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"); } }
/** * 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"); } }
private void checkResults(List<LocalFoo> foos, MappedPreparedStmt<LocalFoo, Integer> preparedQuery, int expectedNum) throws SQLException { DatabaseConnection conn = connectionSource.getReadOnlyConnection(TABLE_NAME); CompiledStatement stmt = null; try { stmt = preparedQuery.compile(conn, StatementType.SELECT); DatabaseResults results = stmt.runQuery(null); int fooC = 0; while (results.next()) { LocalFoo foo2 = preparedQuery.mapRow(results); assertEquals(foos.get(fooC).id, foo2.id); fooC++; } assertEquals(expectedNum, fooC); } finally { IOUtils.closeThrowSqlException(stmt, "compiled statement"); connectionSource.releaseConnection(conn); } }
@Test(expected = SQLException.class) public void testSerializableInvalidResult() throws Exception { Class<LocalByteArray> clazz = LocalByteArray.class; Dao<LocalByteArray, Object> dao = createDao(clazz, true); LocalByteArray foo = new LocalByteArray(); foo.byteField = new byte[] { 1, 2, 3, 4, 5 }; assertEquals(1, dao.create(foo)); DatabaseConnection conn = connectionSource.getReadOnlyConnection(TABLE_NAME); CompiledStatement stmt = null; try { stmt = conn.compileStatement("select * from " + TABLE_NAME, StatementType.SELECT, noFieldTypes, DatabaseConnection.DEFAULT_RESULT_FLAGS, true); DatabaseResults results = stmt.runQuery(null); assertTrue(results.next()); FieldType fieldType = FieldType.createFieldType(databaseType, TABLE_NAME, LocalSerializable.class.getDeclaredField(SERIALIZABLE_COLUMN), LocalSerializable.class); DataType.SERIALIZABLE.getDataPersister().resultToJava(fieldType, results, results.findColumn(BYTE_COLUMN)); } finally { if (stmt != null) { stmt.close(); } connectionSource.releaseConnection(conn); } }
@Test public void testSerializableNoValue() throws Exception { Class<LocalSerializable> clazz = LocalSerializable.class; Dao<LocalSerializable, Object> dao = createDao(clazz, true); LocalSerializable foo = new LocalSerializable(); foo.serializable = null; assertEquals(1, dao.create(foo)); DatabaseConnection conn = connectionSource.getReadOnlyConnection(TABLE_NAME); CompiledStatement stmt = null; try { stmt = conn.compileStatement("select * from " + TABLE_NAME, StatementType.SELECT, noFieldTypes, DatabaseConnection.DEFAULT_RESULT_FLAGS, true); DatabaseResults results = stmt.runQuery(null); assertTrue(results.next()); FieldType fieldType = FieldType.createFieldType(databaseType, TABLE_NAME, clazz.getDeclaredField(SERIALIZABLE_COLUMN), clazz); assertNull(DataType.SERIALIZABLE.getDataPersister().resultToJava(fieldType, results, results.findColumn(SERIALIZABLE_COLUMN))); } finally { if (stmt != null) { stmt.close(); } connectionSource.releaseConnection(conn); } }
@Test(expected = SQLException.class) public void testDateStringResultInvalid() throws Exception { Class<LocalString> clazz = LocalString.class; Dao<LocalString, Object> dao = createDao(clazz, true); LocalString foo = new LocalString(); foo.string = "not a date format"; assertEquals(1, dao.create(foo)); DatabaseConnection conn = connectionSource.getReadOnlyConnection(FOO_TABLE_NAME); CompiledStatement stmt = null; try { stmt = conn.compileStatement("select * from " + TABLE_NAME, StatementType.SELECT, noFieldTypes, DatabaseConnection.DEFAULT_RESULT_FLAGS, true); DatabaseResults results = stmt.runQuery(null); assertTrue(results.next()); int colNum = results.findColumn(STRING_COLUMN); DataType.DATE_STRING.getDataPersister().resultToJava(null, results, colNum); } finally { if (stmt != null) { stmt.close(); } connectionSource.releaseConnection(conn); } }
@Test public void testMapRow() throws Exception { Dao<LocalFoo, Integer> fooDao = createDao(LocalFoo.class, true); LocalFoo foo1 = new LocalFoo(); fooDao.create(foo1); TableInfo<LocalFoo, Integer> tableInfo = new TableInfo<LocalFoo, Integer>(databaseType, LocalFoo.class); MappedPreparedStmt<LocalFoo, Integer> rowMapper = new MappedPreparedStmt<LocalFoo, Integer>(fooDao, tableInfo, null, new FieldType[0], tableInfo.getFieldTypes(), new ArgumentHolder[0], null, StatementType.SELECT, false); DatabaseConnection conn = connectionSource.getReadOnlyConnection(TABLE_NAME); CompiledStatement stmt = null; try { stmt = conn.compileStatement("select * from " + TABLE_NAME, StatementType.SELECT, new FieldType[0], DatabaseConnection.DEFAULT_RESULT_FLAGS, true); DatabaseResults results = stmt.runQuery(null); while (results.next()) { LocalFoo foo2 = rowMapper.mapRow(results); assertEquals(foo1.id, foo2.id); } } finally { if (stmt != null) { stmt.close(); } connectionSource.releaseConnection(conn); } }
@Test public void testEnumStringResultsNoFieldType() throws Exception { Dao<LocalEnumString, Object> dao = createDao(LocalEnumString.class, true); OurEnum val = OurEnum.SECOND; LocalEnumString foo = new LocalEnumString(); foo.ourEnum = val; assertEquals(1, dao.create(foo)); DatabaseConnection conn = connectionSource.getReadOnlyConnection(TABLE_NAME); CompiledStatement stmt = null; try { stmt = conn.compileStatement("select * from " + TABLE_NAME, StatementType.SELECT, noFieldTypes, DatabaseConnection.DEFAULT_RESULT_FLAGS, true); DatabaseResults results = stmt.runQuery(null); assertTrue(results.next()); assertEquals(val.toString(), DataType.ENUM_STRING.getDataPersister().resultToJava(null, results, results.findColumn(ENUM_COLUMN))); } finally { if (stmt != null) { stmt.close(); } connectionSource.releaseConnection(conn); } }
@Test public void testEnumIntResultsNoFieldType() throws Exception { Class<LocalEnumInt> clazz = LocalEnumInt.class; Dao<LocalEnumInt, Object> dao = createDao(clazz, true); OurEnum val = OurEnum.SECOND; LocalEnumInt foo = new LocalEnumInt(); foo.ourEnum = val; assertEquals(1, dao.create(foo)); DatabaseConnection conn = connectionSource.getReadOnlyConnection(FOO_TABLE_NAME); CompiledStatement stmt = null; try { stmt = conn.compileStatement("select * from " + TABLE_NAME, StatementType.SELECT, noFieldTypes, DatabaseConnection.DEFAULT_RESULT_FLAGS, true); DatabaseResults results = stmt.runQuery(null); assertTrue(results.next()); assertEquals(val.ordinal(), DataType.ENUM_INTEGER.getDataPersister().resultToJava(null, results, results.findColumn(ENUM_COLUMN))); } finally { if (stmt != null) { stmt.close(); } connectionSource.releaseConnection(conn); } }