1 /*
2 * Copyright (C) 2021 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 #include "rdb_base_helper.h"
17
18 #include <regex>
19
20 #include "abs_shared_result_set.h"
21 #include "algorithm"
22 #include "data_storage_log_wrapper.h"
23 #include "new"
24 #include "rdb_errno.h"
25 #include "rdb_helper.h"
26 #include "rdb_store.h"
27 #include "type_traits"
28
29 namespace OHOS {
30 namespace NativeRdb {
31 class AbsRdbPredicates;
32 class RdbOpenCallback;
33 class RdbStoreConfig;
34 class ValueObject;
35 class ValuesBucket;
36 } // namespace NativeRdb
37 namespace Telephony {
Insert(int64_t & id,const NativeRdb::ValuesBucket & initialValues,const std::string & table)38 int RdbBaseHelper::Insert(int64_t &id, const NativeRdb::ValuesBucket &initialValues, const std::string &table)
39 {
40 int ret = IsExistStore();
41 if (ret == NativeRdb::E_OK) {
42 ret = store_->Insert(id, table, initialValues);
43 }
44 return ret;
45 }
46
Update(int & changedRows,const std::string & table,const NativeRdb::ValuesBucket & values,const std::string & whereClause,const std::vector<std::string> & whereArgs)47 int RdbBaseHelper::Update(int &changedRows, const std::string &table, const NativeRdb::ValuesBucket &values,
48 const std::string &whereClause, const std::vector<std::string> &whereArgs)
49 {
50 int ret = IsExistStore();
51 if (ret == NativeRdb::E_OK) {
52 ret = store_->Update(changedRows, table, values, whereClause, whereArgs);
53 }
54 return ret;
55 }
56
Update(int & changedRows,const NativeRdb::ValuesBucket & values,const NativeRdb::AbsRdbPredicates & predicate)57 int RdbBaseHelper::Update(int &changedRows, const NativeRdb::ValuesBucket &values,
58 const NativeRdb::AbsRdbPredicates &predicate)
59 {
60 int ret = IsExistStore();
61 if (ret == NativeRdb::E_OK) {
62 ret = store_->Update(changedRows, values, predicate);
63 }
64 return ret;
65 }
66
Delete(int & changedRows,const std::string & table,const std::string & whereClause,const std::vector<std::string> & whereArgs)67 int RdbBaseHelper::Delete(int &changedRows, const std::string &table, const std::string &whereClause,
68 const std::vector<std::string> &whereArgs)
69 {
70 int ret = IsExistStore();
71 if (ret == NativeRdb::E_OK) {
72 ret = store_->Delete(changedRows, table, whereClause, whereArgs);
73 }
74 return ret;
75 }
76
Delete(int & deletedRows,const NativeRdb::AbsRdbPredicates & predicates)77 int RdbBaseHelper::Delete(int &deletedRows, const NativeRdb::AbsRdbPredicates &predicates)
78 {
79 int ret = IsExistStore();
80 if (ret == NativeRdb::E_OK) {
81 ret = store_->Delete(deletedRows, predicates);
82 }
83 return ret;
84 }
85
ExecuteSql(const std::string & sql)86 int RdbBaseHelper::ExecuteSql(const std::string &sql)
87 {
88 int ret = IsExistStore();
89 if (ret == NativeRdb::E_OK) {
90 ret = store_->ExecuteSql(sql);
91 }
92 return ret;
93 }
94
ExecuteSql(const std::string & sql,const std::vector<NativeRdb::ValueObject> & bindArgs)95 int RdbBaseHelper::ExecuteSql(const std::string &sql, const std::vector<NativeRdb::ValueObject> &bindArgs)
96 {
97 int ret = IsExistStore();
98 if (ret == NativeRdb::E_OK) {
99 ret = store_->ExecuteSql(sql, bindArgs);
100 }
101 return ret;
102 }
103
QuerySql(const std::string & sql,const std::vector<std::string> & selectionArgs)104 std::unique_ptr<NativeRdb::AbsSharedResultSet> RdbBaseHelper::QuerySql(
105 const std::string &sql, const std::vector<std::string> &selectionArgs)
106 {
107 int ret = IsExistStore();
108 std::unique_ptr<NativeRdb::AbsSharedResultSet> resultSet;
109 if (ret == NativeRdb::E_OK) {
110 resultSet = store_->QuerySql(sql);
111 }
112 return resultSet;
113 }
114
Query(const NativeRdb::AbsRdbPredicates & predicates,const std::vector<std::string> columns)115 std::unique_ptr<NativeRdb::AbsSharedResultSet> RdbBaseHelper::Query(
116 const NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> columns)
117 {
118 int ret = IsExistStore();
119 std::unique_ptr<NativeRdb::AbsSharedResultSet> resultSet;
120 if (ret == NativeRdb::E_OK) {
121 resultSet = store_->Query(predicates, columns);
122 }
123 return resultSet;
124 }
125
IsExistStore()126 int RdbBaseHelper::IsExistStore()
127 {
128 if (store_ == nullptr) {
129 DATA_STORAGE_LOGE("RdbBaseHelper::IsExistStore NativeRdb::RdbStore is null!");
130 return NativeRdb::E_ERROR;
131 }
132 return NativeRdb::E_OK;
133 }
134
CreateRdbStore(const NativeRdb::RdbStoreConfig & config,int version,NativeRdb::RdbOpenCallback & openCallback,int & errCode)135 void RdbBaseHelper::CreateRdbStore(
136 const NativeRdb::RdbStoreConfig &config, int version, NativeRdb::RdbOpenCallback &openCallback, int &errCode)
137 {
138 DATA_STORAGE_LOGI("RdbBaseHelper::CreateRdbStore");
139 store_ = NativeRdb::RdbHelper::GetRdbStore(config, version, openCallback, errCode);
140 }
141
BeginTransaction()142 int RdbBaseHelper::BeginTransaction()
143 {
144 int ret = IsExistStore();
145 if (ret == NativeRdb::E_OK) {
146 ret = store_->BeginTransaction();
147 }
148 return ret;
149 }
150
RollBack()151 int RdbBaseHelper::RollBack()
152 {
153 int ret = IsExistStore();
154 if (ret == NativeRdb::E_OK) {
155 ret = store_->RollBack();
156 }
157 return ret;
158 }
159
MarkAsCommit()160 int RdbBaseHelper::MarkAsCommit()
161 {
162 int ret = IsExistStore();
163 if (ret == NativeRdb::E_OK) {
164 ret = store_->MarkAsCommit();
165 }
166 return ret;
167 }
168
Commit()169 int RdbBaseHelper::Commit()
170 {
171 int ret = IsExistStore();
172 if (ret == NativeRdb::E_OK) {
173 ret = store_->Commit();
174 }
175 return ret;
176 }
177
EndTransaction()178 int RdbBaseHelper::EndTransaction()
179 {
180 int ret = IsExistStore();
181 if (ret == NativeRdb::E_OK) {
182 ret = store_->EndTransaction();
183 }
184 return ret;
185 }
186
BatchInsert(int64_t & id,const NativeRdb::ValuesBucket & initialValues,const std::string & table)187 int RdbBaseHelper::BatchInsert(int64_t &id, const NativeRdb::ValuesBucket &initialValues, const std::string &table)
188 {
189 return NativeRdb::E_OK;
190 }
191
ReplaceAllStr(std::string & path,const std::string & oldStr,const std::string & newStr)192 void RdbBaseHelper::ReplaceAllStr(std::string &path, const std::string &oldStr, const std::string &newStr)
193 {
194 path = std::regex_replace(path, std::regex(oldStr), newStr);
195 }
196 } // namespace Telephony
197 } // namespace OHOS
198