Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(605)

Side by Side Diff: components/autofill/core/browser/webdata/autofill_table.cc

Issue 207643002: Create new Autofill tables as part of DB migration. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "components/autofill/core/browser/webdata/autofill_table.h" 5 #include "components/autofill/core/browser/webdata/autofill_table.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 #include <limits> 9 #include <limits>
10 #include <map> 10 #include <map>
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 } 373 }
374 374
375 AutofillTable* AutofillTable::FromWebDatabase(WebDatabase* db) { 375 AutofillTable* AutofillTable::FromWebDatabase(WebDatabase* db) {
376 return static_cast<AutofillTable*>(db->GetTable(GetKey())); 376 return static_cast<AutofillTable*>(db->GetTable(GetKey()));
377 } 377 }
378 378
379 WebDatabaseTable::TypeKey AutofillTable::GetTypeKey() const { 379 WebDatabaseTable::TypeKey AutofillTable::GetTypeKey() const {
380 return GetKey(); 380 return GetKey();
381 } 381 }
382 382
383 bool AutofillTable::Init(sql::Connection* db, sql::MetaTable* meta_table) { 383 bool AutofillTable::CreateTablesIfNecessary() {
384 WebDatabaseTable::Init(db, meta_table);
385 return (InitMainTable() && InitCreditCardsTable() && InitProfilesTable() && 384 return (InitMainTable() && InitCreditCardsTable() && InitProfilesTable() &&
386 InitProfileNamesTable() && InitProfileEmailsTable() && 385 InitProfileNamesTable() && InitProfileEmailsTable() &&
387 InitProfilePhonesTable() && InitProfileTrashTable()); 386 InitProfilePhonesTable() && InitProfileTrashTable());
388 } 387 }
389 388
390 bool AutofillTable::IsSyncable() { 389 bool AutofillTable::IsSyncable() {
391 return true; 390 return true;
392 } 391 }
393 392
394 bool AutofillTable::MigrateToVersion(int version, 393 bool AutofillTable::MigrateToVersion(int version,
(...skipping 928 matching lines...) Expand 10 before | Expand all | Expand 10 after
1323 if (!db_->Execute("CREATE TABLE autofill_profiles_trash ( " 1322 if (!db_->Execute("CREATE TABLE autofill_profiles_trash ( "
1324 "guid VARCHAR)")) { 1323 "guid VARCHAR)")) {
1325 NOTREACHED(); 1324 NOTREACHED();
1326 return false; 1325 return false;
1327 } 1326 }
1328 } 1327 }
1329 return true; 1328 return true;
1330 } 1329 }
1331 1330
1332 bool AutofillTable::MigrateToVersion22ClearAutofillEmptyValueElements() { 1331 bool AutofillTable::MigrateToVersion22ClearAutofillEmptyValueElements() {
1332 if (!db_->DoesTableExist("autofill") &&
1333 (!db_->Execute("CREATE TABLE autofill ("
1334 " name VARCHAR,"
1335 " value VARCHAR,"
1336 " value_lower VARCHAR,"
1337 " pair_id INTEGER PRIMARY KEY,"
1338 " count INTEGER DEFAULT 1)") ||
1339 !db_->Execute("CREATE INDEX autofill_name ON autofill (name)") ||
1340 !db_->Execute("CREATE INDEX autofill_name_value_lower ON"
1341 " autofill (name, value_lower)") ||
1342 !db_->Execute("CREATE TABLE autofill_dates ("
1343 " pair_id INTEGER DEFAULT 0,"
1344 " date_created INTEGER DEFAULT 0)") ||
1345 !db_->Execute("CREATE INDEX autofill_dates_pair_id ON"
1346 " autofill (pair_id)")))
1347 return false;
1348
1349
1333 sql::Statement s(db_->GetUniqueStatement( 1350 sql::Statement s(db_->GetUniqueStatement(
1334 "SELECT pair_id FROM autofill WHERE TRIM(value) = \"\"")); 1351 "SELECT pair_id FROM autofill WHERE TRIM(value) = \"\""));
1335 if (!s.is_valid()) 1352 if (!s.is_valid())
1336 return false; 1353 return false;
1337 1354
1338 std::set<int64> ids; 1355 std::set<int64> ids;
1339 while (s.Step()) 1356 while (s.Step())
1340 ids.insert(s.ColumnInt64(0)); 1357 ids.insert(s.ColumnInt64(0));
1341 if (!s.Succeeded()) 1358 if (!s.Succeeded())
1342 return false; 1359 return false;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
1379 // Because this case does not roll back the complete set of SQL 1396 // Because this case does not roll back the complete set of SQL
1380 // transactions properly in case of failure (that is, it does not 1397 // transactions properly in case of failure (that is, it does not
1381 // roll back the table initialization done above), the incoherent 1398 // roll back the table initialization done above), the incoherent
1382 // profile will now see itself as being at version 22 -- but include a 1399 // profile will now see itself as being at version 22 -- but include a
1383 // fully initialized credit_cards table. Every time Chrome runs, it 1400 // fully initialized credit_cards table. Every time Chrome runs, it
1384 // will try to update the web database and fail at this step, unless 1401 // will try to update the web database and fail at this step, unless
1385 // we allow for the faulty assumption described above by checking for 1402 // we allow for the faulty assumption described above by checking for
1386 // the existence of the columns only AFTER we've executed the commands 1403 // the existence of the columns only AFTER we've executed the commands
1387 // to add them. 1404 // to add them.
1388 bool AutofillTable::MigrateToVersion23AddCardNumberEncryptedColumn() { 1405 bool AutofillTable::MigrateToVersion23AddCardNumberEncryptedColumn() {
1406 if (!db_->DoesTableExist("autofill_profiles") &&
1407 (!db_->Execute("CREATE TABLE autofill_profiles ( "
1408 "label VARCHAR, "
1409 "unique_id INTEGER PRIMARY KEY, "
1410 "first_name VARCHAR, "
1411 "middle_name VARCHAR, "
1412 "last_name VARCHAR, "
1413 "email VARCHAR, "
1414 "company_name VARCHAR, "
1415 "address_line_1 VARCHAR, "
1416 "address_line_2 VARCHAR, "
1417 "city VARCHAR, "
1418 "state VARCHAR, "
1419 "zipcode VARCHAR, "
1420 "country VARCHAR, "
1421 "phone VARCHAR, "
1422 "fax VARCHAR)") ||
1423 !db_->Execute("CREATE INDEX autofill_profiles_label_index"
1424 " ON autofill_profiles (label)")))
1425 return false;
1426
1427 if (!db_->DoesTableExist("credit_cards") &&
1428 (!db_->Execute("CREATE TABLE credit_cards ( "
1429 "label VARCHAR, "
1430 "unique_id INTEGER PRIMARY KEY, "
1431 "name_on_card VARCHAR, "
1432 "type VARCHAR, "
1433 "card_number VARCHAR, "
1434 "expiration_month INTEGER, "
1435 "expiration_year INTEGER, "
1436 "verification_code VARCHAR, "
1437 "billing_address VARCHAR, "
1438 "shipping_address VARCHAR)") ||
1439 !db_->Execute("CREATE INDEX credit_cards_label_index"
1440 " ON credit_cards (label)")))
1441 return false;
1442
1389 if (!db_->DoesColumnExist("credit_cards", "card_number_encrypted")) { 1443 if (!db_->DoesColumnExist("credit_cards", "card_number_encrypted")) {
1390 if (!db_->Execute("ALTER TABLE credit_cards ADD COLUMN " 1444 if (!db_->Execute("ALTER TABLE credit_cards ADD COLUMN "
1391 "card_number_encrypted BLOB DEFAULT NULL")) { 1445 "card_number_encrypted BLOB DEFAULT NULL")) {
1392 LOG(WARNING) << "Could not add card_number_encrypted to "
1393 "credit_cards table.";
1394 return false; 1446 return false;
1395 } 1447 }
1396 } 1448 }
1397 1449
1398 if (!db_->DoesColumnExist("credit_cards", "verification_code_encrypted")) { 1450 if (!db_->DoesColumnExist("credit_cards", "verification_code_encrypted")) {
1399 if (!db_->Execute("ALTER TABLE credit_cards ADD COLUMN " 1451 if (!db_->Execute("ALTER TABLE credit_cards ADD COLUMN "
1400 "verification_code_encrypted BLOB DEFAULT NULL")) { 1452 "verification_code_encrypted BLOB DEFAULT NULL")) {
1401 LOG(WARNING) << "Could not add verification_code_encrypted to "
1402 "credit_cards table.";
1403 return false; 1453 return false;
1404 } 1454 }
1405 } 1455 }
1406 1456
1407 return true; 1457 return true;
1408 } 1458 }
1409 1459
1410 // One-time cleanup for http://crbug.com/38364 - In the presence of 1460 // One-time cleanup for http://crbug.com/38364 - In the presence of
1411 // multi-byte UTF-8 characters, that bug could cause Autofill strings 1461 // multi-byte UTF-8 characters, that bug could cause Autofill strings
1412 // to grow larger and more corrupt with each save. The cleanup removes 1462 // to grow larger and more corrupt with each save. The cleanup removes
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after
1716 } 1766 }
1717 1767
1718 return true; 1768 return true;
1719 } 1769 }
1720 1770
1721 // Test the existence of the |first_name| column as an indication that 1771 // Test the existence of the |first_name| column as an indication that
1722 // we need a migration. It is possible that the new |autofill_profiles| 1772 // we need a migration. It is possible that the new |autofill_profiles|
1723 // schema is in place because the table was newly created when migrating 1773 // schema is in place because the table was newly created when migrating
1724 // from a pre-version-22 database. 1774 // from a pre-version-22 database.
1725 bool AutofillTable::MigrateToVersion33ProfilesBasedOnFirstName() { 1775 bool AutofillTable::MigrateToVersion33ProfilesBasedOnFirstName() {
1776 if (!db_->DoesTableExist("autofill_profile_names") &&
1777 !db_->Execute("CREATE TABLE autofill_profile_names ( "
1778 "guid VARCHAR, "
1779 "first_name VARCHAR, "
1780 "middle_name VARCHAR, "
1781 "last_name VARCHAR)"))
1782 return false;
1783
1784 if (!db_->DoesTableExist("autofill_profile_emails") &&
1785 !db_->Execute("CREATE TABLE autofill_profile_emails ( "
1786 "guid VARCHAR, "
1787 "email VARCHAR)"))
1788 return false;
1789
1790 if (!db_->DoesTableExist("autofill_profile_phones") &&
1791 !db_->Execute("CREATE TABLE autofill_profile_phones ( "
1792 "guid VARCHAR, "
1793 "type INTEGER DEFAULT 0, "
1794 "number VARCHAR)"))
1795 return false;
1796
1726 if (db_->DoesColumnExist("autofill_profiles", "first_name")) { 1797 if (db_->DoesColumnExist("autofill_profiles", "first_name")) {
1727 // Create autofill_profiles_temp table that will receive the data. 1798 // Create autofill_profiles_temp table that will receive the data.
1728 if (!db_->DoesTableExist("autofill_profiles_temp")) { 1799 if (!db_->DoesTableExist("autofill_profiles_temp")) {
1729 if (!db_->Execute("CREATE TABLE autofill_profiles_temp ( " 1800 if (!db_->Execute("CREATE TABLE autofill_profiles_temp ( "
1730 "guid VARCHAR PRIMARY KEY, " 1801 "guid VARCHAR PRIMARY KEY, "
1731 "company_name VARCHAR, " 1802 "company_name VARCHAR, "
1732 "address_line_1 VARCHAR, " 1803 "address_line_1 VARCHAR, "
1733 "address_line_2 VARCHAR, " 1804 "address_line_2 VARCHAR, "
1734 "city VARCHAR, " 1805 "city VARCHAR, "
1735 "state VARCHAR, " 1806 "state VARCHAR, "
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
1873 bool AutofillTable::MigrateToVersion35GreatBritainCountryCodes() { 1944 bool AutofillTable::MigrateToVersion35GreatBritainCountryCodes() {
1874 sql::Statement s(db_->GetUniqueStatement( 1945 sql::Statement s(db_->GetUniqueStatement(
1875 "UPDATE autofill_profiles SET country_code=\"GB\" " 1946 "UPDATE autofill_profiles SET country_code=\"GB\" "
1876 "WHERE country_code=\"UK\"")); 1947 "WHERE country_code=\"UK\""));
1877 1948
1878 return s.Run(); 1949 return s.Run();
1879 } 1950 }
1880 1951
1881 // Merge and cull older profiles where possible. 1952 // Merge and cull older profiles where possible.
1882 bool AutofillTable::MigrateToVersion37MergeAndCullOlderProfiles() { 1953 bool AutofillTable::MigrateToVersion37MergeAndCullOlderProfiles() {
1954 if (!db_->DoesTableExist("autofill_profiles_trash") &&
1955 !db_->Execute("CREATE TABLE autofill_profiles_trash (guid VARCHAR)"))
1956 return false;
1957
1883 sql::Statement s(db_->GetUniqueStatement( 1958 sql::Statement s(db_->GetUniqueStatement(
1884 "SELECT guid, date_modified FROM autofill_profiles")); 1959 "SELECT guid, date_modified FROM autofill_profiles"));
1885 1960
1886 // Accumulate the good profiles. 1961 // Accumulate the good profiles.
1887 std::vector<AutofillProfile> accumulated_profiles; 1962 std::vector<AutofillProfile> accumulated_profiles;
1888 std::vector<AutofillProfile*> accumulated_profiles_p; 1963 std::vector<AutofillProfile*> accumulated_profiles_p;
1889 std::map<std::string, int64> modification_map; 1964 std::map<std::string, int64> modification_map;
1890 while (s.Step()) { 1965 while (s.Step()) {
1891 std::string guid = s.ColumnString(0); 1966 std::string guid = s.ColumnString(0);
1892 int64 date_modified = s.ColumnInt64(1); 1967 int64 date_modified = s.ColumnInt64(1);
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after
2179 !db_->Execute("CREATE INDEX autofill_name_value_lower ON " 2254 !db_->Execute("CREATE INDEX autofill_name_value_lower ON "
2180 "autofill (name, value_lower)")) { 2255 "autofill (name, value_lower)")) {
2181 return false; 2256 return false;
2182 } 2257 }
2183 2258
2184 2259
2185 return transaction.Commit(); 2260 return transaction.Commit();
2186 } 2261 }
2187 2262
2188 } // namespace autofill 2263 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698