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 "DatabaseSync.h"
38 #include "PlatformString.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>
52 PassRefPtr<SQLTransactionSync> SQLTransactionSync::create(DatabaseSync* db, PassRefPtr<SQLTransactionSyncCallback> callback, bool readOnly)
54 return adoptRef(new SQLTransactionSync(db, callback, readOnly));
57 SQLTransactionSync::SQLTransactionSync(DatabaseSync* db, PassRefPtr<SQLTransactionSyncCallback> callback, bool readOnly)
59 , m_callback(callback)
60 , m_readOnly(readOnly)
61 , m_hasVersionMismatch(false)
62 , m_modifiedDatabase(false)
63 , m_transactionClient(adoptPtr(new SQLTransactionClient()))
65 ASSERT(m_database->scriptExecutionContext()->isContextThread());
68 SQLTransactionSync::~SQLTransactionSync()
70 ASSERT(m_database->scriptExecutionContext()->isContextThread());
71 if (m_sqliteTransaction && m_sqliteTransaction->inProgress())
75 PassRefPtr<SQLResultSet> SQLTransactionSync::executeSQL(const String& sqlStatement, const Vector<SQLValue>& arguments, ExceptionCode& ec)
77 ASSERT(m_database->scriptExecutionContext()->isContextThread());
79 m_database->setLastErrorMessage("");
81 if (!m_database->opened()) {
82 m_database->setLastErrorMessage("cannot executeSQL because the database is not open");
83 ec = SQLException::UNKNOWN_ERR;
87 if (m_hasVersionMismatch) {
88 m_database->setLastErrorMessage("cannot executeSQL because there is a version mismatch");
89 ec = SQLException::VERSION_ERR;
93 if (sqlStatement.isEmpty())
96 int permissions = DatabaseAuthorizer::ReadWriteMask;
97 if (!m_database->scriptExecutionContext()->allowDatabaseAccess())
98 permissions |= DatabaseAuthorizer::NoAccessMask;
100 permissions |= DatabaseAuthorizer::ReadOnlyMask;
102 SQLStatementSync statement(sqlStatement, arguments, permissions);
104 m_database->resetAuthorizer();
105 bool retryStatement = true;
106 RefPtr<SQLResultSet> resultSet;
107 while (retryStatement) {
108 retryStatement = false;
109 resultSet = statement.execute(m_database.get(), ec);
111 if (m_sqliteTransaction->wasRolledBackBySqlite())
114 if (ec == SQLException::QUOTA_ERR) {
115 if (m_transactionClient->didExceedQuota(database())) {
117 retryStatement = true;
119 m_database->setLastErrorMessage("there was not enough remaining storage space");
126 if (m_database->lastActionChangedDatabase()) {
127 m_modifiedDatabase = true;
128 m_transactionClient->didExecuteStatement(database());
131 return resultSet.release();
134 ExceptionCode SQLTransactionSync::begin()
136 ASSERT(m_database->scriptExecutionContext()->isContextThread());
137 if (!m_database->opened()) {
138 m_database->reportStartTransactionResult(1, SQLException::UNKNOWN_ERR, 0);
139 m_database->setLastErrorMessage("cannot begin transaction because the database is not open");
140 return SQLException::UNKNOWN_ERR;
143 ASSERT(!m_database->sqliteDatabase().transactionInProgress());
145 // Set the maximum usage for this transaction if this transactions is not read-only.
147 m_database->sqliteDatabase().setMaximumSize(m_database->maximumSize());
149 ASSERT(!m_sqliteTransaction);
150 m_sqliteTransaction = adoptPtr(new SQLiteTransaction(m_database->sqliteDatabase(), m_readOnly));
152 m_database->resetDeletes();
153 m_database->disableAuthorizer();
154 m_sqliteTransaction->begin();
155 m_database->enableAuthorizer();
157 // Check if begin() succeeded.
158 if (!m_sqliteTransaction->inProgress()) {
159 ASSERT(!m_database->sqliteDatabase().transactionInProgress());
160 m_database->reportStartTransactionResult(2, SQLException::DATABASE_ERR, m_database->sqliteDatabase().lastError());
161 m_database->setLastErrorMessage("unable to begin transaction",
162 m_database->sqliteDatabase().lastError(), m_database->sqliteDatabase().lastErrorMsg());
163 m_sqliteTransaction.clear();
164 return SQLException::DATABASE_ERR;
167 // Note: We intentionally retrieve the actual version even with an empty expected version.
168 // In multi-process browsers, we take this opportinutiy to update the cached value for
169 // the actual version. In single-process browsers, this is just a map lookup.
170 String actualVersion;
171 if (!m_database->getActualVersionForTransaction(actualVersion)) {
172 m_database->reportStartTransactionResult(3, SQLException::DATABASE_ERR, m_database->sqliteDatabase().lastError());
173 m_database->setLastErrorMessage("unable to read version",
174 m_database->sqliteDatabase().lastError(), m_database->sqliteDatabase().lastErrorMsg());
176 return SQLException::DATABASE_ERR;
178 m_hasVersionMismatch = !m_database->expectedVersion().isEmpty()
179 && (m_database->expectedVersion() != actualVersion);
180 m_database->reportStartTransactionResult(0, -1, 0); // OK
184 ExceptionCode SQLTransactionSync::execute()
186 ASSERT(m_database->scriptExecutionContext()->isContextThread());
187 if (!m_database->opened() || (m_callback && !m_callback->handleEvent(this))) {
188 if (m_database->lastErrorMessage().isEmpty())
189 m_database->setLastErrorMessage("failed to execute transaction callback");
191 return SQLException::UNKNOWN_ERR;
198 ExceptionCode SQLTransactionSync::commit()
200 ASSERT(m_database->scriptExecutionContext()->isContextThread());
201 if (!m_database->opened()) {
202 m_database->reportCommitTransactionResult(1, SQLException::UNKNOWN_ERR, 0);
203 m_database->setLastErrorMessage("unable to commit transaction because the database is not open.");
204 return SQLException::UNKNOWN_ERR;
207 ASSERT(m_sqliteTransaction);
209 m_database->disableAuthorizer();
210 m_sqliteTransaction->commit();
211 m_database->enableAuthorizer();
213 // If the commit failed, the transaction will still be marked as "in progress"
214 if (m_sqliteTransaction->inProgress()) {
215 m_database->reportCommitTransactionResult(2, SQLException::DATABASE_ERR, m_database->sqliteDatabase().lastError());
216 m_database->setLastErrorMessage("unable to commit transaction",
217 m_database->sqliteDatabase().lastError(), m_database->sqliteDatabase().lastErrorMsg());
218 return SQLException::DATABASE_ERR;
221 m_sqliteTransaction.clear();
223 // Vacuum the database if anything was deleted.
224 if (m_database->hadDeletes())
225 m_database->incrementalVacuumIfNeeded();
227 // The commit was successful. If the transaction modified this database, notify the delegates.
228 if (m_modifiedDatabase)
229 m_transactionClient->didCommitWriteTransaction(database());
231 m_database->reportCommitTransactionResult(0, -1, 0); // OK
235 void SQLTransactionSync::rollback()
237 ASSERT(m_database->scriptExecutionContext()->isContextThread());
238 m_database->disableAuthorizer();
239 if (m_sqliteTransaction) {
240 m_sqliteTransaction->rollback();
241 m_sqliteTransaction.clear();
243 m_database->enableAuthorizer();
245 ASSERT(!m_database->sqliteDatabase().transactionInProgress());
248 } // namespace WebCore
250 #endif // ENABLE(SQL_DATABASE)