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

Unified Diff: components/offline_pages/offline_page_metadata_store_sql.cc

Issue 2336813002: [Offline pages] OPMStoreSQL: Upgrade to M55, Upgrade refactoring, conversion refactoring (Closed)
Patch Set: Addressing CR feedback Created 4 years, 3 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/offline_pages/offline_page_metadata_store_sql.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/offline_pages/offline_page_metadata_store_sql.cc
diff --git a/components/offline_pages/offline_page_metadata_store_sql.cc b/components/offline_pages/offline_page_metadata_store_sql.cc
index bb7e9989985d15cb095bc315502a828ef6a6daf9..27d90bcf30f18911b20f3a14b6fe2a3d60b764e7 100644
--- a/components/offline_pages/offline_page_metadata_store_sql.cc
+++ b/components/offline_pages/offline_page_metadata_store_sql.cc
@@ -33,76 +33,72 @@ bool CreateOfflinePagesTable(sql::Connection* db) {
"(offline_id INTEGER PRIMARY KEY NOT NULL,"
" creation_time INTEGER NOT NULL,"
" file_size INTEGER NOT NULL,"
- " version INTEGER NOT NULL,"
" last_access_time INTEGER NOT NULL,"
" access_count INTEGER NOT NULL,"
- // TODO(fgorski): Remove. Never used.
- " status INTEGER NOT NULL DEFAULT 0,"
- // A note on this field: It will be NULL for now and is
- // reserved for later use. We will treat NULL as
- // "Unknown" in any subsequent queries for user_initiated
- // values.
- " user_initiated INTEGER," // this is actually a boolean
" expiration_time INTEGER NOT NULL DEFAULT 0,"
" client_namespace VARCHAR NOT NULL,"
" client_id VARCHAR NOT NULL,"
" online_url VARCHAR NOT NULL,"
- // TODO(fgorski): Remove. Never used.
- " offline_url VARCHAR NOT NULL DEFAULT '',"
" file_path VARCHAR NOT NULL,"
" title VARCHAR NOT NULL DEFAULT ''"
")";
return db->Execute(kSql);
}
-bool RefreshColumnsFrom52To54(sql::Connection* db) {
+bool UpgradeWithQuery(sql::Connection* db, const char* upgrade_sql) {
if (!db->Execute("ALTER TABLE " OFFLINE_PAGES_TABLE_NAME
" RENAME TO temp_" OFFLINE_PAGES_TABLE_NAME)) {
return false;
}
if (!CreateOfflinePagesTable(db))
return false;
- if (!db->Execute(
- "INSERT INTO " OFFLINE_PAGES_TABLE_NAME
- " (offline_id, creation_time, file_size, version, last_access_time, "
- "access_count, status, user_initiated, client_namespace, client_id, "
- "online_url, offline_url, file_path) "
- "SELECT offline_id, creation_time, file_size, version, "
- "last_access_time, "
- "access_count, status, user_initiated, client_namespace, client_id, "
- "online_url, offline_url, file_path "
- "FROM temp_" OFFLINE_PAGES_TABLE_NAME)) {
+ if (!db->Execute(upgrade_sql))
return false;
- }
if (!db->Execute("DROP TABLE IF EXISTS temp_" OFFLINE_PAGES_TABLE_NAME))
return false;
-
return true;
}
-bool RefreshColumnsFrom53To54(sql::Connection* db) {
- if (!db->Execute("ALTER TABLE " OFFLINE_PAGES_TABLE_NAME
- " RENAME TO temp_" OFFLINE_PAGES_TABLE_NAME)) {
- return false;
- }
- if (!CreateOfflinePagesTable(db))
- return false;
- if (!db->Execute(
- "INSERT INTO " OFFLINE_PAGES_TABLE_NAME
- " (offline_id, creation_time, file_size, version, last_access_time, "
- "access_count, status, user_initiated, client_namespace, client_id, "
- "online_url, offline_url, file_path, expiration_time) "
- "SELECT offline_id, creation_time, file_size, version, "
- "last_access_time, "
- "access_count, status, user_initiated, client_namespace, client_id, "
- "online_url, offline_url, file_path, expiration_time "
- "FROM temp_" OFFLINE_PAGES_TABLE_NAME)) {
- return false;
- }
- if (!db->Execute("DROP TABLE IF EXISTS temp_" OFFLINE_PAGES_TABLE_NAME))
- return false;
+bool UpgradeFrom52(sql::Connection* db) {
+ const char kSql[] =
+ "INSERT INTO " OFFLINE_PAGES_TABLE_NAME
+ " (offline_id, creation_time, file_size, last_access_time, "
+ "access_count, client_namespace, client_id, "
+ "online_url, file_path) "
+ "SELECT "
+ "offline_id, creation_time, file_size, last_access_time, "
+ "access_count, client_namespace, client_id, "
+ "online_url, file_path "
+ "FROM temp_" OFFLINE_PAGES_TABLE_NAME;
+ return UpgradeWithQuery(db, kSql);
+}
- return true;
+bool UpgradeFrom53(sql::Connection* db) {
+ const char kSql[] =
+ "INSERT INTO " OFFLINE_PAGES_TABLE_NAME
+ " (offline_id, creation_time, file_size, last_access_time, "
+ "access_count, expiration_time, client_namespace, client_id, "
+ "online_url, file_path) "
+ "SELECT "
+ "offline_id, creation_time, file_size, last_access_time, "
+ "access_count, expiration_time, client_namespace, client_id, "
+ "online_url, file_path "
+ "FROM temp_" OFFLINE_PAGES_TABLE_NAME;
+ return UpgradeWithQuery(db, kSql);
+}
+
+bool UpgradeFrom54(sql::Connection* db) {
+ const char kSql[] =
+ "INSERT INTO " OFFLINE_PAGES_TABLE_NAME
+ " (offline_id, creation_time, file_size, last_access_time, "
+ "access_count, expiration_time, client_namespace, client_id, "
+ "online_url, file_path, title) "
+ "SELECT "
+ "offline_id, creation_time, file_size, last_access_time, "
+ "access_count, expiration_time, client_namespace, client_id, "
+ "online_url, file_path, title "
+ "FROM temp_" OFFLINE_PAGES_TABLE_NAME;
+ return UpgradeWithQuery(db, kSql);
}
bool CreateSchema(sql::Connection* db) {
@@ -117,11 +113,15 @@ bool CreateSchema(sql::Connection* db) {
return false;
}
+ // Upgrade section. Details are described in the header file.
if (!db->DoesColumnExist(OFFLINE_PAGES_TABLE_NAME, "expiration_time")) {
- if (!RefreshColumnsFrom52To54(db))
+ if (!UpgradeFrom52(db))
return false;
} else if (!db->DoesColumnExist(OFFLINE_PAGES_TABLE_NAME, "title")) {
- if (!RefreshColumnsFrom53To54(db))
+ if (!UpgradeFrom53(db))
+ return false;
+ } else if (db->DoesColumnExist(OFFLINE_PAGES_TABLE_NAME, "offline_url")) {
+ if (!UpgradeFrom54(db))
return false;
}
@@ -137,6 +137,26 @@ bool DeleteByOfflineId(sql::Connection* db, int64_t offline_id) {
return statement.Run();
}
+base::FilePath GetPathFromUTF8String(const std::string& path_string) {
+#if defined(OS_POSIX)
+ return base::FilePath(path_string);
+#elif defined(OS_WIN)
+ return base::FilePath(base::UTF8ToWide(path_string));
+#else
+#error Unknown OS
+#endif
+}
+
+std::string GetUTF8StringFromPath(const base::FilePath& path) {
+#if defined(OS_POSIX)
+ return path.value();
+#elif defined(OS_WIN)
+ return base::WideToUTF8(path.value());
+#else
+#error Unknown OS
+#endif
+}
+
// Create an offline page item from a SQL result. Expects complete rows with
// all columns present.
OfflinePageItem MakeOfflinePageItem(sql::Statement* statement) {
@@ -144,25 +164,21 @@ OfflinePageItem MakeOfflinePageItem(sql::Statement* statement) {
base::Time creation_time =
base::Time::FromInternalValue(statement->ColumnInt64(1));
int64_t file_size = statement->ColumnInt64(2);
- ClientId client_id(statement->ColumnString(9),
- statement->ColumnString(10));
- GURL url(statement->ColumnString(11));
-#if defined(OS_POSIX)
- base::FilePath path(statement->ColumnString(13));
-#elif defined(OS_WIN)
- base::FilePath path(base::UTF8ToWide(statement->ColumnString(13)));
-#else
-#error Unknown OS
-#endif
+ base::Time last_access_time =
+ base::Time::FromInternalValue(statement->ColumnInt64(3));
+ int access_count = statement->ColumnInt(4);
+ base::Time expiration_time =
+ base::Time::FromInternalValue(statement->ColumnInt64(5));
+ ClientId client_id(statement->ColumnString(6), statement->ColumnString(7));
+ GURL url(statement->ColumnString(8));
+ base::FilePath path(GetPathFromUTF8String(statement->ColumnString(9)));
+ base::string16 title = statement->ColumnString16(10);
OfflinePageItem item(url, id, client_id, path, file_size, creation_time);
- item.version = statement->ColumnInt(3);
- item.last_access_time = base::Time::FromInternalValue(
- statement->ColumnInt64(4));
- item.access_count = statement->ColumnInt(5);
- item.expiration_time =
- base::Time::FromInternalValue(statement->ColumnInt64(8));
- item.title = statement->ColumnString16(14);
+ item.last_access_time = last_access_time;
+ item.access_count = access_count;
+ item.expiration_time = expiration_time;
+ item.title = title;
return item;
}
@@ -170,31 +186,23 @@ bool InsertOrReplace(sql::Connection* db, const OfflinePageItem& item) {
const char kSql[] =
"INSERT OR REPLACE INTO " OFFLINE_PAGES_TABLE_NAME
" (offline_id, online_url, client_namespace, client_id, file_path, "
- "file_size, creation_time, last_access_time, version, access_count, "
+ "file_size, creation_time, last_access_time, access_count, "
"expiration_time, title)"
" VALUES "
- " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
+ " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql));
statement.BindInt64(0, item.offline_id);
statement.BindString(1, item.url.spec());
statement.BindString(2, item.client_id.name_space);
statement.BindString(3, item.client_id.id);
-#if defined(OS_POSIX)
- std::string path_string = item.file_path.value();
-#elif defined(OS_WIN)
- std::string path_string = base::WideToUTF8(item.file_path.value());
-#else
-#error Unknown OS
-#endif
- statement.BindString(4, path_string);
+ statement.BindString(4, GetUTF8StringFromPath(item.file_path));
statement.BindInt64(5, item.file_size);
statement.BindInt64(6, item.creation_time.ToInternalValue());
statement.BindInt64(7, item.last_access_time.ToInternalValue());
- statement.BindInt(8, item.version);
- statement.BindInt(9, item.access_count);
- statement.BindInt64(10, item.expiration_time.ToInternalValue());
- statement.BindString16(11, item.title);
+ statement.BindInt(8, item.access_count);
+ statement.BindInt64(9, item.expiration_time.ToInternalValue());
+ statement.BindString16(10, item.title);
return statement.Run();
}
« no previous file with comments | « components/offline_pages/offline_page_metadata_store_sql.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698