1 /*
2 * Copyright (c) 2022 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_adaptor.h"
17
18 #include "log_print.h"
19 #include "permission_proxy.h"
20 #include "rdb_utils.h"
21 namespace OHOS::DataShare {
22 std::unique_ptr<Utils::Timer> RdbAdaptor::timer_ = nullptr;
23 ConcurrentMap<UriInfo, std::shared_ptr<RdbDelegate>> RdbAdaptor::delegates_;
24 uint32_t RdbAdaptor::timerId_ = 0;
Insert(const UriInfo & uriInfo,const DataShareValuesBucket & valuesBucket,const int32_t userId)25 int32_t RdbAdaptor::Insert(const UriInfo &uriInfo, const DataShareValuesBucket &valuesBucket, const int32_t userId)
26 {
27 auto delegate = GetDelegate(uriInfo, userId);
28 if (delegate == nullptr) {
29 ZLOGE("delegate null");
30 return -1;
31 }
32 return delegate->Insert(uriInfo.tableName, valuesBucket);
33 }
Update(const UriInfo & uriInfo,const DataSharePredicates & predicate,const DataShareValuesBucket & valuesBucket,const int32_t userId)34 int32_t RdbAdaptor::Update(const UriInfo &uriInfo, const DataSharePredicates &predicate,
35 const DataShareValuesBucket &valuesBucket, const int32_t userId)
36 {
37 auto delegate = GetDelegate(uriInfo, userId);
38 if (delegate == nullptr) {
39 ZLOGE("delegate null");
40 return -1;
41 }
42 return delegate->Update(uriInfo.tableName, predicate, valuesBucket);
43 }
Delete(const UriInfo & uriInfo,const DataSharePredicates & predicate,const int32_t userId)44 int32_t RdbAdaptor::Delete(const UriInfo &uriInfo, const DataSharePredicates &predicate, const int32_t userId)
45 {
46 auto delegate = GetDelegate(uriInfo, userId);
47 if (delegate == nullptr) {
48 ZLOGE("delegate null");
49 return -1;
50 }
51 return delegate->Delete(uriInfo.tableName, predicate);
52 }
Query(const UriInfo & uriInfo,const DataSharePredicates & predicates,const std::vector<std::string> & columns,const int32_t userId)53 std::shared_ptr<DataShareResultSet> RdbAdaptor::Query(const UriInfo &uriInfo, const DataSharePredicates &predicates,
54 const std::vector<std::string> &columns, const int32_t userId)
55 {
56 auto delegate = GetDelegate(uriInfo, userId);
57 if (delegate == nullptr) {
58 ZLOGE("delegate null");
59 return nullptr;
60 }
61 return delegate->Query(uriInfo.tableName, predicates, columns);
62 }
63
GetDelegate(const UriInfo & uriInfo,const int32_t userId)64 std::shared_ptr<RdbDelegate> RdbAdaptor::GetDelegate(const UriInfo &uriInfo, const int32_t userId)
65 {
66 std::shared_ptr<RdbDelegate> value = nullptr;
67 delegates_.Compute(uriInfo,
68 [&uriInfo, &userId, &value](const UriInfo &key, std::shared_ptr<RdbDelegate> &delegate) {
69 if (delegate != nullptr) {
70 ZLOGD("has opened, reuse");
71 value = delegate;
72 return true;
73 }
74 DistributedData::StoreMetaData metaData;
75 if (!PermissionProxy::QueryMetaData(
76 uriInfo.bundleName, uriInfo.moduleName, uriInfo.storeName, metaData, userId)) {
77 return false;
78 }
79 if (timer_ == nullptr) {
80 timer_ = std::make_unique<Utils::Timer>("DataShareSlience");
81 timer_->Setup();
82 }
83 timer_->Unregister(timerId_);
84 timerId_ = timer_->Register(AutoClose, OPEN_TIME, true);
85
86 delegate = std::make_shared<RdbDelegate>(metaData);
87 value = delegate;
88 return true;
89 });
90
91 return value;
92 }
93
AutoClose()94 void RdbAdaptor::AutoClose()
95 {
96 ZLOGD("clear");
97 delegates_.Clear();
98 }
99
RdbDelegate(const StoreMetaData & meta)100 RdbDelegate::RdbDelegate(const StoreMetaData &meta)
101 {
102 int errCode = E_OK;
103 RdbStoreConfig config(meta.dataDir);
104 config.SetCreateNecessary(false);
105 DefaultOpenCallback callback;
106 store_ = RdbHelper::GetRdbStore(config, meta.version, callback, errCode);
107 if (errCode != E_OK) {
108 ZLOGE("GetRdbStore failed %{public}d, %{public}s", errCode, meta.storeId.c_str());
109 }
110 }
111
~RdbDelegate()112 RdbDelegate::~RdbDelegate()
113 {
114 ZLOGI("destroy");
115 }
116
Insert(const std::string & tableName,const DataShareValuesBucket & valuesBucket)117 int64_t RdbDelegate::Insert(const std::string &tableName, const DataShareValuesBucket &valuesBucket)
118 {
119 if (store_ == nullptr) {
120 ZLOGE("store is null");
121 return 0;
122 }
123 int64_t rowId = 0;
124 ValuesBucket bucket = RdbDataShareAdapter::RdbUtils::ToValuesBucket(valuesBucket);
125 int ret = store_->Insert(rowId, tableName, bucket);
126 if (ret != E_OK) {
127 ZLOGE("Insert failed %{public}d", ret);
128 }
129 return rowId;
130 }
Update(const std::string & tableName,const DataSharePredicates & predicate,const DataShareValuesBucket & valuesBucket)131 int64_t RdbDelegate::Update(const std::string &tableName, const DataSharePredicates &predicate,
132 const DataShareValuesBucket &valuesBucket)
133 {
134 if (store_ == nullptr) {
135 ZLOGE("store is null");
136 return 0;
137 }
138 int rowId = 0;
139 ValuesBucket bucket = RdbDataShareAdapter::RdbUtils::ToValuesBucket(valuesBucket);
140 RdbPredicates predicates = RdbDataShareAdapter::RdbUtils::ToPredicates(predicate, tableName);
141 int ret = store_->Update(rowId, bucket, predicates);
142 if (ret != E_OK) {
143 ZLOGE("Insert failed %{public}d", ret);
144 }
145 return rowId;
146 }
Delete(const std::string & tableName,const DataSharePredicates & predicate)147 int64_t RdbDelegate::Delete(const std::string &tableName, const DataSharePredicates &predicate)
148 {
149 if (store_ == nullptr) {
150 ZLOGE("store is null");
151 return 0;
152 }
153 int rowId = 0;
154 RdbPredicates predicates = RdbDataShareAdapter::RdbUtils::ToPredicates(predicate, tableName);
155 int ret = store_->Delete(rowId, predicates);
156 if (ret != E_OK) {
157 ZLOGE("Insert failed %{public}d", ret);
158 }
159 return rowId;
160 }
Query(const std::string & tableName,const DataSharePredicates & predicates,const std::vector<std::string> & columns)161 std::shared_ptr<DataShareResultSet> RdbDelegate::Query(const std::string &tableName,
162 const DataSharePredicates &predicates, const std::vector<std::string> &columns)
163 {
164 if (store_ == nullptr) {
165 ZLOGE("store is null");
166 return nullptr;
167 }
168 RdbPredicates rdbPredicates = RdbDataShareAdapter::RdbUtils::ToPredicates(predicates, tableName);
169 std::shared_ptr<NativeRdb::ResultSet> resultSet = store_->QueryByStep(rdbPredicates, columns);
170 if (resultSet == nullptr) {
171 ZLOGE("Query failed");
172 return nullptr;
173 }
174 auto bridge = RdbDataShareAdapter::RdbUtils::ToResultSetBridge(resultSet);
175 return std::make_shared<DataShare::DataShareResultSet>(bridge);
176 }
177 } // namespace OHOS::DataShare