cros: Split GaiaAuthHost from login screen.
[chromium-blink-merge.git] / sql / meta_table.cc
blob45f4ee09dbee49bf7677181420c79659fd9e9eb2
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "sql/meta_table.h"
7 #include "base/logging.h"
8 #include "base/string_util.h"
9 #include "sql/connection.h"
10 #include "sql/statement.h"
11 #include "sql/transaction.h"
13 namespace sql {
15 // Key used in our meta table for version numbers.
16 static const char kVersionKey[] = "version";
17 static const char kCompatibleVersionKey[] = "last_compatible_version";
19 MetaTable::MetaTable() : db_(NULL) {
22 MetaTable::~MetaTable() {
25 // static
26 bool MetaTable::DoesTableExist(sql::Connection* db) {
27 DCHECK(db);
28 return db->DoesTableExist("meta");
31 bool MetaTable::Init(Connection* db, int version, int compatible_version) {
32 DCHECK(!db_ && db);
33 db_ = db;
35 // If values stored are null or missing entirely, 0 will be reported.
36 // Require new clients to start with a greater initial version.
37 DCHECK_GT(version, 0);
38 DCHECK_GT(compatible_version, 0);
40 // Make sure the table is created an populated atomically.
41 sql::Transaction transaction(db_);
42 if (!transaction.Begin())
43 return false;
45 if (!DoesTableExist(db)) {
46 if (!db_->Execute("CREATE TABLE meta"
47 "(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY, value LONGVARCHAR)"))
48 return false;
50 // Note: there is no index over the meta table. We currently only have a
51 // couple of keys, so it doesn't matter. If we start storing more stuff in
52 // there, we should create an index.
53 SetVersionNumber(version);
54 SetCompatibleVersionNumber(compatible_version);
56 return transaction.Commit();
59 void MetaTable::Reset() {
60 db_ = NULL;
63 void MetaTable::SetVersionNumber(int version) {
64 DCHECK_GT(version, 0);
65 SetValue(kVersionKey, version);
68 int MetaTable::GetVersionNumber() {
69 int version = 0;
70 return GetValue(kVersionKey, &version) ? version : 0;
73 void MetaTable::SetCompatibleVersionNumber(int version) {
74 DCHECK_GT(version, 0);
75 SetValue(kCompatibleVersionKey, version);
78 int MetaTable::GetCompatibleVersionNumber() {
79 int version = 0;
80 return GetValue(kCompatibleVersionKey, &version) ? version : 0;
83 bool MetaTable::SetValue(const char* key, const std::string& value) {
84 Statement s;
85 PrepareSetStatement(&s, key);
86 s.BindString(1, value);
87 return s.Run();
90 bool MetaTable::SetValue(const char* key, int value) {
91 Statement s;
92 PrepareSetStatement(&s, key);
93 s.BindInt(1, value);
94 return s.Run();
97 bool MetaTable::SetValue(const char* key, int64 value) {
98 Statement s;
99 PrepareSetStatement(&s, key);
100 s.BindInt64(1, value);
101 return s.Run();
104 bool MetaTable::GetValue(const char* key, std::string* value) {
105 Statement s;
106 if (!PrepareGetStatement(&s, key))
107 return false;
109 *value = s.ColumnString(0);
110 return true;
113 bool MetaTable::GetValue(const char* key, int* value) {
114 Statement s;
115 if (!PrepareGetStatement(&s, key))
116 return false;
118 *value = s.ColumnInt(0);
119 return true;
122 bool MetaTable::GetValue(const char* key, int64* value) {
123 Statement s;
124 if (!PrepareGetStatement(&s, key))
125 return false;
127 *value = s.ColumnInt64(0);
128 return true;
131 bool MetaTable::DeleteKey(const char* key) {
132 DCHECK(db_);
133 Statement s(db_->GetUniqueStatement("DELETE FROM meta WHERE key=?"));
134 s.BindCString(0, key);
135 return s.Run();
138 void MetaTable::PrepareSetStatement(Statement* statement, const char* key) {
139 DCHECK(db_ && statement);
140 statement->Assign(db_->GetCachedStatement(SQL_FROM_HERE,
141 "INSERT OR REPLACE INTO meta (key,value) VALUES (?,?)"));
142 statement->BindCString(0, key);
145 bool MetaTable::PrepareGetStatement(Statement* statement, const char* key) {
146 DCHECK(db_ && statement);
147 statement->Assign(db_->GetCachedStatement(SQL_FROM_HERE,
148 "SELECT value FROM meta WHERE key=?"));
149 statement->BindCString(0, key);
150 return statement->Step();
153 } // namespace sql