Search in sources :

Example 1 with CustomCondition

use of com.healthmarketscience.sqlbuilder.CustomCondition in project SmartCity-Market by TechnionYP5777.

the class SQLDatabaseConnection method removeCatalogProduct.

/**
	 * Remove product from the SQL database (erase all associate entries in
	 * tables: Product catalog, Ingredients, Locations NOTE: other traces of the
	 * product will not be removed
	 * 
	 * @param p
	 *            - product to remove (only the barcode is used)
	 * @throws CriticalError
	 * @throws SQLException
	 */
private void removeCatalogProduct(SmartCode p) throws CriticalError, SQLException {
    // remove all ingredients of product
    PreparedStatement statement = getParameterizedQuery(generateDeleteQuery(ProductsCatalogIngredientsTable.table, BinaryCondition.equalTo(ProductsCatalogIngredientsTable.barcodeCol, PARAM_MARK)), p.getBarcode());
    statement.executeUpdate();
    closeResources(statement);
    String selectAllLocationsQuery = new SelectQuery().addColumns(ProductsCatalogLocationsTable.locationIDCol).addCondition(BinaryCondition.equalTo(ProductsCatalogLocationsTable.barcodeCol, PARAM_MARK)).validate() + "", deleteLocationsQuery = new DeleteQuery(LocationsTable.table).addCondition(new CustomCondition(LocationsTable.locationIDCol.getColumnNameSQL() + " IN (" + selectAllLocationsQuery + " ) ")).validate() + "";
    PreparedStatement LocationsStatement = getParameterizedQuery(deleteLocationsQuery, p.getBarcode());
    LocationsStatement.executeUpdate();
    closeResources(LocationsStatement);
    // remove barcode form ProductsLocations Table
    PreparedStatement productLocationsStatement = getParameterizedQuery(generateDeleteQuery(ProductsCatalogLocationsTable.table, BinaryCondition.equalTo(ProductsCatalogLocationsTable.barcodeCol, PARAM_MARK)), p.getBarcode());
    productLocationsStatement.executeUpdate();
    closeResources(productLocationsStatement);
    // remove product itself
    PreparedStatement productStatement = getParameterizedQuery(generateDeleteQuery(ProductsCatalogTable.table, BinaryCondition.equalTo(ProductsCatalogTable.barcodeCol, PARAM_MARK)), p.getBarcode());
    productStatement.executeUpdate();
    closeResources(productStatement);
}
Also used : SelectQuery(com.healthmarketscience.sqlbuilder.SelectQuery) CustomCondition(com.healthmarketscience.sqlbuilder.CustomCondition) PreparedStatement(java.sql.PreparedStatement) DeleteQuery(com.healthmarketscience.sqlbuilder.DeleteQuery)

Aggregations

CustomCondition (com.healthmarketscience.sqlbuilder.CustomCondition)1 DeleteQuery (com.healthmarketscience.sqlbuilder.DeleteQuery)1 SelectQuery (com.healthmarketscience.sqlbuilder.SelectQuery)1 PreparedStatement (java.sql.PreparedStatement)1