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" | |
15 #include "sql/statement.h" | 14 #include "sql/statement.h" |
16 #include "sql/transaction.h" | 15 #include "sql/transaction.h" |
17 | 16 |
18 namespace { | 17 namespace { |
19 | 18 |
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 | |
26 void BindShortcutToStatement( | 19 void BindShortcutToStatement( |
27 const history::ShortcutsBackend::Shortcut& shortcut, | 20 const history::ShortcutsBackend::Shortcut& shortcut, |
28 sql::Statement* s) { | 21 sql::Statement* s) { |
29 DCHECK(base::IsValidGUID(shortcut.id)); | 22 DCHECK(base::IsValidGUID(shortcut.id)); |
30 s->BindString(0, shortcut.id); | 23 s->BindString(0, shortcut.id); |
31 s->BindString16(1, shortcut.text); | 24 s->BindString16(1, shortcut.text); |
32 s->BindString16(2, shortcut.match_core.fill_into_edit); | 25 s->BindString16(2, shortcut.match_core.fill_into_edit); |
33 s->BindString(3, shortcut.match_core.destination_url.spec()); | 26 s->BindString(3, shortcut.match_core.destination_url.spec()); |
34 s->BindString16(4, shortcut.match_core.contents); | 27 s->BindString16(4, shortcut.match_core.contents); |
35 s->BindString(5, AutocompleteMatch::ClassificationsToString( | 28 s->BindString(5, AutocompleteMatch::ClassificationsToString( |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 "keyword VARCHAR, last_access_time INTEGER, " | 170 "keyword VARCHAR, last_access_time INTEGER, " |
178 "number_of_hits INTEGER)"); | 171 "number_of_hits INTEGER)"); |
179 } | 172 } |
180 | 173 |
181 // The first version of the shortcuts table lacked the fill_into_edit, | 174 // The first version of the shortcuts table lacked the fill_into_edit, |
182 // transition, type, and keyword columns. | 175 // transition, type, and keyword columns. |
183 if (!db_.DoesColumnExist("omni_box_shortcuts", "fill_into_edit")) { | 176 if (!db_.DoesColumnExist("omni_box_shortcuts", "fill_into_edit")) { |
184 // Perform the upgrade in a transaction to ensure it doesn't happen | 177 // Perform the upgrade in a transaction to ensure it doesn't happen |
185 // incompletely. | 178 // incompletely. |
186 sql::Transaction transaction(&db_); | 179 sql::Transaction transaction(&db_); |
187 if (!(transaction.Begin() && | 180 transaction.Begin(); |
| 181 return |
188 db_.Execute("ALTER TABLE omni_box_shortcuts " | 182 db_.Execute("ALTER TABLE omni_box_shortcuts " |
189 "ADD COLUMN fill_into_edit VARCHAR") && | 183 "ADD COLUMN fill_into_edit VARCHAR") && |
190 db_.Execute("UPDATE omni_box_shortcuts SET fill_into_edit = url") && | 184 db_.Execute("UPDATE omni_box_shortcuts SET fill_into_edit = url") && |
191 db_.Execute("ALTER TABLE omni_box_shortcuts " | 185 db_.Execute("ALTER TABLE omni_box_shortcuts " |
192 "ADD COLUMN transition INTEGER") && | 186 "ADD COLUMN transition INTEGER") && |
193 db_.Execute(base::StringPrintf( | 187 db_.Execute(base::StringPrintf( |
194 "UPDATE omni_box_shortcuts SET transition = %d", | 188 "UPDATE omni_box_shortcuts SET transition = %d", |
195 static_cast<int>(content::PAGE_TRANSITION_TYPED)).c_str()) && | 189 static_cast<int>(content::PAGE_TRANSITION_TYPED)).c_str()) && |
196 db_.Execute("ALTER TABLE omni_box_shortcuts ADD COLUMN type INTEGER") && | 190 db_.Execute("ALTER TABLE omni_box_shortcuts ADD COLUMN type INTEGER") && |
197 db_.Execute(base::StringPrintf( | 191 db_.Execute(base::StringPrintf( |
198 "UPDATE omni_box_shortcuts SET type = %d", | 192 "UPDATE omni_box_shortcuts SET type = %d", |
199 static_cast<int>(AutocompleteMatchType::HISTORY_TITLE)).c_str()) && | 193 static_cast<int>(AutocompleteMatchType::HISTORY_TITLE)).c_str()) && |
200 db_.Execute("ALTER TABLE omni_box_shortcuts " | 194 db_.Execute("ALTER TABLE omni_box_shortcuts " |
201 "ADD COLUMN keyword VARCHAR") && | 195 "ADD COLUMN keyword VARCHAR") && |
202 transaction.Commit())) { | 196 transaction.Commit(); |
203 return false; | |
204 } | |
205 } | 197 } |
206 | 198 |
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 } | |
227 return true; | 199 return true; |
228 } | 200 } |
229 | 201 |
230 } // namespace history | 202 } // namespace history |
OLD | NEW |