OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/precache/core/precache_url_table.h" | 5 #include "components/precache/core/precache_url_table.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "sql/connection.h" | 10 #include "sql/connection.h" |
11 #include "sql/statement.h" | 11 #include "sql/statement.h" |
12 | 12 |
13 using sql::Statement; | 13 using sql::Statement; |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 // Returns the spec of the given URL. | 17 // Returns the spec of the given URL. |
18 std::string GetKey(const GURL& url) { | 18 std::string GetKey(const GURL& url) { |
19 return url.spec(); | 19 return url.spec(); |
20 } | 20 } |
21 | 21 |
22 } // namespace | 22 } // namespace |
23 | 23 |
24 namespace precache { | 24 namespace precache { |
25 | 25 |
| 26 bool PrecacheURLInfo::operator==(const PrecacheURLInfo& other) const { |
| 27 return was_precached == other.was_precached && |
| 28 is_precached == other.is_precached && was_used == other.was_used; |
| 29 } |
| 30 |
26 PrecacheURLTable::PrecacheURLTable() : db_(NULL) {} | 31 PrecacheURLTable::PrecacheURLTable() : db_(NULL) {} |
27 | 32 |
28 PrecacheURLTable::~PrecacheURLTable() {} | 33 PrecacheURLTable::~PrecacheURLTable() {} |
29 | 34 |
30 bool PrecacheURLTable::Init(sql::Connection* db) { | 35 bool PrecacheURLTable::Init(sql::Connection* db) { |
31 DCHECK(!db_); // Init must only be called once. | 36 DCHECK(!db_); // Init must only be called once. |
32 DCHECK(db); // The database connection must be non-NULL. | 37 DCHECK(db); // The database connection must be non-NULL. |
33 db_ = db; | 38 db_ = db; |
34 return CreateTableIfNonExistent(); | 39 return CreateTableIfNonExistent(); |
35 } | 40 } |
36 | 41 |
37 void PrecacheURLTable::AddURL(const GURL& url, | 42 void PrecacheURLTable::AddURL(const GURL& url, |
38 int64_t referrer_host_id, | 43 int64_t referrer_host_id, |
39 bool is_precached, | 44 bool is_precached, |
40 const base::Time& precache_time) { | 45 const base::Time& precache_time) { |
41 Statement statement( | 46 Statement statement( |
42 db_->GetCachedStatement(SQL_FROM_HERE, | 47 db_->GetCachedStatement(SQL_FROM_HERE, |
43 "INSERT OR REPLACE INTO precache_urls (url, " | 48 "INSERT OR REPLACE INTO precache_urls (url, " |
44 "referrer_host_id, was_used, is_precached, time) " | 49 "referrer_host_id, was_used, is_precached, time) " |
45 "VALUES(?,?,0,?,?)")); | 50 "VALUES(?,?,0,?,?)")); |
46 | 51 |
47 statement.BindString(0, GetKey(url)); | 52 statement.BindString(0, GetKey(url)); |
48 statement.BindInt64(1, referrer_host_id); | 53 statement.BindInt64(1, referrer_host_id); |
49 statement.BindInt64(2, is_precached ? 1 : 0); | 54 statement.BindInt64(2, is_precached ? 1 : 0); |
50 statement.BindInt64(3, precache_time.ToInternalValue()); | 55 statement.BindInt64(3, precache_time.ToInternalValue()); |
51 statement.Run(); | 56 statement.Run(); |
52 } | 57 } |
53 | 58 |
54 bool PrecacheURLTable::IsURLPrecached(const GURL& url) { | 59 PrecacheURLInfo PrecacheURLTable::GetURLInfo(const GURL& url) { |
55 Statement statement(db_->GetCachedStatement( | 60 Statement statement(db_->GetCachedStatement( |
56 SQL_FROM_HERE, | 61 SQL_FROM_HERE, |
57 "SELECT time FROM precache_urls WHERE url=? and is_precached=1")); | 62 "SELECT is_precached, was_used FROM precache_urls WHERE url=?")); |
| 63 statement.BindString(0, GetKey(url)); |
58 | 64 |
59 statement.BindString(0, GetKey(url)); | 65 if (statement.Step()) { |
60 return statement.Step(); | 66 return {/*present=*/true, /*is_precached=*/statement.ColumnBool(0), |
61 } | 67 /*was_used==*/statement.ColumnBool(1)}; |
62 | 68 } else { |
63 bool PrecacheURLTable::IsURLPrecachedAndUnused(const GURL& url) { | 69 return {/*present=*/false, /*is_precached=*/false, /*was_used=*/false}; |
64 Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, | 70 } |
65 "SELECT time FROM precache_urls " | |
66 "WHERE url=? and is_precached=1 " | |
67 "and was_used=0")); | |
68 | |
69 statement.BindString(0, GetKey(url)); | |
70 return statement.Step(); | |
71 } | 71 } |
72 | 72 |
73 void PrecacheURLTable::SetPrecachedURLAsUsed(const GURL& url) { | 73 void PrecacheURLTable::SetPrecachedURLAsUsed(const GURL& url) { |
74 Statement statement( | 74 Statement statement( |
75 db_->GetCachedStatement(SQL_FROM_HERE, | 75 db_->GetCachedStatement(SQL_FROM_HERE, |
76 "UPDATE precache_urls SET was_used=1, " | 76 "UPDATE precache_urls SET was_used=1, " |
77 "is_precached=0 " | 77 "is_precached=0 " |
78 "WHERE url=? and was_used=0 and is_precached=1")); | 78 "WHERE url=? and was_used=0 and is_precached=1")); |
79 | 79 |
80 statement.BindString(0, GetKey(url)); | 80 statement.BindString(0, GetKey(url)); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 return false; | 171 return false; |
172 if (!db_->DoesColumnExist("precache_urls", "referrer_host_id") && | 172 if (!db_->DoesColumnExist("precache_urls", "referrer_host_id") && |
173 !db_->Execute( | 173 !db_->Execute( |
174 "ALTER TABLE precache_urls ADD COLUMN referrer_host_id INTEGER")) | 174 "ALTER TABLE precache_urls ADD COLUMN referrer_host_id INTEGER")) |
175 return false; | 175 return false; |
176 } | 176 } |
177 return true; | 177 return true; |
178 } | 178 } |
179 | 179 |
180 } // namespace precache | 180 } // namespace precache |
OLD | NEW |