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