| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2013 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_url_table.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/strings/stringprintf.h" |
| 11 #include "sql/connection.h" |
| 12 #include "sql/statement.h" |
| 13 |
| 14 using sql::Statement; |
| 15 |
| 16 namespace { |
| 17 |
| 18 // Returns the spec of the given URL. |
| 19 std::string GetKey(const GURL& url) { |
| 20 return url.spec(); |
| 21 } |
| 22 |
| 23 } // namespace |
| 24 |
| 25 namespace precache { |
| 26 |
| 27 PrecacheURLTable::PrecacheURLTable() : db_(NULL) {} |
| 28 |
| 29 PrecacheURLTable::~PrecacheURLTable() {} |
| 30 |
| 31 void PrecacheURLTable::Init(sql::Connection* db) { |
| 32 DCHECK(!db_); // Init must only be called once. |
| 33 DCHECK(db); // The database connection must be non-NULL. |
| 34 db_ = db; |
| 35 CreateTableIfNonExistent(); |
| 36 } |
| 37 |
| 38 void PrecacheURLTable::AddURL(const GURL& url, |
| 39 const base::Time& precache_time) { |
| 40 Statement statement(db_->GetCachedStatement( |
| 41 SQL_FROM_HERE, |
| 42 "INSERT OR REPLACE INTO precache_urls (url, time) VALUES(?,?)")); |
| 43 |
| 44 statement.BindString(0, GetKey(url)); |
| 45 statement.BindInt64(1, precache_time.ToInternalValue()); |
| 46 statement.Run(); |
| 47 } |
| 48 |
| 49 bool PrecacheURLTable::HasURL(const GURL& url) { |
| 50 Statement statement(db_->GetCachedStatement( |
| 51 SQL_FROM_HERE, "SELECT time FROM precache_urls WHERE url=?")); |
| 52 |
| 53 statement.BindString(0, GetKey(url)); |
| 54 return statement.Step(); |
| 55 } |
| 56 |
| 57 void PrecacheURLTable::DeleteURL(const GURL& url) { |
| 58 Statement statement(db_->GetCachedStatement( |
| 59 SQL_FROM_HERE, "DELETE FROM precache_urls WHERE url=?")); |
| 60 |
| 61 statement.BindString(0, GetKey(url)); |
| 62 statement.Run(); |
| 63 } |
| 64 |
| 65 void PrecacheURLTable::DeleteAllPrecachedBefore(const base::Time& delete_end) { |
| 66 Statement statement(db_->GetCachedStatement( |
| 67 SQL_FROM_HERE, "DELETE FROM precache_urls WHERE time < ?")); |
| 68 |
| 69 statement.BindInt64(0, delete_end.ToInternalValue()); |
| 70 statement.Run(); |
| 71 } |
| 72 |
| 73 void PrecacheURLTable::GetAllDataForTesting(std::map<GURL, base::Time>* map) { |
| 74 map->clear(); |
| 75 |
| 76 Statement statement(db_->GetCachedStatement( |
| 77 SQL_FROM_HERE, "SELECT url, time FROM precache_urls")); |
| 78 |
| 79 while (statement.Step()) { |
| 80 GURL url = GURL(statement.ColumnString(0)); |
| 81 (*map)[url] = base::Time::FromInternalValue(statement.ColumnInt64(1)); |
| 82 } |
| 83 } |
| 84 |
| 85 void PrecacheURLTable::CreateTableIfNonExistent() { |
| 86 ignore_result(db_->Execute( |
| 87 "CREATE TABLE IF NOT EXISTS precache_urls (url TEXT PRIMARY KEY, time " |
| 88 "INTEGER)")); |
| 89 } |
| 90 |
| 91 } // namespace precache |
| OLD | NEW |