Chromium Code Reviews| 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::INVALID_ID = -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( | |
|
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
| |
| 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), GURL(statement.ColumnString(1)), | |
| 38 statement.ColumnInt64(2), | |
| 39 base::Time::FromInternalValue(statement.ColumnInt64(3))); | |
| 40 } | |
| 41 return PrecacheReferrerHostEntry(PrecacheReferrerHostEntry::INVALID_ID, | |
| 42 GURL(), 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 Statement statement( | |
| 50 db_->GetCachedStatement(SQL_FROM_HERE, | |
| 51 "INSERT INTO precache_referrer_hosts " | |
| 52 "(id, referrer_host, manifest_id, time) " | |
| 53 "VALUES(NULL, ?, ?, ?)")); | |
| 54 | |
| 55 statement.BindString(0, referrer_host); | |
| 56 statement.BindInt64(1, manifest_id); | |
| 57 statement.BindInt64(2, time.ToInternalValue()); | |
| 58 if (statement.Run()) | |
| 59 return db_->GetLastInsertRowId(); | |
| 60 return -1; | |
| 61 } | |
| 62 | |
| 63 void PrecacheReferrerHostTable::DeleteAllEntriesBefore( | |
| 64 const base::Time& delete_end) { | |
| 65 Statement statement(db_->GetCachedStatement( | |
| 66 SQL_FROM_HERE, "DELETE FROM precache_referrer_hosts WHERE time < ?")); | |
| 67 statement.BindInt64(0, delete_end.ToInternalValue()); | |
| 68 statement.Run(); | |
| 69 } | |
| 70 | |
| 71 void PrecacheReferrerHostTable::DeleteAll() { | |
| 72 Statement statement( | |
| 73 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.
| |
| 74 | |
| 75 statement.Run(); | |
| 76 } | |
| 77 | |
| 78 bool PrecacheReferrerHostTable::CreateTableIfNonExistent() { | |
| 79 return db_->Execute( | |
| 80 "CREATE TABLE IF NOT EXISTS precache_referrer_hosts " | |
| 81 "(id INTEGER PRIMARY KEY, referrer_host TEXT KEY, manifest_id INTEGER, " | |
| 82 "time INTEGER)"); | |
| 83 } | |
| 84 | |
| 85 } // namespace precache | |
| OLD | NEW |