Java Code Examples for com.j256.ormlite.dao.Dao#iterator()

The following examples show how to use com.j256.ormlite.dao.Dao#iterator() . 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: LocationLoadTask.java    From mage-android with Apache License 2.0 6 votes vote down vote up
private CloseableIterator<Location> iterator() throws SQLException {
	Dao<Location, Long> dao = DaoStore.getInstance(context).getLocationDao();
	QueryBuilder<Location, Long> query = dao.queryBuilder();
	Where<? extends Temporal, Long> where = query.where().ge("timestamp", locationCollection.getLatestDate());
	User currentUser = null;
	try {
		currentUser = UserHelper.getInstance(context.getApplicationContext()).readCurrentUser();
	} catch (UserException e) {
		e.printStackTrace();
	}
	if (currentUser != null) {
		where.and().ne("user_id", currentUser.getId()).and().eq("event_id", currentUser.getUserLocal().getCurrentEvent().getId());
	}
	if (filter != null) {
		filter.and(where);
	}
	query.orderBy("timestamp", false);


	return dao.iterator(query.prepare());
}
 
Example 2
Source File: SelectIteratorTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testMoveClosed() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);

	CloseableIterator<Foo> iterator = dao.iterator();
	try {
		assertFalse(iterator.hasNext());
		assertNull(iterator.first());
		assertNull(iterator.previous());
		assertNull(iterator.current());
		assertNull(iterator.nextThrow());
		assertNull(iterator.moveRelative(10));
	} finally {
		iterator.close();
	}
}
 
Example 3
Source File: SelectIteratorTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test(expected = SQLException.class)
public void testIteratorJdbcMoveBack() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	Foo foo = new Foo();
	assertEquals(1, dao.create(foo));

	QueryBuilder<Foo, Integer> qb = dao.queryBuilder();
	qb.orderBy(Foo.VAL_COLUMN_NAME, true);
	CloseableIterator<Foo> iterator = dao.iterator(qb.prepare());
	try {
		assertEquals(foo, iterator.first());
		iterator.first();
		fail("Should have thrown");
	} finally {
		iterator.close();
	}
}
 
Example 4
Source File: SelectIteratorTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testIteratorHasNextClosed() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	Foo foo1 = new Foo();
	assertEquals(1, dao.create(foo1));

	assertEquals(1, dao.queryForAll().size());

	CloseableIterator<Foo> iterator = dao.iterator();
	assertTrue(iterator.hasNext());
	iterator.next();

	assertFalse(iterator.hasNext());
	assertNull(iterator.nextThrow());

	iterator.close();
	assertFalse(iterator.hasNext());
}
 
Example 5
Source File: SelectIteratorTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testIteratorRemove() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	Foo foo1 = new Foo();
	assertEquals(1, dao.create(foo1));

	assertEquals(1, dao.queryForAll().size());

	CloseableIterator<Foo> iterator = dao.iterator();
	assertTrue(iterator.hasNext());
	Foo result = iterator.next();
	assertEquals(foo1.id, result.id);
	iterator.remove();

	assertFalse(iterator.hasNext());
	assertNull(iterator.nextThrow());

	assertEquals(0, dao.queryForAll().size());
	iterator.close();
}
 
Example 6
Source File: SelectIteratorTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testIteratorNextRemoveRemoveNoNext() throws Exception {
	Dao<Foo, Object> dao = createDao(Foo.class, true);
	Foo foo1 = new Foo();
	assertEquals(1, dao.create(foo1));
	Foo foo2 = new Foo();
	assertEquals(1, dao.create(foo2));
	CloseableIterator<Foo> iterator = dao.iterator();
	try {
		iterator.next();
		iterator.remove();
		iterator.remove();
	} finally {
		iterator.close();
	}
}
 
Example 7
Source File: SelectIteratorTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testIteratorPrepared() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	Foo foo1 = new Foo();
	assertEquals(1, dao.create(foo1));

	Foo foo2 = new Foo();
	assertEquals(1, dao.create(foo2));

	PreparedQuery<Foo> query = dao.queryBuilder().where().eq(Foo.ID_COLUMN_NAME, foo2.id).prepare();
	CloseableIterator<Foo> iterator = dao.iterator(query);
	assertTrue(iterator.hasNext());
	Foo result = iterator.next();
	assertEquals(foo2.id, result.id);
	assertFalse(iterator.hasNext());
	assertNull(iterator.nextThrow());
}
 
Example 8
Source File: SelectIteratorTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testIterator() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	CloseableIterator<Foo> iterator = dao.iterator();
	assertFalse(iterator.hasNext());

	Foo foo1 = new Foo();
	assertEquals(1, dao.create(foo1));

	Foo foo2 = new Foo();
	assertEquals(1, dao.create(foo2));

	iterator = dao.iterator();
	assertTrue(iterator.hasNext());
	Foo result = iterator.next();
	assertEquals(foo1.id, result.id);
	assertTrue(iterator.hasNext());

	result = iterator.next();
	assertEquals(foo2.id, result.id);

	assertFalse(iterator.hasNext());
	assertNull(iterator.nextThrow());
}
 
Example 9
Source File: ObservationLoadTask.java    From mage-android with Apache License 2.0 6 votes vote down vote up
private CloseableIterator<Observation> iterator() throws SQLException {
    Dao<Observation, Long> dao = DaoStore.getInstance(context).getObservationDao();
    QueryBuilder<Observation, Long> query = dao.queryBuilder();
    Where<Observation, Long> where = query
            .orderBy("timestamp", false)
            .where()
            .ge("last_modified", observationCollection.getLatestDate())
            .and()
            .eq("event_id", currentEventId);

    for (Filter filter : filters) {
        QueryBuilder<?, ?> filterQuery = filter.query();
        if (filterQuery != null) {
            query.join(filterQuery);
        }

        filter.and(where);
    }

    return dao.iterator(query.prepare());
}
 
