Chromium Code Reviews| 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 "sql/connection.h" | |
| 12 #include "sql/statement.h" | |
| 13 | |
| 14 using sql::Statement; | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 const char kUrlTableName[] = "precache_urls"; | |
|
Scott Hess - ex-Googler
2013/11/19 18:47:48
Same comment as for stats table.
sclittle
2013/11/19 23:27:10
Done.
| |
| 19 | |
| 20 // Returns the spec of the given URL. | |
| 21 std::string GetKey(const GURL& url) { | |
| 22 return url.spec(); | |
| 23 } | |
| 24 | |
| 25 } // namespace | |
| 26 | |
| 27 namespace precache { | |
| 28 | |
| 29 PrecacheURLTable::PrecacheURLTable() : db_(NULL) {} | |
| 30 | |
| 31 PrecacheURLTable::~PrecacheURLTable() {} | |
| 32 | |
| 33 void PrecacheURLTable::Init(sql::Connection* db) { | |
| 34 DCHECK(!db_); // Init must only be called once. | |
| 35 DCHECK(db); // The database connection must be non-NULL. | |
| 36 db_ = db; | |
| 37 CreateTableIfNonExistent(); | |
| 38 } | |
| 39 | |
| 40 void PrecacheURLTable::AddURL(const GURL& url, | |
| 41 const base::Time& precache_time) { | |
| 42 DCHECK(db_); | |
|
Scott Hess - ex-Googler
2013/11/19 18:47:48
I think all of these DCHECK(db_) can be gotten rid
sclittle
2013/11/19 23:27:10
Done.
| |
| 43 | |
| 44 Statement statement(db_->GetCachedStatement( | |
| 45 SQL_FROM_HERE, | |
| 46 base::StringPrintf("INSERT OR REPLACE INTO %s (url, time) VALUES(?,?)", | |
| 47 kUrlTableName).c_str())); | |
| 48 | |
| 49 statement.BindString(0, GetKey(url)); | |
| 50 statement.BindInt64(1, precache_time.ToInternalValue()); | |
| 51 statement.Run(); | |
| 52 } | |
| 53 | |
| 54 bool PrecacheURLTable::HasURL(const GURL& url) { | |
| 55 DCHECK(db_); | |
| 56 | |
| 57 Statement statement(db_->GetCachedStatement( | |
| 58 SQL_FROM_HERE, base::StringPrintf("SELECT time FROM %s WHERE url=?", | |
| 59 kUrlTableName).c_str())); | |
| 60 | |
| 61 statement.BindString(0, GetKey(url)); | |
| 62 return statement.Step(); | |
| 63 } | |
| 64 | |
| 65 void PrecacheURLTable::DeleteURL(const GURL& url) { | |
| 66 DCHECK(db_); | |
| 67 | |
| 68 Statement statement(db_->GetCachedStatement( | |
| 69 SQL_FROM_HERE, | |
| 70 base::StringPrintf("DELETE FROM %s WHERE url=?", kUrlTableName).c_str())); | |
| 71 | |
| 72 statement.BindString(0, GetKey(url)); | |
| 73 statement.Run(); | |
| 74 } | |
| 75 | |
| 76 void PrecacheURLTable::DeleteAllPrecachedBefore(const base::Time& delete_end) { | |
| 77 DCHECK(db_); | |
| 78 | |
| 79 Statement statement(db_->GetCachedStatement( | |
| 80 SQL_FROM_HERE, base::StringPrintf("DELETE FROM %s WHERE time < ?", | |
| 81 kUrlTableName).c_str())); | |
| 82 | |
| 83 statement.BindInt64(0, delete_end.ToInternalValue()); | |
| 84 statement.Run(); | |
| 85 } | |
| 86 | |
| 87 void PrecacheURLTable::GetAllDataForTesting(std::map<GURL, base::Time>* map) { | |
| 88 DCHECK(db_); | |
| 89 DCHECK(map); // |map| must not be NULL. | |
| 90 map->clear(); | |
| 91 | |
| 92 Statement statement(db_->GetCachedStatement( | |
| 93 SQL_FROM_HERE, | |
| 94 base::StringPrintf("SELECT url, time FROM %s", kUrlTableName).c_str())); | |
| 95 | |
| 96 while (statement.Step()) { | |
| 97 GURL url = GURL(statement.ColumnString(0)); | |
| 98 (*map)[url] = base::Time::FromInternalValue(statement.ColumnInt64(1)); | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 void PrecacheURLTable::CreateTableIfNonExistent() { | |
| 103 DCHECK(db_); | |
| 104 | |
| 105 if (db_->DoesTableExist(kUrlTableName)) | |
|
Scott Hess - ex-Googler
2013/11/19 18:47:48
Same comment as for stats table.
sclittle
2013/11/19 23:27:10
Done.
| |
| 106 return; | |
| 107 | |
| 108 if (!db_->Execute(base::StringPrintf( | |
| 109 "CREATE TABLE %s (url TEXT, time INTEGER, PRIMARY KEY(url))", | |
|
Scott Hess - ex-Googler
2013/11/19 18:47:48
"url TEXT PRIMARY KEY" should work fine here.
sclittle
2013/11/19 23:27:10
Done.
| |
| 110 kUrlTableName).c_str())) { | |
| 111 DLOG(WARNING) << "Could not create precache URL table in database."; | |
|
Scott Hess - ex-Googler
2013/11/19 18:47:48
Note that the sql/ library is going to complain lo
sclittle
2013/11/19 23:27:10
Good point; if this fails in debug mode, Execute()
| |
| 112 } | |
| 113 } | |
| 114 | |
| 115 } // namespace precache | |
| OLD | NEW |