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"
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() {
26 bool MetaTable::DoesTableExist(sql::Connection
* db
) {
28 return db
->DoesTableExist("meta");
31 bool MetaTable::Init(Connection
* db
, int version
, int compatible_version
) {
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())
45 if (!DoesTableExist(db
)) {
46 if (!db_
->Execute("CREATE TABLE meta"
47 "(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY, value LONGVARCHAR)"))
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() {
63 void MetaTable::SetVersionNumber(int version
) {
64 DCHECK_GT(version
, 0);
65 SetValue(kVersionKey
, version
);
68 int MetaTable::GetVersionNumber() {
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() {
80 return GetValue(kCompatibleVersionKey
, &version
) ? version
: 0;
83 bool MetaTable::SetValue(const char* key
, const std::string
& value
) {
85 PrepareSetStatement(&s
, key
);
86 s
.BindString(1, value
);
90 bool MetaTable::SetValue(const char* key
, int value
) {
92 PrepareSetStatement(&s
, key
);
97 bool MetaTable::SetValue(const char* key
, int64 value
) {
99 PrepareSetStatement(&s
, key
);
100 s
.BindInt64(1, value
);
104 bool MetaTable::GetValue(const char* key
, std::string
* value
) {
106 if (!PrepareGetStatement(&s
, key
))
109 *value
= s
.ColumnString(0);
113 bool MetaTable::GetValue(const char* key
, int* value
) {
115 if (!PrepareGetStatement(&s
, key
))
118 *value
= s
.ColumnInt(0);
122 bool MetaTable::GetValue(const char* key
, int64
* value
) {
124 if (!PrepareGetStatement(&s
, key
))
127 *value
= s
.ColumnInt64(0);
131 bool MetaTable::DeleteKey(const char* key
) {
133 Statement
s(db_
->GetUniqueStatement("DELETE FROM meta WHERE key=?"));
134 s
.BindCString(0, key
);
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();