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 db_
->AddTaggedHistogram("Sqlite.Version", GetVersionNumber());
58 return transaction
.Commit();
61 void MetaTable::Reset() {
65 void MetaTable::SetVersionNumber(int version
) {
66 DCHECK_GT(version
, 0);
67 SetValue(kVersionKey
, version
);
70 int MetaTable::GetVersionNumber() {
72 return GetValue(kVersionKey
, &version
) ? version
: 0;
75 void MetaTable::SetCompatibleVersionNumber(int version
) {
76 DCHECK_GT(version
, 0);
77 SetValue(kCompatibleVersionKey
, version
);
80 int MetaTable::GetCompatibleVersionNumber() {
82 return GetValue(kCompatibleVersionKey
, &version
) ? version
: 0;
85 bool MetaTable::SetValue(const char* key
, const std::string
& value
) {
87 PrepareSetStatement(&s
, key
);
88 s
.BindString(1, value
);
92 bool MetaTable::SetValue(const char* key
, int value
) {
94 PrepareSetStatement(&s
, key
);
99 bool MetaTable::SetValue(const char* key
, int64 value
) {
101 PrepareSetStatement(&s
, key
);
102 s
.BindInt64(1, value
);
106 bool MetaTable::GetValue(const char* key
, std::string
* value
) {
108 if (!PrepareGetStatement(&s
, key
))
111 *value
= s
.ColumnString(0);
115 bool MetaTable::GetValue(const char* key
, int* value
) {
117 if (!PrepareGetStatement(&s
, key
))
120 *value
= s
.ColumnInt(0);
124 bool MetaTable::GetValue(const char* key
, int64
* value
) {
126 if (!PrepareGetStatement(&s
, key
))
129 *value
= s
.ColumnInt64(0);
133 bool MetaTable::DeleteKey(const char* key
) {
135 Statement
s(db_
->GetUniqueStatement("DELETE FROM meta WHERE key=?"));
136 s
.BindCString(0, key
);
140 void MetaTable::PrepareSetStatement(Statement
* statement
, const char* key
) {
141 DCHECK(db_
&& statement
);
142 statement
->Assign(db_
->GetCachedStatement(SQL_FROM_HERE
,
143 "INSERT OR REPLACE INTO meta (key,value) VALUES (?,?)"));
144 statement
->BindCString(0, key
);
147 bool MetaTable::PrepareGetStatement(Statement
* statement
, const char* key
) {
148 DCHECK(db_
&& statement
);
149 statement
->Assign(db_
->GetCachedStatement(SQL_FROM_HERE
,
150 "SELECT value FROM meta WHERE key=?"));
151 statement
->BindCString(0, key
);
152 return statement
->Step();