com.j256.ormlite.misc.IOUtils Java Examples
The following examples show how to use
com.j256.ormlite.misc.IOUtils.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LazyForeignCollection.java From ormlite-core with ISC License | 6 votes |
@Override public boolean removeAll(Collection<?> collection) { boolean changed = false; CloseableIterator<T> iterator = iterator(); try { while (iterator.hasNext()) { if (collection.contains(iterator.next())) { iterator.remove(); changed = true; } } return changed; } finally { IOUtils.closeQuietly(iterator); } }
Example #2
Source File: StatementExecutor.java From ormlite-core with ISC License | 6 votes |
/** * 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"); } }
Example #3
Source File: StatementExecutor.java From ormlite-core with ISC License | 6 votes |
/** * 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"); } }
Example #4
Source File: StatementExecutor.java From ormlite-core with ISC License | 6 votes |
/** * 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"); } }
Example #5
Source File: StatementExecutor.java From ormlite-core with ISC License | 6 votes |
/** * Return a list of all of the data in the table that matches the {@link PreparedStmt}. Should be used carefully if * the table is large. Consider using the {@link Dao#iterator} if this is the case. */ public List<T> query(ConnectionSource connectionSource, PreparedStmt<T> preparedStmt, ObjectCache objectCache) throws SQLException { SelectIterator<T, ID> iterator = buildIterator(/* no dao specified because no removes */null, connectionSource, preparedStmt, objectCache, DatabaseConnection.DEFAULT_RESULT_FLAGS); try { List<T> results = new ArrayList<T>(); while (iterator.hasNextThrow()) { results.add(iterator.nextThrow()); } logger.debug("query of '{}' returned {} results", preparedStmt.getStatement(), results.size()); return results; } finally { IOUtils.closeThrowSqlException(iterator, "iterator"); } }
Example #6
Source File: StatementExecutor.java From ormlite-core with ISC License | 6 votes |
/** * Create and return an {@link SelectIterator} for the class using a prepared statement. */ public SelectIterator<T, ID> buildIterator(BaseDaoImpl<T, ID> classDao, ConnectionSource connectionSource, PreparedStmt<T> preparedStmt, ObjectCache objectCache, int resultFlags) throws SQLException { DatabaseConnection connection = connectionSource.getReadOnlyConnection(tableInfo.getTableName()); CompiledStatement compiledStatement = null; try { compiledStatement = preparedStmt.compile(connection, StatementType.SELECT, resultFlags); SelectIterator<T, ID> iterator = new SelectIterator<T, ID>(tableInfo.getDataClass(), classDao, preparedStmt, connectionSource, connection, compiledStatement, preparedStmt.getStatement(), objectCache); connection = null; compiledStatement = null; return iterator; } finally { IOUtils.closeThrowSqlException(compiledStatement, "compiled statement"); if (connection != null) { connectionSource.releaseConnection(connection); } } }
Example #7
Source File: StatementExecutor.java From ormlite-core with ISC License | 6 votes |
/** * Return a results object associated with an internal iterator that returns String[] results. */ public GenericRawResults<String[]> queryRaw(ConnectionSource connectionSource, String query, 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); GenericRawResults<String[]> rawResults = new RawResultsImpl<String[]>(connectionSource, connection, query, String[].class, compiledStatement, this, objectCache); compiledStatement = null; connection = null; return rawResults; } finally { IOUtils.closeThrowSqlException(compiledStatement, "compiled statement"); if (connection != null) { connectionSource.releaseConnection(connection); } } }
Example #8
Source File: EnumToStringTypeTest.java From ormlite-core with ISC License | 6 votes |
@Test public void testEnumToStringValue() throws Exception { Class<LocalEnumToString> clazz = LocalEnumToString.class; Dao<LocalEnumToString, Object> dao = createDao(clazz, true); LocalEnumToString foo = new LocalEnumToString(); foo.ourEnum = OurEnum.SECOND; assertEquals(1, dao.create(foo)); GenericRawResults<String[]> results = dao.queryRaw("select * from " + TABLE_NAME); CloseableIterator<String[]> iterator = results.closeableIterator(); try { assertTrue(iterator.hasNext()); String[] result = iterator.next(); assertNotNull(result); assertEquals(1, result.length); assertFalse(OurEnum.SECOND.name().equals(result[0])); assertTrue(OurEnum.SECOND.toString().equals(result[0])); } finally { IOUtils.closeQuietly(iterator); } }
Example #9
Source File: AndroidDatabaseConnection.java From ormlite-android with ISC License | 6 votes |
@Override public long queryForLong(String statement, Object[] args, FieldType[] argFieldTypes) throws SQLException { Cursor cursor = null; AndroidDatabaseResults results = null; try { cursor = db.rawQuery(statement, toStrings(args)); results = new AndroidDatabaseResults(cursor, null, false); long result; if (results.first()) { result = results.getLong(0); } else { result = 0L; } logger.trace("{}: query for long raw query returned {}: {}", this, result, statement); return result; } catch (android.database.SQLException e) { throw SqlExceptionUtil.create("queryForLong from database failed: " + statement, e); } finally { closeQuietly(cursor); IOUtils.closeQuietly(results); } }
Example #10
Source File: StatementExecutor.java From ormlite-core with ISC License | 6 votes |
/** * 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, RawRowMapper<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 UserRawRowMapper<UO>(rowMapper, this), objectCache); compiledStatement = null; connection = null; return rawResults; } finally { IOUtils.closeThrowSqlException(compiledStatement, "compiled statement"); if (connection != null) { connectionSource.releaseConnection(connection); } } }
Example #11
Source File: StatementExecutor.java From ormlite-core with ISC License | 6 votes |
/** * 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); } } }
Example #12
Source File: StatementExecutor.java From ormlite-core with ISC License | 6 votes |
/** * 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); } } }
Example #13
Source File: StatementExecutor.java From ormlite-core with ISC License | 6 votes |
/** * 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, DatabaseResultsMapper<UO> mapper, 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, Object[].class, compiledStatement, new UserDatabaseResultsMapper<UO>(mapper), objectCache); compiledStatement = null; connection = null; return rawResults; } finally { IOUtils.closeThrowSqlException(compiledStatement, "compiled statement"); if (connection != null) { connectionSource.releaseConnection(connection); } } }
Example #14
Source File: StatementExecutor.java From ormlite-core with ISC License | 6 votes |
/** * 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"); } }
Example #15
Source File: BaseForeignCollection.java From ormlite-core with ISC License | 6 votes |
/** * Uses the iterator to run through the dao and retain only the items that are in the passed in collection. This * will remove the items from the associated database table as well. * * @return Returns true of the collection was changed at all otherwise false. */ @Override public boolean retainAll(Collection<?> collection) { if (dao == null) { return false; } boolean changed = false; CloseableIterator<T> iterator = closeableIterator(); try { while (iterator.hasNext()) { T data = iterator.next(); if (!collection.contains(data)) { iterator.remove(); changed = true; } } return changed; } finally { IOUtils.closeQuietly(iterator); } }
Example #16
Source File: BaseForeignCollection.java From ormlite-core with ISC License | 6 votes |
/** * Clears the collection and uses the iterator to run through the dao and delete all of the items in the collection * from the associated database table. This is different from removing all of the elements in the table since this * iterator is across just one item's foreign objects. */ @Override public void clear() { if (dao == null) { return; } CloseableIterator<T> iterator = closeableIterator(); try { while (iterator.hasNext()) { iterator.next(); iterator.remove(); } } finally { IOUtils.closeQuietly(iterator); } }
Example #17
Source File: TableUtils.java From ormlite-core with ISC License | 6 votes |
private static <T> int clearTable(ConnectionSource connectionSource, String schemaName, 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 "); } if (schemaName != null && schemaName.length() > 0){ databaseType.appendEscapedEntityName(sb, schemaName); sb.append('.'); } 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); } }
Example #18
Source File: StatementExecutor.java From ormlite-core with ISC License | 6 votes |
/** * 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"); } }
Example #19
Source File: H2DatabaseConnection.java From ormlite-core with ISC License | 6 votes |
@Override public <T> Object queryForOne(String statement, Object[] args, FieldType[] argFieldTypes, GenericRowMapper<T> rowMapper, ObjectCache objectCache) throws SQLException { PreparedStatement stmt = connection.prepareStatement(statement, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); if (args != null) { statementSetArgs(stmt, args, argFieldTypes); } DatabaseResults results = new H2DatabaseResults(stmt.executeQuery(), objectCache, true); if (!results.next()) { // no results at all IOUtils.closeThrowSqlException(results, "results"); return null; } T first = rowMapper.mapRow(results); if (results.next()) { return MORE_THAN_ONE; } else { return first; } }
Example #20
Source File: MappedPreparedQueryTest.java From ormlite-core with ISC License | 6 votes |
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); } }
Example #21
Source File: SelectIterator.java From ormlite-core with ISC License | 6 votes |
/** * Returns whether or not there are any remaining objects in the table. Can be called before next(). * * @throws SQLException * If there was a problem getting more results via SQL. */ public boolean hasNextThrow() throws SQLException { if (closed) { return false; } if (alreadyMoved) { // we do this so multiple hasNext() calls can be made, result would be true or closed is true return true; } boolean result; if (first) { first = false; result = results.first(); } else { result = results.next(); } if (!result) { IOUtils.closeThrowSqlException(this, "iterator"); } alreadyMoved = true; return result; }
Example #22
Source File: LazyForeignCollection.java From ormlite-core with ISC License | 5 votes |
@Override public boolean contains(Object obj) { CloseableIterator<T> iterator = iterator(); try { while (iterator.hasNext()) { if (iterator.next().equals(obj)) { return true; } } return false; } finally { IOUtils.closeQuietly(iterator); } }
Example #23
Source File: LazyForeignCollection.java From ormlite-core with ISC License | 5 votes |
@Override public int size() { CloseableIterator<T> iterator = iterator(); try { int sizeC; for (sizeC = 0; iterator.hasNext(); sizeC++) { // move to next without constructing the object iterator.moveToNext(); } return sizeC; } finally { IOUtils.closeQuietly(iterator); } }
Example #24
Source File: LazyForeignCollection.java From ormlite-core with ISC License | 5 votes |
@Override public boolean isEmpty() { CloseableIterator<T> iterator = iterator(); try { return !iterator.hasNext(); } finally { IOUtils.closeQuietly(iterator); } }
Example #25
Source File: SchemaUtils.java From ormlite-core with ISC License | 5 votes |
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 schema 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 schema 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 schema after-query failed: " + query, e); } finally { // result set is closed by the statement being closed IOUtils.closeThrowSqlException(compiledStmt, "compiled statement"); } stmtC++; } return stmtC; }
Example #26
Source File: LazyForeignCollection.java From ormlite-core with ISC License | 5 votes |
@Override public boolean containsAll(Collection<?> collection) { Set<Object> leftOvers = new HashSet<Object>(collection); CloseableIterator<T> iterator = iterator(); try { while (iterator.hasNext()) { leftOvers.remove(iterator.next()); } return leftOvers.isEmpty(); } finally { IOUtils.closeQuietly(iterator); } }
Example #27
Source File: LazyForeignCollection.java From ormlite-core with ISC License | 5 votes |
@Override public boolean remove(Object data) { CloseableIterator<T> iterator = iterator(); try { while (iterator.hasNext()) { if (iterator.next().equals(data)) { iterator.remove(); return true; } } return false; } finally { IOUtils.closeQuietly(iterator); } }
Example #28
Source File: LazyForeignCollection.java From ormlite-core with ISC License | 5 votes |
@Override public Object[] toArray() { List<T> items = new ArrayList<T>(); CloseableIterator<T> iterator = iterator(); try { while (iterator.hasNext()) { items.add(iterator.next()); } return items.toArray(); } finally { IOUtils.closeQuietly(iterator); } }
Example #29
Source File: LazyForeignCollection.java From ormlite-core with ISC License | 5 votes |
@Override public <E> E[] toArray(E[] array) { List<E> items = null; int itemC = 0; CloseableIterator<T> iterator = iterator(); try { while (iterator.hasNext()) { @SuppressWarnings("unchecked") E castData = (E) iterator.next(); // are we exceeding our capacity in the array? if (itemC >= array.length) { if (items == null) { items = new ArrayList<E>(); for (E arrayData : array) { items.add(arrayData); } } items.add(castData); } else { array[itemC] = castData; } itemC++; } } finally { IOUtils.closeQuietly(iterator); } if (items == null) { if (itemC < array.length - 1) { array[itemC] = null; } return array; } else { return items.toArray(array); } }
Example #30
Source File: LocalLog.java From ormlite-core with ISC License | 5 votes |
/** * Read in our levels from our configuration file. */ static List<PatternLevel> readLevelResourceFile(InputStream stream) { List<PatternLevel> levels = null; if (stream != null) { try { levels = configureClassLevels(stream); } catch (IOException e) { System.err.println( "IO exception reading the log properties file '" + LOCAL_LOG_PROPERTIES_FILE + "': " + e); } finally { IOUtils.closeQuietly(stream); } } return levels; }