This source file includes following definitions.
- ChangeVersion
- FailedMigrationTo
- AddTable
- GetTable
- BeginTransaction
- CommitTransaction
- GetSQLConnection
- MigrateOldVersionsAsNeeded
#include "components/webdata/common/web_database.h"
#include <algorithm>
#include "base/stl_util.h"
#include "sql/statement.h"
#include "sql/transaction.h"
const int WebDatabase::kCurrentVersionNumber = 55;
namespace {
const int kCompatibleVersionNumber = 55;
void ChangeVersion(sql::MetaTable* meta_table,
int version_num,
bool update_compatible_version_num) {
meta_table->SetVersionNumber(version_num);
if (update_compatible_version_num) {
meta_table->SetCompatibleVersionNumber(
std::min(version_num, kCompatibleVersionNumber));
}
}
sql::InitStatus FailedMigrationTo(int version_num) {
LOG(WARNING) << "Unable to update web database to version "
<< version_num << ".";
NOTREACHED();
return sql::INIT_FAILURE;
}
}
WebDatabase::WebDatabase() {}
WebDatabase::~WebDatabase() {
}
void WebDatabase::AddTable(WebDatabaseTable* table) {
tables_[table->GetTypeKey()] = table;
}
WebDatabaseTable* WebDatabase::GetTable(WebDatabaseTable::TypeKey key) {
return tables_[key];
}
void WebDatabase::BeginTransaction() {
db_.BeginTransaction();
}
void WebDatabase::CommitTransaction() {
db_.CommitTransaction();
}
sql::Connection* WebDatabase::GetSQLConnection() {
return &db_;
}
sql::InitStatus WebDatabase::Init(const base::FilePath& db_name) {
db_.set_histogram_tag("Web");
db_.set_page_size(2048);
db_.set_cache_size(32);
db_.set_exclusive_locking();
if (!db_.Open(db_name))
return sql::INIT_FAILURE;
sql::Transaction transaction(&db_);
if (!transaction.Begin())
return sql::INIT_FAILURE;
if (!meta_table_.Init(&db_, kCurrentVersionNumber, kCompatibleVersionNumber))
return sql::INIT_FAILURE;
if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) {
LOG(WARNING) << "Web database is too new.";
return sql::INIT_TOO_NEW;
}
for (TableMap::iterator it = tables_.begin(); it != tables_.end(); ++it) {
it->second->Init(&db_, &meta_table_);
}
sql::InitStatus migration_status = MigrateOldVersionsAsNeeded();
if (migration_status != sql::INIT_OK)
return migration_status;
for (TableMap::iterator it = tables_.begin(); it != tables_.end(); ++it) {
if (!it->second->CreateTablesIfNecessary()) {
LOG(WARNING) << "Unable to initialize the web database.";
return sql::INIT_FAILURE;
}
}
return transaction.Commit() ? sql::INIT_OK : sql::INIT_FAILURE;
}
sql::InitStatus WebDatabase::MigrateOldVersionsAsNeeded() {
int current_version = std::max(meta_table_.GetVersionNumber(),
meta_table_.GetCompatibleVersionNumber());
if (current_version > meta_table_.GetVersionNumber())
ChangeVersion(&meta_table_, current_version, false);
if (current_version < 20) {
LOG(WARNING) << "Web database version " << current_version
<< " is too old to handle.";
NOTREACHED();
return sql::INIT_FAILURE;
}
for (int next_version = current_version + 1;
next_version <= kCurrentVersionNumber;
++next_version) {
for (TableMap::iterator it = tables_.begin(); it != tables_.end(); ++it) {
bool update_compatible_version = false;
if (!it->second->MigrateToVersion(next_version,
&update_compatible_version)) {
return FailedMigrationTo(next_version);
}
ChangeVersion(&meta_table_, next_version, update_compatible_version);
}
}
return sql::INIT_OK;
}