alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 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 | |
David Van Cleve | a86bcb2 | 2020-01-21 17:25:44 | [diff] [blame] | 5 | #ifndef COMPONENTS_SQLITE_PROTO_KEY_VALUE_TABLE_H_ |
| 6 | #define COMPONENTS_SQLITE_PROTO_KEY_VALUE_TABLE_H_ |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 7 | |
| 8 | #include <map> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 12 | #include "sql/statement.h" |
| 13 | |
| 14 | namespace google { |
| 15 | namespace protobuf { |
| 16 | class MessageLite; |
| 17 | } |
Alex Ilin | dc57769 | 2019-05-29 09:27:29 | [diff] [blame] | 18 | } // namespace google |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 19 | |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 20 | namespace sqlite_proto { |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 21 | |
| 22 | namespace internal { |
| 23 | |
| 24 | void BindDataToStatement(const std::string& key, |
| 25 | const google::protobuf::MessageLite& data, |
| 26 | sql::Statement* statement); |
| 27 | |
| 28 | std::string GetSelectAllSql(const std::string& table_name); |
| 29 | std::string GetReplaceSql(const std::string& table_name); |
| 30 | std::string GetDeleteSql(const std::string& table_name); |
| 31 | std::string GetDeleteAllSql(const std::string& table_name); |
| 32 | |
| 33 | } // namespace internal |
| 34 | |
Alexandr Ilin | eb0f08a | 2017-05-30 10:55:34 | [diff] [blame] | 35 | // The backend class helps perform database operations on a single table. The |
| 36 | // table name is passed as a constructor argument. The table schema is fixed: it |
| 37 | // always consists of two columns, TEXT type "key" and BLOB type "proto". The |
| 38 | // class doesn't manage the creation and the deletion of the table. |
| 39 | // |
Alexandr Ilin | 9a16ee7 | 2017-08-16 13:13:34 | [diff] [blame] | 40 | // All the functions except of the constructor must be called on a DB sequence |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 41 | // of the corresponding TableManager. The preferred way to call the methods of |
| 42 | // this class is passing the method to TableManager::ScheduleDBTask(). |
Alexandr Ilin | eb0f08a | 2017-05-30 10:55:34 | [diff] [blame] | 43 | // |
| 44 | // Example: |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 45 | // manager_->ScheduleDBTask( |
Alexandr Ilin | eb0f08a | 2017-05-30 10:55:34 | [diff] [blame] | 46 | // FROM_HERE, |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 47 | // base::BindOnce(&KeyValueTable<PrefetchData>::UpdateData, |
Alexandr Ilin | eb0f08a | 2017-05-30 10:55:34 | [diff] [blame] | 48 | // base::Unretained(table_), key, data)); |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 49 | template <typename T> |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 50 | class KeyValueTable { |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 51 | public: |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 52 | explicit KeyValueTable(const std::string& table_name); |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 53 | // Virtual for testing. |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 54 | virtual ~KeyValueTable() = default; |
| 55 | |
| 56 | KeyValueTable(const KeyValueTable&) = delete; |
| 57 | KeyValueTable& operator=(const KeyValueTable&) = delete; |
| 58 | |
Alexandr Ilin | eb0f08a | 2017-05-30 10:55:34 | [diff] [blame] | 59 | virtual void GetAllData(std::map<std::string, T>* data_map, |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 60 | sql::Database* db) const; |
Alexandr Ilin | eb0f08a | 2017-05-30 10:55:34 | [diff] [blame] | 61 | virtual void UpdateData(const std::string& key, |
| 62 | const T& data, |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 63 | sql::Database* db); |
Alexandr Ilin | eb0f08a | 2017-05-30 10:55:34 | [diff] [blame] | 64 | virtual void DeleteData(const std::vector<std::string>& keys, |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 65 | sql::Database* db); |
| 66 | virtual void DeleteAllData(sql::Database* db); |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 67 | |
| 68 | private: |
| 69 | const std::string table_name_; |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | template <typename T> |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 73 | KeyValueTable<T>::KeyValueTable(const std::string& table_name) |
Alexandr Ilin | eb0f08a | 2017-05-30 10:55:34 | [diff] [blame] | 74 | : table_name_(table_name) {} |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 75 | |
| 76 | template <typename T> |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 77 | void KeyValueTable<T>::GetAllData(std::map<std::string, T>* data_map, |
| 78 | sql::Database* db) const { |
Alexandr Ilin | eb0f08a | 2017-05-30 10:55:34 | [diff] [blame] | 79 | sql::Statement reader(db->GetUniqueStatement( |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 80 | ::sqlite_proto::internal::GetSelectAllSql(table_name_).c_str())); |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 81 | while (reader.Step()) { |
| 82 | auto it = data_map->emplace(reader.ColumnString(0), T()).first; |
| 83 | int size = reader.ColumnByteLength(1); |
| 84 | const void* blob = reader.ColumnBlob(1); |
| 85 | DCHECK(blob); |
| 86 | it->second.ParseFromArray(blob, size); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | template <typename T> |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 91 | void KeyValueTable<T>::UpdateData(const std::string& key, |
| 92 | const T& data, |
| 93 | sql::Database* db) { |
Alexandr Ilin | eb0f08a | 2017-05-30 10:55:34 | [diff] [blame] | 94 | sql::Statement inserter(db->GetUniqueStatement( |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 95 | ::sqlite_proto::internal::GetReplaceSql(table_name_).c_str())); |
| 96 | ::sqlite_proto::internal::BindDataToStatement(key, data, &inserter); |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 97 | inserter.Run(); |
| 98 | } |
| 99 | |
| 100 | template <typename T> |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 101 | void KeyValueTable<T>::DeleteData(const std::vector<std::string>& keys, |
| 102 | sql::Database* db) { |
Alexandr Ilin | eb0f08a | 2017-05-30 10:55:34 | [diff] [blame] | 103 | sql::Statement deleter(db->GetUniqueStatement( |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 104 | ::sqlite_proto::internal::GetDeleteSql(table_name_).c_str())); |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 105 | for (const auto& key : keys) { |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 106 | deleter.BindString(0, key); |
| 107 | deleter.Run(); |
Alexandr Ilin | eb0f08a | 2017-05-30 10:55:34 | [diff] [blame] | 108 | deleter.Reset(true); |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | template <typename T> |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 113 | void KeyValueTable<T>::DeleteAllData(sql::Database* db) { |
Alexandr Ilin | eb0f08a | 2017-05-30 10:55:34 | [diff] [blame] | 114 | sql::Statement deleter(db->GetUniqueStatement( |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 115 | ::sqlite_proto::internal::GetDeleteAllSql(table_name_).c_str())); |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 116 | deleter.Run(); |
| 117 | } |
| 118 | |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 119 | } // namespace sqlite_proto |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 120 | |
David Van Cleve | a86bcb2 | 2020-01-21 17:25:44 | [diff] [blame] | 121 | #endif // COMPONENTS_SQLITE_PROTO_KEY_VALUE_TABLE_H_ |