Java Code Examples for com.j256.ormlite.dao.GenericRawResults#closeableIterator()

The following examples show how to use com.j256.ormlite.dao.GenericRawResults#closeableIterator() . 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: SelectIteratorTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testIteratorRawResults() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	Foo foo1 = new Foo();
	assertEquals(1, dao.create(foo1));

	GenericRawResults<String[]> rawResults = dao.queryRaw("SELECT " + Foo.ID_COLUMN_NAME + " FROM FOO");
	CloseableIterator<String[]> iterator = rawResults.closeableIterator();
	try {
		assertTrue(iterator.hasNext());
		iterator.next();
		iterator.remove();
	} finally {
		iterator.close();
	}
}
 
Example 2
Source File: EnumToStringTypeTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@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 3
Source File: QueryBuilderTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testQueryRawMax() throws Exception {
	Dao<Foo, Object> dao = createDao(Foo.class, true);

	Foo foo1 = new Foo();
	foo1.stringField = "1";
	foo1.val = 10;
	assertEquals(1, dao.create(foo1));
	Foo foo2 = new Foo();
	foo2.stringField = "1";
	foo2.val = 20;
	assertEquals(1, dao.create(foo2));
	Foo foo3 = new Foo();
	foo3.stringField = "2";
	foo3.val = 30;
	assertEquals(1, dao.create(foo3));
	Foo foo4 = new Foo();
	foo4.stringField = "2";
	foo4.val = 40;
	assertEquals(1, dao.create(foo4));

	QueryBuilder<Foo, Object> qb = dao.queryBuilder();
	qb.selectRaw("string, max(val) as val");
	qb.groupBy(Foo.STRING_COLUMN_NAME);
	GenericRawResults<Foo> results = dao.queryRaw(qb.prepareStatementString(), dao.getRawRowMapper());
	assertNotNull(results);
	CloseableIterator<Foo> iterator = results.closeableIterator();
	try {
		assertTrue(iterator.hasNext());
		assertEquals(foo2.val, iterator.next().val);
		assertTrue(iterator.hasNext());
		assertEquals(foo4.val, iterator.next().val);
		assertFalse(iterator.hasNext());
	} finally {
		iterator.close();
	}
}
 
Example 4
Source File: QueryBuilderTest.java    From ormlite-core with ISC License 4 votes vote down vote up
@Test
public void testQueryColumnsPlusQueryRawMax() throws Exception {
	Dao<Foo, Object> dao = createDao(Foo.class, true);

	Foo foo1 = new Foo();
	foo1.stringField = "1";
	foo1.val = 10;
	assertEquals(1, dao.create(foo1));
	Foo foo2 = new Foo();
	foo2.stringField = "1";
	foo2.val = 20;
	assertEquals(1, dao.create(foo2));
	Foo foo3 = new Foo();
	foo3.stringField = "2";
	foo3.val = 40;
	assertEquals(1, dao.create(foo3));
	Foo foo4 = new Foo();
	foo4.stringField = "2";
	foo4.val = 30;
	assertEquals(1, dao.create(foo4));

	QueryBuilder<Foo, Object> qb = dao.queryBuilder();
	qb.selectColumns(Foo.STRING_COLUMN_NAME);
	qb.selectRaw("MAX(val) AS val");
	qb.groupBy(Foo.STRING_COLUMN_NAME);
	GenericRawResults<Foo> results = dao.queryRaw(qb.prepareStatementString(), dao.getRawRowMapper());
	assertNotNull(results);
	CloseableIterator<Foo> iterator = results.closeableIterator();
	try {
		assertTrue(iterator.hasNext());
		Foo result = iterator.next();
		assertEquals(foo2.val, result.val);
		assertEquals(foo2.stringField, result.stringField);
		assertTrue(iterator.hasNext());
		result = iterator.next();
		assertEquals(foo3.val, result.val);
		assertEquals(foo3.stringField, result.stringField);
		assertFalse(iterator.hasNext());
	} finally {
		iterator.close();
	}
}