com.j256.ormlite.dao.ObjectCache Java Examples

The following examples show how to use com.j256.ormlite.dao.ObjectCache. 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 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 #2
Source File: H2DatabaseConnection.java    From ormlite-core with ISC License 6 votes vote down vote up
@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 #3
Source File: SelectIterator.java    From ormlite-core with ISC License 6 votes vote down vote up
/**
 * 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);
	}
}
 
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,
		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 #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, 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 #7
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 #8
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 #9
Source File: StatementExecutor.java    From ormlite-core with ISC License 6 votes vote down vote up
/**
 * 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 #10
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 #11
Source File: MappedDelete.java    From ormlite-core with ISC License 6 votes vote down vote up
/**
 * Delete the object from the database.
 */
public int deleteById(DatabaseConnection databaseConnection, ID id, ObjectCache objectCache) throws SQLException {
	try {
		Object[] args = new Object[] { convertIdToFieldObject(id) };
		int rowC = databaseConnection.delete(statement, args, argFieldTypes);
		logger.debug("delete data with statement '{}' and {} args, changed {} rows", statement, args.length, rowC);
		if (args.length > 0) {
			// need to do the (Object) cast to force args to be a single object
			logger.trace("delete arguments: {}", (Object) args);
		}
		if (rowC > 0 && objectCache != null) {
			objectCache.remove(clazz, id);
		}
		return rowC;
	} catch (SQLException e) {
		throw SqlExceptionUtil.create("Unable to run deleteById stmt on id " + id + ": " + statement, e);
	}
}
 
Example #12
Source File: MappedDelete.java    From ormlite-core with ISC License 6 votes vote down vote up
/**
 * Delete the object from the database.
 */
public int delete(DatabaseConnection databaseConnection, T data, ObjectCache objectCache) throws SQLException {
	try {
		Object[] args = getFieldObjects(data);
		int rowC = databaseConnection.delete(statement, args, argFieldTypes);
		logger.debug("delete data with statement '{}' and {} args, changed {} rows", statement, args.length, rowC);
		if (args.length > 0) {
			// need to do the (Object) cast to force args to be a single object
			logger.trace("delete arguments: {}", (Object) args);
		}
		if (rowC > 0 && objectCache != null) {
			Object id = idField.extractJavaFieldToSqlArgValue(data);
			objectCache.remove(clazz, id);
		}
		return rowC;
	} catch (SQLException e) {
		throw SqlExceptionUtil.create("Unable to run delete stmt on object " + data + ": " + statement, e);
	}
}
 
Example #13
Source File: MappedDeleteCollection.java    From ormlite-core with ISC License 6 votes vote down vote up
private static <T, ID> int updateRows(DatabaseConnection databaseConnection, Class<T> clazz,
		MappedDeleteCollection<T, ID> deleteCollection, Object[] args, ObjectCache objectCache)
		throws SQLException {
	try {
		int rowC = databaseConnection.delete(deleteCollection.statement, args, deleteCollection.argFieldTypes);
		if (rowC > 0 && objectCache != null) {
			for (Object id : args) {
				objectCache.remove(clazz, id);
			}
		}
		logger.debug("delete-collection with statement '{}' and {} args, changed {} rows",
				deleteCollection.statement, args.length, rowC);
		if (args.length > 0) {
			// need to do the (Object) cast to force args to be a single object
			logger.trace("delete-collection arguments: {}", (Object) args);
		}
		return rowC;
	} catch (SQLException e) {
		throw SqlExceptionUtil.create("Unable to run delete collection stmt: " + deleteCollection.statement, e);
	}
}
 
Example #14
Source File: MappedRefresh.java    From ormlite-core with ISC License 6 votes vote down vote up
/**
 * Execute our refresh query statement and then update all of the fields in data with the fields from the result.
 * 
 * @return 1 if we found the object in the table by id or 0 if not.
 */
public int executeRefresh(DatabaseConnection databaseConnection, T data, ObjectCache objectCache)
		throws SQLException {
	@SuppressWarnings("unchecked")
	ID id = (ID) idField.extractJavaFieldValue(data);
	// we don't care about the cache here
	T result = super.execute(databaseConnection, id, null);
	if (result == null) {
		return 0;
	}
	// copy each field from the result into the passed in object
	for (FieldType fieldType : resultsFieldTypes) {
		if (fieldType != idField) {
			fieldType.assignField(connectionSource, data, fieldType.extractJavaFieldValue(result), false,
					objectCache);
		}
	}
	return 1;
}
 
Example #15
Source File: StatementExecutor.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Delete a collection of objects from the database.
 */
public int deleteObjects(DatabaseConnection databaseConnection, Collection<T> datas, ObjectCache objectCache)
		throws SQLException {
	// have to build this on the fly because the collection has variable number of args
	int result = MappedDeleteCollection.deleteObjects(dao, tableInfo, databaseConnection, datas, objectCache);
	if (dao != null && !localIsInBatchMode.get()) {
		dao.notifyChanges();
	}
	return result;
}
 
