OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/precache/core/precache_referrer_host_table.h" |
| 6 |
| 7 #include "sql/connection.h" |
| 8 #include "sql/statement.h" |
| 9 |
| 10 using sql::Statement; |
| 11 |
| 12 namespace precache { |
| 13 |
| 14 const int64_t PrecacheReferrerHostEntry::kInvalidId = -1; |
| 15 |
| 16 bool PrecacheReferrerHostEntry::operator==( |
| 17 const PrecacheReferrerHostEntry& entry) const { |
| 18 return id == entry.id && referrer_host == entry.referrer_host && |
| 19 manifest_id == entry.manifest_id && time == entry.time; |
| 20 } |
| 21 |
| 22 PrecacheReferrerHostTable::PrecacheReferrerHostTable() : db_(NULL) {} |
| 23 |
| 24 PrecacheReferrerHostTable::~PrecacheReferrerHostTable() {} |
| 25 |
| 26 bool PrecacheReferrerHostTable::Init(sql::Connection* db) { |
| 27 DCHECK(!db_); // Init must only be called once. |
| 28 DCHECK(db); // The database connection must be non-NULL. |
| 29 db_ = db; |
| 30 return CreateTableIfNonExistent(); |
| 31 } |
| 32 |
| 33 PrecacheReferrerHostEntry PrecacheReferrerHostTable::GetReferrerHost( |
| 34 const std::string& referrer_host) { |
| 35 Statement statement(db_->GetCachedStatement( |
| 36 SQL_FROM_HERE, |
| 37 "SELECT id, referrer_host, manifest_id, time " |
| 38 "FROM precache_referrer_hosts WHERE referrer_host=?")); |
| 39 |
| 40 statement.BindString(0, referrer_host); |
| 41 if (statement.Step()) { |
| 42 return PrecacheReferrerHostEntry( |
| 43 statement.ColumnInt64(0), statement.ColumnString(1), |
| 44 statement.ColumnInt64(2), |
| 45 base::Time::FromInternalValue(statement.ColumnInt64(3))); |
| 46 } |
| 47 return PrecacheReferrerHostEntry(PrecacheReferrerHostEntry::kInvalidId, |
| 48 std::string(), 0, base::Time()); |
| 49 } |
| 50 |
| 51 int64_t PrecacheReferrerHostTable::UpdateReferrerHost( |
| 52 const std::string& referrer_host, |
| 53 int64_t manifest_id, |
| 54 const base::Time& time) { |
| 55 auto referrer_host_id = GetReferrerHost(referrer_host).id; |
| 56 if (referrer_host_id == PrecacheReferrerHostEntry::kInvalidId) { |
| 57 Statement statement( |
| 58 db_->GetCachedStatement(SQL_FROM_HERE, |
| 59 "INSERT INTO precache_referrer_hosts " |
| 60 "(id, referrer_host, manifest_id, time) " |
| 61 "VALUES(NULL, ?, ?, ?)")); |
| 62 |
| 63 statement.BindString(0, referrer_host); |
| 64 statement.BindInt64(1, manifest_id); |
| 65 statement.BindInt64(2, time.ToInternalValue()); |
| 66 if (statement.Run()) |
| 67 return db_->GetLastInsertRowId(); |
| 68 } else { |
| 69 Statement statement( |
| 70 db_->GetCachedStatement(SQL_FROM_HERE, |
| 71 "UPDATE precache_referrer_hosts " |
| 72 "SET manifest_id=?, time=? " |
| 73 "WHERE id=?")); |
| 74 |
| 75 statement.BindInt64(0, manifest_id); |
| 76 statement.BindInt64(1, time.ToInternalValue()); |
| 77 ; |
| 78 statement.BindInt64(2, referrer_host_id); |
| 79 if (statement.Run()) |
| 80 return referrer_host_id; |
| 81 } |
| 82 return -1; |
| 83 } |
| 84 |
| 85 void PrecacheReferrerHostTable::DeleteAllEntriesBefore( |
| 86 const base::Time& delete_end) { |
| 87 Statement statement(db_->GetCachedStatement( |
| 88 SQL_FROM_HERE, "DELETE FROM precache_referrer_hosts WHERE time < ?")); |
| 89 statement.BindInt64(0, delete_end.ToInternalValue()); |
| 90 statement.Run(); |
| 91 } |
| 92 |
| 93 void PrecacheReferrerHostTable::DeleteAll() { |
| 94 Statement statement(db_->GetCachedStatement( |
| 95 SQL_FROM_HERE, "DELETE FROM precache_referrer_hosts")); |
| 96 |
| 97 statement.Run(); |
| 98 } |
| 99 |
| 100 bool PrecacheReferrerHostTable::CreateTableIfNonExistent() { |
| 101 return db_->Execute( |
| 102 "CREATE TABLE IF NOT EXISTS precache_referrer_hosts " |
| 103 "(id INTEGER PRIMARY KEY, referrer_host TEXT KEY, manifest_id INTEGER, " |
| 104 "time INTEGER)"); |
| 105 } |
| 106 |
| 107 std::map<std::string, PrecacheReferrerHostEntry> |
| 108 PrecacheReferrerHostTable::GetAllDataForTesting() { |
| 109 std::map<std::string, PrecacheReferrerHostEntry> all_data; |
| 110 Statement statement( |
| 111 db_->GetCachedStatement(SQL_FROM_HERE, |
| 112 "SELECT id, referrer_host, manifest_id, time " |
| 113 "FROM precache_referrer_hosts")); |
| 114 while (statement.Step()) { |
| 115 all_data[statement.ColumnString(1)] = PrecacheReferrerHostEntry( |
| 116 statement.ColumnInt64(0), statement.ColumnString(1), |
| 117 statement.ColumnInt64(2), |
| 118 base::Time::FromInternalValue(statement.ColumnInt64(3))); |
| 119 } |
| 120 return all_data; |
| 121 } |
| 122 |
| 123 } // namespace precache |
OLD | NEW |