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.UpdateBatchQuery; 32 33 38 39 public class UpdateBatchQueryBuilder extends BatchQueryBuilder { 40 41 public UpdateBatchQueryBuilder(DbAdapter adapter) { 42 super(adapter); 43 } 44 45 public String createSqlString(BatchQuery batch) { 46 UpdateBatchQuery updateBatch = (UpdateBatchQuery) batch; 47 String table = batch.getDbEntity().getFullyQualifiedName(); 48 List qualifierAttributes = updateBatch.getQualifierAttributes(); 49 List updatedDbAttributes = updateBatch.getUpdatedAttributes(); 50 51 StringBuffer query = new StringBuffer ("UPDATE "); 52 query.append(table).append(" SET "); 53 54 int len = updatedDbAttributes.size(); 55 for (int i = 0; i < len; i++) { 56 if (i > 0) { 57 query.append(", "); 58 } 59 60 DbAttribute attribute = (DbAttribute) updatedDbAttributes.get(i); 61 query.append(attribute.getName()).append(" = ?"); 62 } 63 64 query.append(" WHERE "); 65 66 Iterator i = qualifierAttributes.iterator(); 67 while (i.hasNext()) { 68 DbAttribute attribute = (DbAttribute) i.next(); 69 appendDbAttribute(query, attribute); 70 query.append(updateBatch.isNull(attribute) ? " IS NULL" : " = ?"); 71 72 if (i.hasNext()) { 73 query.append(" AND "); 74 } 75 } 76 77 return query.toString(); 78 } 79 80 83 public void bindParameters(PreparedStatement statement, BatchQuery query) 84 throws SQLException , Exception { 85 86 UpdateBatchQuery updateBatch = (UpdateBatchQuery) query; 87 List qualifierAttributes = updateBatch.getQualifierAttributes(); 88 List updatedDbAttributes = updateBatch.getUpdatedAttributes(); 89 90 int len = updatedDbAttributes.size(); 91 int parameterIndex = 1; 92 for (int i = 0; i < len; i++) { 93 Object value = query.getValue(i); 94 95 DbAttribute attribute = (DbAttribute) updatedDbAttributes.get(i); 96 adapter.bindParameter( 97 statement, 98 value, 99 parameterIndex++, 100 attribute.getType(), 101 attribute.getScale()); 102 } 103 104 for (int i = 0; i < qualifierAttributes.size(); i++) { 105 Object value = query.getValue(len + i); 106 DbAttribute attribute = (DbAttribute) qualifierAttributes.get(i); 107 108 if (updateBatch.isNull(attribute)) { 110 continue; 111 } 112 113 adapter.bindParameter( 114 statement, 115 value, 116 parameterIndex++, 117 attribute.getType(), 118 attribute.getScale()); 119 } 120 } 121 } 122 | Popular Tags |