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
28namespace OHOS {
29namespace NativeRdb {
30class AbsRdbPredicates;
31class RdbOpenCallback;
32class RdbStoreConfig;
33class ValueObject;
34class ValuesBucket;
35} // namespace NativeRdb
36namespace Telephony {
37int 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
46int 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
56int 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
66int 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
76int 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
85int 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
94int 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
103std::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
113std::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
123int 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
132void 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
139int RdbBaseHelper::BeginTransaction()
140{
141    int ret = IsExistStore();
142    if (ret == NativeRdb::E_OK) {
143        ret = store_->BeginTransaction();
144    }
145    return ret;
146}
147
148int RdbBaseHelper::RollBack()
149{
150    int ret = IsExistStore();
151    if (ret == NativeRdb::E_OK) {
152        ret = store_->RollBack();
153    }
154    return ret;
155}
156
157int RdbBaseHelper::Commit()
158{
159    int ret = IsExistStore();
160    if (ret == NativeRdb::E_OK) {
161        ret = store_->Commit();
162    }
163    return ret;
164}
165
166int RdbBaseHelper::BatchInsert(int64_t &id, const NativeRdb::ValuesBucket &initialValues, const std::string &table)
167{
168    return NativeRdb::E_OK;
169}
170
171void 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