| 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 "base/time/time.h" |
| 12 #include "sql/connection.h" |
| 13 #include "sql/statement.h" |
| 14 #include "url/gurl.h" |
| 15 |
| 16 using sql::Statement; |
| 17 |
| 18 namespace precache { |
| 19 |
| 20 namespace { |
| 21 |
| 22 const char kUrlTableName[] = "precache_urls"; |
| 23 |
| 24 } // namespace |
| 25 |
| 26 // static |
| 27 std::string PrecacheURLTable::GetKey(const GURL& url) { |
| 28 return url.spec(); |
| 29 } |
| 30 |
| 31 PrecacheURLTable::PrecacheURLTable() : db_(NULL) {} |
| 32 |
| 33 PrecacheURLTable::~PrecacheURLTable() {} |
| 34 |
| 35 void PrecacheURLTable::Init(sql::Connection* db) { |
| 36 DCHECK(!db_); // Init must only be called once. |
| 37 DCHECK(db); // The database connection must be non-NULL. |
| 38 db_ = db; |
| 39 CreateTableIfNonExistent(); |
| 40 } |
| 41 |
| 42 void PrecacheURLTable::AddURL(const GURL& url, |
| 43 const base::Time& precache_time) { |
| 44 DCHECK(db_); |
| 45 |
| 46 Statement statement(db_->GetCachedStatement( |
| 47 SQL_FROM_HERE, |
| 48 base::StringPrintf("INSERT OR REPLACE INTO %s (url, time) VALUES(?,?)", |
| 49 kUrlTableName).c_str())); |
| 50 statement.BindString(0, GetKey(url)); |
| 51 statement.BindInt64(1, precache_time.ToInternalValue()); |
| 52 |
| 53 DCHECK(statement.Run()); |
| 54 } |
| 55 |
| 56 void PrecacheURLTable::DeleteAllPrecachedBetween(const base::Time& delete_begin, |
| 57 const base::Time& delete_end) { |
| 58 DCHECK(db_); |
| 59 |
| 60 Statement statement(db_->GetCachedStatement( |
| 61 SQL_FROM_HERE, |
| 62 base::StringPrintf("DELETE FROM %s WHERE time >= ? AND time <= ?", |
| 63 kUrlTableName).c_str())); |
| 64 |
| 65 statement.BindInt64(0, delete_begin.ToInternalValue()); |
| 66 statement.BindInt64(1, delete_end.ToInternalValue()); |
| 67 |
| 68 DCHECK(statement.Run()); |
| 69 } |
| 70 |
| 71 bool PrecacheURLTable::HasURL(const GURL& url) { |
| 72 DCHECK(db_); |
| 73 |
| 74 Statement statement(db_->GetCachedStatement( |
| 75 SQL_FROM_HERE, base::StringPrintf("SELECT time FROM %s WHERE url=?", |
| 76 kUrlTableName).c_str())); |
| 77 statement.BindString(0, GetKey(url)); |
| 78 |
| 79 return statement.Step(); |
| 80 } |
| 81 |
| 82 void PrecacheURLTable::DeleteURL(const GURL& url) { |
| 83 DCHECK(db_); |
| 84 |
| 85 Statement statement(db_->GetCachedStatement( |
| 86 SQL_FROM_HERE, |
| 87 base::StringPrintf("DELETE FROM %s WHERE url=?", kUrlTableName).c_str())); |
| 88 statement.BindString(0, GetKey(url)); |
| 89 |
| 90 DCHECK(statement.Run()); |
| 91 } |
| 92 |
| 93 void PrecacheURLTable::GetAllData(std::map<GURL, base::Time>* map) { |
| 94 DCHECK(db_); |
| 95 |
| 96 Statement statement(db_->GetCachedStatement( |
| 97 SQL_FROM_HERE, |
| 98 base::StringPrintf("SELECT url, time FROM %s", kUrlTableName).c_str())); |
| 99 |
| 100 while (statement.Step()) { |
| 101 GURL url = GURL(statement.ColumnString(0)); |
| 102 (*map)[url] = base::Time::FromInternalValue(statement.ColumnInt64(1)); |
| 103 } |
| 104 } |
| 105 |
| 106 void PrecacheURLTable::CreateTableIfNonExistent() { |
| 107 DCHECK(db_); |
| 108 |
| 109 if (db_->DoesTableExist(kUrlTableName)) |
| 110 return; |
| 111 |
| 112 const char* table_creator = |
| 113 "CREATE TABLE %s (url TEXT, time INTEGER, PRIMARY KEY(url))"; |
| 114 |
| 115 DCHECK( |
| 116 db_->Execute(base::StringPrintf(table_creator, kUrlTableName).c_str())); |
| 117 } |
| 118 |
| 119 } // namespace precache |
| OLD | NEW |