• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::shared_ptr<NativeRdb::ResultSet> RdbBaseHelper::QuerySql(
105     const std::string &sql, const std::vector<std::string> &selectionArgs)
106 {
107     int ret = IsExistStore();
108     if (ret == NativeRdb::E_OK) {
109         return store_->QuerySql(sql);
110     }
111     return nullptr;
112 }
113 
Query(const NativeRdb::AbsRdbPredicates & predicates,const std::vector<std::string> columns)114 std::shared_ptr<NativeRdb::ResultSet> RdbBaseHelper::Query(
115     const NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> columns)
116 {
117     int ret = IsExistStore();
118     if (ret == NativeRdb::E_OK) {
119         return store_->Query(predicates, columns);
120     }
121     return nullptr;
122 }
123 
IsExistStore()124 int RdbBaseHelper::IsExistStore()
125 {
126     if (store_ == nullptr) {
127         DATA_STORAGE_LOGE("RdbBaseHelper::IsExistStore NativeRdb::RdbStore is null!");
128         return NativeRdb::E_ERROR;
129     }
130     return NativeRdb::E_OK;
131 }
132 
CreateRdbStore(const NativeRdb::RdbStoreConfig & config,int version,NativeRdb::RdbOpenCallback & openCallback,int & errCode)133 void RdbBaseHelper::CreateRdbStore(
134     const NativeRdb::RdbStoreConfig &config, int version, NativeRdb::RdbOpenCallback &openCallback, int &errCode)
135 {
136     DATA_STORAGE_LOGI("RdbBaseHelper::CreateRdbStore");
137     store_ = NativeRdb::RdbHelper::GetRdbStore(config, version, openCallback, errCode);
138 }
139 
BeginTransaction()140 int RdbBaseHelper::BeginTransaction()
141 {
142     int ret = IsExistStore();
143     if (ret == NativeRdb::E_OK) {
144         ret = store_->BeginTransaction();
145     }
146     return ret;
147 }
148 
RollBack()149 int RdbBaseHelper::RollBack()
150 {
151     int ret = IsExistStore();
152     if (ret == NativeRdb::E_OK) {
153         ret = store_->RollBack();
154     }
155     return ret;
156 }
157 
Commit()158 int RdbBaseHelper::Commit()
159 {
160     int ret = IsExistStore();
161     if (ret == NativeRdb::E_OK) {
162         ret = store_->Commit();
163     }
164     return ret;
165 }
166 
BatchInsert(int64_t & id,const NativeRdb::ValuesBucket & initialValues,const std::string & table)167 int RdbBaseHelper::BatchInsert(int64_t &id, const NativeRdb::ValuesBucket &initialValues, const std::string &table)
168 {
169     return NativeRdb::E_OK;
170 }
171 
ReplaceAllStr(std::string & path,const std::string & oldStr,const std::string & newStr)172 void RdbBaseHelper::ReplaceAllStr(std::string &path, const std::string &oldStr, const std::string &newStr)
173 {
174     path = std::regex_replace(path, std::regex(oldStr), newStr);
175 }
176 } // namespace Telephony
177 } // namespace OHOS
178