Skip to content

Commit

Permalink
Add a DeleteKey() method to delete a key from the meta table. This wi…
Browse files Browse the repository at this point in the history
…ll 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
  • Loading branch information
pkasting@chromium.org committed Mar 3, 2012
1 parent fbfb789 commit c3ebc32
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
80 changes: 40 additions & 40 deletions sql/meta_table.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 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
// found in the LICENSE file.

Expand Down Expand Up @@ -32,8 +32,7 @@ bool MetaTable::Init(Connection* db, int version, int compatible_version) {
db_ = db;
if (!DoesTableExist(db)) {
if (!db_->Execute("CREATE TABLE meta"
"(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY,"
"value LONGVARCHAR)"))
"(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY, value LONGVARCHAR)"))
return false;

// Note: there is no index over the meta table. We currently only have a
Expand All @@ -49,43 +48,61 @@ void MetaTable::Reset() {
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) {
Statement s;
PrepareSetStatement(&s, key);
s.BindString(1, value);
return s.Run();
}

bool MetaTable::GetValue(const char* key, std::string* value) {
bool MetaTable::SetValue(const char* key, int value) {
Statement s;
if (!PrepareGetStatement(&s, key))
return false;

*value = s.ColumnString(0);
return true;
PrepareSetStatement(&s, key);
s.BindInt(1, value);
return s.Run();
}

bool MetaTable::SetValue(const char* key, int value) {
bool MetaTable::SetValue(const char* key, int64 value) {
Statement s;
PrepareSetStatement(&s, key);
s.BindInt(1, value);
s.BindInt64(1, value);
return s.Run();
}

bool MetaTable::GetValue(const char* key, int* value) {
bool MetaTable::GetValue(const char* key, std::string* value) {
Statement s;
if (!PrepareGetStatement(&s, key))
return false;

*value = s.ColumnInt(0);
*value = s.ColumnString(0);
return true;
}

bool MetaTable::SetValue(const char* key, int64 value) {
bool MetaTable::GetValue(const char* key, int* value) {
Statement s;
PrepareSetStatement(&s, key);
s.BindInt64(1, value);
return s.Run();
if (!PrepareGetStatement(&s, key))
return false;

*value = s.ColumnInt(0);
return true;
}

bool MetaTable::GetValue(const char* key, int64* value) {
Expand All @@ -97,26 +114,11 @@ bool MetaTable::GetValue(const char* key, int64* value) {
return true;
}

void MetaTable::SetVersionNumber(int version) {
SetValue(kVersionKey, version);
}

int MetaTable::GetVersionNumber() {
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;
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) {
Expand All @@ -131,9 +133,7 @@ bool MetaTable::PrepareGetStatement(Statement* statement, const char* key) {
statement->Assign(db_->GetCachedStatement(SQL_FROM_HERE,
"SELECT value FROM meta WHERE key=?"));
statement->BindCString(0, key);
if (!statement->Step())
return false;
return true;
return statement->Step();
}

} // namespace sql
5 changes: 4 additions & 1 deletion sql/meta_table.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 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
// found in the LICENSE file.

Expand Down Expand Up @@ -68,6 +68,9 @@ class SQL_EXPORT MetaTable {
bool GetValue(const char* key, int* value);
bool GetValue(const char* key, int64* value);

// Deletes the key from the table.
bool DeleteKey(const char* key);

private:
// Conveniences to prepare the two types of statements used by
// MetaTableHelper.
Expand Down

0 comments on commit c3ebc32

Please sign in to comment.