Example 10
Source File: ObservationFeedFragment.java    From mage-android with Apache License 2.0 6 votes vote down vote up
private Cursor obtainCursor(PreparedQuery<Observation> query, Dao<Observation, Long> oDao) throws SQLException {
	Cursor c = null;
	CloseableIterator<Observation> iterator = oDao.iterator(query);

	// get the raw results which can be cast under Android
	AndroidDatabaseResults results = (AndroidDatabaseResults) iterator.getRawResults();
	c = results.getRawCursor();
	if (c.moveToLast()) {
		long oldestTime = c.getLong(c.getColumnIndex("last_modified"));
		Log.i(LOG_NAME, "last modified is: " + c.getLong(c.getColumnIndex("last_modified")));
		Log.i(LOG_NAME, "querying again in: " + (oldestTime - requeryTime)/60000 + " minutes");
		if (queryUpdateHandle != null) {
			queryUpdateHandle.cancel(true);
		}
		queryUpdateHandle = scheduler.schedule(new Runnable() {
			public void run() {
				updateFilter();
			}
		}, oldestTime - requeryTime, TimeUnit.MILLISECONDS);
		c.moveToFirst();
	}
	return c;
}
 
Example 11
Source File: SelectIteratorTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testIteratorRemoveNoNext() throws Exception {
	Dao<Foo, Object> dao = createDao(Foo.class, true);
	CloseableIterator<Foo> iterator = dao.iterator();
	try {
		iterator.remove();
	} finally {
		iterator.close();
	}
}
 
Example 12
Source File: SelectIteratorTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testIteratorGetRawResults() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	Foo foo1 = new Foo();
	assertEquals(1, dao.create(foo1));

	assertEquals(1, dao.queryForAll().size());

	@SuppressWarnings("unchecked")
	SelectIterator<Foo, String> iterator = (SelectIterator<Foo, String>) dao.iterator();
	DatabaseResults results = iterator.getRawResults();
	assertTrue(results.next());
	iterator.close();
}
 
Example 13
Source File: SelectIteratorTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testMultipleHasNext() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	Foo foo1 = new Foo();
	assertEquals(1, dao.create(foo1));

	CloseableIterator<Foo> iterator = dao.iterator();
	assertTrue(iterator.hasNext());
	assertTrue(iterator.hasNext());
	assertTrue(iterator.hasNext());
	iterator.moveToNext();
	assertFalse(iterator.hasNext());
}
 
Example 14
Source File: SelectIteratorTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testIteratorMoveAround() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	List<Foo> fooList = new ArrayList<Foo>();
	for (int i = 0; i < 10; i++) {
		Foo foo = new Foo();
		foo.val = i;
		assertEquals(1, dao.create(foo));
		fooList.add(foo);
	}

	QueryBuilder<Foo, Integer> qb = dao.queryBuilder();
	qb.orderBy(Foo.VAL_COLUMN_NAME, true);
	CloseableIterator<Foo> iterator = dao.iterator(qb.prepare(), ResultSet.TYPE_SCROLL_INSENSITIVE);
	try {
		assertEquals(fooList.get(0), iterator.first());
		assertEquals(fooList.get(0), iterator.first());
		assertEquals(fooList.get(0), iterator.current());
		assertEquals(fooList.get(1), iterator.next());
		assertEquals(fooList.get(1), iterator.current());
		assertEquals(fooList.get(0), iterator.first());
		assertEquals(fooList.get(0), iterator.current());
		assertEquals(fooList.get(1), iterator.next());
		assertTrue(iterator.hasNext());
		assertEquals(fooList.get(2), iterator.next());
		assertEquals(fooList.get(2), iterator.current());
		assertEquals(fooList.get(1), iterator.previous());
		assertEquals(fooList.get(2), iterator.next());
		assertEquals(fooList.get(1), iterator.moveRelative(-1));
		assertEquals(fooList.get(3), iterator.moveRelative(2));
		assertEquals(fooList.get(9), iterator.moveRelative(6));
		assertFalse(iterator.hasNext());
		assertNull(iterator.current());
	} finally {
		iterator.close();
	}
}
 
Example 15
Source File: SelectIteratorTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testIteratorNextOnly() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	Foo foo = new Foo();
	assertEquals(1, dao.create(foo));

	CloseableIterator<Foo> iterator = dao.iterator();
	try {
		assertEquals(foo, iterator.next());
		iterator.next();
		fail("Should have thrown");
	} finally {
		iterator.close();
	}
}
 
Example 16
Source File: SelectIteratorTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testFirstCurrent() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	Foo foo = new Foo();
	assertEquals(1, dao.create(foo));

	CloseableIterator<Foo> iterator = dao.iterator();
	try {
		assertEquals(foo, iterator.current());
		assertFalse(iterator.hasNext());
		assertNull(iterator.current());
	} finally {
		iterator.close();
	}
}
 
Example 17
Source File: SelectIteratorTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testMoveNone() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);

	CloseableIterator<Foo> iterator = dao.iterator();
	try {
		assertNull(iterator.current());
	} finally {
		iterator.close();
	}

	iterator = dao.iterator();
	try {
		assertNull(iterator.previous());
	} finally {
		iterator.close();
	}

	iterator = dao.iterator();
	try {
		assertNull(iterator.moveRelative(1));
	} finally {
		iterator.close();
	}

	iterator = dao.iterator();
	try {
		assertNull(iterator.nextThrow());
	} finally {
		iterator.close();
	}
}