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/webdata/keyword_table.h" | 5 #include "chrome/browser/webdata/keyword_table.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
57 columns.push_back("logo_id"); | 57 columns.push_back("logo_id"); |
58 } | 58 } |
59 columns.push_back("created_by_policy"); | 59 columns.push_back("created_by_policy"); |
60 columns.push_back("instant_url"); | 60 columns.push_back("instant_url"); |
61 columns.push_back("last_modified"); | 61 columns.push_back("last_modified"); |
62 columns.push_back("sync_guid"); | 62 columns.push_back("sync_guid"); |
63 if (version >= 47) { | 63 if (version >= 47) { |
64 // Column added in version 47. | 64 // Column added in version 47. |
65 columns.push_back("alternate_urls"); | 65 columns.push_back("alternate_urls"); |
66 } | 66 } |
67 if (version >= 49) { | |
68 // Column added in version 49. | |
69 columns.push_back("search_terms_replacement_key"); | |
70 } | |
67 | 71 |
68 return JoinString(columns, std::string(concatenated ? " || " : ", ")); | 72 return JoinString(columns, std::string(concatenated ? " || " : ", ")); |
69 } | 73 } |
70 | 74 |
71 | 75 |
72 // Inserts the data from |data| into |s|. |s| is assumed to have slots for all | 76 // Inserts the data from |data| into |s|. |s| is assumed to have slots for all |
73 // the columns in the keyword table. |id_column| is the slot number to bind | 77 // the columns in the keyword table. |id_column| is the slot number to bind |
74 // |data|'s |id| to; |starting_column| is the slot number of the first of a | 78 // |data|'s |id| to; |starting_column| is the slot number of the first of a |
75 // contiguous set of slots to bind all the other fields to. | 79 // contiguous set of slots to bind all the other fields to. |
76 void BindURLToStatement(const TemplateURLData& data, | 80 void BindURLToStatement(const TemplateURLData& data, |
(...skipping 25 matching lines...) Expand all Loading... | |
102 s->BindInt(starting_column + 7, data.usage_count); | 106 s->BindInt(starting_column + 7, data.usage_count); |
103 s->BindString(starting_column + 8, JoinString(data.input_encodings, ';')); | 107 s->BindString(starting_column + 8, JoinString(data.input_encodings, ';')); |
104 s->BindBool(starting_column + 9, data.show_in_default_list); | 108 s->BindBool(starting_column + 9, data.show_in_default_list); |
105 s->BindString(starting_column + 10, data.suggestions_url); | 109 s->BindString(starting_column + 10, data.suggestions_url); |
106 s->BindInt(starting_column + 11, data.prepopulate_id); | 110 s->BindInt(starting_column + 11, data.prepopulate_id); |
107 s->BindBool(starting_column + 12, data.created_by_policy); | 111 s->BindBool(starting_column + 12, data.created_by_policy); |
108 s->BindString(starting_column + 13, data.instant_url); | 112 s->BindString(starting_column + 13, data.instant_url); |
109 s->BindInt64(starting_column + 14, data.last_modified.ToTimeT()); | 113 s->BindInt64(starting_column + 14, data.last_modified.ToTimeT()); |
110 s->BindString(starting_column + 15, data.sync_guid); | 114 s->BindString(starting_column + 15, data.sync_guid); |
111 s->BindString(starting_column + 16, alternate_urls); | 115 s->BindString(starting_column + 16, alternate_urls); |
116 s->BindString(starting_column + 17, data.search_terms_replacement_key); | |
112 } | 117 } |
113 | 118 |
114 } // anonymous namespace | 119 } // anonymous namespace |
115 | 120 |
116 KeywordTable::KeywordTable(sql::Connection* db, sql::MetaTable* meta_table) | 121 KeywordTable::KeywordTable(sql::Connection* db, sql::MetaTable* meta_table) |
117 : WebDatabaseTable(db, meta_table) { | 122 : WebDatabaseTable(db, meta_table) { |
118 } | 123 } |
119 | 124 |
120 KeywordTable::~KeywordTable() {} | 125 KeywordTable::~KeywordTable() {} |
121 | 126 |
(...skipping 10 matching lines...) Expand all Loading... | |
132 "date_created INTEGER DEFAULT 0," | 137 "date_created INTEGER DEFAULT 0," |
133 "usage_count INTEGER DEFAULT 0," | 138 "usage_count INTEGER DEFAULT 0," |
134 "input_encodings VARCHAR," | 139 "input_encodings VARCHAR," |
135 "show_in_default_list INTEGER," | 140 "show_in_default_list INTEGER," |
136 "suggest_url VARCHAR," | 141 "suggest_url VARCHAR," |
137 "prepopulate_id INTEGER DEFAULT 0," | 142 "prepopulate_id INTEGER DEFAULT 0," |
138 "created_by_policy INTEGER DEFAULT 0," | 143 "created_by_policy INTEGER DEFAULT 0," |
139 "instant_url VARCHAR," | 144 "instant_url VARCHAR," |
140 "last_modified INTEGER DEFAULT 0," | 145 "last_modified INTEGER DEFAULT 0," |
141 "sync_guid VARCHAR," | 146 "sync_guid VARCHAR," |
142 "alternate_urls VARCHAR)"); | 147 "alternate_urls VARCHAR," |
148 "search_terms_replacement_key VARCHAR)"); | |
143 } | 149 } |
144 | 150 |
145 bool KeywordTable::IsSyncable() { | 151 bool KeywordTable::IsSyncable() { |
146 return true; | 152 return true; |
147 } | 153 } |
148 | 154 |
149 bool KeywordTable::AddKeyword(const TemplateURLData& data) { | 155 bool KeywordTable::AddKeyword(const TemplateURLData& data) { |
150 DCHECK(data.id); | 156 DCHECK(data.id); |
151 std::string query("INSERT INTO keywords (" + GetKeywordColumns() + | 157 std::string query("INSERT INTO keywords (" + GetKeywordColumns() + |
152 ") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); | 158 ") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); |
153 sql::Statement s(db_->GetUniqueStatement(query.c_str())); | 159 sql::Statement s(db_->GetUniqueStatement(query.c_str())); |
154 BindURLToStatement(data, &s, 0, 1); | 160 BindURLToStatement(data, &s, 0, 1); |
155 | 161 |
156 return s.Run(); | 162 return s.Run(); |
157 } | 163 } |
158 | 164 |
159 bool KeywordTable::RemoveKeyword(TemplateURLID id) { | 165 bool KeywordTable::RemoveKeyword(TemplateURLID id) { |
160 DCHECK(id); | 166 DCHECK(id); |
161 sql::Statement s( | 167 sql::Statement s( |
162 db_->GetUniqueStatement("DELETE FROM keywords WHERE id = ?")); | 168 db_->GetUniqueStatement("DELETE FROM keywords WHERE id = ?")); |
(...skipping 22 matching lines...) Expand all Loading... | |
185 return succeeded; | 191 return succeeded; |
186 } | 192 } |
187 | 193 |
188 bool KeywordTable::UpdateKeyword(const TemplateURLData& data) { | 194 bool KeywordTable::UpdateKeyword(const TemplateURLData& data) { |
189 DCHECK(data.id); | 195 DCHECK(data.id); |
190 sql::Statement s(db_->GetUniqueStatement("UPDATE keywords SET short_name=?, " | 196 sql::Statement s(db_->GetUniqueStatement("UPDATE keywords SET short_name=?, " |
191 "keyword=?, favicon_url=?, url=?, safe_for_autoreplace=?, " | 197 "keyword=?, favicon_url=?, url=?, safe_for_autoreplace=?, " |
192 "originating_url=?, date_created=?, usage_count=?, input_encodings=?, " | 198 "originating_url=?, date_created=?, usage_count=?, input_encodings=?, " |
193 "show_in_default_list=?, suggest_url=?, prepopulate_id=?, " | 199 "show_in_default_list=?, suggest_url=?, prepopulate_id=?, " |
194 "created_by_policy=?, instant_url=?, last_modified=?, sync_guid=?, " | 200 "created_by_policy=?, instant_url=?, last_modified=?, sync_guid=?, " |
195 "alternate_urls=? WHERE id=?")); | 201 "alternate_urls=?, search_terms_replacement_key=? WHERE id=?")); |
196 BindURLToStatement(data, &s, 17, 0); // "17" binds id() as the last item. | 202 BindURLToStatement(data, &s, 18, 0); // "18" binds id() as the last item. |
197 | 203 |
198 return s.Run(); | 204 return s.Run(); |
199 } | 205 } |
200 | 206 |
201 bool KeywordTable::SetDefaultSearchProviderID(int64 id) { | 207 bool KeywordTable::SetDefaultSearchProviderID(int64 id) { |
202 return meta_table_->SetValue(kDefaultSearchProviderKey, id); | 208 return meta_table_->SetValue(kDefaultSearchProviderKey, id); |
203 } | 209 } |
204 | 210 |
205 int64 KeywordTable::GetDefaultSearchProviderID() { | 211 int64 KeywordTable::GetDefaultSearchProviderID() { |
206 int64 value = kInvalidTemplateURLID; | 212 int64 value = kInvalidTemplateURLID; |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
358 if (!meta_table_->DeleteKey("Default Search Provider ID Backup") || | 364 if (!meta_table_->DeleteKey("Default Search Provider ID Backup") || |
359 !meta_table_->DeleteKey("Default Search Provider ID Backup Signature")) | 365 !meta_table_->DeleteKey("Default Search Provider ID Backup Signature")) |
360 return false; | 366 return false; |
361 | 367 |
362 if (!db_->Execute("DROP TABLE keywords_backup")) | 368 if (!db_->Execute("DROP TABLE keywords_backup")) |
363 return false; | 369 return false; |
364 | 370 |
365 return transaction.Commit(); | 371 return transaction.Commit(); |
366 } | 372 } |
367 | 373 |
374 bool KeywordTable::MigrateToVersion49AddSearchTermsReplacementKeyColumn() { | |
375 sql::Transaction transaction(db_); | |
376 | |
377 // Fill the |search_terms_replacement_key| column with empty strings, | |
Peter Kasting
2012/12/18 01:54:43
Nit: Maybe just: "...strings; see comments in Migr
beaudoin
2012/12/20 04:23:29
Done.
| |
378 // otherwise it breaks code relying on GetTableContents that concatenates the | |
379 // strings from all the columns. | |
380 if (!transaction.Begin() || | |
381 !db_->Execute("ALTER TABLE keywords ADD COLUMN " | |
382 "search_terms_replacement_key VARCHAR DEFAULT ''")) | |
383 return false; | |
384 | |
385 return transaction.Commit(); | |
386 } | |
387 | |
368 // static | 388 // static |
369 bool KeywordTable::GetKeywordDataFromStatement(const sql::Statement& s, | 389 bool KeywordTable::GetKeywordDataFromStatement(const sql::Statement& s, |
370 TemplateURLData* data) { | 390 TemplateURLData* data) { |
371 DCHECK(data); | 391 DCHECK(data); |
372 | 392 |
373 data->short_name = s.ColumnString16(1); | 393 data->short_name = s.ColumnString16(1); |
374 data->SetKeyword(s.ColumnString16(2)); | 394 data->SetKeyword(s.ColumnString16(2)); |
375 // Due to past bugs, we might have persisted entries with empty URLs. Avoid | 395 // Due to past bugs, we might have persisted entries with empty URLs. Avoid |
376 // reading these out. (GetKeywords() will delete these entries on return.) | 396 // reading these out. (GetKeywords() will delete these entries on return.) |
377 // NOTE: This code should only be needed as long as we might be reading such | 397 // NOTE: This code should only be needed as long as we might be reading such |
(...skipping 21 matching lines...) Expand all Loading... | |
399 scoped_ptr<Value> value(json_reader.ReadToValue(s.ColumnString(17))); | 419 scoped_ptr<Value> value(json_reader.ReadToValue(s.ColumnString(17))); |
400 ListValue* alternate_urls_value; | 420 ListValue* alternate_urls_value; |
401 if (value.get() && value->GetAsList(&alternate_urls_value)) { | 421 if (value.get() && value->GetAsList(&alternate_urls_value)) { |
402 std::string alternate_url; | 422 std::string alternate_url; |
403 for (size_t i = 0; i < alternate_urls_value->GetSize(); ++i) { | 423 for (size_t i = 0; i < alternate_urls_value->GetSize(); ++i) { |
404 if (alternate_urls_value->GetString(i, &alternate_url)) | 424 if (alternate_urls_value->GetString(i, &alternate_url)) |
405 data->alternate_urls.push_back(alternate_url); | 425 data->alternate_urls.push_back(alternate_url); |
406 } | 426 } |
407 } | 427 } |
408 | 428 |
429 data->search_terms_replacement_key = s.ColumnString(18); | |
430 | |
409 return true; | 431 return true; |
410 } | 432 } |
411 | 433 |
412 bool KeywordTable::GetTableContents(const char* table_name, | 434 bool KeywordTable::GetTableContents(const char* table_name, |
413 int table_version, | 435 int table_version, |
414 std::string* contents) { | 436 std::string* contents) { |
415 DCHECK(contents); | 437 DCHECK(contents); |
416 | 438 |
417 if (!db_->DoesTableExist(table_name)) | 439 if (!db_->DoesTableExist(table_name)) |
418 return false; | 440 return false; |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
527 } | 549 } |
528 } | 550 } |
529 | 551 |
530 // Replace the old table with the new one. | 552 // Replace the old table with the new one. |
531 sql = "DROP TABLE " + name; | 553 sql = "DROP TABLE " + name; |
532 if (!db_->Execute(sql.c_str())) | 554 if (!db_->Execute(sql.c_str())) |
533 return false; | 555 return false; |
534 sql = "ALTER TABLE keywords_temp RENAME TO " + name; | 556 sql = "ALTER TABLE keywords_temp RENAME TO " + name; |
535 return db_->Execute(sql.c_str()); | 557 return db_->Execute(sql.c_str()); |
536 } | 558 } |
OLD | NEW |