OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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_session_table.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/time/time.h" |
| 11 #include "components/precache/core/proto/unfinished_work.pb.h" |
| 12 #include "sql/connection.h" |
| 13 #include "sql/statement.h" |
| 14 |
| 15 using sql::Statement; |
| 16 |
| 17 namespace precache { |
| 18 |
| 19 PrecacheSessionTable::PrecacheSessionTable() : db_(nullptr) {} |
| 20 |
| 21 PrecacheSessionTable::~PrecacheSessionTable() {} |
| 22 |
| 23 bool PrecacheSessionTable::Init(sql::Connection* db) { |
| 24 DCHECK(!db_); // Init must only be called once. |
| 25 DCHECK(db); // The database connection must be non-NULL. |
| 26 db_ = db; |
| 27 return CreateTableIfNonExistent(); |
| 28 } |
| 29 |
| 30 // Store unfinished work. |
| 31 void PrecacheSessionTable::SaveUnfinishedWork( |
| 32 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work) { |
| 33 Statement statement(db_->GetCachedStatement( |
| 34 SQL_FROM_HERE, |
| 35 "INSERT OR REPLACE INTO precache_session (type, value) VALUES(?,?)")); |
| 36 statement.BindInt(0, static_cast<int>(UNFINISHED_WORK)); |
| 37 statement.BindString(1, unfinished_work->SerializeAsString()); |
| 38 statement.Run(); |
| 39 } |
| 40 |
| 41 // Retrieve unfinished work. |
| 42 std::unique_ptr<PrecacheUnfinishedWork> |
| 43 PrecacheSessionTable::GetUnfinishedWork() { |
| 44 Statement statement(db_->GetCachedStatement( |
| 45 SQL_FROM_HERE, "SELECT value from precache_session where type=?")); |
| 46 statement.BindInt(0, static_cast<int>(UNFINISHED_WORK)); |
| 47 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work( |
| 48 new PrecacheUnfinishedWork()); |
| 49 if (statement.Step()) |
| 50 unfinished_work->ParseFromString(statement.ColumnString(0)); |
| 51 return unfinished_work; |
| 52 } |
| 53 |
| 54 |
| 55 |
| 56 void PrecacheSessionTable::DeleteUnfinishedWork() { |
| 57 Statement statement( |
| 58 db_->GetCachedStatement( |
| 59 SQL_FROM_HERE, "DELETE FROM precache_session where type=?")); |
| 60 statement.BindInt(0, static_cast<int>(UNFINISHED_WORK)); |
| 61 statement.Run(); |
| 62 } |
| 63 |
| 64 bool PrecacheSessionTable::CreateTableIfNonExistent() { |
| 65 return db_->Execute( |
| 66 "CREATE TABLE IF NOT EXISTS precache_session (type INTEGER PRIMARY KEY, " |
| 67 "value STRING)"); |
| 68 } |
| 69 |
| 70 } // namespace precache |
OLD | NEW |