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