1 package com.imcode.db.commands; 2 3 import com.imcode.db.DatabaseConnection; 4 import com.imcode.db.DatabaseException; 5 import org.apache.commons.collections.CollectionUtils; 6 import org.apache.commons.collections.Transformer; 7 import org.apache.commons.lang.StringUtils; 8 9 import java.util.Arrays; 10 import java.util.Collection; 11 12 public class DeleteWhereColumnsEqualDatabaseCommand extends ColumnValuesDatabaseCommand { 13 14 public DeleteWhereColumnsEqualDatabaseCommand( String table, String column, Object columnValue ) { 15 this(table, new Object[][] {{column, columnValue}}); 16 } 17 18 public DeleteWhereColumnsEqualDatabaseCommand(String table, Object[][] columnNamesAndValues) { 19 super(table, columnNamesAndValues); 20 } 21 22 public Object executeOn( DatabaseConnection connection ) throws DatabaseException { 23 Collection whereClauses = CollectionUtils.collect(Arrays.asList(columnNames), new ColumnNameToWhereClauseTransformer()); 24 String joinedWhereClauses = StringUtils.join(whereClauses.iterator()," AND ") ; 25 return new Integer(connection.executeUpdate("DELETE FROM "+tableName+" WHERE "+joinedWhereClauses, columnValues)) ; 26 } 27 28 private static class ColumnNameToWhereClauseTransformer implements Transformer { 29 public Object transform(Object object) { 30 return object + " = ?" ; 31 } 32 } 33 }