Java Code Examples for com.j256.ormlite.support.ConnectionSource#getReadOnlyConnection()

The following examples show how to use com.j256.ormlite.support.ConnectionSource#getReadOnlyConnection() . 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
/**
 * 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 2
Source File: StatementExecutor.java    From ormlite-core with ISC License 6 votes vote down vote up
/**
 * 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 3
Source File: StatementExecutor.java    From ormlite-core with ISC License 6 votes vote down vote up
/**
 * 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 4
Source File: StatementExecutor.java    From ormlite-core with ISC License 6 votes vote down vote up
/**
 * 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 5
Source File: StatementExecutor.java    From ormlite-core with ISC License 6 votes vote down vote up
/**
 * 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 6
Source File: StatementExecutor.java    From ormlite-core with ISC License 6 votes vote down vote up
/**
 * 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 7
Source File: FieldType.java    From ormlite-core with ISC License 4 votes vote down vote up
private Object createForeignObject(ConnectionSource connectionSource, Object val, ObjectCache objectCache)
		throws SQLException {

	// try to stop the level counters objects from being created
	LevelCounters levelCounters = threadLevelCounters.get();
	if (levelCounters == null) {
		// only create a shell if we are not in auto-refresh mode
		if (!fieldConfig.isForeignAutoRefresh()) {
			return createForeignShell(connectionSource, val, objectCache);
		}
		levelCounters = new LevelCounters();
		threadLevelCounters.set(levelCounters);
	}

	// we record the current auto-refresh level which will be used along the way
	if (levelCounters.autoRefreshLevel == 0) {
		// if we aren't in an auto-refresh loop then don't _start_ an new loop if auto-refresh is not configured
		if (!fieldConfig.isForeignAutoRefresh()) {
			return createForeignShell(connectionSource, val, objectCache);
		}
		levelCounters.autoRefreshLevelMax = fieldConfig.getMaxForeignAutoRefreshLevel();
	}
	// if we have recursed the proper number of times, return a shell with just the id set
	if (levelCounters.autoRefreshLevel >= levelCounters.autoRefreshLevelMax) {
		return createForeignShell(connectionSource, val, objectCache);
	}

	/*
	 * We may not have a mapped query for id because we aren't auto-refreshing ourselves. But a parent class may be
	 * auto-refreshing us with a level > 1 so we may need to build our query-for-id optimization on the fly here.
	 */
	if (mappedQueryForForeignField == null) {
		@SuppressWarnings("unchecked")
		Dao<Object, Object> castDao = (Dao<Object, Object>) foreignDao;
		MappedQueryForFieldEq<Object, Object> castMappedQueryForId =
				MappedQueryForFieldEq.build(castDao, castDao.getTableInfo(), foreignIdField);
		mappedQueryForForeignField = castMappedQueryForId;
	}
	levelCounters.autoRefreshLevel++;
	try {
		DatabaseConnection databaseConnection = connectionSource.getReadOnlyConnection(tableName);
		try {
			// recurse and get the sub-object
			@SuppressWarnings("unchecked")
			MappedQueryForFieldEq<Object, Object> castMappedQueryForForeignField =
					(MappedQueryForFieldEq<Object, Object>) mappedQueryForForeignField;
			return castMappedQueryForForeignField.execute(databaseConnection, val, objectCache);
		} finally {
			connectionSource.releaseConnection(databaseConnection);
		}
	} finally {
		levelCounters.autoRefreshLevel--;
		if (levelCounters.autoRefreshLevel <= 0) {
			threadLevelCounters.remove();
		}
	}
}