1 21 package oracle.toplink.essentials.sequencing; 23 24 import java.util.Vector ; 25 import java.math.BigDecimal ; 26 import oracle.toplink.essentials.sessions.Record; 27 import oracle.toplink.essentials.queryframework.*; 28 import oracle.toplink.essentials.internal.databaseaccess.Accessor; 29 import oracle.toplink.essentials.internal.sessions.AbstractSession; 30 31 36 public class QuerySequence extends StandardSequence { 37 protected ValueReadQuery selectQuery; 38 protected DataModifyQuery updateQuery; 39 protected boolean shouldAcquireValueAfterInsert; 40 protected boolean shouldUseTransaction; 41 protected boolean shouldSkipUpdate; 42 protected boolean shouldSelectBeforeUpdate; 43 protected boolean wasSelectQueryCreated; 44 protected boolean wasUpdateQueryCreated; 45 46 public QuerySequence() { 47 super(); 48 } 49 50 public QuerySequence(String name) { 51 super(name); 52 } 53 54 public QuerySequence(String name, int size) { 55 super(name, size); 56 } 57 58 public QuerySequence(String name, int size, int initialValue) { 59 super(name, size, initialValue); 60 } 61 62 public QuerySequence(boolean shouldAcquireValueAfterInsert, boolean shouldUseTransaction) { 63 super(); 64 setShouldAcquireValueAfterInsert(shouldAcquireValueAfterInsert); 65 setShouldUseTransaction(shouldUseTransaction); 66 } 67 68 public QuerySequence(String name, boolean shouldAcquireValueAfterInsert, boolean shouldUseTransaction) { 69 super(name); 70 setShouldAcquireValueAfterInsert(shouldAcquireValueAfterInsert); 71 setShouldUseTransaction(shouldUseTransaction); 72 } 73 74 public QuerySequence(String name, int size, boolean shouldAcquireValueAfterInsert, boolean shouldUseTransaction) { 75 super(name, size); 76 setShouldAcquireValueAfterInsert(shouldAcquireValueAfterInsert); 77 setShouldUseTransaction(shouldUseTransaction); 78 } 79 80 public QuerySequence(String name, int size, int initialValue, 81 boolean shouldAcquireValueAfterInsert, boolean shouldUseTransaction) { 82 super(name, size, initialValue); 83 setShouldAcquireValueAfterInsert(shouldAcquireValueAfterInsert); 84 setShouldUseTransaction(shouldUseTransaction); 85 } 86 87 public boolean equals(Object obj) { 88 if (obj instanceof QuerySequence && super.equals(obj)) { 89 QuerySequence other = (QuerySequence)obj; 90 return (getSelectQuery() == other.getSelectQuery()) && (getUpdateQuery() == other.getUpdateQuery()) && (shouldAcquireValueAfterInsert() == other.shouldAcquireValueAfterInsert()) && (shouldUseTransaction() == other.shouldUseTransaction()) && (shouldSkipUpdate() == other.shouldSkipUpdate()) && (shouldSelectBeforeUpdate() == other.shouldSelectBeforeUpdate()); 91 92 } else { 93 return false; 94 } 95 } 96 97 100 public boolean shouldAcquireValueAfterInsert() { 101 return shouldAcquireValueAfterInsert; 102 } 103 104 107 public void setShouldAcquireValueAfterInsert(boolean shouldAcquireValueAfterInsert) { 108 this.shouldAcquireValueAfterInsert = shouldAcquireValueAfterInsert; 109 } 110 111 114 public boolean shouldUseTransaction() { 115 return shouldUseTransaction; 116 } 117 118 121 public void setShouldUseTransaction(boolean shouldUseTransaction) { 122 this.shouldUseTransaction = shouldUseTransaction; 123 } 124 125 128 public void setSelectQuery(ValueReadQuery query) { 129 selectQuery = query; 130 } 131 132 135 public ValueReadQuery getSelectQuery() { 136 return selectQuery; 137 } 138 139 142 public void setUpdateQuery(DataModifyQuery query) { 143 updateQuery = query; 144 } 145 146 149 public DataModifyQuery getUpdateQuery() { 150 return updateQuery; 151 } 152 153 156 public void setShouldSkipUpdate(boolean shouldSkipUpdate) { 157 this.shouldSkipUpdate = shouldSkipUpdate; 158 } 159 160 163 public boolean shouldSkipUpdate() { 164 return shouldSkipUpdate; 165 } 166 167 170 public void setShouldSelectBeforeUpdate(boolean shouldSelectBeforeUpdate) { 171 this.shouldSelectBeforeUpdate = shouldSelectBeforeUpdate; 172 } 173 174 177 public boolean shouldSelectBeforeUpdate() { 178 return shouldSelectBeforeUpdate; 179 } 180 181 184 protected ValueReadQuery buildSelectQuery() { 185 return null; 186 } 187 188 191 protected DataModifyQuery buildUpdateQuery() { 192 return null; 193 } 194 195 198 protected ValueReadQuery buildSelectQuery(String seqName, Integer size) { 199 return null; 200 } 201 202 205 protected DataModifyQuery buildUpdateQuery(String seqName, Number sizeOrNewValue) { 206 return null; 207 } 208 209 212 public void onConnect() { 213 super.onConnect(); 214 if (getSelectQuery() == null) { 215 setSelectQuery(buildSelectQuery()); 216 wasSelectQueryCreated = getSelectQuery() != null; 217 } 218 if ((getUpdateQuery() == null) && !shouldSkipUpdate()) { 219 setUpdateQuery(buildUpdateQuery()); 220 wasUpdateQueryCreated = getUpdateQuery() != null; 221 } 222 } 223 224 227 public void onDisconnect() { 228 if (wasSelectQueryCreated) { 229 setSelectQuery(null); 230 wasSelectQueryCreated = false; 231 } 232 if (wasUpdateQueryCreated) { 233 setUpdateQuery(null); 234 wasUpdateQueryCreated = false; 235 } 236 super.onDisconnect(); 237 } 238 239 242 protected Number updateAndSelectSequence(Accessor accessor, AbstractSession writeSession, String seqName, int size) { 243 Integer sizeInteger = new Integer (size); 244 if (shouldSkipUpdate()) { 245 return (Number )select(accessor, writeSession, seqName, sizeInteger); 246 } else { 247 if (shouldSelectBeforeUpdate()) { 248 Object result = select(accessor, writeSession, seqName, sizeInteger); 249 BigDecimal currentValue; 250 if (result instanceof Number ) { 251 currentValue = new BigDecimal (((Number )result).longValue()); 252 } else if (result instanceof String ) { 253 currentValue = new BigDecimal ((String )result); 254 } else if (result instanceof Record) { 255 Object val = ((Record)result).get("text()"); 256 currentValue = new BigDecimal ((String )val); 257 } else { 258 return null; 260 } 261 262 BigDecimal newValue = currentValue.add(new BigDecimal (size)); 264 265 update(accessor, writeSession, seqName, newValue); 266 return newValue; 267 } else { 268 update(accessor, writeSession, seqName, sizeInteger); 269 return (Number )select(accessor, writeSession, seqName, sizeInteger); 270 } 271 } 272 } 273 274 277 protected Object select(Accessor accessor, AbstractSession writeSession, String seqName, Integer size) { 278 ValueReadQuery query = getSelectQuery(); 279 if (query != null) { 280 if (accessor != null) { 281 query.setIsUserDefined(true); 285 query.checkPrepare(writeSession, null); 286 query = (ValueReadQuery)query.clone(); 287 query.setAccessor(accessor); 288 } 289 } else { 290 query = buildSelectQuery(seqName, size); 291 if (accessor != null) { 292 query.setAccessor(accessor); 293 } 294 } 295 Vector args = createArguments(query, seqName, size); 296 if (args != null) { 297 return writeSession.executeQuery(query, args); 298 } else { 299 return writeSession.executeQuery(query); 300 } 301 } 302 303 306 protected void update(Accessor accessor, AbstractSession writeSession, String seqName, Number sizeOrNewValue) { 307 DataModifyQuery query = getUpdateQuery(); 308 if (query != null) { 309 if (accessor != null) { 310 query.setIsUserDefined(true); 314 query.checkPrepare(writeSession, null); 315 query = (DataModifyQuery)query.clone(); 316 query.setAccessor(accessor); 317 } 318 } else { 319 query = buildUpdateQuery(seqName, sizeOrNewValue); 320 if (query == null) { 321 return; 322 } 323 if (accessor != null) { 324 query.setAccessor(accessor); 325 } 326 } 327 Vector args = createArguments(query, seqName, sizeOrNewValue); 328 if (args != null) { 329 writeSession.executeQuery(query, args); 330 } else { 331 writeSession.executeQuery(query); 332 } 333 } 334 335 338 protected Vector createArguments(DatabaseQuery query, String seqName, Number sizeOrNewValue) { 339 int nArgs = query.getArguments().size(); 340 if (nArgs > 0) { 341 Vector args = new Vector (nArgs); 342 args.addElement(seqName); 343 if (nArgs > 1) { 344 args.addElement(sizeOrNewValue); 345 } 346 return args; 347 } else { 348 return null; 349 } 350 } 351 } 352 | Popular Tags |