OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 #ifndef CHROME_BROWSER_PREDICTORS_GLOWPLUG_KEY_VALUE_TABLE_H_ |
| 6 #define CHROME_BROWSER_PREDICTORS_GLOWPLUG_KEY_VALUE_TABLE_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <string> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/strings/stringprintf.h" |
| 13 #include "sql/statement.h" |
| 14 |
| 15 namespace google { |
| 16 namespace protobuf { |
| 17 class MessageLite; |
| 18 } |
| 19 } |
| 20 |
| 21 namespace predictors { |
| 22 |
| 23 namespace internal { |
| 24 |
| 25 void BindDataToStatement(const std::string& key, |
| 26 const google::protobuf::MessageLite& data, |
| 27 sql::Statement* statement); |
| 28 |
| 29 std::string GetSelectAllSql(const std::string& table_name); |
| 30 std::string GetReplaceSql(const std::string& table_name); |
| 31 std::string GetDeleteSql(const std::string& table_name); |
| 32 std::string GetDeleteAllSql(const std::string& table_name); |
| 33 |
| 34 } // namespace internal |
| 35 |
| 36 template <typename T> |
| 37 class GlowplugKeyValueTable { |
| 38 public: |
| 39 GlowplugKeyValueTable(const std::string& table_name, sql::Connection* db); |
| 40 // Virtual for testing. |
| 41 virtual void GetAllData(std::map<std::string, T>* data_map) const; |
| 42 virtual void UpdateData(const std::string& key, const T& data); |
| 43 virtual void DeleteData(const std::vector<std::string>& keys); |
| 44 virtual void DeleteAllData(); |
| 45 |
| 46 private: |
| 47 const std::string table_name_; |
| 48 sql::Connection* db_; |
| 49 |
| 50 DISALLOW_COPY_AND_ASSIGN(GlowplugKeyValueTable); |
| 51 }; |
| 52 |
| 53 template <typename T> |
| 54 GlowplugKeyValueTable<T>::GlowplugKeyValueTable(const std::string& table_name, |
| 55 sql::Connection* db) |
| 56 : table_name_(table_name), db_(db) {} |
| 57 |
| 58 template <typename T> |
| 59 void GlowplugKeyValueTable<T>::GetAllData( |
| 60 std::map<std::string, T>* data_map) const { |
| 61 sql::Statement reader(db_->GetUniqueStatement( |
| 62 ::predictors::internal::GetSelectAllSql(table_name_).c_str())); |
| 63 while (reader.Step()) { |
| 64 auto it = data_map->emplace(reader.ColumnString(0), T()).first; |
| 65 int size = reader.ColumnByteLength(1); |
| 66 const void* blob = reader.ColumnBlob(1); |
| 67 DCHECK(blob); |
| 68 it->second.ParseFromArray(blob, size); |
| 69 } |
| 70 } |
| 71 |
| 72 template <typename T> |
| 73 void GlowplugKeyValueTable<T>::UpdateData(const std::string& key, |
| 74 const T& data) { |
| 75 sql::Statement inserter(db_->GetUniqueStatement( |
| 76 ::predictors::internal::GetReplaceSql(table_name_).c_str())); |
| 77 ::predictors::internal::BindDataToStatement(key, data, &inserter); |
| 78 inserter.Run(); |
| 79 } |
| 80 |
| 81 template <typename T> |
| 82 void GlowplugKeyValueTable<T>::DeleteData( |
| 83 const std::vector<std::string>& keys) { |
| 84 auto statement = db_->GetUniqueStatement( |
| 85 ::predictors::internal::GetDeleteSql(table_name_).c_str()); |
| 86 for (const auto& key : keys) { |
| 87 sql::Statement deleter(statement); |
| 88 deleter.BindString(0, key); |
| 89 deleter.Run(); |
| 90 } |
| 91 } |
| 92 |
| 93 template <typename T> |
| 94 void GlowplugKeyValueTable<T>::DeleteAllData() { |
| 95 sql::Statement deleter(db_->GetUniqueStatement( |
| 96 ::predictors::internal::GetDeleteAllSql(table_name_).c_str())); |
| 97 deleter.Run(); |
| 98 } |
| 99 |
| 100 } // namespace predictors |
| 101 |
| 102 #endif // CHROME_BROWSER_PREDICTORS_GLOWPLUG_KEY_VALUE_TABLE_H_ |
OLD | NEW |