1 19 20 21 package org.apache.cayenne.access.trans; 22 23 import java.sql.PreparedStatement ; 24 import java.sql.SQLException ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import org.apache.cayenne.dba.DbAdapter; 29 import org.apache.cayenne.map.DbAttribute; 30 import org.apache.cayenne.query.BatchQuery; 31 import org.apache.cayenne.query.DeleteBatchQuery; 32 33 38 39 public class DeleteBatchQueryBuilder extends BatchQueryBuilder { 40 41 public DeleteBatchQueryBuilder(DbAdapter adapter) { 42 super(adapter); 43 } 44 45 public String createSqlString(BatchQuery batch) { 46 DeleteBatchQuery deleteBatch = (DeleteBatchQuery) batch; 47 String table = batch.getDbEntity().getFullyQualifiedName(); 48 List qualifierAttributes = deleteBatch.getQualifierAttributes(); 49 50 StringBuffer query = new StringBuffer ("DELETE FROM "); 51 query.append(table).append(" WHERE "); 52 53 Iterator i = qualifierAttributes.iterator(); 54 while (i.hasNext()) { 55 DbAttribute attribute = (DbAttribute) i.next(); 56 appendDbAttribute(query, attribute); 57 query.append(deleteBatch.isNull(attribute) ? " IS NULL" : " = ?"); 58 59 if (i.hasNext()) { 60 query.append(" AND "); 61 } 62 } 63 64 return query.toString(); 65 } 66 67 70 public void bindParameters(PreparedStatement statement, BatchQuery query) 71 throws SQLException , Exception { 72 73 DeleteBatchQuery deleteBatch = (DeleteBatchQuery) query; 74 List qualifierAttributes = deleteBatch.getQualifierAttributes(); 75 76 int parameterIndex = 1; 77 78 for (int i = 0; i < qualifierAttributes.size(); i++) { 79 Object value = query.getValue(i); 80 DbAttribute attribute = (DbAttribute) qualifierAttributes.get(i); 81 82 if (deleteBatch.isNull(attribute)) { 84 continue; 85 } 86 87 adapter.bindParameter( 88 statement, 89 value, 90 parameterIndex++, 91 attribute.getType(), 92 attribute.getScale()); 93 } 94 } 95 } 96 | Popular Tags |