Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(224)

Side by Side Diff: chrome/browser/meta_table_helper.cc

Issue 150094: Refactor MetaTableHelper to remove redundant table name prefixing with databa... (Closed) Base URL: svn://chrome-svn.corp.google.com/chrome/trunk/src/
Patch Set: '' Created 11 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 "chrome/browser/meta_table_helper.h" 5 #include "chrome/browser/meta_table_helper.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 "chrome/common/sqlite_utils.h" 9 #include "chrome/common/sqlite_utils.h"
10 10
11 // Key used in our meta table for version numbers. 11 // Key used in our meta table for version numbers.
12 static const char kVersionKey[] = "version"; 12 static const char kVersionKey[] = "version";
13 static const char kCompatibleVersionKey[] = "last_compatible_version"; 13 static const char kCompatibleVersionKey[] = "last_compatible_version";
14 14
15 // static
16 void MetaTableHelper::PrimeCache(const std::string& db_name, sqlite3* db) {
17 // A statement must be open for the preload command to work. If the meta
18 // table doesn't exist, it probably means this is a new database and there
19 // is nothing to preload (so it's OK we do nothing).
20 SQLStatement dummy;
21 if (!DoesSqliteTableExist(db, db_name.c_str(), "meta"))
22 return;
23 std::string sql("SELECT * from ");
24 appendMetaTableName(db_name, &sql);
25 if (dummy.prepare(db, sql.c_str()) != SQLITE_OK)
26 return;
27 if (dummy.step() != SQLITE_ROW)
28 return;
29
30 sqlite3Preload(db);
31 }
32
15 MetaTableHelper::MetaTableHelper() : db_(NULL) { 33 MetaTableHelper::MetaTableHelper() : db_(NULL) {
16 } 34 }
17 35
18 MetaTableHelper::~MetaTableHelper() { 36 MetaTableHelper::~MetaTableHelper() {
19 } 37 }
20 38
21 bool MetaTableHelper::Init(const std::string& db_name, 39 bool MetaTableHelper::Init(const std::string& db_name,
22 int version, 40 int version,
23 int compatible_version, 41 int compatible_version,
24 sqlite3* db) { 42 sqlite3* db) {
25 DCHECK(!db_ && db); 43 DCHECK(!db_ && db);
26 db_ = db; 44 db_ = db;
27 db_name_ = db_name; 45 db_name_ = db_name;
28 if (!DoesSqliteTableExist(db_, db_name.c_str(), "meta")) { 46 if (!DoesSqliteTableExist(db_, db_name.c_str(), "meta")) {
29 // Build the sql. 47 // Build the sql.
30 std::string sql("CREATE TABLE "); 48 std::string sql("CREATE TABLE ");
31 if (!db_name.empty()) { 49 appendMetaTableName(db_name, &sql);
32 // Want a table name of the form db_name.meta 50 sql.append("(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY,"
33 sql.append(db_name); 51 "value LONGVARCHAR)");
34 sql.push_back('.');
35 }
36 sql.append("meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY,"
37 "value LONGVARCHAR)");
38 52
39 if (sqlite3_exec(db_, sql.c_str(), NULL, NULL, NULL) != SQLITE_OK) 53 if (sqlite3_exec(db_, sql.c_str(), NULL, NULL, NULL) != SQLITE_OK)
40 return false; 54 return false;
41 55
42 // Note: there is no index over the meta table. We currently only have a 56 // Note: there is no index over the meta table. We currently only have a
43 // couple of keys, so it doesn't matter. If we start storing more stuff in 57 // couple of keys, so it doesn't matter. If we start storing more stuff in
44 // there, we should create an index. 58 // there, we should create an index.
45 SetVersionNumber(version); 59 SetVersionNumber(version);
46 SetCompatibleVersionNumber(compatible_version); 60 SetCompatibleVersionNumber(compatible_version);
47 } 61 }
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 SetValue(kCompatibleVersionKey, version); 137 SetValue(kCompatibleVersionKey, version);
124 } 138 }
125 139
126 int MetaTableHelper::GetCompatibleVersionNumber() { 140 int MetaTableHelper::GetCompatibleVersionNumber() {
127 int version; 141 int version;
128 if (!GetValue(kCompatibleVersionKey, &version)) 142 if (!GetValue(kCompatibleVersionKey, &version))
129 return 0; 143 return 0;
130 return version; 144 return version;
131 } 145 }
132 146
147 // static
148 void MetaTableHelper::appendMetaTableName(const std::string& db_name,
149 std::string* sql) {
150 if (!db_name.empty()) {
151 sql->append(db_name);
152 sql->push_back('.');
153 }
154 sql->append("meta");
155 }
156
133 bool MetaTableHelper::PrepareSetStatement(SQLStatement* statement, 157 bool MetaTableHelper::PrepareSetStatement(SQLStatement* statement,
134 const std::string& key) { 158 const std::string& key) {
135 DCHECK(db_ && statement); 159 DCHECK(db_ && statement);
136 std::string sql("INSERT OR REPLACE INTO "); 160 std::string sql("INSERT OR REPLACE INTO ");
137 if (db_name_.size() > 0) { 161 appendMetaTableName(db_name_, &sql);
138 sql.append(db_name_); 162 sql.append("(key,value) VALUES(?,?)");
139 sql.push_back('.');
140 }
141 sql.append("meta(key,value) VALUES(?,?)");
142 if (statement->prepare(db_, sql.c_str()) != SQLITE_OK) { 163 if (statement->prepare(db_, sql.c_str()) != SQLITE_OK) {
143 NOTREACHED() << sqlite3_errmsg(db_); 164 NOTREACHED() << sqlite3_errmsg(db_);
144 return false; 165 return false;
145 } 166 }
146 statement->bind_text(0, key.c_str()); 167 statement->bind_text(0, key.c_str());
147 return true; 168 return true;
148 } 169 }
149 170
150 bool MetaTableHelper::PrepareGetStatement(SQLStatement* statement, 171 bool MetaTableHelper::PrepareGetStatement(SQLStatement* statement,
151 const std::string& key) { 172 const std::string& key) {
152 DCHECK(db_ && statement); 173 DCHECK(db_ && statement);
153 std::string sql("SELECT value FROM "); 174 std::string sql("SELECT value FROM ");
154 if (db_name_.size() > 0) { 175 appendMetaTableName(db_name_, &sql);
155 sql.append(db_name_); 176 sql.append(" WHERE key = ?");
156 sql.push_back('.');
157 }
158 sql.append("meta WHERE key = ?");
159 if (statement->prepare(db_, sql.c_str()) != SQLITE_OK) { 177 if (statement->prepare(db_, sql.c_str()) != SQLITE_OK) {
160 NOTREACHED() << sqlite3_errmsg(db_); 178 NOTREACHED() << sqlite3_errmsg(db_);
161 return false; 179 return false;
162 } 180 }
163 statement->bind_string(0, key); 181 statement->bind_string(0, key);
164 if (statement->step() != SQLITE_ROW) 182 if (statement->step() != SQLITE_ROW)
165 return false; 183 return false;
166 return true; 184 return true;
167 } 185 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698