1 21 package oracle.toplink.essentials.queryframework; 23 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 27 import oracle.toplink.essentials.exceptions.DatabaseException; 28 import oracle.toplink.essentials.exceptions.QueryException; 29 import oracle.toplink.essentials.expressions.Expression; 30 import oracle.toplink.essentials.descriptors.ClassDescriptor; 31 import oracle.toplink.essentials.expressions.ExpressionBuilder; 32 33 57 public class UpdateAllQuery extends ModifyAllQuery { 58 59 protected HashMap m_updateClauses; 60 61 65 public UpdateAllQuery() { 66 super(); 67 } 68 69 74 public UpdateAllQuery(Class referenceClass) { 75 super(referenceClass); 76 } 77 78 84 public UpdateAllQuery(Class referenceClass, Expression selectionCriteria) { 85 super(referenceClass, selectionCriteria); 86 } 87 88 96 public UpdateAllQuery(Class referenceClass, ExpressionBuilder expressionBuilder) { 97 super(referenceClass); 98 this.defaultBuilder = expressionBuilder; 99 } 100 101 107 public void addUpdate(Expression field, Object value) { 108 addUpdateInternal(field, value); 109 } 110 111 117 public void addUpdate(String attributeName, Object value) { 118 addUpdateInternal(attributeName, value); 119 } 120 121 129 public void addUpdate(Expression field, Expression value) { 130 addUpdateInternal(field, value); 131 } 132 133 139 public void addUpdate(String attributeName, Expression value) { 140 addUpdateInternal(attributeName, value); 141 } 142 143 146 protected void addUpdateInternal(Object fieldObject, Object valueObject) { 147 if(fieldObject == null) { 148 throw QueryException.updateAllQueryAddUpdateFieldIsNull(this); 149 } 150 if(m_updateClauses == null) { 151 m_updateClauses = new HashMap (); 152 } 153 m_updateClauses.put(fieldObject, valueObject); 154 } 155 156 164 public Object executeDatabaseQuery() throws DatabaseException { 165 result = getQueryMechanism().updateAll(); mergeChangesIntoSharedCache(); 167 return result; 168 } 169 170 174 public HashMap getUpdateClauses() { 175 return m_updateClauses; 176 } 177 178 182 public boolean isUpdateAllQuery() { 183 return true; 184 } 185 186 189 protected void prepare() throws QueryException { 190 super.prepare(); 192 Class referenceClass = getReferenceClass(); 193 194 if (referenceClass == null) { 196 throw QueryException.referenceClassMissing(this); 197 } 198 199 if (getDescriptor() == null) { 201 ClassDescriptor desc = getSession().getDescriptor(referenceClass); 202 203 if (desc == null) { 204 throw QueryException.descriptorIsMissing(referenceClass, this); 205 } 206 207 setDescriptor(desc); 208 } 209 210 ClassDescriptor descriptor = getDescriptor(); 211 212 if (descriptor.isAggregateDescriptor()) { 214 throw QueryException.aggregateObjectCannotBeDeletedOrWritten(descriptor, this); 215 } 216 217 if ((getUpdateClauses() == null || getUpdateClauses().isEmpty()) && isExpressionQuery()) { 220 throw QueryException.updateStatementsNotSpecified(); 221 } 222 223 getQueryMechanism().prepareUpdateAll(); 224 } 225 226 233 protected void initializeQuerySpecificDefaultBuilder() { 234 super.initializeQuerySpecificDefaultBuilder(); 235 if(this.defaultBuilder == null && m_updateClauses != null) { 236 Iterator it = m_updateClauses.values().iterator(); 237 while(it.hasNext() ) { 238 Object value = it.next(); 239 if(value != null) { 240 if(value instanceof Expression) { 241 Expression valueExpression = (Expression)value; 242 this.defaultBuilder = valueExpression.getBuilder(); 243 if(this.defaultBuilder != null) { 244 return; 245 } 246 } 247 } 248 } 249 it = m_updateClauses.keySet().iterator(); 250 while(it.hasNext() ) { 251 Object field = it.next(); 252 if(field instanceof Expression) { 253 Expression fieldExpression = (Expression)field; 254 this.defaultBuilder = fieldExpression.getBuilder(); 255 if(this.defaultBuilder != null) { 256 return; 257 } 258 } 259 } 260 } 261 } 262 263 } | Popular Tags |