001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.store.jdbc;
018
019/**
020 * 
021 * 
022 * @org.apache.xbean.XBean element="statements"
023 * 
024 */
025public class Statements {
026
027    protected String messageTableName = "ACTIVEMQ_MSGS";
028    protected String durableSubAcksTableName = "ACTIVEMQ_ACKS";
029    protected String lockTableName = "ACTIVEMQ_LOCK";
030    protected String binaryDataType = "BLOB";
031    protected String containerNameDataType = "VARCHAR(250)";
032    protected String msgIdDataType = "VARCHAR(250)";
033    protected String sequenceDataType = "BIGINT";
034    protected String longDataType = "BIGINT";
035    protected String stringIdDataType = "VARCHAR(250)";
036    protected boolean useExternalMessageReferences;
037
038    private String tablePrefix = "";
039    private String addMessageStatement;
040    private String updateMessageStatement;
041    private String removeMessageStatement;
042    private String findMessageSequenceIdStatement;
043    private String findMessageStatement;
044    private String findMessageByIdStatement;
045    private String findAllMessagesStatement;
046    private String findLastSequenceIdInMsgsStatement;
047    private String findLastSequenceIdInAcksStatement;
048    private String createDurableSubStatement;
049    private String findDurableSubStatement;
050    private String findAllDurableSubsStatement;
051    private String updateLastPriorityAckRowOfDurableSubStatement;
052    private String deleteSubscriptionStatement;
053    private String findAllDurableSubMessagesStatement;
054    private String findDurableSubMessagesStatement;
055    private String findDurableSubMessagesByPriorityStatement;
056    private String findAllDestinationsStatement;
057    private String removeAllMessagesStatement;
058    private String removeAllSubscriptionsStatement;
059    private String[] createSchemaStatements;
060    private String[] createLockSchemaStatements;
061    private String[] dropSchemaStatements;
062    private String lockCreateStatement;
063    private String lockUpdateStatement;
064    private String nextDurableSubscriberMessageStatement;
065    private String durableSubscriberMessageCountStatement;
066    private String lastAckedDurableSubscriberMessageStatement;
067    private String destinationMessageCountStatement;
068    private String findNextMessagesStatement;
069    private String findNextMessagesByPriorityStatement;
070    private boolean useLockCreateWhereClause;
071    private String findAllMessageIdsStatement;
072    private String lastProducerSequenceIdStatement;
073    private String selectDurablePriorityAckStatement;
074
075    private String insertDurablePriorityAckStatement;
076    private String updateDurableLastAckStatement;
077    private String deleteOldMessagesStatementWithPriority;
078    private String durableSubscriberMessageCountStatementWithPriority;
079    private String dropAckPKAlterStatementEnd;
080    private String updateXidFlagStatement;
081    private String findOpsPendingOutcomeStatement;
082    private String clearXidFlagStatement;
083    private String updateDurableLastAckInTxStatement;
084    private String findAcksPendingOutcomeStatement;
085    private String clearDurableLastAckInTxStatement;
086    private String updateDurableLastAckWithPriorityStatement;
087    private String updateDurableLastAckWithPriorityInTxStatement;
088    private String findXidByIdStatement;
089    private String leaseObtainStatement;
090    private String currentDateTimeStatement;
091    private String leaseUpdateStatement;
092    private String leaseOwnerStatement;
093
094    public String[] getCreateSchemaStatements() {
095        if (createSchemaStatements == null) {
096            createSchemaStatements = new String[] {
097                "CREATE TABLE " + getFullMessageTableName() + "(" + "ID " + sequenceDataType + " NOT NULL"
098                    + ", CONTAINER " + containerNameDataType + ", MSGID_PROD " + msgIdDataType + ", MSGID_SEQ "
099                    + sequenceDataType + ", EXPIRATION " + longDataType + ", MSG "
100                    + (useExternalMessageReferences ? stringIdDataType : binaryDataType)
101                    + ", PRIMARY KEY ( ID ) )",
102                "CREATE INDEX " + getFullMessageTableName() + "_MIDX ON " + getFullMessageTableName() + " (MSGID_PROD,MSGID_SEQ)",
103                "CREATE INDEX " + getFullMessageTableName() + "_CIDX ON " + getFullMessageTableName() + " (CONTAINER)",
104                "CREATE INDEX " + getFullMessageTableName() + "_EIDX ON " + getFullMessageTableName() + " (EXPIRATION)",
105                "CREATE TABLE " + getFullAckTableName() + "(" + "CONTAINER " + containerNameDataType + " NOT NULL"
106                    + ", SUB_DEST " + stringIdDataType 
107                    + ", CLIENT_ID " + stringIdDataType + " NOT NULL" + ", SUB_NAME " + stringIdDataType
108                    + " NOT NULL" + ", SELECTOR " + stringIdDataType + ", LAST_ACKED_ID " + sequenceDataType
109                    + ", PRIMARY KEY ( CONTAINER, CLIENT_ID, SUB_NAME))", 
110                "ALTER TABLE " + getFullMessageTableName() + " ADD PRIORITY " + sequenceDataType,
111                "CREATE INDEX " + getFullMessageTableName() + "_PIDX ON " + getFullMessageTableName() + " (PRIORITY)",
112                "ALTER TABLE " + getFullMessageTableName() + " ADD XID " + stringIdDataType,
113                "ALTER TABLE " + getFullAckTableName() + " ADD PRIORITY " + sequenceDataType  + " DEFAULT 5 NOT NULL",
114                "ALTER TABLE " + getFullAckTableName() + " ADD XID " + stringIdDataType,
115                "ALTER TABLE " + getFullAckTableName() + " " + getDropAckPKAlterStatementEnd(),
116                "ALTER TABLE " + getFullAckTableName() + " ADD PRIMARY KEY (CONTAINER, CLIENT_ID, SUB_NAME, PRIORITY)",
117                "CREATE INDEX " + getFullMessageTableName() + "_XIDX ON " + getFullMessageTableName() + " (XID)",
118                "CREATE INDEX " + getFullAckTableName() + "_XIDX ON " + getFullAckTableName() + " (XID)"
119            };
120        }
121        getCreateLockSchemaStatements();
122        String[] allCreateStatements = new String[createSchemaStatements.length + createLockSchemaStatements.length];
123        System.arraycopy(createSchemaStatements, 0, allCreateStatements, 0, createSchemaStatements.length);
124        System.arraycopy(createLockSchemaStatements, 0, allCreateStatements, createSchemaStatements.length, createLockSchemaStatements.length);
125
126        return allCreateStatements;
127    }
128
129    public String[] getCreateLockSchemaStatements() {
130        if (createLockSchemaStatements == null) {
131            createLockSchemaStatements = new String[] {
132                "CREATE TABLE " + getFullLockTableName()
133                    + "( ID " + longDataType + " NOT NULL, TIME " + longDataType
134                    + ", BROKER_NAME " + stringIdDataType + ", PRIMARY KEY (ID) )",
135                "INSERT INTO " + getFullLockTableName() + "(ID) VALUES (1)"
136            };
137        }
138        return createLockSchemaStatements;
139    }
140
141    public String getDropAckPKAlterStatementEnd() {
142        if (dropAckPKAlterStatementEnd == null) {
143            dropAckPKAlterStatementEnd = "DROP PRIMARY KEY";
144        }
145        return dropAckPKAlterStatementEnd;
146    }
147
148    public void setDropAckPKAlterStatementEnd(String dropAckPKAlterStatementEnd) {
149        this.dropAckPKAlterStatementEnd = dropAckPKAlterStatementEnd;
150    }
151
152    public String[] getDropSchemaStatements() {
153        if (dropSchemaStatements == null) {
154            dropSchemaStatements = new String[] {"DROP TABLE " + getFullAckTableName() + "",
155                                                 "DROP TABLE " + getFullMessageTableName() + "",
156                                                 "DROP TABLE " + getFullLockTableName() + ""};
157        }
158        return dropSchemaStatements;
159    }
160
161    public String getAddMessageStatement() {
162        if (addMessageStatement == null) {
163            addMessageStatement = "INSERT INTO "
164                                  + getFullMessageTableName()
165                                  + "(ID, MSGID_PROD, MSGID_SEQ, CONTAINER, EXPIRATION, PRIORITY, MSG, XID) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
166        }
167        return addMessageStatement;
168    }
169
170    public String getUpdateMessageStatement() {
171        if (updateMessageStatement == null) {
172            updateMessageStatement = "UPDATE " + getFullMessageTableName() + " SET MSG=? WHERE MSGID_PROD=? AND MSGID_SEQ=? AND CONTAINER=?";
173        }
174        return updateMessageStatement;
175    }
176
177    public String getRemoveMessageStatement() {
178        if (removeMessageStatement == null) {
179            removeMessageStatement = "DELETE FROM " + getFullMessageTableName() + " WHERE ID=?";
180        }
181        return removeMessageStatement;
182    }
183
184    public String getFindMessageSequenceIdStatement() {
185        if (findMessageSequenceIdStatement == null) {
186            findMessageSequenceIdStatement = "SELECT ID, PRIORITY FROM " + getFullMessageTableName()
187                                             + " WHERE MSGID_PROD=? AND MSGID_SEQ=? AND CONTAINER=?";
188        }
189        return findMessageSequenceIdStatement;
190    }
191
192    public String getFindMessageStatement() {
193        if (findMessageStatement == null) {
194            findMessageStatement = "SELECT MSG FROM " + getFullMessageTableName() + " WHERE MSGID_PROD=? AND MSGID_SEQ=?";
195        }
196        return findMessageStatement;
197    }
198
199    public String getFindMessageByIdStatement() {
200        if (findMessageByIdStatement == null) {
201                findMessageByIdStatement = "SELECT MSG FROM " + getFullMessageTableName() + " WHERE ID=?";
202        }
203        return findMessageByIdStatement;
204    }
205
206    public String getFindXidByIdStatement() {
207        if (findXidByIdStatement == null) {
208            findXidByIdStatement = "SELECT XID FROM " + getFullMessageTableName() + " WHERE ID=?";
209        }
210        return findXidByIdStatement;
211    }
212
213    public String getFindAllMessagesStatement() {
214        if (findAllMessagesStatement == null) {
215            findAllMessagesStatement = "SELECT ID, MSG FROM " + getFullMessageTableName()
216                                       + " WHERE CONTAINER=? ORDER BY ID";
217        }
218        return findAllMessagesStatement;
219    }
220    
221    public String getFindAllMessageIdsStatement() {
222        //  this needs to be limited maybe need to use getFindLastSequenceIdInMsgsStatement
223        // and work back for X
224        if (findAllMessageIdsStatement == null) {
225            findAllMessageIdsStatement = "SELECT ID, MSGID_PROD, MSGID_SEQ FROM " + getFullMessageTableName()
226                                       + " ORDER BY ID DESC";
227        }
228        return findAllMessageIdsStatement;
229    }
230
231    public String getFindLastSequenceIdInMsgsStatement() {
232        if (findLastSequenceIdInMsgsStatement == null) {
233            findLastSequenceIdInMsgsStatement = "SELECT MAX(ID) FROM " + getFullMessageTableName();
234        }
235        return findLastSequenceIdInMsgsStatement;
236    }
237
238    public String getLastProducerSequenceIdStatement() {
239        if (lastProducerSequenceIdStatement == null) {
240            lastProducerSequenceIdStatement = "SELECT MAX(MSGID_SEQ) FROM " + getFullMessageTableName()
241                                            + " WHERE MSGID_PROD=?";
242        }
243        return lastProducerSequenceIdStatement;
244    }
245
246
247    public String getFindLastSequenceIdInAcksStatement() {
248        if (findLastSequenceIdInAcksStatement == null) {
249            findLastSequenceIdInAcksStatement = "SELECT MAX(LAST_ACKED_ID) FROM " + getFullAckTableName();
250        }
251        return findLastSequenceIdInAcksStatement;
252    }
253
254    public String getCreateDurableSubStatement() {
255        if (createDurableSubStatement == null) {
256            createDurableSubStatement = "INSERT INTO "
257                                        + getFullAckTableName()
258                                        + "(CONTAINER, CLIENT_ID, SUB_NAME, SELECTOR, LAST_ACKED_ID, SUB_DEST, PRIORITY) "
259                                        + "VALUES (?, ?, ?, ?, ?, ?, ?)";
260        }
261        return createDurableSubStatement;
262    }
263
264    public String getFindDurableSubStatement() {
265        if (findDurableSubStatement == null) {
266            findDurableSubStatement = "SELECT SELECTOR, SUB_DEST " + "FROM " + getFullAckTableName()
267                                      + " WHERE CONTAINER=? AND CLIENT_ID=? AND SUB_NAME=?";
268        }
269        return findDurableSubStatement;
270    }
271
272    public String getFindAllDurableSubsStatement() {
273        if (findAllDurableSubsStatement == null) {
274            findAllDurableSubsStatement = "SELECT SELECTOR, SUB_NAME, CLIENT_ID, SUB_DEST" + " FROM "
275                                          + getFullAckTableName() + " WHERE CONTAINER=? AND PRIORITY=0";
276        }
277        return findAllDurableSubsStatement;
278    }
279
280    public String getUpdateLastPriorityAckRowOfDurableSubStatement() {
281        if (updateLastPriorityAckRowOfDurableSubStatement == null) {
282            updateLastPriorityAckRowOfDurableSubStatement = "UPDATE " + getFullAckTableName() + " SET LAST_ACKED_ID=?"
283                                                 + " WHERE CONTAINER=? AND CLIENT_ID=? AND SUB_NAME=? AND PRIORITY=?";
284        }
285        return updateLastPriorityAckRowOfDurableSubStatement;
286    }
287
288    public String getDeleteSubscriptionStatement() {
289        if (deleteSubscriptionStatement == null) {
290            deleteSubscriptionStatement = "DELETE FROM " + getFullAckTableName()
291                                          + " WHERE CONTAINER=? AND CLIENT_ID=? AND SUB_NAME=?";
292        }
293        return deleteSubscriptionStatement;
294    }
295
296    public String getFindAllDurableSubMessagesStatement() {
297        if (findAllDurableSubMessagesStatement == null) {
298            findAllDurableSubMessagesStatement = "SELECT M.ID, M.MSG FROM " + getFullMessageTableName()
299                                                 + " M, " + getFullAckTableName() + " D "
300                                                 + " WHERE D.CONTAINER=? AND D.CLIENT_ID=? AND D.SUB_NAME=?"
301                                                 + " AND M.CONTAINER=D.CONTAINER AND M.ID > D.LAST_ACKED_ID"
302                                                 + " ORDER BY M.PRIORITY DESC, M.ID";
303        }
304        return findAllDurableSubMessagesStatement;
305    }
306
307    public String getFindDurableSubMessagesStatement() {
308        if (findDurableSubMessagesStatement == null) {
309            findDurableSubMessagesStatement = "SELECT M.ID, M.MSG FROM " + getFullMessageTableName() + " M, "
310                                              + getFullAckTableName() + " D "
311                                              + " WHERE D.CONTAINER=? AND D.CLIENT_ID=? AND D.SUB_NAME=?"
312                                              + " AND M.XID IS NULL"
313                                              + " AND M.CONTAINER=D.CONTAINER AND M.ID > D.LAST_ACKED_ID"
314                                              + " AND M.ID > ?"
315                                              + " ORDER BY M.ID";
316        }
317        return findDurableSubMessagesStatement;
318    }
319    
320    public String getFindDurableSubMessagesByPriorityStatement() {
321        if (findDurableSubMessagesByPriorityStatement == null) {
322            findDurableSubMessagesByPriorityStatement = "SELECT M.ID, M.MSG FROM " + getFullMessageTableName() + " M,"
323                                              + " " + getFullAckTableName() + " D"
324                                              + " WHERE D.CONTAINER=? AND D.CLIENT_ID=? AND D.SUB_NAME=?"
325                                              + " AND M.XID IS NULL"
326                                              + " AND M.CONTAINER=D.CONTAINER"
327                                              + " AND M.PRIORITY=D.PRIORITY AND M.ID > D.LAST_ACKED_ID"
328                                              + " AND M.ID > ? AND M.PRIORITY = ?"
329                                              + " ORDER BY M.ID";
330        }
331        return findDurableSubMessagesByPriorityStatement;
332    }    
333
334    public String findAllDurableSubMessagesStatement() {
335        if (findAllDurableSubMessagesStatement == null) {
336            findAllDurableSubMessagesStatement = "SELECT M.ID, M.MSG FROM " + getFullMessageTableName()
337                                                 + " M, " + getFullAckTableName() + " D "
338                                                 + " WHERE D.CONTAINER=? AND D.CLIENT_ID=? AND D.SUB_NAME=?"
339                                                 + " AND M.CONTAINER=D.CONTAINER AND M.ID > D.LAST_ACKED_ID"
340                                                 + " ORDER BY M.ID";
341        }
342        return findAllDurableSubMessagesStatement;
343    }
344
345    public String getNextDurableSubscriberMessageStatement() {
346        if (nextDurableSubscriberMessageStatement == null) {
347            nextDurableSubscriberMessageStatement = "SELECT M.ID, M.MSG FROM "
348                                                    + getFullMessageTableName()
349                                                    + " M, "
350                                                    + getFullAckTableName()
351                                                    + " D "
352                                                    + " WHERE D.CONTAINER=? AND D.CLIENT_ID=? AND D.SUB_NAME=?"
353                                                    + " AND M.CONTAINER=D.CONTAINER AND M.ID > ?"
354                                                    + " ORDER BY M.ID ";
355        }
356        return nextDurableSubscriberMessageStatement;
357    }
358
359    /**
360     * @return the durableSubscriberMessageCountStatement
361     */
362
363    public String getDurableSubscriberMessageCountStatement() {
364        if (durableSubscriberMessageCountStatement == null) {
365            durableSubscriberMessageCountStatement = "SELECT COUNT(*) FROM "
366                                                     + getFullMessageTableName()
367                                                     + " M, "
368                                                     + getFullAckTableName()
369                                                     + " D "
370                                                     + " WHERE D.CONTAINER=? AND D.CLIENT_ID=? AND D.SUB_NAME=?"
371                                                     + " AND M.CONTAINER=D.CONTAINER "
372                                                     + "     AND M.ID >"
373                                                     + "          ( SELECT LAST_ACKED_ID FROM " + getFullAckTableName()
374                                                     + "           WHERE CONTAINER=D.CONTAINER AND CLIENT_ID=D.CLIENT_ID"
375                                                     + "           AND SUB_NAME=D.SUB_NAME )";
376
377        }
378        return durableSubscriberMessageCountStatement;
379    }
380
381    public String getDurableSubscriberMessageCountStatementWithPriority() {
382        if (durableSubscriberMessageCountStatementWithPriority == null) {
383            durableSubscriberMessageCountStatementWithPriority = "SELECT COUNT(*) FROM "
384                                                     + getFullMessageTableName()
385                                                     + " M, "
386                                                     + getFullAckTableName()
387                                                     + " D "
388                                                     + " WHERE D.CONTAINER=? AND D.CLIENT_ID=? AND D.SUB_NAME=?"
389                                                     + " AND M.CONTAINER=D.CONTAINER "
390                                                     + " AND M.PRIORITY=D.PRIORITY "
391                                                     + " AND M.ID > D.LAST_ACKED_ID";
392        }
393
394        return durableSubscriberMessageCountStatementWithPriority;
395    }
396
397    public String getFindAllDestinationsStatement() {
398        if (findAllDestinationsStatement == null) {
399            findAllDestinationsStatement = "SELECT DISTINCT CONTAINER FROM " + getFullMessageTableName()
400                    + " UNION SELECT DISTINCT CONTAINER FROM "  + getFullAckTableName();
401        }
402        return findAllDestinationsStatement;
403    }
404
405    public String getRemoveAllMessagesStatement() {
406        if (removeAllMessagesStatement == null) {
407            removeAllMessagesStatement = "DELETE FROM " + getFullMessageTableName() + " WHERE CONTAINER=?";
408        }
409        return removeAllMessagesStatement;
410    }
411
412    public String getRemoveAllSubscriptionsStatement() {
413        if (removeAllSubscriptionsStatement == null) {
414            removeAllSubscriptionsStatement = "DELETE FROM " + getFullAckTableName() + " WHERE CONTAINER=?";
415        }
416        return removeAllSubscriptionsStatement;
417    }
418
419    public String getDeleteOldMessagesStatementWithPriority() {
420        if (deleteOldMessagesStatementWithPriority == null) {
421            deleteOldMessagesStatementWithPriority = "DELETE FROM " + getFullMessageTableName()
422                                         + " WHERE (PRIORITY=? AND ID <= "
423                                         + "     ( SELECT min(" + getFullAckTableName() + ".LAST_ACKED_ID)"
424                                         + "       FROM " + getFullAckTableName() + " WHERE "
425                                         +          getFullAckTableName() + ".CONTAINER="
426                                         +          getFullMessageTableName() + ".CONTAINER"
427                                         + "        AND " + getFullAckTableName() + ".PRIORITY=?)"
428                                         + "   )";
429        }
430        return deleteOldMessagesStatementWithPriority;
431    }
432
433    public String getLockCreateStatement() {
434        if (lockCreateStatement == null) {
435            lockCreateStatement = "SELECT * FROM " + getFullLockTableName();
436            if (useLockCreateWhereClause) {
437                lockCreateStatement += " WHERE ID = 1";
438            }
439            lockCreateStatement += " FOR UPDATE";
440        }
441        return lockCreateStatement;
442    }
443
444    public String getLeaseObtainStatement() {
445        if (leaseObtainStatement == null) {
446            leaseObtainStatement = "UPDATE " + getFullLockTableName()
447                    + " SET BROKER_NAME=?, TIME=?"
448                    + " WHERE (TIME IS NULL OR TIME < ?) AND ID = 1";
449        }
450        return leaseObtainStatement;
451    }
452
453    public String getCurrentDateTime() {
454        if (currentDateTimeStatement == null) {
455            currentDateTimeStatement = "SELECT CURRENT_TIMESTAMP FROM " + getFullLockTableName();
456        }
457        return currentDateTimeStatement;
458    }
459
460    public String getLeaseUpdateStatement() {
461        if (leaseUpdateStatement == null) {
462            leaseUpdateStatement = "UPDATE " + getFullLockTableName()
463                    + " SET BROKER_NAME=?, TIME=?"
464                    + " WHERE BROKER_NAME=? AND ID = 1";
465        }
466        return leaseUpdateStatement;
467    }
468
469    public String getLeaseOwnerStatement() {
470        if (leaseOwnerStatement == null) {
471            leaseOwnerStatement = "SELECT BROKER_NAME, TIME FROM " + getFullLockTableName()
472                    + " WHERE ID = 1";
473        }
474        return leaseOwnerStatement;
475    }
476
477    public String getLockUpdateStatement() {
478        if (lockUpdateStatement == null) {
479            lockUpdateStatement = "UPDATE " + getFullLockTableName() + " SET TIME = ? WHERE ID = 1";
480        }
481        return lockUpdateStatement;
482    }
483
484    /**
485     * @return the destinationMessageCountStatement
486     */
487    public String getDestinationMessageCountStatement() {
488        if (destinationMessageCountStatement == null) {
489            destinationMessageCountStatement = "SELECT COUNT(*) FROM " + getFullMessageTableName()
490                                               + " WHERE CONTAINER=? AND XID IS NULL";
491        }
492        return destinationMessageCountStatement;
493    }
494
495    /**
496     * @return the findNextMessagesStatement
497     */
498    public String getFindNextMessagesStatement() {
499        if (findNextMessagesStatement == null) {
500            findNextMessagesStatement = "SELECT ID, MSG FROM " + getFullMessageTableName()
501                                        + " WHERE CONTAINER=? AND ID > ? AND ID < ? AND XID IS NULL ORDER BY ID";
502        }
503        return findNextMessagesStatement;
504    }
505
506    /**
507     * @return the findNextMessagesStatement
508     */
509    public String getFindNextMessagesByPriorityStatement() {
510        if (findNextMessagesByPriorityStatement == null) {
511            findNextMessagesByPriorityStatement = "SELECT ID, MSG FROM " + getFullMessageTableName()
512                                        + " WHERE CONTAINER=?"
513                                        + " AND XID IS NULL"
514                                        + " AND ((ID > ? AND ID < ? AND PRIORITY = ?) OR PRIORITY < ?)"
515                                        + " ORDER BY PRIORITY DESC, ID";
516        }
517        return findNextMessagesByPriorityStatement;
518    }    
519    
520    /**
521     * @return the lastAckedDurableSubscriberMessageStatement
522     */
523    public String getLastAckedDurableSubscriberMessageStatement() {
524        if (lastAckedDurableSubscriberMessageStatement == null) {
525            lastAckedDurableSubscriberMessageStatement = "SELECT MAX(LAST_ACKED_ID) FROM "
526                                                         + getFullAckTableName()
527                                                         + " WHERE CONTAINER=? AND CLIENT_ID=? AND SUB_NAME=?";                                                    
528        }
529        return lastAckedDurableSubscriberMessageStatement;
530    }
531
532    public String getSelectDurablePriorityAckStatement() {
533        if (selectDurablePriorityAckStatement == null) {
534            selectDurablePriorityAckStatement = "SELECT LAST_ACKED_ID FROM " + getFullAckTableName()
535                                                    + " WHERE CONTAINER=? AND CLIENT_ID=? AND SUB_NAME=?"
536                                                    + " AND PRIORITY = ?";
537        }
538        return selectDurablePriorityAckStatement;
539    }
540
541    public String getInsertDurablePriorityAckStatement() {
542        if (insertDurablePriorityAckStatement == null) {
543            insertDurablePriorityAckStatement = "INSERT INTO "
544                                  + getFullAckTableName()
545                                  + "(CONTAINER, CLIENT_ID, SUB_NAME, PRIORITY)"
546                                  + " VALUES (?, ?, ?, ?)";            
547       }
548        return insertDurablePriorityAckStatement;
549    }
550
551
552    public String getUpdateDurableLastAckStatement() {
553        if (updateDurableLastAckStatement == null) {
554            updateDurableLastAckStatement  = "UPDATE " + getFullAckTableName()
555                    + " SET LAST_ACKED_ID=?, XID = NULL WHERE CONTAINER=? AND CLIENT_ID=? AND SUB_NAME=?";
556        }
557        return  updateDurableLastAckStatement;
558    }
559
560    public String getUpdateDurableLastAckInTxStatement() {
561        if (updateDurableLastAckInTxStatement == null) {
562            updateDurableLastAckInTxStatement = "UPDATE " + getFullAckTableName()
563                    + " SET XID=? WHERE CONTAINER=? AND CLIENT_ID=? AND SUB_NAME=?";
564        }
565        return updateDurableLastAckInTxStatement;
566    }
567
568    public String getUpdateDurableLastAckWithPriorityStatement() {
569        if (updateDurableLastAckWithPriorityStatement == null) {
570            updateDurableLastAckWithPriorityStatement  = "UPDATE " + getFullAckTableName()
571                    + " SET LAST_ACKED_ID=?, XID = NULL WHERE CONTAINER=? AND CLIENT_ID=? AND SUB_NAME=? AND PRIORITY=?";
572        }
573        return  updateDurableLastAckWithPriorityStatement;
574    }
575
576    public String getUpdateDurableLastAckWithPriorityInTxStatement() {
577        if (updateDurableLastAckWithPriorityInTxStatement == null) {
578            updateDurableLastAckWithPriorityInTxStatement  = "UPDATE " + getFullAckTableName()
579                    + " SET XID=? WHERE CONTAINER=? AND CLIENT_ID=? AND SUB_NAME=? AND PRIORITY=?";
580        }
581        return  updateDurableLastAckWithPriorityInTxStatement;
582    }
583
584    public String getClearDurableLastAckInTxStatement() {
585        if (clearDurableLastAckInTxStatement == null) {
586            clearDurableLastAckInTxStatement = "UPDATE " + getFullAckTableName()
587                    + " SET XID = NULL WHERE CONTAINER=? AND CLIENT_ID=? AND SUB_NAME=? AND PRIORITY=?";
588        }
589        return clearDurableLastAckInTxStatement;
590    }
591
592    public String getFindOpsPendingOutcomeStatement() {
593        if (findOpsPendingOutcomeStatement == null) {
594            findOpsPendingOutcomeStatement = "SELECT ID, XID, MSG FROM " + getFullMessageTableName()
595                    + " WHERE XID IS NOT NULL ORDER BY ID";
596        }
597        return findOpsPendingOutcomeStatement;
598    }
599
600    public String getFindAcksPendingOutcomeStatement() {
601        if (findAcksPendingOutcomeStatement == null) {
602            findAcksPendingOutcomeStatement = "SELECT XID," +
603                    " CONTAINER, CLIENT_ID, SUB_NAME FROM " + getFullAckTableName()
604                    + " WHERE XID IS NOT NULL";
605        }
606        return findAcksPendingOutcomeStatement;
607    }
608
609    public String getUpdateXidFlagStatement() {
610        if (updateXidFlagStatement == null) {
611            updateXidFlagStatement = "UPDATE " + getFullMessageTableName()
612                    + " SET XID = ? WHERE ID = ?";
613        }
614        return updateXidFlagStatement;
615    }
616
617    public String getClearXidFlagStatement() {
618        if (clearXidFlagStatement == null) {
619            clearXidFlagStatement = "UPDATE "  + getFullMessageTableName()
620                    + " SET XID = NULL, ID = ? WHERE ID = ?";
621        }
622        return clearXidFlagStatement;
623    }
624
625    public String getFullMessageTableName() {
626        return getTablePrefix() + getMessageTableName();
627    }
628
629    public String getFullAckTableName() {
630        return getTablePrefix() + getDurableSubAcksTableName();
631    }
632
633    public String getFullLockTableName() {
634        return getTablePrefix() + getLockTableName();
635    }
636
637    /**
638     * @return Returns the containerNameDataType.
639     */
640    public String getContainerNameDataType() {
641        return containerNameDataType;
642    }
643
644    /**
645     * @param containerNameDataType The containerNameDataType to set.
646     */
647    public void setContainerNameDataType(String containerNameDataType) {
648        this.containerNameDataType = containerNameDataType;
649    }
650
651    /**
652     * @return Returns the messageDataType.
653     */
654    public String getBinaryDataType() {
655        return binaryDataType;
656    }
657
658    /**
659     * @param messageDataType The messageDataType to set.
660     */
661    public void setBinaryDataType(String messageDataType) {
662        this.binaryDataType = messageDataType;
663    }
664
665    /**
666     * @return Returns the messageTableName.
667     */
668    public String getMessageTableName() {
669        return messageTableName;
670    }
671
672    /**
673     * @param messageTableName The messageTableName to set.
674     */
675    public void setMessageTableName(String messageTableName) {
676        this.messageTableName = messageTableName;
677    }
678
679    /**
680     * @return Returns the msgIdDataType.
681     */
682    public String getMsgIdDataType() {
683        return msgIdDataType;
684    }
685
686    /**
687     * @param msgIdDataType The msgIdDataType to set.
688     */
689    public void setMsgIdDataType(String msgIdDataType) {
690        this.msgIdDataType = msgIdDataType;
691    }
692
693    /**
694     * @return Returns the sequenceDataType.
695     */
696    public String getSequenceDataType() {
697        return sequenceDataType;
698    }
699
700    /**
701     * @param sequenceDataType The sequenceDataType to set.
702     */
703    public void setSequenceDataType(String sequenceDataType) {
704        this.sequenceDataType = sequenceDataType;
705    }
706
707    /**
708     * @return Returns the tablePrefix.
709     */
710    public String getTablePrefix() {
711        return tablePrefix;
712    }
713
714    /**
715     * @param tablePrefix The tablePrefix to set.
716     */
717    public void setTablePrefix(String tablePrefix) {
718        this.tablePrefix = tablePrefix;
719    }
720
721    /**
722     * @return Returns the durableSubAcksTableName.
723     */
724    public String getDurableSubAcksTableName() {
725        return durableSubAcksTableName;
726    }
727
728    /**
729     * @param durableSubAcksTableName The durableSubAcksTableName to set.
730     */
731    public void setDurableSubAcksTableName(String durableSubAcksTableName) {
732        this.durableSubAcksTableName = durableSubAcksTableName;
733    }
734
735    public String getLockTableName() {
736        return lockTableName;
737    }
738
739    public void setLockTableName(String lockTableName) {
740        this.lockTableName = lockTableName;
741    }
742
743    public String getLongDataType() {
744        return longDataType;
745    }
746
747    public void setLongDataType(String longDataType) {
748        this.longDataType = longDataType;
749    }
750
751    public String getStringIdDataType() {
752        return stringIdDataType;
753    }
754
755    public void setStringIdDataType(String stringIdDataType) {
756        this.stringIdDataType = stringIdDataType;
757    }
758
759    public void setUseExternalMessageReferences(boolean useExternalMessageReferences) {
760        this.useExternalMessageReferences = useExternalMessageReferences;
761    }
762
763    public boolean isUseExternalMessageReferences() {
764        return useExternalMessageReferences;
765    }
766
767    public void setAddMessageStatement(String addMessageStatment) {
768        this.addMessageStatement = addMessageStatment;
769    }
770
771    public void setCreateDurableSubStatement(String createDurableSubStatment) {
772        this.createDurableSubStatement = createDurableSubStatment;
773    }
774
775    public void setCreateSchemaStatements(String[] createSchemaStatments) {
776        this.createSchemaStatements = createSchemaStatments;
777    }
778
779    public void setCreateLockSchemaStatements(String[] createLockSchemaStatments) {
780        this.createLockSchemaStatements = createLockSchemaStatments;
781    }
782
783    public void setDeleteOldMessagesStatementWithPriority(String deleteOldMessagesStatementWithPriority) {
784        this.deleteOldMessagesStatementWithPriority = deleteOldMessagesStatementWithPriority;
785    }
786
787    public void setDeleteSubscriptionStatement(String deleteSubscriptionStatment) {
788        this.deleteSubscriptionStatement = deleteSubscriptionStatment;
789    }
790
791    public void setDropSchemaStatements(String[] dropSchemaStatments) {
792        this.dropSchemaStatements = dropSchemaStatments;
793    }
794
795    public void setFindAllDestinationsStatement(String findAllDestinationsStatment) {
796        this.findAllDestinationsStatement = findAllDestinationsStatment;
797    }
798
799    public void setFindAllDurableSubMessagesStatement(String findAllDurableSubMessagesStatment) {
800        this.findAllDurableSubMessagesStatement = findAllDurableSubMessagesStatment;
801    }
802
803    public void setFindAllDurableSubsStatement(String findAllDurableSubsStatment) {
804        this.findAllDurableSubsStatement = findAllDurableSubsStatment;
805    }
806
807    public void setFindAllMessagesStatement(String findAllMessagesStatment) {
808        this.findAllMessagesStatement = findAllMessagesStatment;
809    }
810
811    public void setFindDurableSubStatement(String findDurableSubStatment) {
812        this.findDurableSubStatement = findDurableSubStatment;
813    }
814
815    public void setFindLastSequenceIdInAcksStatement(String findLastSequenceIdInAcks) {
816        this.findLastSequenceIdInAcksStatement = findLastSequenceIdInAcks;
817    }
818
819    public void setFindLastSequenceIdInMsgsStatement(String findLastSequenceIdInMsgs) {
820        this.findLastSequenceIdInMsgsStatement = findLastSequenceIdInMsgs;
821    }
822
823    public void setFindMessageSequenceIdStatement(String findMessageSequenceIdStatment) {
824        this.findMessageSequenceIdStatement = findMessageSequenceIdStatment;
825    }
826
827    public void setFindMessageStatement(String findMessageStatment) {
828        this.findMessageStatement = findMessageStatment;
829    }
830    
831    public void setFindMessageByIdStatement(String findMessageByIdStatement) {
832        this.findMessageByIdStatement = findMessageByIdStatement;
833    }
834
835    public void setRemoveAllMessagesStatement(String removeAllMessagesStatment) {
836        this.removeAllMessagesStatement = removeAllMessagesStatment;
837    }
838
839    public void setRemoveAllSubscriptionsStatement(String removeAllSubscriptionsStatment) {
840        this.removeAllSubscriptionsStatement = removeAllSubscriptionsStatment;
841    }
842
843    public void setRemoveMessageStatment(String removeMessageStatement) {
844        this.removeMessageStatement = removeMessageStatement;
845    }
846
847    public void setUpdateLastPriorityAckRowOfDurableSubStatement(String updateLastPriorityAckRowOfDurableSubStatement) {
848        this.updateLastPriorityAckRowOfDurableSubStatement = updateLastPriorityAckRowOfDurableSubStatement;
849    }
850
851    public void setUpdateMessageStatement(String updateMessageStatment) {
852        this.updateMessageStatement = updateMessageStatment;
853    }
854
855    public boolean isUseLockCreateWhereClause() {
856        return useLockCreateWhereClause;
857    }
858
859    public void setUseLockCreateWhereClause(boolean useLockCreateWhereClause) {
860        this.useLockCreateWhereClause = useLockCreateWhereClause;
861    }
862
863    public void setLockCreateStatement(String lockCreateStatement) {
864        this.lockCreateStatement = lockCreateStatement;
865    }
866
867    public void setLockUpdateStatement(String lockUpdateStatement) {
868        this.lockUpdateStatement = lockUpdateStatement;
869    }
870
871    /**
872     * @param findDurableSubMessagesStatement the
873     *                findDurableSubMessagesStatement to set
874     */
875    public void setFindDurableSubMessagesStatement(String findDurableSubMessagesStatement) {
876        this.findDurableSubMessagesStatement = findDurableSubMessagesStatement;
877    }
878
879    /**
880     * @param nextDurableSubscriberMessageStatement the nextDurableSubscriberMessageStatement to set
881     */
882    public void setNextDurableSubscriberMessageStatement(String nextDurableSubscriberMessageStatement) {
883        this.nextDurableSubscriberMessageStatement = nextDurableSubscriberMessageStatement;
884    }
885
886    /**
887     * @param durableSubscriberMessageCountStatement the durableSubscriberMessageCountStatement to set
888     */
889    public void setDurableSubscriberMessageCountStatement(String durableSubscriberMessageCountStatement) {
890        this.durableSubscriberMessageCountStatement = durableSubscriberMessageCountStatement;
891    }
892
893    public void setDurableSubscriberMessageCountStatementWithPriority(String durableSubscriberMessageCountStatementWithPriority) {
894        this.durableSubscriberMessageCountStatementWithPriority = durableSubscriberMessageCountStatementWithPriority;
895    }
896
897    /**
898     * @param findNextMessagesStatement the findNextMessagesStatement to set
899     */
900    public void setFindNextMessagesStatement(String findNextMessagesStatement) {
901        this.findNextMessagesStatement = findNextMessagesStatement;
902    }
903
904    /**
905     * @param destinationMessageCountStatement the destinationMessageCountStatement to set
906     */
907    public void setDestinationMessageCountStatement(String destinationMessageCountStatement) {
908        this.destinationMessageCountStatement = destinationMessageCountStatement;
909    }
910
911    /**
912     * @param lastAckedDurableSubscriberMessageStatement the lastAckedDurableSubscriberMessageStatement to set
913     */
914    public void setLastAckedDurableSubscriberMessageStatement(
915                                                              String lastAckedDurableSubscriberMessageStatement) {
916        this.lastAckedDurableSubscriberMessageStatement = lastAckedDurableSubscriberMessageStatement;
917    }
918
919
920    public void setLastProducerSequenceIdStatement(String lastProducerSequenceIdStatement) {
921        this.lastProducerSequenceIdStatement = lastProducerSequenceIdStatement;
922    }
923
924    public void setSelectDurablePriorityAckStatement(String selectDurablePriorityAckStatement) {
925        this.selectDurablePriorityAckStatement = selectDurablePriorityAckStatement;
926    }
927
928    public void setInsertDurablePriorityAckStatement(String insertDurablePriorityAckStatement) {
929        this.insertDurablePriorityAckStatement = insertDurablePriorityAckStatement;
930    }
931
932    public void setUpdateDurableLastAckStatement(String updateDurableLastAckStatement) {
933        this.updateDurableLastAckStatement = updateDurableLastAckStatement;
934    }
935
936    public void setUpdateXidFlagStatement(String updateXidFlagStatement) {
937        this.updateXidFlagStatement = updateXidFlagStatement;
938    }
939
940    public void setFindOpsPendingOutcomeStatement(String findOpsPendingOutcomeStatement) {
941        this.findOpsPendingOutcomeStatement = findOpsPendingOutcomeStatement;
942    }
943
944    public void setClearXidFlagStatement(String clearXidFlagStatement) {
945        this.clearXidFlagStatement = clearXidFlagStatement;
946    }
947
948    public void setUpdateDurableLastAckInTxStatement(String updateDurableLastAckInTxStatement) {
949        this.updateDurableLastAckInTxStatement = updateDurableLastAckInTxStatement;
950    }
951
952    public void setFindAcksPendingOutcomeStatement(String findAcksPendingOutcomeStatement) {
953        this.findAcksPendingOutcomeStatement = findAcksPendingOutcomeStatement;
954    }
955
956    public void setClearDurableLastAckInTxStatement(String clearDurableLastAckInTxStatement) {
957        this.clearDurableLastAckInTxStatement = clearDurableLastAckInTxStatement;
958    }
959
960    public void setUpdateDurableLastAckWithPriorityStatement(String updateDurableLastAckWithPriorityStatement) {
961        this.updateDurableLastAckWithPriorityStatement = updateDurableLastAckWithPriorityStatement;
962    }
963
964    public void setUpdateDurableLastAckWithPriorityInTxStatement(String updateDurableLastAckWithPriorityInTxStatement) {
965        this.updateDurableLastAckWithPriorityInTxStatement = updateDurableLastAckWithPriorityInTxStatement;
966    }
967
968    public void setFindXidByIdStatement(String findXidByIdStatement) {
969        this.findXidByIdStatement = findXidByIdStatement;
970    }
971
972    public void setLeaseObtainStatement(String leaseObtainStatement) {
973        this.leaseObtainStatement = leaseObtainStatement;
974    }
975
976    public void setCurrentDateTimeStatement(String currentDateTimeStatement) {
977        this.currentDateTimeStatement = currentDateTimeStatement;
978    }
979
980    public void setLeaseUpdateStatement(String leaseUpdateStatement) {
981        this.leaseUpdateStatement = leaseUpdateStatement;
982    }
983
984    public void setLeaseOwnerStatement(String leaseOwnerStatement) {
985        this.leaseOwnerStatement = leaseOwnerStatement;
986    }
987}