1 /*
2 * Copyright (c) 2023 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 #define LOG_TAG "RdbAdaptor"
16 #include "rdb_delegate.h"
17
18 #include "resultset_json_formatter.h"
19 #include "log_print.h"
20 #include "rdb_utils.h"
21 #include "scheduler_manager.h"
22 #include "utils/anonymous.h"
23
24 namespace OHOS::DataShare {
25 constexpr static int32_t MAX_RESULTSET_COUNT = 16;
26 std::atomic<int32_t> RdbDelegate::resultSetCount = 0;
27 enum REMIND_TIMER_ARGS : int32_t {
28 ARG_DB_PATH = 0,
29 ARG_VERSION,
30 ARG_URI,
31 ARG_SUBSCRIBER_ID,
32 ARG_BUNDLE_NAME,
33 ARG_USER_ID,
34 ARG_TIME,
35 ARGS_SIZE
36 };
RemindTimerFunc(const std::vector<std::string> & args)37 std::string RemindTimerFunc(const std::vector<std::string> &args)
38 {
39 size_t size = args.size();
40 if (size != ARGS_SIZE) {
41 ZLOGE("RemindTimerFunc args size error, %{public}zu", size);
42 return "";
43 }
44 std::string dbPath = args[ARG_DB_PATH];
45 int version = std::strtol(args[ARG_VERSION].c_str(), nullptr, 0);
46 Key key(args[ARG_URI], std::strtoll(args[ARG_SUBSCRIBER_ID].c_str(), nullptr, 0), args[ARG_BUNDLE_NAME]);
47 int64_t reminderTime = std::strtoll(args[ARG_TIME].c_str(), nullptr, 0);
48 int32_t userId = std::strtol(args[ARG_USER_ID].c_str(), nullptr, 0);
49 SchedulerManager::GetInstance().SetTimer(dbPath, userId, version, key, reminderTime);
50 return args[ARG_TIME];
51 }
52
RdbDelegate(const std::string & dir,int version,bool registerFunction)53 RdbDelegate::RdbDelegate(const std::string &dir, int version, bool registerFunction)
54 {
55 RdbStoreConfig config(dir);
56 config.SetCreateNecessary(false);
57 if (registerFunction) {
58 config.SetScalarFunction("remindTimer", ARGS_SIZE, RemindTimerFunc);
59 }
60 DefaultOpenCallback callback;
61 store_ = RdbHelper::GetRdbStore(config, version, callback, errCode_);
62 if (errCode_ != E_OK) {
63 ZLOGW("GetRdbStore failed, errCode is %{public}d, dir is %{public}s", errCode_,
64 DistributedData::Anonymous::Change(dir).c_str());
65 }
66 }
67
Insert(const std::string & tableName,const DataShareValuesBucket & valuesBucket)68 int64_t RdbDelegate::Insert(const std::string &tableName, const DataShareValuesBucket &valuesBucket)
69 {
70 if (store_ == nullptr) {
71 ZLOGE("store is null");
72 return 0;
73 }
74 int64_t rowId = 0;
75 ValuesBucket bucket = RdbDataShareAdapter::RdbUtils::ToValuesBucket(valuesBucket);
76 int ret = store_->Insert(rowId, tableName, bucket);
77 if (ret != E_OK) {
78 ZLOGE("Insert failed %{public}s %{public}d", tableName.c_str(), ret);
79 }
80 return rowId;
81 }
Update(const std::string & tableName,const DataSharePredicates & predicate,const DataShareValuesBucket & valuesBucket)82 int64_t RdbDelegate::Update(
83 const std::string &tableName, const DataSharePredicates &predicate, const DataShareValuesBucket &valuesBucket)
84 {
85 if (store_ == nullptr) {
86 ZLOGE("store is null");
87 return 0;
88 }
89 int changeCount = 0;
90 ValuesBucket bucket = RdbDataShareAdapter::RdbUtils::ToValuesBucket(valuesBucket);
91 RdbPredicates predicates = RdbDataShareAdapter::RdbUtils::ToPredicates(predicate, tableName);
92 int ret = store_->Update(changeCount, bucket, predicates);
93 if (ret != E_OK) {
94 ZLOGE("Update failed %{public}s %{public}d", tableName.c_str(), ret);
95 }
96 return changeCount;
97 }
Delete(const std::string & tableName,const DataSharePredicates & predicate)98 int64_t RdbDelegate::Delete(const std::string &tableName, const DataSharePredicates &predicate)
99 {
100 if (store_ == nullptr) {
101 ZLOGE("store is null");
102 return 0;
103 }
104 int changeCount = 0;
105 RdbPredicates predicates = RdbDataShareAdapter::RdbUtils::ToPredicates(predicate, tableName);
106 int ret = store_->Delete(changeCount, predicates);
107 if (ret != E_OK) {
108 ZLOGE("Delete failed %{public}s %{public}d", tableName.c_str(), ret);
109 }
110 return changeCount;
111 }
Query(const std::string & tableName,const DataSharePredicates & predicates,const std::vector<std::string> & columns,int & errCode)112 std::shared_ptr<DataShareResultSet> RdbDelegate::Query(const std::string &tableName,
113 const DataSharePredicates &predicates, const std::vector<std::string> &columns, int &errCode)
114 {
115 if (store_ == nullptr) {
116 ZLOGE("store is null");
117 errCode = errCode_;
118 return nullptr;
119 }
120 int count = resultSetCount.fetch_add(1);
121 ZLOGD("start query %{public}d", count);
122 if (count > MAX_RESULTSET_COUNT) {
123 ZLOGE("resultSetCount is full");
124 resultSetCount--;
125 return nullptr;
126 }
127 RdbPredicates rdbPredicates = RdbDataShareAdapter::RdbUtils::ToPredicates(predicates, tableName);
128 std::shared_ptr<NativeRdb::ResultSet> resultSet = store_->QueryByStep(rdbPredicates, columns);
129 if (resultSet == nullptr) {
130 ZLOGE("Query failed %{public}s", tableName.c_str());
131 resultSetCount--;
132 return nullptr;
133 }
134 auto bridge = RdbDataShareAdapter::RdbUtils::ToResultSetBridge(resultSet);
135 return std::shared_ptr<DataShareResultSet>(new DataShareResultSet(bridge), [](auto p) {
136 ZLOGD("release resultset");
137 resultSetCount--;
138 delete p;
139 });
140 }
141
Query(const std::string & sql,const std::vector<std::string> & selectionArgs)142 std::string RdbDelegate::Query(const std::string &sql, const std::vector<std::string> &selectionArgs)
143 {
144 if (store_ == nullptr) {
145 ZLOGE("store is null");
146 return "";
147 }
148 auto resultSet = store_->QueryByStep(sql, selectionArgs);
149 if (resultSet == nullptr) {
150 ZLOGE("Query failed %{private}s", sql.c_str());
151 return "";
152 }
153 ResultSetJsonFormatter formatter(std::move(resultSet));
154 return DistributedData::Serializable::Marshall(formatter);
155 }
156
QuerySql(const std::string & sql)157 std::shared_ptr<NativeRdb::ResultSet> RdbDelegate::QuerySql(const std::string &sql)
158 {
159 if (store_ == nullptr) {
160 ZLOGE("store is null");
161 return nullptr;
162 }
163 return store_->QuerySql(sql);
164 }
165 } // namespace OHOS::DataShare