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

Side by Side Diff: sql/meta_table.cc

Issue 9592026: Create meta table atomically. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 9 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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"
11 #include "sql/transaction.h"
11 12
12 namespace sql { 13 namespace sql {
13 14
14 // Key used in our meta table for version numbers. 15 // Key used in our meta table for version numbers.
15 static const char kVersionKey[] = "version"; 16 static const char kVersionKey[] = "version";
16 static const char kCompatibleVersionKey[] = "last_compatible_version"; 17 static const char kCompatibleVersionKey[] = "last_compatible_version";
17 18
18 MetaTable::MetaTable() : db_(NULL) { 19 MetaTable::MetaTable() : db_(NULL) {
19 } 20 }
20 21
21 MetaTable::~MetaTable() { 22 MetaTable::~MetaTable() {
22 } 23 }
23 24
24 // static 25 // static
25 bool MetaTable::DoesTableExist(sql::Connection* db) { 26 bool MetaTable::DoesTableExist(sql::Connection* db) {
26 DCHECK(db); 27 DCHECK(db);
27 return db->DoesTableExist("meta"); 28 return db->DoesTableExist("meta");
28 } 29 }
29 30
30 bool MetaTable::Init(Connection* db, int version, int compatible_version) { 31 bool MetaTable::Init(Connection* db, int version, int compatible_version) {
31 DCHECK(!db_ && db); 32 DCHECK(!db_ && db);
32 db_ = db; 33 db_ = db;
34
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.
benjhayden 2012/03/06 22:34:35 s/ an / and /
41 sql::Transaction transaction(db_);
42 if (!transaction.Begin())
43 return false;
44
33 if (!DoesTableExist(db)) { 45 if (!DoesTableExist(db)) {
34 if (!db_->Execute("CREATE TABLE meta" 46 if (!db_->Execute("CREATE TABLE meta"
35 "(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY, value LONGVARCHAR)")) 47 "(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY, value LONGVARCHAR)"))
36 return false; 48 return false;
37 49
38 // Note: there is no index over the meta table. We currently only have a 50 // Note: there is no index over the meta table. We currently only have a
39 // couple of keys, so it doesn't matter. If we start storing more stuff in 51 // couple of keys, so it doesn't matter. If we start storing more stuff in
40 // there, we should create an index. 52 // there, we should create an index.
41 SetVersionNumber(version); 53 SetVersionNumber(version);
42 SetCompatibleVersionNumber(compatible_version); 54 SetCompatibleVersionNumber(compatible_version);
43 } 55 }
44 return true; 56 return transaction.Commit();
45 } 57 }
46 58
47 void MetaTable::Reset() { 59 void MetaTable::Reset() {
48 db_ = NULL; 60 db_ = NULL;
49 } 61 }
50 62
51 void MetaTable::SetVersionNumber(int version) { 63 void MetaTable::SetVersionNumber(int version) {
64 DCHECK_GT(version, 0);
52 SetValue(kVersionKey, version); 65 SetValue(kVersionKey, version);
53 } 66 }
54 67
55 int MetaTable::GetVersionNumber() { 68 int MetaTable::GetVersionNumber() {
56 int version = 0; 69 int version = 0;
57 return GetValue(kVersionKey, &version) ? version : 0; 70 return GetValue(kVersionKey, &version) ? version : 0;
58 } 71 }
59 72
60 void MetaTable::SetCompatibleVersionNumber(int version) { 73 void MetaTable::SetCompatibleVersionNumber(int version) {
74 DCHECK_GT(version, 0);
61 SetValue(kCompatibleVersionKey, version); 75 SetValue(kCompatibleVersionKey, version);
62 } 76 }
63 77
64 int MetaTable::GetCompatibleVersionNumber() { 78 int MetaTable::GetCompatibleVersionNumber() {
65 int version = 0; 79 int version = 0;
66 return GetValue(kCompatibleVersionKey, &version) ? version : 0; 80 return GetValue(kCompatibleVersionKey, &version) ? version : 0;
67 } 81 }
68 82
69 bool MetaTable::SetValue(const char* key, const std::string& value) { 83 bool MetaTable::SetValue(const char* key, const std::string& value) {
70 Statement s; 84 Statement s;
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 144
131 bool MetaTable::PrepareGetStatement(Statement* statement, const char* key) { 145 bool MetaTable::PrepareGetStatement(Statement* statement, const char* key) {
132 DCHECK(db_ && statement); 146 DCHECK(db_ && statement);
133 statement->Assign(db_->GetCachedStatement(SQL_FROM_HERE, 147 statement->Assign(db_->GetCachedStatement(SQL_FROM_HERE,
134 "SELECT value FROM meta WHERE key=?")); 148 "SELECT value FROM meta WHERE key=?"));
135 statement->BindCString(0, key); 149 statement->BindCString(0, key);
136 return statement->Step(); 150 return statement->Step();
137 } 151 }
138 152
139 } // namespace sql 153 } // namespace sql
OLDNEW
« chrome/browser/net/sqlite_persistent_cookie_store_unittest.cc ('K') | « sql/meta_table.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698