Java Code Examples for com.j256.ormlite.dao.CloseableIterator#next()

The following examples show how to use com.j256.ormlite.dao.CloseableIterator#next() . 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: UploadQueue.java    From AndroidAPS with GNU Affero General Public License v3.0 6 votes vote down vote up
String textList() {
    String result = "";
    CloseableIterator<DbRequest> iterator;
    try {
        iterator = MainApp.getDbHelper().getDbRequestInterator();
        try {
            while (iterator.hasNext()) {
                DbRequest dbr = iterator.next();
                result += "<br>";
                result += dbr.action.toUpperCase() + " ";
                result += dbr.collection + ": ";
                result += dbr.data;
            }
        } finally {
            iterator.close();
        }
    } catch (SQLException e) {
        log.error("Unhandled exception", e);
    }
    return result;
}
 
Example 2
Source File: Contents.java    From geopackage-core-java with MIT License 6 votes vote down vote up
/**
 * Get the Geometry Columns, should only return one or no value
 * 
 * @return geometry columns
 */
public GeometryColumns getGeometryColumns() {
	GeometryColumns result = null;
	if (geometryColumns.size() > 1) {
		// This shouldn't happen with the unique table name constraint on
		// geometry columns
		throw new GeoPackageException(
				"Unexpected state. More than one GeometryColumn has a foreign key to the Contents. Count: "
						+ geometryColumns.size());
	} else if (geometryColumns.size() == 1) {
		CloseableIterator<GeometryColumns> iterator = geometryColumns
				.closeableIterator();
		try {
			result = iterator.next();
		} finally {
			try {
				iterator.close();
			} catch (IOException e) {
				throw new GeoPackageException(
						"Failed to close the Geometry Columns iterator", e);
			}
		}
	}
	return result;
}
 
Example 3
Source File: Contents.java    From geopackage-core-java with MIT License 6 votes vote down vote up
/**
 * Get the Tile Matrix Set, should only return one or no value
 * 
 * @return tile matrix set
 */
public TileMatrixSet getTileMatrixSet() {
	TileMatrixSet result = null;
	if (tileMatrixSet.size() > 1) {
		// This shouldn't happen with the table name primary key on tile
		// matrix set
		throw new GeoPackageException(
				"Unexpected state. More than one TileMatrixSet has a foreign key to the Contents. Count: "
						+ tileMatrixSet.size());
	} else if (tileMatrixSet.size() == 1) {
		CloseableIterator<TileMatrixSet> iterator = tileMatrixSet
				.closeableIterator();
		try {
			result = iterator.next();
		} finally {
			try {
				iterator.close();
			} catch (IOException e) {
				throw new GeoPackageException(
						"Failed to close the Tile Matrix Set iterator", e);
			}
		}
	}
	return result;
}
 
Example 4
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 5
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 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 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 8
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 9
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 10
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 11
Source File: DefaultFeatureTiles.java    From geopackage-java with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public BufferedImage drawTile(int zoom, BoundingBox boundingBox,
		CloseableIterator<GeometryIndex> results) {

	FeatureTileGraphics graphics = new FeatureTileGraphics(tileWidth,
			tileHeight);

	// Feature projection to web mercator projection
	ProjectionTransform webMercatorTransform = getWebMercatorTransform();
	BoundingBox expandedBoundingBox = expandBoundingBox(boundingBox);

	boolean drawn = false;
	while (results.hasNext()) {
		GeometryIndex geometryIndex = results.next();
		FeatureRow featureRow = getFeatureIndex()
				.getFeatureRow(geometryIndex);
		if (drawFeature(zoom, boundingBox, expandedBoundingBox,
				webMercatorTransform, graphics, featureRow)) {
			drawn = true;
		}
	}
	try {
		results.close();
	} catch (IOException e) {
		log.log(Level.WARNING, "Failed to close geometry index results", e);
	}

	BufferedImage image = null;
	if (drawn) {
		image = graphics.createImage();
		image = checkIfDrawn(image);
	} else {
		graphics.dispose();
	}

	return image;
}
 
Example 12
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 13
Source File: ForeignCollectionMain.java    From ormlite-jdbc with ISC License 4 votes vote down vote up
private void readWriteData() throws Exception {
	// create an instance of Account
	String name = "Buzz Lightyear";
	Account account = new Account(name);

	// persist the account object to the database
	accountDao.create(account);

	// create an associated Order for the Account
	// Buzz bought 2 of item #21312 for a price of $12.32
	int quantity1 = 2;
	int itemNumber1 = 21312;
	float price1 = 12.32F;
	Order order1 = new Order(account, itemNumber1, price1, quantity1);
	orderDao.create(order1);

	// create another Order for the Account
	// Buzz also bought 1 of item #785 for a price of $7.98
	int quantity2 = 1;
	int itemNumber2 = 785;
	float price2 = 7.98F;
	Order order2 = new Order(account, itemNumber2, price2, quantity2);
	orderDao.create(order2);

	Account accountResult = accountDao.queryForId(account.getId());
	ForeignCollection<Order> orders = accountResult.getOrders();

	// sanity checks
	CloseableIterator<Order> iterator = orders.closeableIterator();
	try {
		assertTrue(iterator.hasNext());
		Order order = iterator.next();
		assertEquals(itemNumber1, order.getItemNumber());
		assertSame(accountResult, order.getAccount());
		assertTrue(iterator.hasNext());
		order = iterator.next();
		assertEquals(itemNumber2, order.getItemNumber());
		assertFalse(iterator.hasNext());
	} finally {
		// must always close our iterators otherwise connections to the database are held open
		iterator.close();
	}

	// create another Order for the Account
	// Buzz also bought 1 of item #785 for a price of $7.98
	int quantity3 = 50;
	int itemNumber3 = 78315;
	float price3 = 72.98F;
	Order order3 = new Order(account, itemNumber3, price3, quantity3);

	// now let's add this order via the foreign collection
	orders.add(order3);
	// now there are 3 of them in there
	assertEquals(3, orders.size());

	List<Order> orderList = orderDao.queryForAll();
	// and 3 in the database
	assertEquals(3, orderList.size());
}
 
Example 14
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();
	}
}