Example #16
Source File: JdbcDatabaseResults.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
@Override
public ObjectCache getObjectCacheForStore() {
	if (cacheStore) {
		return objectCache;
	} else {
		return null;
	}
}
 
Example #17
Source File: JdbcDatabaseResults.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
public JdbcDatabaseResults(PreparedStatement preparedStmt, ResultSet resultSet, ObjectCache objectCache,
		boolean cacheStore) throws SQLException {
	this.preparedStmt = preparedStmt;
	this.resultSet = resultSet;
	this.metaData = resultSet.getMetaData();
	this.objectCache = objectCache;
	this.cacheStore = cacheStore;
}
 
Example #18
Source File: StatementExecutor.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Create a new entry in the database from an object.
 */
public int create(DatabaseConnection databaseConnection, T data, ObjectCache objectCache) throws SQLException {
	if (mappedInsert == null) {
		mappedInsert = MappedCreate.build(dao, tableInfo);
	}
	int result = mappedInsert.insert(databaseType, databaseConnection, data, objectCache);
	if (dao != null && !localIsInBatchMode.get()) {
		dao.notifyChanges();
	}
	return result;
}
 
Example #19
Source File: StatementExecutor.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Update an object in the database.
 */
public int update(DatabaseConnection databaseConnection, T data, ObjectCache objectCache) throws SQLException {
	if (mappedUpdate == null) {
		mappedUpdate = MappedUpdate.build(dao, tableInfo);
	}
	int result = mappedUpdate.update(databaseConnection, data, objectCache);
	if (dao != null && !localIsInBatchMode.get()) {
		dao.notifyChanges();
	}
	return result;
}
 
Example #20
Source File: StatementExecutor.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Update an object in the database to change its id to the newId parameter.
 */
public int updateId(DatabaseConnection databaseConnection, T data, ID newId, ObjectCache objectCache)
		throws SQLException {
	if (mappedUpdateId == null) {
		mappedUpdateId = MappedUpdateId.build(dao, tableInfo);
	}
	int result = mappedUpdateId.execute(databaseConnection, data, newId, objectCache);
	if (dao != null && !localIsInBatchMode.get()) {
		dao.notifyChanges();
	}
	return result;
}
 
Example #21
Source File: StatementExecutor.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Does a query for the object's Id and copies in each of the field values from the database to refresh the data
 * parameter.
 */
public int refresh(DatabaseConnection databaseConnection, T data, ObjectCache objectCache) throws SQLException {
	if (mappedRefresh == null) {
		mappedRefresh = MappedRefresh.build(dao, tableInfo);
	}
	return mappedRefresh.executeRefresh(databaseConnection, data, objectCache);
}
 
Example #22
Source File: StatementExecutor.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Delete an object from the database.
 */
public int delete(DatabaseConnection databaseConnection, T data, ObjectCache objectCache) throws SQLException {
	if (mappedDelete == null) {
		mappedDelete = MappedDelete.build(dao, tableInfo);
	}
	int result = mappedDelete.delete(databaseConnection, data, objectCache);
	if (dao != null && !localIsInBatchMode.get()) {
		dao.notifyChanges();
	}
	return result;
}
 
Example #23
Source File: StatementExecutor.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Delete an object from the database by id.
 */
public int deleteById(DatabaseConnection databaseConnection, ID id, ObjectCache objectCache) throws SQLException {
	if (mappedDelete == null) {
		mappedDelete = MappedDelete.build(dao, tableInfo);
	}
	int result = mappedDelete.deleteById(databaseConnection, id, objectCache);
	if (dao != null && !localIsInBatchMode.get()) {
		dao.notifyChanges();
	}
	return result;
}
 
Example #24
Source File: MappedUpdateId.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Update the id field of the object in the database.
 */
public int execute(DatabaseConnection databaseConnection, T data, ID newId, ObjectCache objectCache)
		throws SQLException {
	try {
		// the arguments are the new-id and old-id
		Object[] args = new Object[] { convertIdToFieldObject(newId), extractIdToFieldObject(data) };
		int rowC = databaseConnection.update(statement, args, argFieldTypes);
		if (rowC > 0) {
			if (objectCache != null) {
				Object oldId = idField.extractJavaFieldValue(data);
				T obj = objectCache.updateId(clazz, oldId, newId);
				if (obj != null && obj != data) {
					// if our cached value is not the data that will be updated then we need to update it specially
					idField.assignField(connectionSource, obj, newId, false, objectCache);
				}
			}
			// adjust the object to assign the new id
			idField.assignField(connectionSource, data, newId, false, objectCache);
		}
		logger.debug("updating-id with statement '{}' and {} args, changed {} rows", statement, args.length, rowC);
		if (args.length > 0) {
			// need to do the cast otherwise we only print the first object in args
			logger.trace("updating-id arguments: {}", (Object) args);
		}
		return rowC;
	} catch (SQLException e) {
		throw SqlExceptionUtil.create("Unable to run update-id stmt on object " + data + ": " + statement, e);
	}
}
 
Example #25
Source File: StatementExecutor.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Delete a collection of objects from the database.
 */
