| 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 "chrome/browser/history/shortcuts_database.h" | 5 #include "chrome/browser/history/shortcuts_database.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/guid.h" | 9 #include "base/guid.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "chrome/common/autocomplete_match_type.h" | 13 #include "chrome/common/autocomplete_match_type.h" |
| 14 #include "content/public/common/page_transition_types.h" | 14 #include "content/public/common/page_transition_types.h" |
| 15 #include "sql/meta_table.h" |
| 15 #include "sql/statement.h" | 16 #include "sql/statement.h" |
| 16 #include "sql/transaction.h" | 17 #include "sql/transaction.h" |
| 17 | 18 |
| 18 | 19 |
| 19 // Helpers -------------------------------------------------------------------- | 20 // Helpers -------------------------------------------------------------------- |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 24 // Current version number. We write databases at the "current" version number, |
| 25 // but any previous version that can read the "compatible" one can make do with |
| 26 // our database without *too* many bad effects. |
| 27 const int kCurrentVersionNumber = 1; |
| 28 const int kCompatibleVersionNumber = 1; |
| 29 |
| 23 void BindShortcutToStatement( | 30 void BindShortcutToStatement( |
| 24 const history::ShortcutsDatabase::Shortcut& shortcut, | 31 const history::ShortcutsDatabase::Shortcut& shortcut, |
| 25 sql::Statement* s) { | 32 sql::Statement* s) { |
| 26 DCHECK(base::IsValidGUID(shortcut.id)); | 33 DCHECK(base::IsValidGUID(shortcut.id)); |
| 27 s->BindString(0, shortcut.id); | 34 s->BindString(0, shortcut.id); |
| 28 s->BindString16(1, shortcut.text); | 35 s->BindString16(1, shortcut.text); |
| 29 s->BindString16(2, shortcut.match_core.fill_into_edit); | 36 s->BindString16(2, shortcut.match_core.fill_into_edit); |
| 30 s->BindString(3, shortcut.match_core.destination_url.spec()); | 37 s->BindString(3, shortcut.match_core.destination_url.spec()); |
| 31 s->BindString16(4, shortcut.match_core.contents); | 38 s->BindString16(4, shortcut.match_core.contents); |
| 32 s->BindString(5, shortcut.match_core.contents_class); | 39 s->BindString(5, shortcut.match_core.contents_class); |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 "keyword VARCHAR, last_access_time INTEGER, " | 228 "keyword VARCHAR, last_access_time INTEGER, " |
| 222 "number_of_hits INTEGER)"); | 229 "number_of_hits INTEGER)"); |
| 223 } | 230 } |
| 224 | 231 |
| 225 // The first version of the shortcuts table lacked the fill_into_edit, | 232 // The first version of the shortcuts table lacked the fill_into_edit, |
| 226 // transition, type, and keyword columns. | 233 // transition, type, and keyword columns. |
| 227 if (!db_.DoesColumnExist("omni_box_shortcuts", "fill_into_edit")) { | 234 if (!db_.DoesColumnExist("omni_box_shortcuts", "fill_into_edit")) { |
| 228 // Perform the upgrade in a transaction to ensure it doesn't happen | 235 // Perform the upgrade in a transaction to ensure it doesn't happen |
| 229 // incompletely. | 236 // incompletely. |
| 230 sql::Transaction transaction(&db_); | 237 sql::Transaction transaction(&db_); |
| 231 transaction.Begin(); | 238 if (!(transaction.Begin() && |
| 232 return | |
| 233 db_.Execute("ALTER TABLE omni_box_shortcuts " | 239 db_.Execute("ALTER TABLE omni_box_shortcuts " |
| 234 "ADD COLUMN fill_into_edit VARCHAR") && | 240 "ADD COLUMN fill_into_edit VARCHAR") && |
| 235 db_.Execute("UPDATE omni_box_shortcuts SET fill_into_edit = url") && | 241 db_.Execute("UPDATE omni_box_shortcuts SET fill_into_edit = url") && |
| 236 db_.Execute("ALTER TABLE omni_box_shortcuts " | 242 db_.Execute("ALTER TABLE omni_box_shortcuts " |
| 237 "ADD COLUMN transition INTEGER") && | 243 "ADD COLUMN transition INTEGER") && |
| 238 db_.Execute(base::StringPrintf( | 244 db_.Execute(base::StringPrintf( |
| 239 "UPDATE omni_box_shortcuts SET transition = %d", | 245 "UPDATE omni_box_shortcuts SET transition = %d", |
| 240 static_cast<int>(content::PAGE_TRANSITION_TYPED)).c_str()) && | 246 static_cast<int>(content::PAGE_TRANSITION_TYPED)).c_str()) && |
| 241 db_.Execute("ALTER TABLE omni_box_shortcuts ADD COLUMN type INTEGER") && | 247 db_.Execute("ALTER TABLE omni_box_shortcuts ADD COLUMN type INTEGER") && |
| 242 db_.Execute(base::StringPrintf( | 248 db_.Execute(base::StringPrintf( |
| 243 "UPDATE omni_box_shortcuts SET type = %d", | 249 "UPDATE omni_box_shortcuts SET type = %d", |
| 244 static_cast<int>(AutocompleteMatchType::HISTORY_TITLE)).c_str()) && | 250 static_cast<int>(AutocompleteMatchType::HISTORY_TITLE)).c_str()) && |
| 245 db_.Execute("ALTER TABLE omni_box_shortcuts " | 251 db_.Execute("ALTER TABLE omni_box_shortcuts " |
| 246 "ADD COLUMN keyword VARCHAR") && | 252 "ADD COLUMN keyword VARCHAR") && |
| 247 transaction.Commit(); | 253 transaction.Commit())) { |
| 254 return false; |
| 255 } |
| 248 } | 256 } |
| 249 | 257 |
| 258 if (!sql::MetaTable::DoesTableExist(&db_)) { |
| 259 meta_table_.Init(&db_, kCurrentVersionNumber, kCompatibleVersionNumber); |
| 260 sql::Transaction transaction(&db_); |
| 261 if (!(transaction.Begin() && |
| 262 // Migrate old SEARCH_OTHER_ENGINE values to the new type value. |
| 263 db_.Execute(base::StringPrintf("UPDATE omni_box_shortcuts " |
| 264 "SET type = 13 WHERE type = 9").c_str()) && |
| 265 // Migrate old EXTENSION_APP values to the new type value. |
| 266 db_.Execute(base::StringPrintf("UPDATE omni_box_shortcuts " |
| 267 "SET type = 14 WHERE type = 10").c_str()) && |
| 268 // Migrate old CONTACT values to the new type value. |
| 269 db_.Execute(base::StringPrintf("UPDATE omni_box_shortcuts " |
| 270 "SET type = 15 WHERE type = 11").c_str()) && |
| 271 // Migrate old BOOKMARK_TITLE values to the new type value. |
| 272 db_.Execute(base::StringPrintf("UPDATE omni_box_shortcuts " |
| 273 "SET type = 16 WHERE type = 12").c_str()) && |
| 274 transaction.Commit())) { |
| 275 return false; |
| 276 } |
| 277 } |
| 250 return true; | 278 return true; |
| 251 } | 279 } |
| 252 | 280 |
| 253 } // namespace history | 281 } // namespace history |
| OLD | NEW |