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.
5 #include "components/precache/core/precache_url_table.h"
9 #include "base/logging.h"
10 #include "sql/connection.h"
11 #include "sql/statement.h"
17 // Returns the spec of the given URL.
18 std::string
GetKey(const GURL
& url
) {
26 PrecacheURLTable::PrecacheURLTable() : db_(NULL
) {}
28 PrecacheURLTable::~PrecacheURLTable() {}
30 bool PrecacheURLTable::Init(sql::Connection
* db
) {
31 DCHECK(!db_
); // Init must only be called once.
32 DCHECK(db
); // The database connection must be non-NULL.
34 return CreateTableIfNonExistent();
37 void PrecacheURLTable::AddURL(const GURL
& url
,
38 const base::Time
& precache_time
) {
39 Statement
statement(db_
->GetCachedStatement(
41 "INSERT OR REPLACE INTO precache_urls (url, time) VALUES(?,?)"));
43 statement
.BindString(0, GetKey(url
));
44 statement
.BindInt64(1, precache_time
.ToInternalValue());
48 bool PrecacheURLTable::HasURL(const GURL
& url
) {
49 Statement
statement(db_
->GetCachedStatement(
50 SQL_FROM_HERE
, "SELECT time FROM precache_urls WHERE url=?"));
52 statement
.BindString(0, GetKey(url
));
53 return statement
.Step();
56 void PrecacheURLTable::DeleteURL(const GURL
& url
) {
57 Statement
statement(db_
->GetCachedStatement(
58 SQL_FROM_HERE
, "DELETE FROM precache_urls WHERE url=?"));
60 statement
.BindString(0, GetKey(url
));
64 void PrecacheURLTable::DeleteAllPrecachedBefore(const base::Time
& delete_end
) {
65 Statement
statement(db_
->GetCachedStatement(
66 SQL_FROM_HERE
, "DELETE FROM precache_urls WHERE time < ?"));
68 statement
.BindInt64(0, delete_end
.ToInternalValue());
72 void PrecacheURLTable::GetAllDataForTesting(std::map
<GURL
, base::Time
>* map
) {
75 Statement
statement(db_
->GetCachedStatement(
76 SQL_FROM_HERE
, "SELECT url, time FROM precache_urls"));
78 while (statement
.Step()) {
79 GURL url
= GURL(statement
.ColumnString(0));
80 (*map
)[url
] = base::Time::FromInternalValue(statement
.ColumnInt64(1));
84 bool PrecacheURLTable::CreateTableIfNonExistent() {
86 "CREATE TABLE IF NOT EXISTS precache_urls (url TEXT PRIMARY KEY, time "
90 } // namespace precache