public int deleteIds(DatabaseConnection databaseConnection, Collection<ID> ids, ObjectCache objectCache)
		throws SQLException {
	// have to build this on the fly because the collection has variable number of args
	int result = MappedDeleteCollection.deleteIds(dao, tableInfo, databaseConnection, ids, objectCache);
	if (dao != null && !localIsInBatchMode.get()) {
		dao.notifyChanges();
	}
	return result;
}
 
Example #26
Source File: FieldType.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Assign an ID value to this field.
 */
public Object assignIdValue(ConnectionSource connectionSource, Object data, Number val, ObjectCache objectCache)
		throws SQLException {
	Object idVal = dataPersister.convertIdNumber(val);
	if (idVal == null) {
		throw new SQLException("Invalid class " + dataPersister + " for sequence-id " + this);
	} else {
		assignField(connectionSource, data, idVal, false, objectCache);
		return idVal;
	}
}
 
Example #27
Source File: FieldType.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Create a shell object and assign its id field.
 */
private <FT, FID> FT createForeignShell(ConnectionSource connectionSource, Object val, ObjectCache objectCache)
		throws SQLException {
	@SuppressWarnings("unchecked")
	Dao<FT, FID> castDao = (Dao<FT, FID>) foreignDao;
	FT foreignObject = castDao.createObjectInstance();
	foreignIdField.assignField(connectionSource, foreignObject, val, false, objectCache);
	return foreignObject;
}
 
Example #28
Source File: DatabaseConnectionProxy.java    From ormlite-core with ISC License 5 votes vote down vote up
@Override
public <T> Object queryForOne(String statement, Object[] args, FieldType[] argfieldTypes,
		GenericRowMapper<T> rowMapper, ObjectCache objectCache) throws SQLException {
	if (proxy == null) {
		return null;
	} else {
		return proxy.queryForOne(statement, args, argfieldTypes, rowMapper, objectCache);
	}
}
 
Example #29
Source File: BaseMappedQueryTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testMappedQueryCached() throws Exception {
	Field field = Foo.class.getDeclaredField(Foo.ID_COLUMN_NAME);
	String tableName = "basefoo";
	FieldType[] resultFieldTypes =
			new FieldType[] { FieldType.createFieldType(databaseType, tableName, field, Foo.class) };
	Dao<Foo, Integer> dao = createDao(Foo.class, false);
	BaseMappedQuery<Foo, Integer> baseMappedQuery = new BaseMappedQuery<Foo, Integer>(dao, baseFooTableInfo,
			"select * from " + tableName, new FieldType[0], resultFieldTypes) {
	};
	DatabaseResults results = createMock(DatabaseResults.class);
	int colN = 1;
	ObjectCache objectCache = createMock(ObjectCache.class);
	expect(results.getObjectCacheForRetrieve()).andReturn(objectCache);
	int id = 63365;
	expect(results.getInt(colN)).andReturn(id);
	expect(objectCache.get(Foo.class, id)).andReturn(null);
	objectCache.put(eq(Foo.class), eq(id), isA(Foo.class));
	expect(results.getObjectCacheForStore()).andReturn(objectCache);
	expect(results.findColumn(Foo.ID_COLUMN_NAME)).andReturn(colN);
	expect(results.getInt(colN)).andReturn(id);

	replay(results, objectCache);
	Foo baseFoo = baseMappedQuery.mapRow(results);
	assertNotNull(baseFoo);
	assertEquals(id, baseFoo.id);
	verify(results, objectCache);
}
 
Example #30
Source File: FieldTypeTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testForeignAutoRefresh() throws Exception {
	Field field = ForeignAutoRefresh.class.getDeclaredField("foreign");
	ConnectionSource connectionSource = createMock(ConnectionSource.class);
	DatabaseConnection connection = createMock(DatabaseConnection.class);
	expect(connectionSource.getDatabaseType()).andReturn(databaseType).anyTimes();
	expect(connectionSource.getReadOnlyConnection("ForeignAutoRefresh")).andReturn(connection);
	ForeignForeign foreignForeign = new ForeignForeign();
	String stuff = "21312j3213";
	int id = 4123123;
	foreignForeign.id = id;
	foreignForeign.stuff = stuff;
	expect(connection.queryForOne(isA(String.class), isA(Object[].class), isA(FieldType[].class),
			isA(GenericRowMapper.class), (ObjectCache) isNull())).andReturn(foreignForeign);
	connectionSource.releaseConnection(connection);
	DatabaseResults results = createMock(DatabaseResults.class);
	ForeignAutoRefresh foreign = new ForeignAutoRefresh();
	replay(results, connectionSource, connection);
	FieldType fieldType = FieldType.createFieldType(databaseType, ForeignAutoRefresh.class.getSimpleName(), field,
			ForeignAutoRefresh.class);
	fieldType.configDaoInformation(connectionSource, ForeignAutoRefresh.class);
	assertNull(foreign.foreign);
	fieldType.assignField(connectionSource, foreign, id, false, null);
	assertNotNull(foreign.foreign);
	assertEquals(id, foreign.foreign.id);
	assertEquals(stuff, foreign.foreign.stuff);
	verify(results, connectionSource, connection);
}