1 56 57 package org.objectstyle.cayenne.access.trans; 58 59 import java.sql.PreparedStatement ; 60 import java.sql.SQLException ; 61 import java.util.Iterator ; 62 import java.util.List ; 63 64 import org.objectstyle.cayenne.dba.DbAdapter; 65 import org.objectstyle.cayenne.map.DbAttribute; 66 import org.objectstyle.cayenne.query.BatchQuery; 67 import org.objectstyle.cayenne.query.UpdateBatchQuery; 68 69 74 75 public class UpdateBatchQueryBuilder extends BatchQueryBuilder { 76 77 public UpdateBatchQueryBuilder(DbAdapter adapter) { 78 super(adapter); 79 } 80 81 public String createSqlString(BatchQuery batch) { 82 UpdateBatchQuery updateBatch = (UpdateBatchQuery) batch; 83 String table = batch.getDbEntity().getFullyQualifiedName(); 84 List qualifierAttributes = updateBatch.getQualifierAttributes(); 85 List updatedDbAttributes = updateBatch.getUpdatedAttributes(); 86 87 StringBuffer query = new StringBuffer ("UPDATE "); 88 query.append(table).append(" SET "); 89 90 int len = updatedDbAttributes.size(); 91 for (int i = 0; i < len; i++) { 92 if (i > 0) { 93 query.append(", "); 94 } 95 96 DbAttribute attribute = (DbAttribute) updatedDbAttributes.get(i); 97 query.append(attribute.getName()).append(" = ?"); 98 } 99 100 query.append(" WHERE "); 101 102 Iterator i = qualifierAttributes.iterator(); 103 while (i.hasNext()) { 104 DbAttribute attribute = (DbAttribute) i.next(); 105 appendDbAttribute(query, attribute); 106 query.append(updateBatch.isNull(attribute) ? " IS NULL" : " = ?"); 107 108 if (i.hasNext()) { 109 query.append(" AND "); 110 } 111 } 112 113 return query.toString(); 114 } 115 116 119 public void bindParameters(PreparedStatement statement, BatchQuery query) 120 throws SQLException , Exception { 121 122 UpdateBatchQuery updateBatch = (UpdateBatchQuery) query; 123 List qualifierAttributes = updateBatch.getQualifierAttributes(); 124 List updatedDbAttributes = updateBatch.getUpdatedAttributes(); 125 126 int len = updatedDbAttributes.size(); 127 int parameterIndex = 1; 128 for (int i = 0; i < len; i++) { 129 Object value = query.getValue(i); 130 131 DbAttribute attribute = (DbAttribute) updatedDbAttributes.get(i); 132 adapter.bindParameter( 133 statement, 134 value, 135 parameterIndex++, 136 attribute.getType(), 137 attribute.getPrecision()); 138 } 139 140 for (int i = 0; i < qualifierAttributes.size(); i++) { 141 Object value = query.getValue(len + i); 142 143 if (null == value) 145 continue; 146 147 DbAttribute attribute = (DbAttribute) qualifierAttributes.get(i); 148 adapter.bindParameter( 149 statement, 150 value, 151 parameterIndex++, 152 attribute.getType(), 153 attribute.getPrecision()); 154 } 155 } 156 } | Popular Tags |