ChangeVersionWrapper.cpp [plain text]
#include "config.h"
#include "ChangeVersionWrapper.h"
#include "Database.h"
#include "SQLError.h"
#include <wtf/PassRefPtr.h>
#include <wtf/RefPtr.h>
namespace WebCore {
ChangeVersionWrapper::ChangeVersionWrapper(const String& oldVersion, const String& newVersion)
: m_oldVersion(oldVersion.isolatedCopy())
, m_newVersion(newVersion.isolatedCopy())
{
}
bool ChangeVersionWrapper::performPreflight(SQLTransactionBackend* transaction)
{
ASSERT(transaction && transaction->database());
DatabaseBackend* database = transaction->database();
String actualVersion;
if (!database->getVersionFromDatabase(actualVersion)) {
int sqliteError = database->sqliteDatabase().lastError();
m_sqlError = SQLError::create(SQLError::UNKNOWN_ERR, "unable to read the current version",
sqliteError, database->sqliteDatabase().lastErrorMsg());
return false;
}
if (actualVersion != m_oldVersion) {
m_sqlError = SQLError::create(SQLError::VERSION_ERR, "current version of the database and `oldVersion` argument do not match");
return false;
}
return true;
}
bool ChangeVersionWrapper::performPostflight(SQLTransactionBackend* transaction)
{
ASSERT(transaction && transaction->database());
DatabaseBackend* database = transaction->database();
if (!database->setVersionInDatabase(m_newVersion)) {
int sqliteError = database->sqliteDatabase().lastError();
m_sqlError = SQLError::create(SQLError::UNKNOWN_ERR, "unable to set new version in database",
sqliteError, database->sqliteDatabase().lastErrorMsg());
return false;
}
database->setExpectedVersion(m_newVersion);
return true;
}
void ChangeVersionWrapper::handleCommitFailedAfterPostflight(SQLTransactionBackend* transaction)
{
transaction->database()->setCachedVersion(m_oldVersion);
}
}