1 // Copyright (c) 2011 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"
14 // Key used in our meta table for version numbers.
15 static const char kVersionKey
[] = "version";
16 static const char kCompatibleVersionKey
[] = "last_compatible_version";
18 MetaTable::MetaTable() : db_(NULL
) {
21 MetaTable::~MetaTable() {
25 bool MetaTable::DoesTableExist(sql::Connection
* db
) {
27 return db
->DoesTableExist("meta");
30 bool MetaTable::Init(Connection
* db
, int version
, int compatible_version
) {
33 if (!DoesTableExist(db
)) {
34 if (!db_
->Execute("CREATE TABLE meta"
35 "(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY,"
36 "value LONGVARCHAR)"))
39 // Note: there is no index over the meta table. We currently only have a
40 // couple of keys, so it doesn't matter. If we start storing more stuff in
41 // there, we should create an index.
42 SetVersionNumber(version
);
43 SetCompatibleVersionNumber(compatible_version
);
48 void MetaTable::Reset() {
52 bool MetaTable::SetValue(const char* key
, const std::string
& value
) {
54 if (!PrepareSetStatement(&s
, key
))
56 s
.BindString(1, value
);
60 bool MetaTable::GetValue(const char* key
, std::string
* value
) {
62 if (!PrepareGetStatement(&s
, key
))
65 *value
= s
.ColumnString(0);
69 bool MetaTable::SetValue(const char* key
, int value
) {
71 if (!PrepareSetStatement(&s
, key
))
78 bool MetaTable::GetValue(const char* key
, int* value
) {
80 if (!PrepareGetStatement(&s
, key
))
83 *value
= s
.ColumnInt(0);
87 bool MetaTable::SetValue(const char* key
, int64 value
) {
89 if (!PrepareSetStatement(&s
, key
))
91 s
.BindInt64(1, value
);
95 bool MetaTable::GetValue(const char* key
, int64
* value
) {
97 if (!PrepareGetStatement(&s
, key
))
100 *value
= s
.ColumnInt64(0);
104 void MetaTable::SetVersionNumber(int version
) {
105 SetValue(kVersionKey
, version
);
108 int MetaTable::GetVersionNumber() {
110 if (!GetValue(kVersionKey
, &version
))
115 void MetaTable::SetCompatibleVersionNumber(int version
) {
116 SetValue(kCompatibleVersionKey
, version
);
119 int MetaTable::GetCompatibleVersionNumber() {
121 if (!GetValue(kCompatibleVersionKey
, &version
))
126 bool MetaTable::PrepareSetStatement(Statement
* statement
, const char* key
) {
127 DCHECK(db_
&& statement
);
128 statement
->Assign(db_
->GetCachedStatement(SQL_FROM_HERE
,
129 "INSERT OR REPLACE INTO meta (key,value) VALUES (?,?)"));
130 if (!statement
->is_valid()) {
131 NOTREACHED() << db_
->GetErrorMessage();
134 statement
->BindCString(0, key
);
138 bool MetaTable::PrepareGetStatement(Statement
* statement
, const char* key
) {
139 DCHECK(db_
&& statement
);
140 statement
->Assign(db_
->GetCachedStatement(SQL_FROM_HERE
,
141 "SELECT value FROM meta WHERE key=?"));
142 if (!statement
->is_valid()) {
143 NOTREACHED() << db_
->GetErrorMessage();
146 statement
->BindCString(0, key
);
147 if (!statement
->Step())