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