2
* Copyright (C) 2010 Google Inc. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are
8
* * Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* * Redistributions in binary form must reproduce the above
11
* copyright notice, this list of conditions and the following disclaimer
12
* in the documentation and/or other materials provided with the
14
* * Neither the name of Google Inc. nor the names of its
15
* contributors may be used to endorse or promote products derived from
16
* this software without specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
#include "SQLTransactionSync.h"
34
#if ENABLE(SQL_DATABASE)
36
#include "DatabaseAuthorizer.h"
37
#include "DatabaseContext.h"
38
#include "DatabaseSync.h"
39
#include "SQLException.h"
40
#include "SQLResultSet.h"
41
#include "SQLStatementSync.h"
42
#include "SQLTransactionClient.h"
43
#include "SQLTransactionSyncCallback.h"
45
#include "SQLiteTransaction.h"
46
#include "ScriptExecutionContext.h"
47
#include <wtf/PassRefPtr.h>
48
#include <wtf/RefPtr.h>
49
#include <wtf/text/WTFString.h>
53
PassRefPtr<SQLTransactionSync> SQLTransactionSync::create(DatabaseSync* db, PassRefPtr<SQLTransactionSyncCallback> callback, bool readOnly)
55
return adoptRef(new SQLTransactionSync(db, callback, readOnly));
58
SQLTransactionSync::SQLTransactionSync(DatabaseSync* db, PassRefPtr<SQLTransactionSyncCallback> callback, bool readOnly)
60
, m_callback(callback)
61
, m_readOnly(readOnly)
62
, m_hasVersionMismatch(false)
63
, m_modifiedDatabase(false)
64
, m_transactionClient(adoptPtr(new SQLTransactionClient()))
66
ASSERT(m_database->scriptExecutionContext()->isContextThread());
69
SQLTransactionSync::~SQLTransactionSync()
71
ASSERT(m_database->scriptExecutionContext()->isContextThread());
72
if (m_sqliteTransaction && m_sqliteTransaction->inProgress())
76
PassRefPtr<SQLResultSet> SQLTransactionSync::executeSQL(const String& sqlStatement, const Vector<SQLValue>& arguments, ExceptionCode& ec)
78
ASSERT(m_database->scriptExecutionContext()->isContextThread());
80
m_database->setLastErrorMessage("");
82
if (!m_database->opened()) {
83
m_database->setLastErrorMessage("cannot executeSQL because the database is not open");
84
ec = SQLException::UNKNOWN_ERR;
88
if (m_hasVersionMismatch) {
89
m_database->setLastErrorMessage("cannot executeSQL because there is a version mismatch");
90
ec = SQLException::VERSION_ERR;
94
if (sqlStatement.isEmpty())
97
int permissions = DatabaseAuthorizer::ReadWriteMask;
98
if (!m_database->databaseContext()->allowDatabaseAccess())
99
permissions |= DatabaseAuthorizer::NoAccessMask;
101
permissions |= DatabaseAuthorizer::ReadOnlyMask;
103
SQLStatementSync statement(sqlStatement, arguments, permissions);
105
m_database->resetAuthorizer();
106
bool retryStatement = true;
107
RefPtr<SQLResultSet> resultSet;
108
while (retryStatement) {
109
retryStatement = false;
110
resultSet = statement.execute(m_database.get(), ec);
112
if (m_sqliteTransaction->wasRolledBackBySqlite())
115
if (ec == SQLException::QUOTA_ERR) {
116
if (m_transactionClient->didExceedQuota(database())) {
118
retryStatement = true;
120
m_database->setLastErrorMessage("there was not enough remaining storage space");
127
if (m_database->lastActionChangedDatabase()) {
128
m_modifiedDatabase = true;
129
m_transactionClient->didExecuteStatement(database());
132
return resultSet.release();
135
ExceptionCode SQLTransactionSync::begin()
137
ASSERT(m_database->scriptExecutionContext()->isContextThread());
138
if (!m_database->opened()) {
139
m_database->reportStartTransactionResult(1, SQLException::UNKNOWN_ERR, 0);
140
m_database->setLastErrorMessage("cannot begin transaction because the database is not open");
141
return SQLException::UNKNOWN_ERR;
144
ASSERT(!m_database->sqliteDatabase().transactionInProgress());
146
// Set the maximum usage for this transaction if this transactions is not read-only.
148
m_database->sqliteDatabase().setMaximumSize(m_database->maximumSize());
150
ASSERT(!m_sqliteTransaction);
151
m_sqliteTransaction = adoptPtr(new SQLiteTransaction(m_database->sqliteDatabase(), m_readOnly));
153
m_database->resetDeletes();
154
m_database->disableAuthorizer();
155
m_sqliteTransaction->begin();
156
m_database->enableAuthorizer();
158
// Check if begin() succeeded.
159
if (!m_sqliteTransaction->inProgress()) {
160
ASSERT(!m_database->sqliteDatabase().transactionInProgress());
161
m_database->reportStartTransactionResult(2, SQLException::DATABASE_ERR, m_database->sqliteDatabase().lastError());
162
m_database->setLastErrorMessage("unable to begin transaction",
163
m_database->sqliteDatabase().lastError(), m_database->sqliteDatabase().lastErrorMsg());
164
m_sqliteTransaction.clear();
165
return SQLException::DATABASE_ERR;
168
// Note: We intentionally retrieve the actual version even with an empty expected version.
169
// In multi-process browsers, we take this opportinutiy to update the cached value for
170
// the actual version. In single-process browsers, this is just a map lookup.
171
String actualVersion;
172
if (!m_database->getActualVersionForTransaction(actualVersion)) {
173
m_database->reportStartTransactionResult(3, SQLException::DATABASE_ERR, m_database->sqliteDatabase().lastError());
174
m_database->setLastErrorMessage("unable to read version",
175
m_database->sqliteDatabase().lastError(), m_database->sqliteDatabase().lastErrorMsg());
177
return SQLException::DATABASE_ERR;
179
m_hasVersionMismatch = !m_database->expectedVersion().isEmpty()
180
&& (m_database->expectedVersion() != actualVersion);
181
m_database->reportStartTransactionResult(0, -1, 0); // OK
185
ExceptionCode SQLTransactionSync::execute()
187
ASSERT(m_database->scriptExecutionContext()->isContextThread());
188
if (!m_database->opened() || (m_callback && !m_callback->handleEvent(this))) {
189
if (m_database->lastErrorMessage().isEmpty())
190
m_database->setLastErrorMessage("failed to execute transaction callback");
192
return SQLException::UNKNOWN_ERR;
199
ExceptionCode SQLTransactionSync::commit()
201
ASSERT(m_database->scriptExecutionContext()->isContextThread());
202
if (!m_database->opened()) {
203
m_database->reportCommitTransactionResult(1, SQLException::UNKNOWN_ERR, 0);
204
m_database->setLastErrorMessage("unable to commit transaction because the database is not open.");
205
return SQLException::UNKNOWN_ERR;
208
ASSERT(m_sqliteTransaction);
210
m_database->disableAuthorizer();
211
m_sqliteTransaction->commit();
212
m_database->enableAuthorizer();
214
// If the commit failed, the transaction will still be marked as "in progress"
215
if (m_sqliteTransaction->inProgress()) {
216
m_database->reportCommitTransactionResult(2, SQLException::DATABASE_ERR, m_database->sqliteDatabase().lastError());
217
m_database->setLastErrorMessage("unable to commit transaction",
218
m_database->sqliteDatabase().lastError(), m_database->sqliteDatabase().lastErrorMsg());
219
return SQLException::DATABASE_ERR;
222
m_sqliteTransaction.clear();
224
// Vacuum the database if anything was deleted.
225
if (m_database->hadDeletes())
226
m_database->incrementalVacuumIfNeeded();
228
// The commit was successful. If the transaction modified this database, notify the delegates.
229
if (m_modifiedDatabase)
230
m_transactionClient->didCommitWriteTransaction(database());
232
m_database->reportCommitTransactionResult(0, -1, 0); // OK
236
void SQLTransactionSync::rollback()
238
ASSERT(m_database->scriptExecutionContext()->isContextThread());
239
m_database->disableAuthorizer();
240
if (m_sqliteTransaction) {
241
m_sqliteTransaction->rollback();
242
m_sqliteTransaction.clear();
244
m_database->enableAuthorizer();
246
ASSERT(!m_database->sqliteDatabase().transactionInProgress());
249
} // namespace WebCore
251
#endif // ENABLE(SQL_DATABASE)