• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "data_share_service_proxy.h"
17 
18 #include "datashare_log.h"
19 #include "ishared_result_set.h"
20 #include "itypes_utils.h"
21 
22 namespace OHOS::DataShare {
DataShareServiceProxy(const sptr<IRemoteObject> & object)23 DataShareServiceProxy::DataShareServiceProxy(const sptr<IRemoteObject> &object)
24     : IRemoteProxy<IDataShareService>(object)
25 {
26     LOG_INFO("Construct complete.");
27 }
28 
Insert(const std::string & uri,const DataShareValuesBucket & valuesBucket)29 int32_t DataShareServiceProxy::Insert(const std::string &uri, const DataShareValuesBucket &valuesBucket)
30 {
31     MessageParcel data;
32     if (!data.WriteInterfaceToken(IDataShareService::GetDescriptor())) {
33         LOG_ERROR("Write descriptor failed!");
34         return DATA_SHARE_ERROR;
35     }
36     if (!ITypesUtils::Marshal(data, uri, valuesBucket)) {
37         LOG_ERROR("Write to message parcel failed!");
38         return DATA_SHARE_ERROR;
39     }
40 
41     MessageParcel reply;
42     MessageOption option;
43     int32_t err = Remote()->SendRequest(DATA_SHARE_SERVICE_CMD_INSERT, data, reply, option);
44     if (err != NO_ERROR) {
45         LOG_ERROR("Insert fail to SendRequest. uri: %{public}s, err: %{public}d", uri.c_str(), err);
46         return DATA_SHARE_ERROR;
47     }
48     return reply.ReadInt32();
49 }
50 
Update(const std::string & uri,const DataSharePredicates & predicate,const DataShareValuesBucket & valuesBucket)51 int32_t DataShareServiceProxy::Update(
52     const std::string &uri, const DataSharePredicates &predicate, const DataShareValuesBucket &valuesBucket)
53 {
54     MessageParcel data;
55     if (!data.WriteInterfaceToken(IDataShareService::GetDescriptor())) {
56         LOG_ERROR("Write descriptor failed!");
57         return DATA_SHARE_ERROR;
58     }
59     if (!ITypesUtils::Marshal(data, uri, predicate, valuesBucket)) {
60         LOG_ERROR("Write to message parcel failed!");
61         return DATA_SHARE_ERROR;
62     }
63 
64     MessageParcel reply;
65     MessageOption option;
66     int32_t err = Remote()->SendRequest(DATA_SHARE_SERVICE_CMD_UPDATE, data, reply, option);
67     if (err != NO_ERROR) {
68         LOG_ERROR("Update fail to SendRequest. uri: %{public}s, err: %{public}d", uri.c_str(), err);
69         return DATA_SHARE_ERROR;
70     }
71     return reply.ReadInt32();
72 }
73 
Delete(const std::string & uri,const DataSharePredicates & predicate)74 int32_t DataShareServiceProxy::Delete(const std::string &uri, const DataSharePredicates &predicate)
75 {
76     MessageParcel data;
77     if (!data.WriteInterfaceToken(IDataShareService::GetDescriptor())) {
78         LOG_ERROR("Write descriptor failed!");
79         return DATA_SHARE_ERROR;
80     }
81     if (!ITypesUtils::Marshal(data, uri, predicate)) {
82         LOG_ERROR("Write to message parcel failed!");
83         return DATA_SHARE_ERROR;
84     }
85 
86     MessageParcel reply;
87     MessageOption option;
88     int32_t err = Remote()->SendRequest(DATA_SHARE_SERVICE_CMD_DELETE, data, reply, option);
89     if (err != NO_ERROR) {
90         LOG_ERROR("Delete fail to SendRequest. uri: %{public}s, err: %{public}d", uri.c_str(), err);
91         return DATA_SHARE_ERROR;
92     }
93     return reply.ReadInt32();
94 }
95 
Query(const std::string & uri,const DataSharePredicates & predicates,const std::vector<std::string> & columns)96 std::shared_ptr<DataShareResultSet> DataShareServiceProxy::Query(
97     const std::string &uri, const DataSharePredicates &predicates, const std::vector<std::string> &columns)
98 {
99     MessageParcel data;
100     if (!data.WriteInterfaceToken(IDataShareService::GetDescriptor())) {
101         LOG_ERROR("WriteInterfaceToken failed!");
102         return nullptr;
103     }
104 
105     if (!ITypesUtils::Marshal(data, uri, predicates, columns)) {
106         LOG_ERROR("Write to message parcel failed!");
107         return nullptr;
108     }
109 
110     MessageParcel reply;
111     MessageOption option;
112     int32_t err = Remote()->SendRequest(DATA_SHARE_SERVICE_CMD_QUERY, data, reply, option);
113     if (err != NO_ERROR) {
114         LOG_ERROR("Query fail to SendRequest. uri: %{public}s, err: %{public}d", uri.c_str(), err);
115         return nullptr;
116     }
117     return ISharedResultSet::ReadFromParcel(reply);
118 }
119 } // namespace OHOS::DataShare