1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef TABLE_BASE_OPERATOR_H
17 #define TABLE_BASE_OPERATOR_H
18
19 #include <memory>
20 #include <string>
21
22 #include "idatabase.h"
23 #include "itable.h"
24 #include "itable_operator.h"
25 #include "sqlite_db.h"
26 #include "update_define.h"
27 #include "update_log.h"
28
29 namespace OHOS {
30 namespace UpdateEngine {
31 template <typename Table, typename T>
32 class TableBaseOperator : public ITableOperator<T> {
33 public:
34 explicit TableBaseOperator(std::shared_ptr<IDataBase> dataBase);
35 virtual ~TableBaseOperator() = default;
36 bool Insert(const T &value);
37 bool Insert(const std::vector<T> &values) final;
38 bool DeleteById(int id);
39 bool DeleteAll();
40 bool DeleteAll(int32_t &deletedRows) final;
41 bool Delete(const NativeRdb::RdbPredicates &predicates);
42 bool Delete(int32_t &deletedRows, const NativeRdb::RdbPredicates &predicates) final;
43 bool QueryAll(std::vector<T> &results) final;
44 bool Query(std::vector<T> &results, const NativeRdb::RdbPredicates &predicates) final;
45 bool Update(const NativeRdb::ValuesBucket &values, const NativeRdb::RdbPredicates &predicate);
46 bool Update(
47 int &changedRows, const NativeRdb::ValuesBucket &values, const NativeRdb::RdbPredicates &predicate) final;
48
49 protected:
50 std::string GetTableName();
51
52 private:
53 std::shared_ptr<IDataBase> dataBase_ = nullptr;
54 std::shared_ptr<ITable<T>> table_ = nullptr;
55 };
56
57 template <typename Table, typename T>
TableBaseOperator(std::shared_ptr<IDataBase> dataBase)58 TableBaseOperator<Table, T>::TableBaseOperator(std::shared_ptr<IDataBase> dataBase)
59 {
60 dataBase_ = dataBase;
61 table_ = std::make_shared<Table>();
62 }
63
64 template <typename Table, typename T>
Insert(const T & value)65 bool TableBaseOperator<Table, T>::Insert(const T &value)
66 {
67 std::vector<T> values{value};
68 return Insert(values);
69 }
70
71 template <typename Table, typename T>
Insert(const std::vector<T> & values)72 bool TableBaseOperator<Table, T>::Insert(const std::vector<T> &values)
73 {
74 std::vector<NativeRdb::ValuesBucket> dbValues;
75 ENGINE_CHECK(table_ != nullptr, return false, "TableBaseOperator Insert table is null");
76 table_->BuildDbValues(values, dbValues);
77
78 ENGINE_CHECK(dataBase_ != nullptr, return false, "TableBaseOperator Insert db is null");
79 return dataBase_->Insert(GetTableName(), dbValues);
80 }
81
82 template <typename Table, typename T>
DeleteAll()83 bool TableBaseOperator<Table, T>::DeleteAll()
84 {
85 int32_t deletedRows = 0;
86 return DeleteAll(deletedRows);
87 }
88
89 template <typename Table, typename T>
DeleteAll(int32_t & deletedRows)90 bool TableBaseOperator<Table, T>::DeleteAll(int32_t &deletedRows)
91 {
92 NativeRdb::RdbPredicates predicates(GetTableName());
93 return Delete(deletedRows, predicates);
94 }
95
96 template <typename Table, typename T>
Delete(const NativeRdb::RdbPredicates & predicates)97 bool TableBaseOperator<Table, T>::Delete(const NativeRdb::RdbPredicates &predicates)
98 {
99 int32_t deletedRows = 0;
100 return Delete(deletedRows, predicates);
101 }
102
103 template <typename Table, typename T>
Delete(int32_t & deletedRows,const NativeRdb::RdbPredicates & predicates)104 bool TableBaseOperator<Table, T>::Delete(int32_t &deletedRows, const NativeRdb::RdbPredicates &predicates)
105 {
106 ENGINE_CHECK(dataBase_ != nullptr, return false, "TableBaseOperator Delete db is null");
107 return dataBase_->Delete(deletedRows, predicates);
108 }
109
110 template <typename Table, typename T>
DeleteById(int id)111 bool TableBaseOperator<Table, T>::DeleteById(int id)
112 {
113 ENGINE_CHECK(dataBase_ != nullptr, return false, "TableBaseOperator DeleteById db is null");
114 OHOS::NativeRdb::RdbPredicates predicates(GetTableName());
115 int32_t deletedRows = 0;
116 predicates.EqualTo(COLUMN_ID, std::to_string(id));
117 return dataBase_->Delete(deletedRows, predicates);
118 }
119
120 template <typename Table, typename T>
QueryAll(std::vector<T> & results)121 bool TableBaseOperator<Table, T>::QueryAll(std::vector<T> &results)
122 {
123 OHOS::NativeRdb::RdbPredicates predicates(GetTableName());
124 return Query(results, predicates);
125 }
126
127 template <typename Table, typename T>
Query(std::vector<T> & results,const NativeRdb::RdbPredicates & predicates)128 bool TableBaseOperator<Table, T>::Query(std::vector<T> &results, const NativeRdb::RdbPredicates &predicates)
129 {
130 std::vector<std::string> columns;
131 ENGINE_CHECK(dataBase_ != nullptr, return false, "TableBaseOperator Query db is null");
132 auto resultSet = dataBase_->Query(predicates, columns);
133 ENGINE_CHECK(resultSet != nullptr, return false, "TableBaseOperator Query failed to get result");
134
135 ENGINE_CHECK(table_ != nullptr, return false, "TableBaseOperator Query table is null");
136 table_->ParseDbValues(std::move(resultSet), results);
137 return true;
138 }
139
140 template <typename Table, typename T>
Update(const NativeRdb::ValuesBucket & values,const NativeRdb::RdbPredicates & predicates)141 bool TableBaseOperator<Table, T>::Update(
142 const NativeRdb::ValuesBucket &values, const NativeRdb::RdbPredicates &predicates)
143 {
144 int changedRows = 0;
145 return Update(changedRows, values, predicates);
146 }
147
148 template <typename Table, typename T>
Update(int & changedRows,const NativeRdb::ValuesBucket & values,const NativeRdb::RdbPredicates & predicates)149 bool TableBaseOperator<Table, T>::Update(
150 int &changedRows, const NativeRdb::ValuesBucket &values, const NativeRdb::RdbPredicates &predicates)
151 {
152 ENGINE_CHECK(dataBase_ != nullptr, return false, "TableBaseOperator Update db is null");
153 return dataBase_->Update(changedRows, values, predicates);
154 }
155
156 template <typename Table, typename T>
GetTableName()157 std::string TableBaseOperator<Table, T>::GetTableName()
158 {
159 if (table_ == nullptr) {
160 ENGINE_LOGE("TableBaseOperator GetTableName table is null");
161 return "default";
162 }
163 return table_->GetTableName();
164 }
165 } // namespace UpdateEngine
166 } // namespace OHOS
167 #endif // TABLE_BASE_OPERATOR_H