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
16 #include "general_controller_provider_impl.h"
17
18 #include "datashare_log.h"
19
20 namespace OHOS {
21 namespace DataShare {
22 constexpr int INVALID_VALUE = -1;
Insert(const Uri & uri,const DataShareValuesBucket & value)23 int GeneralControllerProviderImpl::Insert(const Uri &uri, const DataShareValuesBucket &value)
24 {
25 auto connection = connection_;
26 if (connection == nullptr) {
27 LOG_ERROR("connection is nullptr");
28 return INVALID_VALUE;
29 }
30 auto proxy = connection->GetDataShareProxy(uri_, token_);
31 if (proxy == nullptr) {
32 LOG_ERROR("proxy is nullptr");
33 return INVALID_VALUE;
34 }
35 return proxy->Insert(uri, value);
36 }
37
Update(const Uri & uri,const DataSharePredicates & predicates,const DataShareValuesBucket & value)38 int GeneralControllerProviderImpl::Update(const Uri &uri, const DataSharePredicates &predicates,
39 const DataShareValuesBucket &value)
40 {
41 auto connection = connection_;
42 if (connection == nullptr) {
43 LOG_ERROR("connection is nullptr");
44 return INVALID_VALUE;
45 }
46 auto proxy = connection->GetDataShareProxy(uri_, token_);
47 if (proxy == nullptr) {
48 LOG_ERROR("proxy is nullptr");
49 return INVALID_VALUE;
50 }
51 return proxy->Update(uri, predicates, value);
52 }
53
Delete(const Uri & uri,const DataSharePredicates & predicates)54 int GeneralControllerProviderImpl::Delete(const Uri &uri, const DataSharePredicates &predicates)
55 {
56 auto connection = connection_;
57 if (connection == nullptr) {
58 LOG_ERROR("connection is nullptr");
59 return INVALID_VALUE;
60 }
61 auto proxy = connection->GetDataShareProxy(uri_, token_);
62 if (proxy == nullptr) {
63 LOG_ERROR("proxy is nullptr");
64 return INVALID_VALUE;
65 }
66 return proxy->Delete(uri, predicates);
67 }
68
Query(const Uri & uri,const DataSharePredicates & predicates,std::vector<std::string> & columns,DatashareBusinessError & businessError)69 std::shared_ptr<DataShareResultSet> GeneralControllerProviderImpl::Query(const Uri &uri,
70 const DataSharePredicates &predicates, std::vector<std::string> &columns, DatashareBusinessError &businessError)
71 {
72 auto connection = connection_;
73 if (connection == nullptr) {
74 LOG_ERROR("connection is nullptr");
75 return nullptr;
76 }
77 auto proxy = connection->GetDataShareProxy(uri_, token_);
78 if (proxy == nullptr) {
79 LOG_ERROR("proxy is nullptr");
80 return nullptr;
81 }
82 return proxy->Query(uri, predicates, columns, businessError);
83 }
84
RegisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)85 void GeneralControllerProviderImpl::RegisterObserver(const Uri &uri,
86 const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
87 {
88 auto connection = connection_;
89 if (connection == nullptr) {
90 LOG_ERROR("connection is nullptr");
91 return;
92 }
93 auto proxy = connection->GetDataShareProxy(uri_, token_);
94 if (proxy == nullptr) {
95 LOG_ERROR("proxy is nullptr");
96 return;
97 }
98 proxy->RegisterObserver(uri, dataObserver);
99 }
100
UnregisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)101 void GeneralControllerProviderImpl::UnregisterObserver(const Uri &uri,
102 const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
103 {
104 auto connection = connection_;
105 if (connection == nullptr) {
106 LOG_ERROR("connection is nullptr");
107 return;
108 }
109 auto proxy = connection->GetDataShareProxy(uri_, token_);
110 if (proxy == nullptr) {
111 LOG_ERROR("proxy is nullptr");
112 return;
113 }
114 proxy->UnregisterObserver(uri, dataObserver);
115 }
116
NotifyChange(const Uri & uri)117 void GeneralControllerProviderImpl::NotifyChange(const Uri &uri)
118 {
119 auto connection = connection_;
120 if (connection == nullptr) {
121 LOG_ERROR("connection is nullptr");
122 return;
123 }
124 auto proxy = connection->GetDataShareProxy(uri_, token_);
125 if (proxy == nullptr) {
126 LOG_ERROR("proxy is nullptr");
127 return;
128 }
129 proxy->NotifyChange(uri);
130 }
131
GeneralControllerProviderImpl(std::shared_ptr<DataShareConnection> connection,const Uri & uri,const sptr<IRemoteObject> & token)132 GeneralControllerProviderImpl::GeneralControllerProviderImpl(std::shared_ptr<DataShareConnection> connection,
133 const Uri &uri, const sptr<IRemoteObject> &token) : connection_(connection), token_(token), uri_(uri)
134 {
135 }
136 } // namespace DataShare
137 } // namespace OHOS