Print Preview: Refactoring print/cancel button and print summary.
[chromium-blink-merge.git] / sql / meta_table.cc
blobc4736ef37f06d300b5e0f47f546e4bc725ea3361
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"
12 namespace sql {
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() {
24 // static
25 bool MetaTable::DoesTableExist(sql::Connection* db) {
26 DCHECK(db);
27 return db->DoesTableExist("meta");
30 bool MetaTable::Init(Connection* db, int version, int compatible_version) {
31 DCHECK(!db_ && db);
32 db_ = db;
33 if (!DoesTableExist(db)) {
34 if (!db_->Execute("CREATE TABLE meta"
35 "(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY,"
36 "value LONGVARCHAR)"))
37 return false;
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);
45 return true;
48 void MetaTable::Reset() {
49 db_ = NULL;
52 bool MetaTable::SetValue(const char* key, const std::string& value) {
53 Statement s;
54 if (!PrepareSetStatement(&s, key))
55 return false;
56 s.BindString(1, value);
57 return s.Run();
60 bool MetaTable::GetValue(const char* key, std::string* value) {
61 Statement s;
62 if (!PrepareGetStatement(&s, key))
63 return false;
65 *value = s.ColumnString(0);
66 return true;
69 bool MetaTable::SetValue(const char* key, int value) {
70 Statement s;
71 if (!PrepareSetStatement(&s, key))
72 return false;
74 s.BindInt(1, value);
75 return s.Run();
78 bool MetaTable::GetValue(const char* key, int* value) {
79 Statement s;
80 if (!PrepareGetStatement(&s, key))
81 return false;
83 *value = s.ColumnInt(0);
84 return true;
87 bool MetaTable::SetValue(const char* key, int64 value) {
88 Statement s;
89 if (!PrepareSetStatement(&s, key))
90 return false;
91 s.BindInt64(1, value);
92 return s.Run();
95 bool MetaTable::GetValue(const char* key, int64* value) {
96 Statement s;
97 if (!PrepareGetStatement(&s, key))
98 return false;
100 *value = s.ColumnInt64(0);
101 return true;
104 void MetaTable::SetVersionNumber(int version) {
105 SetValue(kVersionKey, version);
108 int MetaTable::GetVersionNumber() {
109 int version = 0;
110 if (!GetValue(kVersionKey, &version))
111 return 0;
112 return version;
115 void MetaTable::SetCompatibleVersionNumber(int version) {
116 SetValue(kCompatibleVersionKey, version);
119 int MetaTable::GetCompatibleVersionNumber() {
120 int version = 0;
121 if (!GetValue(kCompatibleVersionKey, &version))
122 return 0;
123 return 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();
132 return false;
134 statement->BindCString(0, key);
135 return true;
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();
144 return false;
146 statement->BindCString(0, key);
147 if (!statement->Step())
148 return false;
149 return true;
152 } // namespace sql