Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/offline_pages/offline_page_metadata_store_sql.h" | 5 #include "components/offline_pages/offline_page_metadata_store_sql.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
| 13 #include "base/sequenced_task_runner.h" | 13 #include "base/sequenced_task_runner.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "components/offline_pages/offline_page_item.h" | 16 #include "components/offline_pages/offline_page_item.h" |
| 17 #include "sql/connection.h" | 17 #include "sql/connection.h" |
| 18 #include "sql/statement.h" | 18 #include "sql/statement.h" |
| 19 #include "sql/transaction.h" | 19 #include "sql/transaction.h" |
| 20 | 20 |
| 21 namespace offline_pages { | 21 namespace offline_pages { |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 // This is a macro instead of a const so that | 25 // This is a macro instead of a const so that |
| 26 // it can be used inline in other SQL statements below. | 26 // it can be used inline in other SQL statements below. |
| 27 #define OFFLINE_PAGES_TABLE_NAME "offlinepages_v1" | 27 #define OFFLINE_PAGES_TABLE_NAME "offlinepages_v1" |
| 28 | 28 |
| 29 const char kOfflinePagesColumns[] = | 29 const char kOfflinePagesColumns[] = |
|
jianli
2016/06/03 21:12:18
Could you please comment that new column should be
romax
2016/06/03 23:11:39
Done.
| |
| 30 "(offline_id INTEGER PRIMARY KEY NOT NULL," | 30 "(offline_id INTEGER PRIMARY KEY NOT NULL," |
| 31 " creation_time INTEGER NOT NULL," | 31 " creation_time INTEGER NOT NULL," |
| 32 " file_size INTEGER NOT NULL," | 32 " file_size INTEGER NOT NULL," |
| 33 " version INTEGER NOT NULL," | 33 " version INTEGER NOT NULL," |
| 34 " last_access_time INTEGER NOT NULL," | 34 " last_access_time INTEGER NOT NULL," |
| 35 " access_count INTEGER NOT NULL," | 35 " access_count INTEGER NOT NULL," |
| 36 " status INTEGER NOT NULL DEFAULT 0," | 36 " status INTEGER NOT NULL DEFAULT 0," |
| 37 // A note on this field: It will be NULL for now and is reserved for | 37 // A note on this field: It will be NULL for now and is reserved for |
| 38 // later use. We will treat NULL as "Unknown" in any subsequent queries | 38 // later use. We will treat NULL as "Unknown" in any subsequent queries |
| 39 // for user_initiated values. | 39 // for user_initiated values. |
| 40 " user_initiated INTEGER," // this is actually a boolean | 40 " user_initiated INTEGER," // this is actually a boolean |
| 41 " expiration_time INTEGER NOT NULL," | 41 " expiration_time INTEGER NOT NULL DEFAULT 0," |
| 42 " client_namespace VARCHAR NOT NULL," | 42 " client_namespace VARCHAR NOT NULL," |
| 43 " client_id VARCHAR NOT NULL," | 43 " client_id VARCHAR NOT NULL," |
| 44 " online_url VARCHAR NOT NULL," | 44 " online_url VARCHAR NOT NULL," |
| 45 " offline_url VARCHAR NOT NULL DEFAULT ''," | 45 " offline_url VARCHAR NOT NULL DEFAULT ''," |
| 46 " file_path VARCHAR NOT NULL" | 46 " file_path VARCHAR NOT NULL" |
| 47 ")"; | 47 ")"; |
| 48 | 48 |
| 49 // This is cloned from //content/browser/appcache/appcache_database.cc | 49 // This is cloned from //content/browser/appcache/appcache_database.cc |
| 50 struct TableInfo { | 50 struct TableInfo { |
| 51 const char* table_name; | 51 const char* table_name; |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 74 OP_FILE_PATH, | 74 OP_FILE_PATH, |
| 75 }; | 75 }; |
| 76 | 76 |
| 77 bool CreateTable(sql::Connection* db, const TableInfo& table_info) { | 77 bool CreateTable(sql::Connection* db, const TableInfo& table_info) { |
| 78 std::string sql("CREATE TABLE "); | 78 std::string sql("CREATE TABLE "); |
| 79 sql += table_info.table_name; | 79 sql += table_info.table_name; |
| 80 sql += table_info.columns; | 80 sql += table_info.columns; |
| 81 return db->Execute(sql.c_str()); | 81 return db->Execute(sql.c_str()); |
| 82 } | 82 } |
| 83 | 83 |
| 84 bool RefreshColumns(sql::Connection* db) { | |
| 85 if (!db->Execute("ALTER TABLE " OFFLINE_PAGES_TABLE_NAME | |
| 86 " RENAME TO temp_" OFFLINE_PAGES_TABLE_NAME)) { | |
| 87 return false; | |
| 88 } | |
| 89 if (!CreateTable(db, kOfflinePagesTable)) | |
| 90 return false; | |
| 91 if (!db->Execute( | |
| 92 "INSERT INTO " OFFLINE_PAGES_TABLE_NAME | |
| 93 " (offline_id, creation_time, file_size, version, last_access_time, " | |
| 94 "access_count, status, user_initiated, client_namespace, client_id, " | |
| 95 "online_url, offline_url, file_path) " | |
| 96 "SELECT offline_id, creation_time, file_size, version, " | |
| 97 "last_access_time, " | |
| 98 "access_count, status, user_initiated, client_namespace, client_id, " | |
| 99 "online_url, offline_url, file_path " | |
| 100 "FROM temp_" OFFLINE_PAGES_TABLE_NAME)) { | |
| 101 return false; | |
| 102 } | |
| 103 if (!db->Execute("DROP TABLE IF EXISTS temp_" OFFLINE_PAGES_TABLE_NAME)) | |
| 104 return false; | |
| 105 | |
| 106 return true; | |
| 107 } | |
| 108 | |
| 84 bool CreateSchema(sql::Connection* db) { | 109 bool CreateSchema(sql::Connection* db) { |
| 85 // If you create a transaction but don't Commit() it is automatically | 110 // If you create a transaction but don't Commit() it is automatically |
| 86 // rolled back by its destructor when it falls out of scope. | 111 // rolled back by its destructor when it falls out of scope. |
| 87 sql::Transaction transaction(db); | 112 sql::Transaction transaction(db); |
| 88 if (!transaction.Begin()) | 113 if (!transaction.Begin()) |
| 89 return false; | 114 return false; |
| 90 if (db->DoesTableExist(kOfflinePagesTable.table_name)) | 115 if (db->DoesTableExist(kOfflinePagesTable.table_name)) { |
|
jianli
2016/06/03 21:12:18
I suggest to reorder all these if checks to make t
romax
2016/06/03 23:11:39
emm I dont think we can return true since it may c
| |
| 116 if (!db->DoesColumnExist(OFFLINE_PAGES_TABLE_NAME, "expiration_time")) { | |
| 117 if (!RefreshColumns(db)) | |
| 118 return false; | |
| 119 return transaction.Commit(); | |
| 120 } | |
| 91 return true; | 121 return true; |
| 122 } | |
| 92 | 123 |
| 93 if (!CreateTable(db, kOfflinePagesTable)) | 124 if (!CreateTable(db, kOfflinePagesTable)) |
| 94 return false; | 125 return false; |
| 95 | 126 |
| 96 // TODO(bburns): Add indices here. | 127 // TODO(bburns): Add indices here. |
| 97 return transaction.Commit(); | 128 return transaction.Commit(); |
| 98 } | 129 } |
| 99 | 130 |
| 100 bool DeleteByOfflineId(sql::Connection* db, int64_t offline_id) { | 131 bool DeleteByOfflineId(sql::Connection* db, int64_t offline_id) { |
| 101 static const char kSql[] = | 132 static const char kSql[] = |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 329 } | 360 } |
| 330 | 361 |
| 331 void OfflinePageMetadataStoreSQL::Reset(const ResetCallback& callback) { | 362 void OfflinePageMetadataStoreSQL::Reset(const ResetCallback& callback) { |
| 332 background_task_runner_->PostTask( | 363 background_task_runner_->PostTask( |
| 333 FROM_HERE, | 364 FROM_HERE, |
| 334 base::Bind(&OfflinePageMetadataStoreSQL::ResetSync, base::Passed(&db_), | 365 base::Bind(&OfflinePageMetadataStoreSQL::ResetSync, base::Passed(&db_), |
| 335 base::ThreadTaskRunnerHandle::Get(), callback)); | 366 base::ThreadTaskRunnerHandle::Get(), callback)); |
| 336 } | 367 } |
| 337 | 368 |
| 338 } // namespace offline_pages | 369 } // namespace offline_pages |
| OLD | NEW |