use of com.healthmarketscience.sqlbuilder.DeleteQuery 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);
}
Aggregations