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" |
(...skipping 17 matching lines...) Expand all Loading... |
28 PrecacheURLTable::~PrecacheURLTable() {} | 28 PrecacheURLTable::~PrecacheURLTable() {} |
29 | 29 |
30 bool PrecacheURLTable::Init(sql::Connection* db) { | 30 bool PrecacheURLTable::Init(sql::Connection* db) { |
31 DCHECK(!db_); // Init must only be called once. | 31 DCHECK(!db_); // Init must only be called once. |
32 DCHECK(db); // The database connection must be non-NULL. | 32 DCHECK(db); // The database connection must be non-NULL. |
33 db_ = db; | 33 db_ = db; |
34 return CreateTableIfNonExistent(); | 34 return CreateTableIfNonExistent(); |
35 } | 35 } |
36 | 36 |
37 void PrecacheURLTable::AddURL(const GURL& url, | 37 void PrecacheURLTable::AddURL(const GURL& url, |
| 38 int64_t referrer_host_id, |
| 39 bool is_precached, |
38 const base::Time& precache_time) { | 40 const base::Time& precache_time) { |
39 Statement statement(db_->GetCachedStatement( | 41 Statement statement( |
40 SQL_FROM_HERE, | 42 db_->GetCachedStatement(SQL_FROM_HERE, |
41 "INSERT OR REPLACE INTO precache_urls (url, time) VALUES(?,?)")); | 43 "INSERT OR REPLACE INTO precache_urls (url, " |
| 44 "referrer_host_id, was_used, is_precached, time) " |
| 45 "VALUES(?,?,0,?,?)")); |
42 | 46 |
43 statement.BindString(0, GetKey(url)); | 47 statement.BindString(0, GetKey(url)); |
44 statement.BindInt64(1, precache_time.ToInternalValue()); | 48 statement.BindInt64(1, referrer_host_id); |
| 49 statement.BindInt64(2, is_precached ? 1 : 0); |
| 50 statement.BindInt64(3, precache_time.ToInternalValue()); |
45 statement.Run(); | 51 statement.Run(); |
46 } | 52 } |
47 | 53 |
48 bool PrecacheURLTable::HasURL(const GURL& url) { | 54 bool PrecacheURLTable::IsURLPrecached(const GURL& url) { |
49 Statement statement(db_->GetCachedStatement( | 55 Statement statement(db_->GetCachedStatement( |
50 SQL_FROM_HERE, "SELECT time FROM precache_urls WHERE url=?")); | 56 SQL_FROM_HERE, |
| 57 "SELECT time FROM precache_urls WHERE url=? and is_precached=1")); |
51 | 58 |
52 statement.BindString(0, GetKey(url)); | 59 statement.BindString(0, GetKey(url)); |
53 return statement.Step(); | 60 return statement.Step(); |
54 } | 61 } |
55 | 62 |
56 void PrecacheURLTable::DeleteURL(const GURL& url) { | 63 bool PrecacheURLTable::IsURLPrecachedAndUnused(const GURL& url) { |
57 Statement statement(db_->GetCachedStatement( | 64 Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, |
58 SQL_FROM_HERE, "DELETE FROM precache_urls WHERE url=?")); | 65 "SELECT time FROM precache_urls " |
| 66 "WHERE url=? and is_precached=1 " |
| 67 "and was_used=0")); |
59 | 68 |
60 statement.BindString(0, GetKey(url)); | 69 statement.BindString(0, GetKey(url)); |
| 70 return statement.Step(); |
| 71 } |
| 72 |
| 73 void PrecacheURLTable::SetPrecachedURLAsUsed(const GURL& url) { |
| 74 Statement statement( |
| 75 db_->GetCachedStatement(SQL_FROM_HERE, |
| 76 "UPDATE precache_urls SET was_used=1, " |
| 77 "is_precached=0 " |
| 78 "WHERE url=? and was_used=0 and is_precached=1")); |
| 79 |
| 80 statement.BindString(0, GetKey(url)); |
| 81 statement.Run(); |
| 82 } |
| 83 |
| 84 void PrecacheURLTable::SetURLAsNotPrecached(const GURL& url) { |
| 85 Statement statement( |
| 86 db_->GetCachedStatement(SQL_FROM_HERE, |
| 87 "UPDATE precache_urls SET is_precached=0 " |
| 88 "WHERE url=? and is_precached=1")); |
| 89 statement.BindString(0, GetKey(url)); |
| 90 statement.Run(); |
| 91 } |
| 92 |
| 93 void PrecacheURLTable::GetURLListForReferrerHost( |
| 94 int64_t referrer_host_id, |
| 95 std::vector<GURL>* used_urls, |
| 96 std::vector<GURL>* unused_urls) { |
| 97 Statement statement(db_->GetCachedStatement( |
| 98 SQL_FROM_HERE, |
| 99 "SELECT url, was_used from precache_urls where referrer_host_id=?")); |
| 100 statement.BindInt64(0, referrer_host_id); |
| 101 while (statement.Step()) { |
| 102 GURL url(statement.ColumnString(0)); |
| 103 if (statement.ColumnInt(1)) |
| 104 used_urls->push_back(url); |
| 105 else |
| 106 unused_urls->push_back(url); |
| 107 } |
| 108 } |
| 109 |
| 110 void PrecacheURLTable::ClearAllForReferrerHost(int64_t referrer_host_id) { |
| 111 Statement statement(db_->GetCachedStatement( |
| 112 SQL_FROM_HERE, |
| 113 "UPDATE precache_urls SET was_used=0 WHERE referrer_host_id=?")); |
| 114 |
| 115 statement.BindInt64(0, referrer_host_id); |
61 statement.Run(); | 116 statement.Run(); |
62 } | 117 } |
63 | 118 |
64 void PrecacheURLTable::DeleteAllPrecachedBefore(const base::Time& delete_end) { | 119 void PrecacheURLTable::DeleteAllPrecachedBefore(const base::Time& delete_end) { |
65 Statement statement(db_->GetCachedStatement( | 120 Statement statement(db_->GetCachedStatement( |
66 SQL_FROM_HERE, "DELETE FROM precache_urls WHERE time < ?")); | 121 SQL_FROM_HERE, "DELETE FROM precache_urls WHERE time < ?")); |
67 | 122 |
68 statement.BindInt64(0, delete_end.ToInternalValue()); | 123 statement.BindInt64(0, delete_end.ToInternalValue()); |
69 statement.Run(); | 124 statement.Run(); |
70 } | 125 } |
71 | 126 |
72 void PrecacheURLTable::DeleteAll() { | 127 void PrecacheURLTable::DeleteAll() { |
73 Statement statement( | 128 Statement statement( |
74 db_->GetCachedStatement(SQL_FROM_HERE, "DELETE FROM precache_urls")); | 129 db_->GetCachedStatement(SQL_FROM_HERE, "DELETE FROM precache_urls")); |
75 | 130 |
76 statement.Run(); | 131 statement.Run(); |
77 } | 132 } |
78 | 133 |
79 void PrecacheURLTable::GetAllDataForTesting(std::map<GURL, base::Time>* map) { | 134 void PrecacheURLTable::GetAllDataForTesting(std::map<GURL, base::Time>* map) { |
80 map->clear(); | 135 map->clear(); |
81 | 136 |
82 Statement statement(db_->GetCachedStatement( | 137 Statement statement(db_->GetCachedStatement( |
83 SQL_FROM_HERE, "SELECT url, time FROM precache_urls")); | 138 SQL_FROM_HERE, |
| 139 "SELECT url, time FROM precache_urls where is_precached=1")); |
84 | 140 |
85 while (statement.Step()) { | 141 while (statement.Step()) { |
86 GURL url = GURL(statement.ColumnString(0)); | 142 GURL url = GURL(statement.ColumnString(0)); |
87 (*map)[url] = base::Time::FromInternalValue(statement.ColumnInt64(1)); | 143 (*map)[url] = base::Time::FromInternalValue(statement.ColumnInt64(1)); |
88 } | 144 } |
89 } | 145 } |
90 | 146 |
91 bool PrecacheURLTable::CreateTableIfNonExistent() { | 147 bool PrecacheURLTable::CreateTableIfNonExistent() { |
92 return db_->Execute( | 148 if (!db_->DoesTableExist("precache_urls")) { |
93 "CREATE TABLE IF NOT EXISTS precache_urls (url TEXT PRIMARY KEY, time " | 149 return db_->Execute( |
94 "INTEGER)"); | 150 "CREATE TABLE precache_urls " |
| 151 "(url TEXT PRIMARY KEY, referrer_host_id INTEGER, was_used INTEGER, " |
| 152 "is_precached INTEGER, " |
| 153 "time INTEGER)"); |
| 154 } else { |
| 155 // Migrate the table by creating the missing columns. |
| 156 if (!db_->DoesColumnExist("precache_urls", "was_used") && |
| 157 !db_->Execute("ALTER TABLE precache_urls ADD COLUMN was_used INTEGER")) |
| 158 return false; |
| 159 if (!db_->DoesColumnExist("precache_urls", "is_precached") && |
| 160 !db_->Execute( |
| 161 "ALTER TABLE precache_urls ADD COLUMN is_precached INTEGER")) |
| 162 return false; |
| 163 if (!db_->DoesColumnExist("precache_urls", "referrer_host_id") && |
| 164 !db_->Execute( |
| 165 "ALTER TABLE precache_urls ADD COLUMN referrer_host_id INTEGER")) |
| 166 return false; |
| 167 } |
| 168 return true; |
95 } | 169 } |
96 | 170 |
97 } // namespace precache | 171 } // namespace precache |
OLD | NEW |