Java Code Examples for com.j256.ormlite.support.DatabaseResults#first()

The following examples show how to use com.j256.ormlite.support.DatabaseResults#first() . 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: StatementExecutor.java    From ormlite-core with ISC License 6 votes vote down vote up
/**
 * 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 2
Source File: StatementExecutor.java    From ormlite-core with ISC License 6 votes vote down vote up
/**
 * 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 3
Source File: StatementExecutor.java    From ormlite-core with ISC License 6 votes vote down vote up
/**
 * 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 4
Source File: ApsTableUtils.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 5
Source File: TableUtils.java    From ormlite-core with ISC License 5 votes vote down vote up
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;
}
 
Example 6
Source File: SchemaUtils.java    From ormlite-core with ISC License 5 votes vote down vote up
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;
}