• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <wtf/Threading.h>
32 
33 #include "SQLiteTransaction.h"
34 #include "SQLStatement.h"
35 #include "SQLTransactionCallback.h"
36 #include "SQLTransactionErrorCallback.h"
37 #include <wtf/Deque.h>
38 #include <wtf/Forward.h>
39 #include <wtf/OwnPtr.h>
40 #include <wtf/RefPtr.h>
41 #include <wtf/Vector.h>
42 
43 namespace WebCore {
44 
45 typedef int ExceptionCode;
46 
47 class Database;
48 class SQLError;
49 class SQLStatementCallback;
50 class SQLStatementErrorCallback;
51 class SQLTransaction;
52 class SQLValue;
53 class String;
54 class VoidCallback;
55 
56 class SQLTransactionWrapper : public ThreadSafeShared<SQLTransactionWrapper> {
57 public:
~SQLTransactionWrapper()58     virtual ~SQLTransactionWrapper() { }
59     virtual bool performPreflight(SQLTransaction*) = 0;
60     virtual bool performPostflight(SQLTransaction*) = 0;
61 
62     virtual SQLError* sqlError() const = 0;
63 };
64 
65 class SQLTransaction : public ThreadSafeShared<SQLTransaction> {
66 public:
67     static PassRefPtr<SQLTransaction> create(Database*, PassRefPtr<SQLTransactionCallback>, PassRefPtr<SQLTransactionErrorCallback>, PassRefPtr<VoidCallback>, PassRefPtr<SQLTransactionWrapper>);
68 
69     ~SQLTransaction();
70 
71     void executeSQL(const String& sqlStatement, const Vector<SQLValue>& arguments,
72                     PassRefPtr<SQLStatementCallback> callback, PassRefPtr<SQLStatementErrorCallback> callbackError, ExceptionCode& e);
73 
74     bool performNextStep();
75     void performPendingCallback();
76 
database()77     Database* database() { return m_database.get(); }
78 
79 private:
80     SQLTransaction(Database*, PassRefPtr<SQLTransactionCallback>, PassRefPtr<SQLTransactionErrorCallback>, PassRefPtr<VoidCallback>, PassRefPtr<SQLTransactionWrapper>);
81 
82     typedef void (SQLTransaction::*TransactionStepMethod)();
83     TransactionStepMethod m_nextStep;
84 
85     void enqueueStatement(PassRefPtr<SQLStatement>);
86 
87     void checkAndHandleClosedDatabase();
88 
89     void openTransactionAndPreflight();
90     void deliverTransactionCallback();
91     void scheduleToRunStatements();
92     void runStatements();
93     void getNextStatement();
94     bool runCurrentStatement();
95     void handleCurrentStatementError();
96     void deliverStatementCallback();
97     void deliverQuotaIncreaseCallback();
98     void postflightAndCommit();
99     void deliverSuccessCallback();
100     void cleanupAfterSuccessCallback();
101     void handleTransactionError(bool inCallback);
102     void deliverTransactionErrorCallback();
103     void cleanupAfterTransactionErrorCallback();
104 
105 #ifndef NDEBUG
106     static const char* debugStepName(TransactionStepMethod);
107 #endif
108 
109     RefPtr<SQLStatement> m_currentStatement;
110 
111     bool m_executeSqlAllowed;
112 
113     RefPtr<Database> m_database;
114     RefPtr<SQLTransactionWrapper> m_wrapper;
115     RefPtr<SQLTransactionCallback> m_callback;
116     RefPtr<VoidCallback> m_successCallback;
117     RefPtr<SQLTransactionErrorCallback> m_errorCallback;
118     RefPtr<SQLError> m_transactionError;
119     bool m_shouldRetryCurrentStatement;
120     bool m_shouldCommitAfterErrorCallback;
121     bool m_modifiedDatabase;
122 
123     Mutex m_statementMutex;
124     Deque<RefPtr<SQLStatement> > m_statementQueue;
125 
126     OwnPtr<SQLiteTransaction> m_sqliteTransaction;
127 };
128 
129 } // namespace WebCore
130 
131 #endif // SQLTransaction_h
132