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