Java Code Examples for com.j256.ormlite.stmt.SelectArg#setValue()

The following examples show how to use com.j256.ormlite.stmt.SelectArg#setValue() . 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: JdbcBaseDaoImplTest.java    From ormlite-jdbc with ISC License 6 votes vote down vote up
/**
 * Returns the object if the query failed or null otherwise.
 */
private boolean checkQueryResult(Dao<AllTypes, Integer> allDao, QueryBuilder<AllTypes, Integer> qb,
		AllTypes allTypes, String fieldName, Object value, boolean required) throws SQLException {
	qb.where().eq(fieldName, value);
	List<AllTypes> results = allDao.query(qb.prepare());
	if (required) {
		assertEquals(1, results.size());
		assertTrue(allDao.objectsEqual(allTypes, results.get(0)));
	} else if (results.size() == 1) {
		assertTrue(allDao.objectsEqual(allTypes, results.get(0)));
	} else {
		return false;
	}

	SelectArg selectArg = new SelectArg();
	qb.where().eq(fieldName, selectArg);
	selectArg.setValue(value);
	results = allDao.query(qb.prepare());
	assertEquals(1, results.size());
	assertTrue(allDao.objectsEqual(allTypes, results.get(0)));
	return true;
}
 
Example 2
Source File: BaseForeignCollection.java    From ormlite-core with ISC License 6 votes vote down vote up
protected PreparedQuery<T> getPreparedQuery() throws SQLException {
	if (dao == null) {
		return null;
	}
	if (preparedQuery == null) {
		SelectArg fieldArg = new SelectArg();
		fieldArg.setValue(parentId);
		QueryBuilder<T, ID> qb = dao.queryBuilder();
		if (orderColumn != null) {
			qb.orderBy(orderColumn, orderAscending);
		}
		preparedQuery = qb.where().eq(foreignFieldType.getColumnName(), fieldArg).prepare();
		if (preparedQuery instanceof MappedPreparedStmt) {
			@SuppressWarnings("unchecked")
			MappedPreparedStmt<T, Object> mappedStmt = ((MappedPreparedStmt<T, Object>) preparedQuery);
			mappedStmt.setParentInformation(parent, parentId);
		}
	}
	return preparedQuery;
}
 
Example 3
Source File: SimpleMain.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
/**
 * Example of created a query with a ? argument using the {@link SelectArg} object. You then can set the value of
 * this object at a later time.
 */
private void useSelectArgFeature() throws Exception {

	String name1 = "foo";
	String name2 = "bar";
	String name3 = "baz";
	assertEquals(1, accountDao.create(new Account(name1)));
	assertEquals(1, accountDao.create(new Account(name2)));
	assertEquals(1, accountDao.create(new Account(name3)));

	QueryBuilder<Account, Integer> statementBuilder = accountDao.queryBuilder();
	SelectArg selectArg = new SelectArg();
	// build a query with the WHERE clause set to 'name = ?'
	statementBuilder.where().like(Account.NAME_FIELD_NAME, selectArg);
	PreparedQuery<Account> preparedQuery = statementBuilder.prepare();

	// now we can set the select arg (?) and run the query
	selectArg.setValue(name1);
	List<Account> results = accountDao.query(preparedQuery);
	assertEquals("Should have found 1 account matching our query", 1, results.size());
	assertEquals(name1, results.get(0).getName());

	selectArg.setValue(name2);
	results = accountDao.query(preparedQuery);
	assertEquals("Should have found 1 account matching our query", 1, results.size());
	assertEquals(name2, results.get(0).getName());

	selectArg.setValue(name3);
	results = accountDao.query(preparedQuery);
	assertEquals("Should have found 1 account matching our query", 1, results.size());
	assertEquals(name3, results.get(0).getName());
}
 
Example 4
Source File: JdbcBaseDaoImplTest.java    From ormlite-jdbc with ISC License 4 votes vote down vote up
@Test
public void testInSubQuerySelectArgs() throws Exception {
	Dao<Foo, Integer> fooDao = createDao(Foo.class, true);
	Dao<Basic, String> basicDao = createDao(Basic.class, true);

	Basic basic1 = new Basic();
	String string1 = "ewpofjewgprgrg";
	basic1.id = string1;
	assertEquals(1, basicDao.create(basic1));
	Basic basic2 = new Basic();
	String string2 = "e2432423432wpofjewgprgrg";
	basic2.id = string2;
	assertEquals(1, basicDao.create(basic2));

	Foo foo1 = new Foo();
	foo1.stuff = basic1.id;
	Foo foo2 = new Foo();
	foo2.stuff = basic2.id;

	int num1 = 7;
	for (int i = 0; i < num1; i++) {
		assertEquals(1, fooDao.create(foo1));
	}
	int num2 = 17;
	long maxId = 0;
	for (int i = 0; i < num2; i++) {
		assertEquals(1, fooDao.create(foo2));
		if (foo2.id > maxId) {
			maxId = foo2.id;
		}
	}
	// using seletArgs
	SelectArg arg1 = new SelectArg();
	SelectArg arg2 = new SelectArg();
	QueryBuilder<Basic, String> bqb = basicDao.queryBuilder();
	bqb.selectColumns(Basic.ID_FIELD);
	bqb.where().eq(Basic.ID_FIELD, arg1);
	PreparedQuery<Foo> preparedQuery =
			fooDao.queryBuilder().where().in(Foo.STUFF_FIELD_NAME, bqb).and().lt(Foo.ID_FIELD_NAME, arg2).prepare();
	arg1.setValue(string1);
	// this should get none
	arg2.setValue(0);
	List<Foo> results = fooDao.query(preparedQuery);
	assertEquals(0, results.size());
}