This source file includes following definitions.
- RecordDeprecationEvent
- DoesTableExist
- RazeIfDeprecated
- Init
- Reset
- SetVersionNumber
- GetVersionNumber
- SetCompatibleVersionNumber
- GetCompatibleVersionNumber
- SetValue
- SetValue
- SetValue
- GetValue
- GetValue
- GetValue
- DeleteKey
- PrepareSetStatement
- PrepareGetStatement
#include "sql/meta_table.h"
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/strings/string_util.h"
#include "sql/connection.h"
#include "sql/statement.h"
#include "sql/transaction.h"
namespace {
const char kVersionKey[] = "version";
const char kCompatibleVersionKey[] = "last_compatible_version";
enum DeprecationEventType {
DEPRECATION_DATABASE_NOT_EMPTY = 0,
DEPRECATION_DATABASE_UNKNOWN,
DEPRECATION_FAILED_VERSION,
DEPRECATION_NO_VERSION,
DEPRECATION_RAZED,
DEPRECATION_RAZE_FAILED,
DEPRECATION_EVENT_MAX,
};
void RecordDeprecationEvent(DeprecationEventType deprecation_event) {
UMA_HISTOGRAM_ENUMERATION("Sqlite.DeprecationVersionResult",
deprecation_event, DEPRECATION_EVENT_MAX);
}
}
namespace sql {
MetaTable::MetaTable() : db_(NULL) {
}
MetaTable::~MetaTable() {
}
bool MetaTable::DoesTableExist(sql::Connection* db) {
DCHECK(db);
return db->DoesTableExist("meta");
}
void MetaTable::RazeIfDeprecated(Connection* db, int deprecated_version) {
DCHECK_GT(deprecated_version, 0);
DCHECK_EQ(0, db->transaction_nesting());
if (!DoesTableExist(db)) {
sql::Statement s(db->GetUniqueStatement(
"SELECT COUNT(*) FROM sqlite_master"));
if (s.Step()) {
if (s.ColumnInt(0) != 0) {
RecordDeprecationEvent(DEPRECATION_DATABASE_NOT_EMPTY);
}
} else {
RecordDeprecationEvent(DEPRECATION_DATABASE_UNKNOWN);
}
return;
}
sql::Statement s(db->GetUniqueStatement(
"SELECT value FROM meta WHERE key=?"));
s.BindCString(0, kVersionKey);
if (!s.Step()) {
if (!s.Succeeded()) {
RecordDeprecationEvent(DEPRECATION_FAILED_VERSION);
} else {
RecordDeprecationEvent(DEPRECATION_NO_VERSION);
}
return;
}
int version = s.ColumnInt(0);
s.Clear();
if (version <= deprecated_version) {
if (db->Raze()) {
RecordDeprecationEvent(DEPRECATION_RAZED);
} else {
RecordDeprecationEvent(DEPRECATION_RAZE_FAILED);
}
return;
}
}
bool MetaTable::Init(Connection* db, int version, int compatible_version) {
DCHECK(!db_ && db);
db_ = db;
DCHECK_GT(version, 0);
DCHECK_GT(compatible_version, 0);
sql::Transaction transaction(db_);
if (!transaction.Begin())
return false;
if (!DoesTableExist(db)) {
if (!db_->Execute("CREATE TABLE meta"
"(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY, value LONGVARCHAR)"))
return false;
SetVersionNumber(version);
SetCompatibleVersionNumber(compatible_version);
} else {
db_->AddTaggedHistogram("Sqlite.Version", GetVersionNumber());
}
return transaction.Commit();
}
void MetaTable::Reset() {
db_ = NULL;
}
void MetaTable::SetVersionNumber(int version) {
DCHECK_GT(version, 0);
SetValue(kVersionKey, version);
}
int MetaTable::GetVersionNumber() {
int version = 0;
return GetValue(kVersionKey, &version) ? version : 0;
}
void MetaTable::SetCompatibleVersionNumber(int version) {
DCHECK_GT(version, 0);
SetValue(kCompatibleVersionKey, version);
}
int MetaTable::GetCompatibleVersionNumber() {
int version = 0;
return GetValue(kCompatibleVersionKey, &version) ? version : 0;
}
bool MetaTable::SetValue(const char* key, const std::string& value) {
Statement s;
PrepareSetStatement(&s, key);
s.BindString(1, value);
return s.Run();
}
bool MetaTable::SetValue(const char* key, int value) {
Statement s;
PrepareSetStatement(&s, key);
s.BindInt(1, value);
return s.Run();
}
bool MetaTable::SetValue(const char* key, int64 value) {
Statement s;
PrepareSetStatement(&s, key);
s.BindInt64(1, value);
return s.Run();
}
bool MetaTable::GetValue(const char* key, std::string* value) {
Statement s;
if (!PrepareGetStatement(&s, key))
return false;
*value = s.ColumnString(0);
return true;
}
bool MetaTable::GetValue(const char* key, int* value) {
Statement s;
if (!PrepareGetStatement(&s, key))
return false;
*value = s.ColumnInt(0);
return true;
}
bool MetaTable::GetValue(const char* key, int64* value) {
Statement s;
if (!PrepareGetStatement(&s, key))
return false;
*value = s.ColumnInt64(0);
return true;
}
bool MetaTable::DeleteKey(const char* key) {
DCHECK(db_);
Statement s(db_->GetUniqueStatement("DELETE FROM meta WHERE key=?"));
s.BindCString(0, key);
return s.Run();
}
void MetaTable::PrepareSetStatement(Statement* statement, const char* key) {
DCHECK(db_ && statement);
statement->Assign(db_->GetCachedStatement(SQL_FROM_HERE,
"INSERT OR REPLACE INTO meta (key,value) VALUES (?,?)"));
statement->BindCString(0, key);
}
bool MetaTable::PrepareGetStatement(Statement* statement, const char* key) {
DCHECK(db_ && statement);
statement->Assign(db_->GetCachedStatement(SQL_FROM_HERE,
"SELECT value FROM meta WHERE key=?"));
statement->BindCString(0, key);
return statement->Step();
}
}