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: 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>, 70 PassRefPtr<VoidCallback>, PassRefPtr<SQLTransactionWrapper>, bool readOnly = false); 71 72 ~SQLTransaction(); 73 74 void executeSQL(const String& sqlStatement, const Vector<SQLValue>& arguments, 75 PassRefPtr<SQLStatementCallback> callback, PassRefPtr<SQLStatementErrorCallback> callbackError, ExceptionCode& e); 76 77 void lockAcquired(); 78 bool performNextStep(); 79 void performPendingCallback(); 80 81 Database* database() { return m_database.get(); } 82 bool isReadOnly() { return m_readOnly; } 83 void notifyDatabaseThreadIsShuttingDown(); 84 85 private: 86 SQLTransaction(Database*, PassRefPtr<SQLTransactionCallback>, PassRefPtr<SQLTransactionErrorCallback>, 87 PassRefPtr<VoidCallback>, PassRefPtr<SQLTransactionWrapper>, bool readOnly); 88 89 typedef void (SQLTransaction::*TransactionStepMethod)(); 90 TransactionStepMethod m_nextStep; 91 92 void enqueueStatement(PassRefPtr<SQLStatement>); 93 94 void checkAndHandleClosedDatabase(); 95 96 void acquireLock(); 97 void openTransactionAndPreflight(); 98 void deliverTransactionCallback(); 99 void scheduleToRunStatements(); 100 void runStatements(); 101 void getNextStatement(); 102 bool runCurrentStatement(); 103 void handleCurrentStatementError(); 104 void deliverStatementCallback(); 105 void deliverQuotaIncreaseCallback(); 106 void postflightAndCommit(); 107 void deliverSuccessCallback(); 108 void cleanupAfterSuccessCallback(); 109 void handleTransactionError(bool inCallback); 110 void deliverTransactionErrorCallback(); 111 void cleanupAfterTransactionErrorCallback(); 112 113 #ifndef NDEBUG 114 static const char* debugStepName(TransactionStepMethod); 115 #endif 116 117 RefPtr<SQLStatement> m_currentStatement; 118 119 bool m_executeSqlAllowed; 120 121 RefPtr<Database> m_database; 122 RefPtr<SQLTransactionWrapper> m_wrapper; 123 RefPtr<SQLTransactionCallback> m_callback; 124 RefPtr<VoidCallback> m_successCallback; 125 RefPtr<SQLTransactionErrorCallback> m_errorCallback; 126 RefPtr<SQLError> m_transactionError; 127 bool m_shouldRetryCurrentStatement; 128 bool m_modifiedDatabase; 129 bool m_lockAcquired; 130 bool m_readOnly; 131 132 Mutex m_statementMutex; 133 Deque<RefPtr<SQLStatement> > m_statementQueue; 134 135 OwnPtr<SQLiteTransaction> m_sqliteTransaction; 136 }; 137 138 } // namespace WebCore 139 140 #endif 141 142 #endif // SQLTransaction_h 143