OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sql/meta_table.h" | 5 #include "sql/meta_table.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "sql/connection.h" | 9 #include "sql/connection.h" |
10 #include "sql/statement.h" | 10 #include "sql/statement.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 bool MetaTable::DoesTableExist(sql::Connection* db) { | 25 bool MetaTable::DoesTableExist(sql::Connection* db) { |
26 DCHECK(db); | 26 DCHECK(db); |
27 return db->DoesTableExist("meta"); | 27 return db->DoesTableExist("meta"); |
28 } | 28 } |
29 | 29 |
30 bool MetaTable::Init(Connection* db, int version, int compatible_version) { | 30 bool MetaTable::Init(Connection* db, int version, int compatible_version) { |
31 DCHECK(!db_ && db); | 31 DCHECK(!db_ && db); |
32 db_ = db; | 32 db_ = db; |
33 if (!DoesTableExist(db)) { | 33 if (!DoesTableExist(db)) { |
34 if (!db_->Execute("CREATE TABLE meta" | 34 if (!db_->Execute("CREATE TABLE meta" |
35 "(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY," | 35 "(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY, value LONGVARCHAR)")) |
36 "value LONGVARCHAR)")) | |
37 return false; | 36 return false; |
38 | 37 |
39 // Note: there is no index over the meta table. We currently only have a | 38 // 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 | 39 // couple of keys, so it doesn't matter. If we start storing more stuff in |
41 // there, we should create an index. | 40 // there, we should create an index. |
42 SetVersionNumber(version); | 41 SetVersionNumber(version); |
43 SetCompatibleVersionNumber(compatible_version); | 42 SetCompatibleVersionNumber(compatible_version); |
44 } | 43 } |
45 return true; | 44 return true; |
46 } | 45 } |
47 | 46 |
48 void MetaTable::Reset() { | 47 void MetaTable::Reset() { |
49 db_ = NULL; | 48 db_ = NULL; |
50 } | 49 } |
51 | 50 |
| 51 void MetaTable::SetVersionNumber(int version) { |
| 52 SetValue(kVersionKey, version); |
| 53 } |
| 54 |
| 55 int MetaTable::GetVersionNumber() { |
| 56 int version = 0; |
| 57 return GetValue(kVersionKey, &version) ? version : 0; |
| 58 } |
| 59 |
| 60 void MetaTable::SetCompatibleVersionNumber(int version) { |
| 61 SetValue(kCompatibleVersionKey, version); |
| 62 } |
| 63 |
| 64 int MetaTable::GetCompatibleVersionNumber() { |
| 65 int version = 0; |
| 66 return GetValue(kCompatibleVersionKey, &version) ? version : 0; |
| 67 } |
| 68 |
52 bool MetaTable::SetValue(const char* key, const std::string& value) { | 69 bool MetaTable::SetValue(const char* key, const std::string& value) { |
53 Statement s; | 70 Statement s; |
54 PrepareSetStatement(&s, key); | 71 PrepareSetStatement(&s, key); |
55 s.BindString(1, value); | 72 s.BindString(1, value); |
56 return s.Run(); | 73 return s.Run(); |
57 } | 74 } |
58 | 75 |
| 76 bool MetaTable::SetValue(const char* key, int value) { |
| 77 Statement s; |
| 78 PrepareSetStatement(&s, key); |
| 79 s.BindInt(1, value); |
| 80 return s.Run(); |
| 81 } |
| 82 |
| 83 bool MetaTable::SetValue(const char* key, int64 value) { |
| 84 Statement s; |
| 85 PrepareSetStatement(&s, key); |
| 86 s.BindInt64(1, value); |
| 87 return s.Run(); |
| 88 } |
| 89 |
59 bool MetaTable::GetValue(const char* key, std::string* value) { | 90 bool MetaTable::GetValue(const char* key, std::string* value) { |
60 Statement s; | 91 Statement s; |
61 if (!PrepareGetStatement(&s, key)) | 92 if (!PrepareGetStatement(&s, key)) |
62 return false; | 93 return false; |
63 | 94 |
64 *value = s.ColumnString(0); | 95 *value = s.ColumnString(0); |
65 return true; | 96 return true; |
66 } | 97 } |
67 | 98 |
68 bool MetaTable::SetValue(const char* key, int value) { | |
69 Statement s; | |
70 PrepareSetStatement(&s, key); | |
71 s.BindInt(1, value); | |
72 return s.Run(); | |
73 } | |
74 | |
75 bool MetaTable::GetValue(const char* key, int* value) { | 99 bool MetaTable::GetValue(const char* key, int* value) { |
76 Statement s; | 100 Statement s; |
77 if (!PrepareGetStatement(&s, key)) | 101 if (!PrepareGetStatement(&s, key)) |
78 return false; | 102 return false; |
79 | 103 |
80 *value = s.ColumnInt(0); | 104 *value = s.ColumnInt(0); |
81 return true; | 105 return true; |
82 } | 106 } |
83 | 107 |
84 bool MetaTable::SetValue(const char* key, int64 value) { | |
85 Statement s; | |
86 PrepareSetStatement(&s, key); | |
87 s.BindInt64(1, value); | |
88 return s.Run(); | |
89 } | |
90 | |
91 bool MetaTable::GetValue(const char* key, int64* value) { | 108 bool MetaTable::GetValue(const char* key, int64* value) { |
92 Statement s; | 109 Statement s; |
93 if (!PrepareGetStatement(&s, key)) | 110 if (!PrepareGetStatement(&s, key)) |
94 return false; | 111 return false; |
95 | 112 |
96 *value = s.ColumnInt64(0); | 113 *value = s.ColumnInt64(0); |
97 return true; | 114 return true; |
98 } | 115 } |
99 | 116 |
100 void MetaTable::SetVersionNumber(int version) { | 117 bool MetaTable::DeleteKey(const char* key) { |
101 SetValue(kVersionKey, version); | 118 DCHECK(db_); |
102 } | 119 Statement s(db_->GetUniqueStatement("DELETE FROM meta WHERE key=?")); |
103 | 120 s.BindCString(0, key); |
104 int MetaTable::GetVersionNumber() { | 121 return s.Run(); |
105 int version = 0; | |
106 if (!GetValue(kVersionKey, &version)) | |
107 return 0; | |
108 return version; | |
109 } | |
110 | |
111 void MetaTable::SetCompatibleVersionNumber(int version) { | |
112 SetValue(kCompatibleVersionKey, version); | |
113 } | |
114 | |
115 int MetaTable::GetCompatibleVersionNumber() { | |
116 int version = 0; | |
117 if (!GetValue(kCompatibleVersionKey, &version)) | |
118 return 0; | |
119 return version; | |
120 } | 122 } |
121 | 123 |
122 void MetaTable::PrepareSetStatement(Statement* statement, const char* key) { | 124 void MetaTable::PrepareSetStatement(Statement* statement, const char* key) { |
123 DCHECK(db_ && statement); | 125 DCHECK(db_ && statement); |
124 statement->Assign(db_->GetCachedStatement(SQL_FROM_HERE, | 126 statement->Assign(db_->GetCachedStatement(SQL_FROM_HERE, |
125 "INSERT OR REPLACE INTO meta (key,value) VALUES (?,?)")); | 127 "INSERT OR REPLACE INTO meta (key,value) VALUES (?,?)")); |
126 statement->BindCString(0, key); | 128 statement->BindCString(0, key); |
127 } | 129 } |
128 | 130 |
129 bool MetaTable::PrepareGetStatement(Statement* statement, const char* key) { | 131 bool MetaTable::PrepareGetStatement(Statement* statement, const char* key) { |
130 DCHECK(db_ && statement); | 132 DCHECK(db_ && statement); |
131 statement->Assign(db_->GetCachedStatement(SQL_FROM_HERE, | 133 statement->Assign(db_->GetCachedStatement(SQL_FROM_HERE, |
132 "SELECT value FROM meta WHERE key=?")); | 134 "SELECT value FROM meta WHERE key=?")); |
133 statement->BindCString(0, key); | 135 statement->BindCString(0, key); |
134 if (!statement->Step()) | 136 return statement->Step(); |
135 return false; | |
136 return true; | |
137 } | 137 } |
138 | 138 |
139 } // namespace sql | 139 } // namespace sql |
OLD | NEW |