@Override public CompiledStatement compileStatement(String statement, StatementType type, FieldType[] argFieldTypes, int resultFlags, boolean cacheStore) throws SQLException { if (proxy == null) { return null; } else { return proxy.compileStatement(statement, type, argFieldTypes, resultFlags, cacheStore); } }
@Override public CompiledStatement compileStatement(String statement, StatementType type, FieldType[] argFieldTypes, int resultFlags, boolean cacheStore) throws SQLException { if (proxy == null) { return null; } else { return proxy.compileStatement(statement, type, argFieldTypes, resultFlags, cacheStore); } }
@Override public CompiledStatement compile(DatabaseConnection databaseConnection, StatementType type, int resultFlags) throws SQLException { if (this.type != type) { throw new SQLException("Could not compile this " + this.type + " statement since the caller is expecting a " + type + " statement. Check your QueryBuilder methods."); } CompiledStatement stmt = databaseConnection.compileStatement(statement, type, argFieldTypes, resultFlags, cacheStore); // this may return null if the stmt had to be closed return assignStatementArguments(stmt); }
@Override public CompiledStatement compile(DatabaseConnection databaseConnection, StatementType type, int resultFlags) throws SQLException { if (this.type != type) { throw new SQLException("Could not compile this " + this.type + " statement since the caller is expecting a " + type + " statement. Check your QueryBuilder methods."); } CompiledStatement stmt = databaseConnection.compileStatement(statement, type, argFieldTypes, resultFlags, cacheStore); // this may return null if the stmt had to be closed return assignStatementArguments(stmt); }
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); } }
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 testCompileStatementStringStatementTypeFieldTypeArrayInt() throws Exception { DatabaseConnection conn = createMock(DatabaseConnection.class); String statement = "select foo from bar"; StatementType type = StatementType.DELETE; int flags = 11253123; expect(conn.compileStatement(statement, type, null, flags, false)).andReturn(null); conn.close(); DatabaseConnectionProxy proxy = new DatabaseConnectionProxy(conn); replay(conn); proxy.compileStatement(statement, type, null, flags, false); proxy.close(); verify(conn); }
/** * Return the number of rows affected. */ public int updateRaw(DatabaseConnection connection, String statement, String[] arguments) throws SQLException { logger.debug("running raw update statement: {}", statement); if (arguments.length > 0) { // need to do the (Object) cast to force args to be a single object logger.trace("update arguments: {}", (Object) arguments); } CompiledStatement compiledStatement = connection.compileStatement(statement, StatementType.UPDATE, noFieldTypes, DatabaseConnection.DEFAULT_RESULT_FLAGS, false); try { assignStatementArguments(compiledStatement, arguments); return compiledStatement.runUpdate(); } finally { IOUtils.closeThrowSqlException(compiledStatement, "compiled statement"); } }
/** * Return true if it worked else false. */ public int executeRaw(DatabaseConnection connection, String statement, String[] arguments) throws SQLException { logger.debug("running raw execute statement: {}", statement); if (arguments.length > 0) { // need to do the (Object) cast to force args to be a single object logger.trace("execute arguments: {}", (Object) arguments); } CompiledStatement compiledStatement = connection.compileStatement(statement, StatementType.EXECUTE, noFieldTypes, DatabaseConnection.DEFAULT_RESULT_FLAGS, false); try { assignStatementArguments(compiledStatement, arguments); return compiledStatement.runExecute(); } finally { IOUtils.closeThrowSqlException(compiledStatement, "compiled statement"); } }
/** * Return true if it worked else false. */ public int executeRaw(DatabaseConnection connection, String statement, String[] arguments) throws SQLException { logger.debug("running raw execute statement: {}", statement); if (arguments.length > 0) { // need to do the (Object) cast to force args to be a single object logger.trace("execute arguments: {}", (Object) arguments); } CompiledStatement compiledStatement = connection.compileStatement(statement, StatementType.EXECUTE, noFieldTypes, DatabaseConnection.DEFAULT_RESULT_FLAGS, false); try { assignStatementArguments(compiledStatement, arguments); return compiledStatement.runExecute(); } finally { IOUtils.closeThrowSqlException(compiledStatement, "compiled statement"); } }
/** * Return the number of rows affected. */ public int updateRaw(DatabaseConnection connection, String statement, String[] arguments) throws SQLException { logger.debug("running raw update statement: {}", statement); if (arguments.length > 0) { // need to do the (Object) cast to force args to be a single object logger.trace("update arguments: {}", (Object) arguments); } CompiledStatement compiledStatement = connection.compileStatement(statement, StatementType.UPDATE, noFieldTypes, DatabaseConnection.DEFAULT_RESULT_FLAGS, false); try { assignStatementArguments(compiledStatement, arguments); return compiledStatement.runUpdate(); } finally { 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"); } }
/** * Return a results object associated with an internal iterator is mapped by the user's rowMapper. */ public <UO> GenericRawResults<UO> queryRaw(ConnectionSource connectionSource, String query, DataType[] columnTypes, RawRowObjectMapper<UO> rowMapper, String[] arguments, ObjectCache objectCache) throws SQLException { logger.debug("executing raw query for: {}", 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); } DatabaseConnection connection = connectionSource.getReadOnlyConnection(tableInfo.getTableName()); CompiledStatement compiledStatement = null; try { compiledStatement = connection.compileStatement(query, StatementType.SELECT, noFieldTypes, DatabaseConnection.DEFAULT_RESULT_FLAGS, false); assignStatementArguments(compiledStatement, arguments); RawResultsImpl<UO> rawResults = new RawResultsImpl<UO>(connectionSource, connection, query, String[].class, compiledStatement, new UserRawRowObjectMapper<UO>(rowMapper, columnTypes), objectCache); compiledStatement = null; connection = null; return rawResults; } finally { IOUtils.closeThrowSqlException(compiledStatement, "compiled statement"); if (connection != null) { connectionSource.releaseConnection(connection); } } }
/** * Return a results object associated with an internal iterator that returns Object[] results. */ public GenericRawResults<Object[]> queryRaw(ConnectionSource connectionSource, String query, DataType[] columnTypes, String[] arguments, ObjectCache objectCache) throws SQLException { logger.debug("executing raw query for: {}", 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); } DatabaseConnection connection = connectionSource.getReadOnlyConnection(tableInfo.getTableName()); CompiledStatement compiledStatement = null; try { compiledStatement = connection.compileStatement(query, StatementType.SELECT, noFieldTypes, DatabaseConnection.DEFAULT_RESULT_FLAGS, false); assignStatementArguments(compiledStatement, arguments); RawResultsImpl<Object[]> rawResults = new RawResultsImpl<Object[]>(connectionSource, connection, query, Object[].class, compiledStatement, new ObjectArrayRowMapper(columnTypes), objectCache); compiledStatement = null; connection = null; return rawResults; } finally { IOUtils.closeThrowSqlException(compiledStatement, "compiled statement"); if (connection != null) { connectionSource.releaseConnection(connection); } } }
@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); } }