Chromium Code Reviews| Index: components/precache/core/precache_url_table.cc |
| diff --git a/components/precache/core/precache_url_table.cc b/components/precache/core/precache_url_table.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3a6b409d51db70d23bc0e20d734875bbea0a05ea |
| --- /dev/null |
| +++ b/components/precache/core/precache_url_table.cc |
| @@ -0,0 +1,115 @@ |
| +// 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 "base/strings/stringprintf.h" |
| +#include "sql/connection.h" |
| +#include "sql/statement.h" |
| + |
| +using sql::Statement; |
| + |
| +namespace { |
| + |
| +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.
|
| + |
| +// 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() {} |
| + |
| +void PrecacheURLTable::Init(sql::Connection* db) { |
| + DCHECK(!db_); // Init must only be called once. |
| + DCHECK(db); // The database connection must be non-NULL. |
| + db_ = db; |
| + CreateTableIfNonExistent(); |
| +} |
| + |
| +void PrecacheURLTable::AddURL(const GURL& url, |
| + const base::Time& precache_time) { |
| + 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.
|
| + |
| + Statement statement(db_->GetCachedStatement( |
| + SQL_FROM_HERE, |
| + base::StringPrintf("INSERT OR REPLACE INTO %s (url, time) VALUES(?,?)", |
| + kUrlTableName).c_str())); |
| + |
| + statement.BindString(0, GetKey(url)); |
| + statement.BindInt64(1, precache_time.ToInternalValue()); |
| + statement.Run(); |
| +} |
| + |
| +bool PrecacheURLTable::HasURL(const GURL& url) { |
| + DCHECK(db_); |
| + |
| + Statement statement(db_->GetCachedStatement( |
| + SQL_FROM_HERE, base::StringPrintf("SELECT time FROM %s WHERE url=?", |
| + kUrlTableName).c_str())); |
| + |
| + statement.BindString(0, GetKey(url)); |
| + return statement.Step(); |
| +} |
| + |
| +void PrecacheURLTable::DeleteURL(const GURL& url) { |
| + DCHECK(db_); |
| + |
| + Statement statement(db_->GetCachedStatement( |
| + SQL_FROM_HERE, |
| + base::StringPrintf("DELETE FROM %s WHERE url=?", kUrlTableName).c_str())); |
| + |
| + statement.BindString(0, GetKey(url)); |
| + statement.Run(); |
| +} |
| + |
| +void PrecacheURLTable::DeleteAllPrecachedBefore(const base::Time& delete_end) { |
| + DCHECK(db_); |
| + |
| + Statement statement(db_->GetCachedStatement( |
| + SQL_FROM_HERE, base::StringPrintf("DELETE FROM %s WHERE time < ?", |
| + kUrlTableName).c_str())); |
| + |
| + statement.BindInt64(0, delete_end.ToInternalValue()); |
| + statement.Run(); |
| +} |
| + |
| +void PrecacheURLTable::GetAllDataForTesting(std::map<GURL, base::Time>* map) { |
| + DCHECK(db_); |
| + DCHECK(map); // |map| must not be NULL. |
| + map->clear(); |
| + |
| + Statement statement(db_->GetCachedStatement( |
| + SQL_FROM_HERE, |
| + base::StringPrintf("SELECT url, time FROM %s", kUrlTableName).c_str())); |
| + |
| + while (statement.Step()) { |
| + GURL url = GURL(statement.ColumnString(0)); |
| + (*map)[url] = base::Time::FromInternalValue(statement.ColumnInt64(1)); |
| + } |
| +} |
| + |
| +void PrecacheURLTable::CreateTableIfNonExistent() { |
| + DCHECK(db_); |
| + |
| + 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.
|
| + return; |
| + |
| + if (!db_->Execute(base::StringPrintf( |
| + "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.
|
| + kUrlTableName).c_str())) { |
| + 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()
|
| + } |
| +} |
| + |
| +} // namespace precache |