OLD | NEW |
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 "webkit/dom_storage/dom_storage_database.h" | 5 #include "webkit/dom_storage/dom_storage_database.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "sql/diagnostic_error_delegate.h" | 9 #include "sql/diagnostic_error_delegate.h" |
10 #include "sql/statement.h" | 10 #include "sql/statement.h" |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 | 65 |
66 void DomStorageDatabase::ReadAllValues(ValuesMap* result) { | 66 void DomStorageDatabase::ReadAllValues(ValuesMap* result) { |
67 if (!LazyOpen(false)) | 67 if (!LazyOpen(false)) |
68 return; | 68 return; |
69 | 69 |
70 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, | 70 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, |
71 "SELECT * from ItemTable")); | 71 "SELECT * from ItemTable")); |
72 DCHECK(statement.is_valid()); | 72 DCHECK(statement.is_valid()); |
73 | 73 |
74 while (statement.Step()) { | 74 while (statement.Step()) { |
75 string16 key = statement.ColumnString16(0); | 75 base::string16 key = statement.ColumnString16(0); |
76 string16 value; | 76 base::string16 value; |
77 statement.ColumnBlobAsString16(1, &value); | 77 statement.ColumnBlobAsString16(1, &value); |
78 (*result)[key] = NullableString16(value, false); | 78 (*result)[key] = NullableString16(value, false); |
79 } | 79 } |
80 known_to_be_empty_ = result->empty(); | 80 known_to_be_empty_ = result->empty(); |
81 } | 81 } |
82 | 82 |
83 bool DomStorageDatabase::CommitChanges(bool clear_all_first, | 83 bool DomStorageDatabase::CommitChanges(bool clear_all_first, |
84 const ValuesMap& changes) { | 84 const ValuesMap& changes) { |
85 if (!LazyOpen(!changes.empty())) { | 85 if (!LazyOpen(!changes.empty())) { |
86 // If we're being asked to commit changes that will result in an | 86 // If we're being asked to commit changes that will result in an |
(...skipping 11 matching lines...) Expand all Loading... |
98 if (!db_->Execute("DELETE FROM ItemTable")) | 98 if (!db_->Execute("DELETE FROM ItemTable")) |
99 return false; | 99 return false; |
100 known_to_be_empty_ = true; | 100 known_to_be_empty_ = true; |
101 } | 101 } |
102 | 102 |
103 bool did_delete = false; | 103 bool did_delete = false; |
104 bool did_insert = false; | 104 bool did_insert = false; |
105 ValuesMap::const_iterator it = changes.begin(); | 105 ValuesMap::const_iterator it = changes.begin(); |
106 for(; it != changes.end(); ++it) { | 106 for(; it != changes.end(); ++it) { |
107 sql::Statement statement; | 107 sql::Statement statement; |
108 string16 key = it->first; | 108 base::string16 key = it->first; |
109 NullableString16 value = it->second; | 109 NullableString16 value = it->second; |
110 if (value.is_null()) { | 110 if (value.is_null()) { |
111 statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE, | 111 statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE, |
112 "DELETE FROM ItemTable WHERE key=?")); | 112 "DELETE FROM ItemTable WHERE key=?")); |
113 statement.BindString16(0, key); | 113 statement.BindString16(0, key); |
114 did_delete = true; | 114 did_delete = true; |
115 } else { | 115 } else { |
116 statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE, | 116 statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE, |
117 "INSERT INTO ItemTable VALUES (?,?)")); | 117 "INSERT INTO ItemTable VALUES (?,?)")); |
118 statement.BindString16(0, key); | 118 statement.BindString16(0, key); |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 | 277 |
278 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, | 278 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, |
279 "SELECT * FROM ItemTable")); | 279 "SELECT * FROM ItemTable")); |
280 DCHECK(statement.is_valid()); | 280 DCHECK(statement.is_valid()); |
281 | 281 |
282 // Need to migrate from TEXT value column to BLOB. | 282 // Need to migrate from TEXT value column to BLOB. |
283 // Store the current database content so we can re-insert | 283 // Store the current database content so we can re-insert |
284 // the data into the new V2 table. | 284 // the data into the new V2 table. |
285 ValuesMap values; | 285 ValuesMap values; |
286 while (statement.Step()) { | 286 while (statement.Step()) { |
287 string16 key = statement.ColumnString16(0); | 287 base::string16 key = statement.ColumnString16(0); |
288 NullableString16 value(statement.ColumnString16(1), false); | 288 NullableString16 value(statement.ColumnString16(1), false); |
289 values[key] = value; | 289 values[key] = value; |
290 } | 290 } |
291 | 291 |
292 sql::Transaction migration(db_.get()); | 292 sql::Transaction migration(db_.get()); |
293 return migration.Begin() && | 293 return migration.Begin() && |
294 db_->Execute("DROP TABLE ItemTable") && | 294 db_->Execute("DROP TABLE ItemTable") && |
295 CreateTableV2() && | 295 CreateTableV2() && |
296 CommitChanges(false, values) && | 296 CommitChanges(false, values) && |
297 migration.Commit(); | 297 migration.Commit(); |
298 } | 298 } |
299 | 299 |
300 void DomStorageDatabase::Close() { | 300 void DomStorageDatabase::Close() { |
301 db_.reset(NULL); | 301 db_.reset(NULL); |
302 } | 302 } |
303 | 303 |
304 } // namespace dom_storage | 304 } // namespace dom_storage |
OLD | NEW |