blob: 45f4ee09dbee49bf7677181420c79659fd9e9eb2 [file] [log] [blame]
[email protected]c3ebc322012-03-03 02:07:461// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]e5ffd0e42009-09-11 21:30:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]f0a54b22011-07-19 18:40:215#include "sql/meta_table.h"
[email protected]e5ffd0e42009-09-11 21:30:566
[email protected]e5ffd0e42009-09-11 21:30:567#include "base/logging.h"
8#include "base/string_util.h"
[email protected]f0a54b22011-07-19 18:40:219#include "sql/connection.h"
10#include "sql/statement.h"
[email protected]1bc85b22012-03-08 04:53:4911#include "sql/transaction.h"
[email protected]e5ffd0e42009-09-11 21:30:5612
13namespace sql {
14
15// Key used in our meta table for version numbers.
16static const char kVersionKey[] = "version";
17static const char kCompatibleVersionKey[] = "last_compatible_version";
18
19MetaTable::MetaTable() : db_(NULL) {
20}
21
22MetaTable::~MetaTable() {
23}
24
[email protected]24864e42011-01-10 20:38:5125// static
26bool MetaTable::DoesTableExist(sql::Connection* db) {
27 DCHECK(db);
28 return db->DoesTableExist("meta");
29}
30
[email protected]e5ffd0e42009-09-11 21:30:5631bool MetaTable::Init(Connection* db, int version, int compatible_version) {
32 DCHECK(!db_ && db);
33 db_ = db;
[email protected]1bc85b22012-03-08 04:53:4934
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);
39
40 // Make sure the table is created an populated atomically.
41 sql::Transaction transaction(db_);
42 if (!transaction.Begin())
43 return false;
44
[email protected]35f2094c2009-12-29 22:46:5545 if (!DoesTableExist(db)) {
[email protected]e5ffd0e42009-09-11 21:30:5646 if (!db_->Execute("CREATE TABLE meta"
[email protected]c3ebc322012-03-03 02:07:4647 "(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY, value LONGVARCHAR)"))
[email protected]e5ffd0e42009-09-11 21:30:5648 return false;
49
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);
55 }
[email protected]1bc85b22012-03-08 04:53:4956 return transaction.Commit();
[email protected]e5ffd0e42009-09-11 21:30:5657}
58
[email protected]470b0dd2010-06-29 03:20:4059void MetaTable::Reset() {
60 db_ = NULL;
61}
62
[email protected]c3ebc322012-03-03 02:07:4663void MetaTable::SetVersionNumber(int version) {
[email protected]1bc85b22012-03-08 04:53:4964 DCHECK_GT(version, 0);
[email protected]c3ebc322012-03-03 02:07:4665 SetValue(kVersionKey, version);
66}
67
68int MetaTable::GetVersionNumber() {
69 int version = 0;
70 return GetValue(kVersionKey, &version) ? version : 0;
71}
72
73void MetaTable::SetCompatibleVersionNumber(int version) {
[email protected]1bc85b22012-03-08 04:53:4974 DCHECK_GT(version, 0);
[email protected]c3ebc322012-03-03 02:07:4675 SetValue(kCompatibleVersionKey, version);
76}
77
78int MetaTable::GetCompatibleVersionNumber() {
79 int version = 0;
80 return GetValue(kCompatibleVersionKey, &version) ? version : 0;
81}
82
[email protected]e5ffd0e42009-09-11 21:30:5683bool MetaTable::SetValue(const char* key, const std::string& value) {
84 Statement s;
[email protected]eff1fa522011-12-12 23:50:5985 PrepareSetStatement(&s, key);
[email protected]e5ffd0e42009-09-11 21:30:5686 s.BindString(1, value);
87 return s.Run();
88}
89
[email protected]c3ebc322012-03-03 02:07:4690bool MetaTable::SetValue(const char* key, int value) {
91 Statement s;
92 PrepareSetStatement(&s, key);
93 s.BindInt(1, value);
94 return s.Run();
95}
96
97bool MetaTable::SetValue(const char* key, int64 value) {
98 Statement s;
99 PrepareSetStatement(&s, key);
100 s.BindInt64(1, value);
101 return s.Run();
102}
103
[email protected]e5ffd0e42009-09-11 21:30:56104bool MetaTable::GetValue(const char* key, std::string* value) {
105 Statement s;
106 if (!PrepareGetStatement(&s, key))
107 return false;
108
109 *value = s.ColumnString(0);
110 return true;
111}
112
[email protected]e5ffd0e42009-09-11 21:30:56113bool MetaTable::GetValue(const char* key, int* value) {
114 Statement s;
115 if (!PrepareGetStatement(&s, key))
116 return false;
117
118 *value = s.ColumnInt(0);
119 return true;
120}
121
[email protected]e5ffd0e42009-09-11 21:30:56122bool MetaTable::GetValue(const char* key, int64* value) {
123 Statement s;
124 if (!PrepareGetStatement(&s, key))
125 return false;
126
127 *value = s.ColumnInt64(0);
128 return true;
129}
130
[email protected]c3ebc322012-03-03 02:07:46131bool 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();
[email protected]e5ffd0e42009-09-11 21:30:56136}
137
[email protected]eff1fa522011-12-12 23:50:59138void MetaTable::PrepareSetStatement(Statement* statement, const char* key) {
[email protected]e5ffd0e42009-09-11 21:30:56139 DCHECK(db_ && statement);
140 statement->Assign(db_->GetCachedStatement(SQL_FROM_HERE,
141 "INSERT OR REPLACE INTO meta (key,value) VALUES (?,?)"));
[email protected]e5ffd0e42009-09-11 21:30:56142 statement->BindCString(0, key);
[email protected]e5ffd0e42009-09-11 21:30:56143}
144
145bool 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=?"));
[email protected]e5ffd0e42009-09-11 21:30:56149 statement->BindCString(0, key);
[email protected]c3ebc322012-03-03 02:07:46150 return statement->Step();
[email protected]e5ffd0e42009-09-11 21:30:56151}
152
153} // namespace sql