1 21 package oracle.toplink.essentials.internal.queryframework; 23 24 import java.util.*; 25 import oracle.toplink.essentials.internal.expressions.*; 26 import oracle.toplink.essentials.internal.databaseaccess.*; 27 import oracle.toplink.essentials.expressions.*; 28 import oracle.toplink.essentials.internal.helper.*; 29 import oracle.toplink.essentials.exceptions.*; 30 import oracle.toplink.essentials.queryframework.*; 31 import oracle.toplink.essentials.sessions.SessionProfiler; 32 33 43 public class StatementQueryMechanism extends CallQueryMechanism { 44 protected SQLStatement sqlStatement; 45 46 47 protected Vector sqlStatements; 48 49 54 public StatementQueryMechanism(DatabaseQuery query) { 55 super(query); 56 } 57 58 63 public StatementQueryMechanism(DatabaseQuery query, SQLStatement statement) { 64 super(query); 65 this.sqlStatement = statement; 66 } 67 68 71 public void clearStatement() { 72 if (isExpressionQueryMechanism()) { 74 setSQLStatement(null); 75 setSQLStatements(null); 76 } 77 } 78 79 82 public DatabaseQueryMechanism clone(DatabaseQuery queryClone) { 83 StatementQueryMechanism clone = (StatementQueryMechanism)super.clone(queryClone); 84 if ((!hasMultipleStatements()) && (getSQLStatement() != null)) { 85 clone.setSQLStatement((SQLStatement)sqlStatement.clone()); 86 } else { 87 Vector currentStatements = getSQLStatements(); 88 if (currentStatements != null) { 89 Vector statementClone = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(currentStatements.size()); 90 Enumeration enumtr = currentStatements.elements(); 91 while (enumtr.hasMoreElements()) { 92 statementClone.addElement(((SQLStatement)enumtr.nextElement()).clone()); 93 } 94 clone.setSQLStatements(statementClone); 95 } 96 } 97 return clone; 98 } 99 100 106 public Integer deleteObject() throws DatabaseException { 107 if ((!hasMultipleCalls()) && (getCall() == null)) { 109 prepareDeleteObject(); 110 if ((!hasMultipleCalls()) && (getCall() == null)) { 111 return new Integer (1); } 113 } 114 115 return super.deleteObject(); 116 } 117 118 123 public Integer executeNoSelect() throws DatabaseException { 124 if ((!hasMultipleCalls()) && (getCall() == null)) { 126 prepareExecuteNoSelect(); 127 } 128 129 return super.executeNoSelect(); 130 } 131 132 135 public Expression getSelectionCriteria() { 136 return getSQLStatement().getWhereClause(); 137 } 138 139 143 public SQLStatement getSQLStatement() { 144 return sqlStatement; 145 } 146 147 151 public Vector getSQLStatements() { 152 if (sqlStatements == null) { 153 sqlStatements = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(3); 154 } 155 return sqlStatements; 156 } 157 158 162 public boolean hasMultipleStatements() { 163 return (sqlStatements != null) && (!sqlStatements.isEmpty()); 164 } 165 166 170 public void insertObject() throws DatabaseException { 171 if ((!hasMultipleCalls()) && (getCall() == null)) { 173 prepareInsertObject(); 174 } 175 176 super.insertObject(); 177 } 178 179 184 public void insertObject(boolean reprepare) { 185 if (reprepare) { 186 setCalls(null); 188 prepareInsertObject(); 189 } 190 insertObject(); 191 } 192 193 196 public boolean isCallQueryMechanism() { 197 return false; 198 } 199 200 203 public boolean isStatementQueryMechanism() { 204 return true; 205 } 206 207 214 public void prepare() { 215 if ((!hasMultipleStatements()) && (getSQLStatement() == null)) { 216 throw QueryException.sqlStatementNotSetProperly(getQuery()); 217 } 218 219 } 221 222 225 public void prepareCursorSelectAllRows() { 226 setCallFromStatement(); 227 clearStatement(); 229 230 super.prepareCursorSelectAllRows(); 231 } 232 233 236 public void prepareDeleteAll() { 237 setCallFromStatement(); 238 clearStatement(); 240 241 super.prepareDeleteAll(); 242 } 243 244 247 public void prepareDeleteObject() { 248 setCallFromStatement(); 249 clearStatement(); 251 252 super.prepareDeleteObject(); 253 } 254 255 258 public void prepareDoesExist(DatabaseField field) { 259 setCallFromStatement(); 260 clearStatement(); 262 263 getCall().returnOneRow(); 264 prepareCall(); 265 } 266 267 270 public void prepareExecuteNoSelect() { 271 setCallFromStatement(); 272 clearStatement(); 274 275 super.prepareExecuteNoSelect(); 276 } 277 278 281 public void prepareExecuteSelect() { 282 setCallFromStatement(); 283 clearStatement(); 285 286 super.prepareExecuteSelect(); 287 } 288 289 292 public void prepareInsertObject() { 293 if (getModifyRow() == null) { 295 return; 296 } 297 298 if (hasMultipleStatements()) { 299 for (Enumeration statementEnum = getSQLStatements().elements(); 300 statementEnum.hasMoreElements();) { 301 ((SQLModifyStatement)statementEnum.nextElement()).setModifyRow(getModifyRow()); 302 } 303 } else if (getSQLStatement() != null) { 304 ((SQLModifyStatement)getSQLStatement()).setModifyRow(getModifyRow()); 305 } 306 setCallFromStatement(); 307 clearStatement(); 309 310 super.prepareInsertObject(); 311 } 312 313 316 public void prepareSelectAllRows() { 317 setCallFromStatement(); 318 clearStatement(); 320 321 super.prepareSelectAllRows(); 322 } 323 324 327 public void prepareSelectOneRow() { 328 setCallFromStatement(); 329 clearStatement(); 331 332 super.prepareSelectOneRow(); 333 } 334 335 338 public void prepareUpdateObject() { 339 if (getModifyRow() == null) { 341 return; 342 } 343 344 if (hasMultipleStatements()) { 345 for (Enumeration statementEnum = getSQLStatements().elements(); 346 statementEnum.hasMoreElements();) { 347 ((SQLModifyStatement)statementEnum.nextElement()).setModifyRow(getModifyRow()); 348 } 349 } else if (getSQLStatement() != null) { 350 ((SQLModifyStatement)getSQLStatement()).setModifyRow(getModifyRow()); 351 } 352 setCallFromStatement(); 353 clearStatement(); 355 356 super.prepareUpdateObject(); 357 } 358 359 362 public void prepareUpdateAll() { 363 setCallFromStatement(); clearStatement(); super.prepareUpdateAll(); 366 } 367 368 371 protected void setCallFromStatement() { 372 getSession().startOperationProfile(SessionProfiler.SQL_GENERATION); 374 if (hasMultipleStatements()) { 375 for (Enumeration statementEnum = getSQLStatements().elements(); 376 statementEnum.hasMoreElements();) { 377 DatabaseCall call = null; 379 if (getDescriptor() != null) { 380 call = getDescriptor().buildCallFromStatement((SQLStatement)statementEnum.nextElement(), getSession()); 381 } else { 382 call = ((SQLStatement)statementEnum.nextElement()).buildCall(getSession()); 383 } 384 385 if (call != null) { 387 addCall(call); 388 } 389 } 390 } else { 391 DatabaseCall call = null; 392 if (getDescriptor() != null) { 393 call = getDescriptor().buildCallFromStatement(getSQLStatement(), getSession()); 394 } else { 395 call = getSQLStatement().buildCall(getSession()); 396 } 397 398 if (call != null) { 400 setCall(call); 401 } 402 } 403 404 getSession().endOperationProfile(SessionProfiler.SQL_GENERATION); 406 } 407 408 411 public void setSQLStatement(SQLStatement statement) { 412 this.sqlStatement = statement; 413 } 414 415 419 protected void setSQLStatements(Vector sqlStatements) { 420 this.sqlStatements = sqlStatements; 421 } 422 423 428 public Integer updateObject() throws DatabaseException { 429 if ((!hasMultipleCalls()) && (getCall() == null)) { 431 prepareUpdateObject(); 432 if ((!hasMultipleCalls()) && (getCall() == null)) { 433 return new Integer (1); } 435 } 436 437 return super.updateObject(); 438 } 439 } 440 | Popular Tags |