Index: components/precache/core/precache_referrer_host_table.cc |
diff --git a/components/precache/core/precache_referrer_host_table.cc b/components/precache/core/precache_referrer_host_table.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ad5f4274131f8c190c24e40df5d951b21736fe95 |
--- /dev/null |
+++ b/components/precache/core/precache_referrer_host_table.cc |
@@ -0,0 +1,85 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/precache/core/precache_referrer_host_table.h" |
+ |
+#include "sql/connection.h" |
+#include "sql/statement.h" |
+ |
+using sql::Statement; |
+ |
+namespace precache { |
+ |
+const int64_t PrecacheReferrerHostEntry::INVALID_ID = -1; |
+ |
+PrecacheReferrerHostTable::PrecacheReferrerHostTable() : db_(NULL) {} |
+ |
+PrecacheReferrerHostTable::~PrecacheReferrerHostTable() {} |
+ |
+bool PrecacheReferrerHostTable::Init(sql::Connection* db) { |
+ DCHECK(!db_); // Init must only be called once. |
+ DCHECK(db); // The database connection must be non-NULL. |
+ db_ = db; |
+ return CreateTableIfNonExistent(); |
+} |
+ |
+PrecacheReferrerHostEntry PrecacheReferrerHostTable::GetReferrerHost( |
bengr
2016/08/11 18:49:15
Can this method be const?
Raj
2016/08/12 19:04:21
Actually const. But cannot be const. :)
db_ is bei
|
+ const std::string& referrer_host) { |
+ Statement statement(db_->GetCachedStatement( |
+ SQL_FROM_HERE, |
+ "SELECT id, referrer_host, manifest_id, time " |
+ "FROM precache_referrer_hosts WHERE referrer_host=?")); |
+ |
+ statement.BindString(0, referrer_host); |
+ if (statement.Step()) { |
+ return PrecacheReferrerHostEntry( |
+ statement.ColumnInt64(0), GURL(statement.ColumnString(1)), |
+ statement.ColumnInt64(2), |
+ base::Time::FromInternalValue(statement.ColumnInt64(3))); |
+ } |
+ return PrecacheReferrerHostEntry(PrecacheReferrerHostEntry::INVALID_ID, |
+ GURL(), 0, base::Time()); |
+} |
+ |
+int64_t PrecacheReferrerHostTable::UpdateReferrerHost( |
+ const std::string& referrer_host, |
+ int64_t manifest_id, |
+ const base::Time& time) { |
+ Statement statement( |
+ db_->GetCachedStatement(SQL_FROM_HERE, |
+ "INSERT INTO precache_referrer_hosts " |
+ "(id, referrer_host, manifest_id, time) " |
+ "VALUES(NULL, ?, ?, ?)")); |
+ |
+ statement.BindString(0, referrer_host); |
+ statement.BindInt64(1, manifest_id); |
+ statement.BindInt64(2, time.ToInternalValue()); |
+ if (statement.Run()) |
+ return db_->GetLastInsertRowId(); |
+ return -1; |
+} |
+ |
+void PrecacheReferrerHostTable::DeleteAllEntriesBefore( |
+ const base::Time& delete_end) { |
+ Statement statement(db_->GetCachedStatement( |
+ SQL_FROM_HERE, "DELETE FROM precache_referrer_hosts WHERE time < ?")); |
+ statement.BindInt64(0, delete_end.ToInternalValue()); |
+ statement.Run(); |
+} |
+ |
+void PrecacheReferrerHostTable::DeleteAll() { |
+ Statement statement( |
+ db_->GetCachedStatement(SQL_FROM_HERE, "DELETE FROM precache_urls")); |
sclittle
2016/08/11 22:52:36
Update this to say "precache_referrer_hosts".
Cou
Raj
2016/08/12 19:04:21
Done.
Adding tests.
|
+ |
+ statement.Run(); |
+} |
+ |
+bool PrecacheReferrerHostTable::CreateTableIfNonExistent() { |
+ return db_->Execute( |
+ "CREATE TABLE IF NOT EXISTS precache_referrer_hosts " |
+ "(id INTEGER PRIMARY KEY, referrer_host TEXT KEY, manifest_id INTEGER, " |
+ "time INTEGER)"); |
+} |
+ |
+} // namespace precache |