SQLTransactionState SQLTransactionBackend::postflightAndCommit() { ASSERT(m_lockAcquired); // Spec 4.3.2.7: Perform postflight steps, jumping to the error callback if they fail. if (m_wrapper && !m_wrapper->performPostflight(this)) { m_transactionError = m_wrapper->sqlError(); if (!m_transactionError) { m_database->reportCommitTransactionResult(3, SQLError::UNKNOWN_ERR, 0); m_transactionError = SQLError::create(SQLError::UNKNOWN_ERR, "unknown error occurred during transaction postflight"); } return nextStateForTransactionError(); } // Spec 4.3.2.7: Commit the transaction, jumping to the error callback if that fails. ASSERT(m_sqliteTransaction); m_database->disableAuthorizer(); m_sqliteTransaction->commit(); m_database->enableAuthorizer(); releaseOriginLockIfNeeded(); // If the commit failed, the transaction will still be marked as "in progress" if (m_sqliteTransaction->inProgress()) { if (m_wrapper) m_wrapper->handleCommitFailedAfterPostflight(this); m_database->reportCommitTransactionResult(4, SQLError::DATABASE_ERR, m_database->sqliteDatabase().lastError()); m_transactionError = SQLError::create(SQLError::DATABASE_ERR, "unable to commit transaction", m_database->sqliteDatabase().lastError(), m_database->sqliteDatabase().lastErrorMsg()); return nextStateForTransactionError(); } m_database->reportCommitTransactionResult(0, -1, 0); // OK // Vacuum the database if anything was deleted. if (m_database->hadDeletes()) m_database->incrementalVacuumIfNeeded(); // The commit was successful. If the transaction modified this database, notify the delegates. if (m_modifiedDatabase) m_database->transactionClient()->didCommitWriteTransaction(database()); // Spec 4.3.2.8: Deliver success callback, if there is one. return SQLTransactionState::DeliverSuccessCallback; }
SQLTransactionState SQLTransactionBackend::nextStateForCurrentStatementError() { // Spec 4.3.2.6.6: error - Call the statement's error callback, but if there was no error callback, // or the transaction was rolled back, jump to the transaction error callback if (m_currentStatementBackend->hasStatementErrorCallback() && !m_sqliteTransaction->wasRolledBackBySqlite()) return SQLTransactionState::DeliverStatementCallback; m_transactionError = m_currentStatementBackend->sqlError(); if (!m_transactionError) m_transactionError = SQLError::create(SQLError::DATABASE_ERR, "the statement failed to execute"); return nextStateForTransactionError(); }
SQLTransactionState SQLTransactionBackend::nextStateForCurrentStatementError() { // Spec 4.3.2.6.6: error - Call the statement's error callback, but if there was no error callback, // or the transaction was rolled back, jump to the transaction error callback if (m_currentStatementBackend->hasStatementErrorCallback() && !m_sqliteTransaction->wasRolledBackBySqlite()) return SQLTransactionState::DeliverStatementCallback; if (m_currentStatementBackend->sqlError()) { m_transactionError = SQLErrorData::create(*m_currentStatementBackend->sqlError()); } else { m_database->reportCommitTransactionResult(1, SQLError::DATABASE_ERR, 0); m_transactionError = SQLErrorData::create(SQLError::DATABASE_ERR, "the statement failed to execute"); } return nextStateForTransactionError(); }
SQLTransactionState SQLTransaction::deliverStatementCallback() { // Spec 4.3.2.6.6 and 4.3.2.6.3: If the statement callback went wrong, jump to the transaction error callback // Otherwise, continue to loop through the statement queue m_executeSqlAllowed = true; SQLStatement* currentStatement = m_backend->currentStatement(); ASSERT(currentStatement); bool result = currentStatement->performCallback(this); m_executeSqlAllowed = false; if (result) { m_database->reportCommitTransactionResult(2, SQLError::UNKNOWN_ERR, 0); m_transactionError = SQLErrorData::create(SQLError::UNKNOWN_ERR, "the statement callback raised an exception or statement error callback did not return false"); return nextStateForTransactionError(); } return SQLTransactionState::RunStatements; }
SQLTransactionState SQLTransactionBackend::openTransactionAndPreflight() { ASSERT(!m_database->sqliteDatabase().transactionInProgress()); ASSERT(m_lockAcquired); LOG(StorageAPI, "Opening and preflighting transaction %p", this); // If the database was deleted, jump to the error callback if (Database::from(m_database.get())->deleted()) { m_database->reportStartTransactionResult(1, SQLError::UNKNOWN_ERR, 0); m_transactionError = SQLError::create(SQLError::UNKNOWN_ERR, "unable to open a transaction, because the user deleted the database"); return nextStateForTransactionError(); } // Set the maximum usage for this transaction if this transactions is not read-only if (!m_readOnly) m_database->sqliteDatabase().setMaximumSize(m_database->maximumSize()); ASSERT(!m_sqliteTransaction); m_sqliteTransaction = adoptPtr(new SQLiteTransaction(m_database->sqliteDatabase(), m_readOnly)); m_database->resetDeletes(); m_database->disableAuthorizer(); m_sqliteTransaction->begin(); m_database->enableAuthorizer(); // Spec 4.3.2.1+2: Open a transaction to the database, jumping to the error callback if that fails if (!m_sqliteTransaction->inProgress()) { ASSERT(!m_database->sqliteDatabase().transactionInProgress()); m_database->reportStartTransactionResult(2, SQLError::DATABASE_ERR, m_database->sqliteDatabase().lastError()); m_transactionError = SQLError::create(SQLError::DATABASE_ERR, "unable to begin transaction", m_database->sqliteDatabase().lastError(), m_database->sqliteDatabase().lastErrorMsg()); m_sqliteTransaction.clear(); return nextStateForTransactionError(); } // Note: We intentionally retrieve the actual version even with an empty expected version. // In multi-process browsers, we take this opportinutiy to update the cached value for // the actual version. In single-process browsers, this is just a map lookup. String actualVersion; if (!m_database->getActualVersionForTransaction(actualVersion)) { m_database->reportStartTransactionResult(3, SQLError::DATABASE_ERR, m_database->sqliteDatabase().lastError()); m_transactionError = SQLError::create(SQLError::DATABASE_ERR, "unable to read version", m_database->sqliteDatabase().lastError(), m_database->sqliteDatabase().lastErrorMsg()); m_database->disableAuthorizer(); m_sqliteTransaction.clear(); m_database->enableAuthorizer(); return nextStateForTransactionError(); } m_hasVersionMismatch = !m_database->expectedVersion().isEmpty() && (m_database->expectedVersion() != actualVersion); // Spec 4.3.2.3: Perform preflight steps, jumping to the error callback if they fail if (m_wrapper && !m_wrapper->performPreflight(this)) { m_database->disableAuthorizer(); m_sqliteTransaction.clear(); m_database->enableAuthorizer(); m_transactionError = m_wrapper->sqlError(); if (!m_transactionError) { m_database->reportStartTransactionResult(4, SQLError::UNKNOWN_ERR, 0); m_transactionError = SQLError::create(SQLError::UNKNOWN_ERR, "unknown error occurred during transaction preflight"); } return nextStateForTransactionError(); } // Spec 4.3.2.4: Invoke the transaction callback with the new SQLTransaction object if (m_hasCallback) return SQLTransactionState::DeliverTransactionCallback; // If we have no callback to make, skip pass to the state after: return SQLTransactionState::RunStatements; }