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 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/guid.h" | 10 #include "base/guid.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "sql/meta_table.h" |
14 #include "sql/statement.h" | 15 #include "sql/statement.h" |
15 #include "sql/transaction.h" | 16 #include "sql/transaction.h" |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
| 20 // Current version number. We write databases at the "current" version number, |
| 21 // but any previous version that can read the "compatible" one can make do with |
| 22 // our database without *too* many bad effects. |
| 23 const int kCurrentVersionNumber = 1; |
| 24 const int kCompatibleVersionNumber = 1; |
| 25 |
19 void BindShortcutToStatement( | 26 void BindShortcutToStatement( |
20 const history::ShortcutsBackend::Shortcut& shortcut, | 27 const history::ShortcutsBackend::Shortcut& shortcut, |
21 sql::Statement* s) { | 28 sql::Statement* s) { |
22 DCHECK(base::IsValidGUID(shortcut.id)); | 29 DCHECK(base::IsValidGUID(shortcut.id)); |
23 s->BindString(0, shortcut.id); | 30 s->BindString(0, shortcut.id); |
24 s->BindString16(1, shortcut.text); | 31 s->BindString16(1, shortcut.text); |
25 s->BindString16(2, shortcut.match_core.fill_into_edit); | 32 s->BindString16(2, shortcut.match_core.fill_into_edit); |
26 s->BindString(3, shortcut.match_core.destination_url.spec()); | 33 s->BindString(3, shortcut.match_core.destination_url.spec()); |
27 s->BindString16(4, shortcut.match_core.contents); | 34 s->BindString16(4, shortcut.match_core.contents); |
28 s->BindString(5, AutocompleteMatch::ClassificationsToString( | 35 s->BindString(5, AutocompleteMatch::ClassificationsToString( |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 "keyword VARCHAR, last_access_time INTEGER, " | 177 "keyword VARCHAR, last_access_time INTEGER, " |
171 "number_of_hits INTEGER)"); | 178 "number_of_hits INTEGER)"); |
172 } | 179 } |
173 | 180 |
174 // The first version of the shortcuts table lacked the fill_into_edit, | 181 // The first version of the shortcuts table lacked the fill_into_edit, |
175 // transition, type, and keyword columns. | 182 // transition, type, and keyword columns. |
176 if (!db_.DoesColumnExist("omni_box_shortcuts", "fill_into_edit")) { | 183 if (!db_.DoesColumnExist("omni_box_shortcuts", "fill_into_edit")) { |
177 // Perform the upgrade in a transaction to ensure it doesn't happen | 184 // Perform the upgrade in a transaction to ensure it doesn't happen |
178 // incompletely. | 185 // incompletely. |
179 sql::Transaction transaction(&db_); | 186 sql::Transaction transaction(&db_); |
180 transaction.Begin(); | 187 if (!(transaction.Begin() && |
181 return | |
182 db_.Execute("ALTER TABLE omni_box_shortcuts " | 188 db_.Execute("ALTER TABLE omni_box_shortcuts " |
183 "ADD COLUMN fill_into_edit VARCHAR") && | 189 "ADD COLUMN fill_into_edit VARCHAR") && |
184 db_.Execute("UPDATE omni_box_shortcuts SET fill_into_edit = url") && | 190 db_.Execute("UPDATE omni_box_shortcuts SET fill_into_edit = url") && |
185 db_.Execute("ALTER TABLE omni_box_shortcuts " | 191 db_.Execute("ALTER TABLE omni_box_shortcuts " |
186 "ADD COLUMN transition INTEGER") && | 192 "ADD COLUMN transition INTEGER") && |
187 db_.Execute(base::StringPrintf( | 193 db_.Execute(base::StringPrintf( |
188 "UPDATE omni_box_shortcuts SET transition = %d", | 194 "UPDATE omni_box_shortcuts SET transition = %d", |
189 static_cast<int>(content::PAGE_TRANSITION_TYPED)).c_str()) && | 195 static_cast<int>(content::PAGE_TRANSITION_TYPED)).c_str()) && |
190 db_.Execute("ALTER TABLE omni_box_shortcuts ADD COLUMN type INTEGER") && | 196 db_.Execute("ALTER TABLE omni_box_shortcuts ADD COLUMN type INTEGER") && |
191 db_.Execute(base::StringPrintf( | 197 db_.Execute(base::StringPrintf( |
192 "UPDATE omni_box_shortcuts SET type = %d", | 198 "UPDATE omni_box_shortcuts SET type = %d", |
193 static_cast<int>(AutocompleteMatchType::HISTORY_TITLE)).c_str()) && | 199 static_cast<int>(AutocompleteMatchType::HISTORY_TITLE)).c_str()) && |
194 db_.Execute("ALTER TABLE omni_box_shortcuts " | 200 db_.Execute("ALTER TABLE omni_box_shortcuts " |
195 "ADD COLUMN keyword VARCHAR") && | 201 "ADD COLUMN keyword VARCHAR") && |
196 transaction.Commit(); | 202 transaction.Commit())) { |
| 203 return false; |
| 204 } |
197 } | 205 } |
198 | 206 |
| 207 if (!sql::MetaTable::DoesTableExist(&db_)) { |
| 208 meta_table_.Init(&db_, kCurrentVersionNumber, kCompatibleVersionNumber); |
| 209 sql::Transaction transaction(&db_); |
| 210 if (!(transaction.Begin() && |
| 211 // Migrate old SEARCH_OTHER_ENGINE values to the new type value. |
| 212 db_.Execute(base::StringPrintf("UPDATE omni_box_shortcuts " |
| 213 "SET type = 13 WHERE type = 9").c_str()) && |
| 214 // Migrate old EXTENSION_APP values to the new type value. |
| 215 db_.Execute(base::StringPrintf("UPDATE omni_box_shortcuts " |
| 216 "SET type = 14 WHERE type = 10").c_str()) && |
| 217 // Migrate old CONTACT values to the new type value. |
| 218 db_.Execute(base::StringPrintf("UPDATE omni_box_shortcuts " |
| 219 "SET type = 15 WHERE type = 11").c_str()) && |
| 220 // Migrate old BOOKMARK_TITLE values to the new type value. |
| 221 db_.Execute(base::StringPrintf("UPDATE omni_box_shortcuts " |
| 222 "SET type = 16 WHERE type = 12").c_str()) && |
| 223 transaction.Commit())) { |
| 224 return false; |
| 225 } |
| 226 } |
199 return true; | 227 return true; |
200 } | 228 } |
201 | 229 |
202 } // namespace history | 230 } // namespace history |
OLD | NEW |