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

The following examples show how to use com.j256.ormlite.dao.CloseableIterator#close() . 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
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 Project: geopackage-core-java   File: Contents.java    License: 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 Project: geopackage-core-java   File: Contents.java    License: 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 Project: ormlite-core   File: SelectIteratorTest.java    License: 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 5
Source Project: ormlite-core   File: SelectIteratorTest.java    License: 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 Project: ormlite-core   File: SelectIteratorTest.java    License: 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 7
Source Project: ormlite-core   File: SelectIteratorTest.java    License: 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 8
Source Project: ormlite-core   File: SelectIteratorTest.java    License: 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 9
Source Project: ormlite-core   File: SelectIteratorTest.java    License: 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 10
Source Project: geopackage-java   File: DefaultFeatureTiles.java    License: 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 11
Source Project: ormlite-core   File: SelectIteratorTest.java    License: 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 Project: ormlite-core   File: SelectIteratorTest.java    License: 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 13
Source Project: ormlite-core   File: SelectIteratorTest.java    License: 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 14
Source Project: ormlite-core   File: SelectIteratorTest.java    License: 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 15
Source Project: ormlite-core   File: SelectIteratorTest.java    License: 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();
	}
}
 
Example 16
Source Project: ormlite-core   File: QueryBuilderTest.java    License: 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 17
Source Project: geopackage-java   File: FeatureTiles.java    License: MIT License 4 votes vote down vote up
/**
 * Draw a tile image from the x, y, and zoom level by querying features in
 * the tile location
 *
 * @param x
 *            x coordinate
 * @param y
 *            y coordinate
 * @param zoom
 *            zoom level
 * @return drawn image, or null
 */
public BufferedImage drawTileQueryIndex(int x, int y, int zoom) {

	// Get the web mercator bounding box
	BoundingBox webMercatorBoundingBox = TileBoundingBoxUtils
			.getWebMercatorBoundingBox(x, y, zoom);

	BufferedImage image = null;

	// Query for the geometry count matching the bounds in the index
	long tileCount = queryIndexedFeaturesCount(webMercatorBoundingBox);

	// Draw if at least one geometry exists
	if (tileCount > 0) {

		// Query for geometries matching the bounds in the index
		CloseableIterator<GeometryIndex> results = queryIndexedFeatures(
				webMercatorBoundingBox);

		try {

			if (maxFeaturesPerTile == null
					|| tileCount <= maxFeaturesPerTile.longValue()) {

				// Draw the tile image
				image = drawTile(zoom, webMercatorBoundingBox, results);

			} else if (maxFeaturesTileDraw != null) {

				// Draw the max features tile
				image = maxFeaturesTileDraw.drawTile(tileWidth, tileHeight,
						tileCount, results);
			}
		} finally {
			try {
				results.close();
			} catch (IOException e) {
				LOGGER.log(Level.WARNING,
						"Failed to close result set for query on x: " + x
								+ ", y: " + y + ", zoom: " + zoom,
						e);
			}
		}
	}

	return image;
}
 
Example 18
Source Project: ormlite-jdbc   File: ForeignCollectionMain.java    License: 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 19
Source Project: ormlite-core   File: QueryBuilderTest.java    License: 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();
	}
}