Commit c3ebc326 authored by pkasting@chromium.org's avatar pkasting@chromium.org

Add a DeleteKey() method to delete a key from the meta table. This will be...

Add a DeleteKey() method to delete a key from the meta table.  This will be used in the TemplateURL refactoring changes.

Reorder function definitions to match declaration order.  Shorten a few bits.

BUG=none
TEST=none
Review URL: https://chromiumcodereview.appspot.com/9584031

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@124823 0039d316-1c4b-4281-b951-d872f2087c98
parent fbfb789c
// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
...@@ -32,8 +32,7 @@ bool MetaTable::Init(Connection* db, int version, int compatible_version) { ...@@ -32,8 +32,7 @@ bool MetaTable::Init(Connection* db, int version, int compatible_version) {
db_ = db; db_ = db;
if (!DoesTableExist(db)) { if (!DoesTableExist(db)) {
if (!db_->Execute("CREATE TABLE meta" if (!db_->Execute("CREATE TABLE meta"
"(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY," "(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY, value LONGVARCHAR)"))
"value LONGVARCHAR)"))
return false; return false;
// Note: there is no index over the meta table. We currently only have a // Note: there is no index over the meta table. We currently only have a
...@@ -49,6 +48,24 @@ void MetaTable::Reset() { ...@@ -49,6 +48,24 @@ void MetaTable::Reset() {
db_ = NULL; db_ = NULL;
} }
void MetaTable::SetVersionNumber(int version) {
SetValue(kVersionKey, version);
}
int MetaTable::GetVersionNumber() {
int version = 0;
return GetValue(kVersionKey, &version) ? version : 0;
}
void MetaTable::SetCompatibleVersionNumber(int version) {
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) { bool MetaTable::SetValue(const char* key, const std::string& value) {
Statement s; Statement s;
PrepareSetStatement(&s, key); PrepareSetStatement(&s, key);
...@@ -56,36 +73,36 @@ bool MetaTable::SetValue(const char* key, const std::string& value) { ...@@ -56,36 +73,36 @@ bool MetaTable::SetValue(const char* key, const std::string& value) {
return s.Run(); return s.Run();
} }
bool MetaTable::GetValue(const char* key, std::string* value) { bool MetaTable::SetValue(const char* key, int value) {
Statement s; Statement s;
if (!PrepareGetStatement(&s, key)) PrepareSetStatement(&s, key);
return false; s.BindInt(1, value);
return s.Run();
*value = s.ColumnString(0);
return true;
} }
bool MetaTable::SetValue(const char* key, int value) { bool MetaTable::SetValue(const char* key, int64 value) {
Statement s; Statement s;
PrepareSetStatement(&s, key); PrepareSetStatement(&s, key);
s.BindInt(1, value); s.BindInt64(1, value);
return s.Run(); return s.Run();
} }
bool MetaTable::GetValue(const char* key, int* value) { bool MetaTable::GetValue(const char* key, std::string* value) {
Statement s; Statement s;
if (!PrepareGetStatement(&s, key)) if (!PrepareGetStatement(&s, key))
return false; return false;
*value = s.ColumnInt(0); *value = s.ColumnString(0);
return true; return true;
} }
bool MetaTable::SetValue(const char* key, int64 value) { bool MetaTable::GetValue(const char* key, int* value) {
Statement s; Statement s;
PrepareSetStatement(&s, key); if (!PrepareGetStatement(&s, key))
s.BindInt64(1, value); return false;
return s.Run();
*value = s.ColumnInt(0);
return true;
} }
bool MetaTable::GetValue(const char* key, int64* value) { bool MetaTable::GetValue(const char* key, int64* value) {
...@@ -97,26 +114,11 @@ bool MetaTable::GetValue(const char* key, int64* value) { ...@@ -97,26 +114,11 @@ bool MetaTable::GetValue(const char* key, int64* value) {
return true; return true;
} }
void MetaTable::SetVersionNumber(int version) { bool MetaTable::DeleteKey(const char* key) {
SetValue(kVersionKey, version); DCHECK(db_);
} Statement s(db_->GetUniqueStatement("DELETE FROM meta WHERE key=?"));
s.BindCString(0, key);
int MetaTable::GetVersionNumber() { return s.Run();
int version = 0;
if (!GetValue(kVersionKey, &version))
return 0;
return version;
}
void MetaTable::SetCompatibleVersionNumber(int version) {
SetValue(kCompatibleVersionKey, version);
}
int MetaTable::GetCompatibleVersionNumber() {
int version = 0;
if (!GetValue(kCompatibleVersionKey, &version))
return 0;
return version;
} }
void MetaTable::PrepareSetStatement(Statement* statement, const char* key) { void MetaTable::PrepareSetStatement(Statement* statement, const char* key) {
...@@ -131,9 +133,7 @@ bool MetaTable::PrepareGetStatement(Statement* statement, const char* key) { ...@@ -131,9 +133,7 @@ bool MetaTable::PrepareGetStatement(Statement* statement, const char* key) {
statement->Assign(db_->GetCachedStatement(SQL_FROM_HERE, statement->Assign(db_->GetCachedStatement(SQL_FROM_HERE,
"SELECT value FROM meta WHERE key=?")); "SELECT value FROM meta WHERE key=?"));
statement->BindCString(0, key); statement->BindCString(0, key);
if (!statement->Step()) return statement->Step();
return false;
return true;
} }
} // namespace sql } // namespace sql
// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
...@@ -68,6 +68,9 @@ class SQL_EXPORT MetaTable { ...@@ -68,6 +68,9 @@ class SQL_EXPORT MetaTable {
bool GetValue(const char* key, int* value); bool GetValue(const char* key, int* value);
bool GetValue(const char* key, int64* value); bool GetValue(const char* key, int64* value);
// Deletes the key from the table.
bool DeleteKey(const char* key);
private: private:
// Conveniences to prepare the two types of statements used by // Conveniences to prepare the two types of statements used by
// MetaTableHelper. // MetaTableHelper.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment