1 /* 2 * Copyright (C) 2007 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 14 * its contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 #ifndef SQLTransaction_h 29 #define SQLTransaction_h 30 31 #if ENABLE(DATABASE) 32 33 #include <wtf/Threading.h> 34 35 #include "SQLiteTransaction.h" 36 #include "SQLStatement.h" 37 #include "SQLTransactionCallback.h" 38 #include "SQLTransactionErrorCallback.h" 39 #include <wtf/Deque.h> 40 #include <wtf/Forward.h> 41 #include <wtf/OwnPtr.h> 42 #include <wtf/RefPtr.h> 43 #include <wtf/Vector.h> 44 45 namespace WebCore { 46 47 typedef int ExceptionCode; 48 49 class Database; 50 class SQLError; 51 class SQLStatementCallback; 52 class SQLStatementErrorCallback; 53 class SQLTransaction; 54 class SQLValue; 55 class String; 56 class VoidCallback; 57 58 class SQLTransactionWrapper : public ThreadSafeShared<SQLTransactionWrapper> { 59 public: ~SQLTransactionWrapper()60 virtual ~SQLTransactionWrapper() { } 61 virtual bool performPreflight(SQLTransaction*) = 0; 62 virtual bool performPostflight(SQLTransaction*) = 0; 63 64 virtual SQLError* sqlError() const = 0; 65 }; 66 67 class SQLTransaction : public ThreadSafeShared<SQLTransaction> { 68 public: 69 static PassRefPtr<SQLTransaction> create(Database*, PassRefPtr<SQLTransactionCallback>, PassRefPtr<SQLTransactionErrorCallback>, PassRefPtr<VoidCallback>, PassRefPtr<SQLTransactionWrapper>); 70 71 ~SQLTransaction(); 72 73 void executeSQL(const String& sqlStatement, const Vector<SQLValue>& arguments, 74 PassRefPtr<SQLStatementCallback> callback, PassRefPtr<SQLStatementErrorCallback> callbackError, ExceptionCode& e); 75 76 bool performNextStep(); 77 void performPendingCallback(); 78 database()79 Database* database() { return m_database.get(); } 80 81 private: 82 SQLTransaction(Database*, PassRefPtr<SQLTransactionCallback>, PassRefPtr<SQLTransactionErrorCallback>, PassRefPtr<VoidCallback>, PassRefPtr<SQLTransactionWrapper>); 83 84 typedef void (SQLTransaction::*TransactionStepMethod)(); 85 TransactionStepMethod m_nextStep; 86 87 void enqueueStatement(PassRefPtr<SQLStatement>); 88 89 void checkAndHandleClosedDatabase(); 90 91 void openTransactionAndPreflight(); 92 void deliverTransactionCallback(); 93 void scheduleToRunStatements(); 94 void runStatements(); 95 void getNextStatement(); 96 bool runCurrentStatement(); 97 void handleCurrentStatementError(); 98 void deliverStatementCallback(); 99 void deliverQuotaIncreaseCallback(); 100 void postflightAndCommit(); 101 void deliverSuccessCallback(); 102 void cleanupAfterSuccessCallback(); 103 void handleTransactionError(bool inCallback); 104 void deliverTransactionErrorCallback(); 105 void cleanupAfterTransactionErrorCallback(); 106 107 #ifndef NDEBUG 108 static const char* debugStepName(TransactionStepMethod); 109 #endif 110 111 RefPtr<SQLStatement> m_currentStatement; 112 113 bool m_executeSqlAllowed; 114 115 RefPtr<Database> m_database; 116 RefPtr<SQLTransactionWrapper> m_wrapper; 117 RefPtr<SQLTransactionCallback> m_callback; 118 RefPtr<VoidCallback> m_successCallback; 119 RefPtr<SQLTransactionErrorCallback> m_errorCallback; 120 RefPtr<SQLError> m_transactionError; 121 bool m_shouldRetryCurrentStatement; 122 bool m_modifiedDatabase; 123 124 Mutex m_statementMutex; 125 Deque<RefPtr<SQLStatement> > m_statementQueue; 126 127 OwnPtr<SQLiteTransaction> m_sqliteTransaction; 128 }; 129 130 } // namespace WebCore 131 132 #endif 133 134 #endif // SQLTransaction_h 135