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