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 "ishared_result_set_proxy.h"
17
18 #include "iremote_proxy.h"
19 #include "logger.h"
20 #include "rdb_errno.h"
21 #include "shared_block.h"
22
23 namespace OHOS::NativeRdb {
24 using namespace OHOS::Rdb;
25
26 using ResultSetCode = OHOS::DistributedRdb::RelationalStore::IResultSetInterfaceCode;
27
28 std::function<std::shared_ptr<AbsSharedResultSet>(
29 MessageParcel &parcel)> ISharedResultSet::consumerCreator_ = ISharedResultSetProxy::CreateProxy;
30 BrokerDelegator<ISharedResultSetProxy> ISharedResultSetProxy::delegator_;
ISharedResultSetProxy(const sptr<OHOS::IRemoteObject> & impl)31 ISharedResultSetProxy::ISharedResultSetProxy(const sptr<OHOS::IRemoteObject> &impl)
32 : IRemoteProxy<ISharedResultSet>(impl)
33 {
34 }
35
CreateProxy(MessageParcel & parcel)36 std::shared_ptr<AbsSharedResultSet> ISharedResultSetProxy::CreateProxy(MessageParcel &parcel)
37 {
38 sptr<IRemoteObject> remoter = parcel.ReadRemoteObject();
39 if (remoter == nullptr) {
40 return nullptr;
41 }
42 sptr<ISharedResultSet> result = iface_cast<ISharedResultSet>(remoter);
43 if (result->GetBlock() == nullptr) {
44 AppDataFwk::SharedBlock::ReadMessageParcel(parcel, result->sharedBlock_);
45 }
46 return std::shared_ptr<AbsSharedResultSet>(result.GetRefPtr(), [keep = result] (AbsSharedResultSet *) {});
47 }
48
GetAllColumnNames(std::vector<std::string> & columnNames)49 int ISharedResultSetProxy::GetAllColumnNames(std::vector<std::string> &columnNames)
50 {
51 LOG_DEBUG("GetAllColumnNames Begin");
52 if (!columnNames_.empty()) {
53 columnNames = columnNames_;
54 return E_OK;
55 }
56 MessageParcel request;
57 request.WriteInterfaceToken(GetDescriptor());
58 MessageParcel reply;
59 MessageOption msgOption;
60 int errCode = Remote()->SendRequest(
61 static_cast<uint32_t>(ResultSetCode::FUNC_GET_ALL_COLUMN_NAMES), request, reply, msgOption);
62 if (errCode != 0) {
63 LOG_ERROR("GetAllColumnNames IPC Error %{public}x", errCode);
64 return -errCode;
65 }
66 errCode = reply.ReadInt32();
67 if (errCode != E_OK) {
68 LOG_ERROR("GetAllColumnNames Reply Error %{public}d", errCode);
69 return errCode;
70 }
71 if (!reply.ReadStringVector(&columnNames)) {
72 return E_INVALID_PARCEL;
73 }
74 columnNames_ = columnNames;
75 return E_OK;
76 }
77
GetRowCount(int & count)78 int ISharedResultSetProxy::GetRowCount(int &count)
79 {
80 LOG_DEBUG("GetRowCount Begin");
81 if (rowCount_ >= 0) {
82 count = rowCount_;
83 return E_OK;
84 }
85 MessageParcel request;
86 request.WriteInterfaceToken(GetDescriptor());
87 MessageParcel reply;
88 MessageOption msgOption;
89 int errCode = Remote()->SendRequest(
90 static_cast<uint32_t>(ResultSetCode::FUNC_GET_ROW_COUNT), request, reply, msgOption);
91 if (errCode != 0) {
92 LOG_ERROR("GetRowCount IPC Error %{public}x", errCode);
93 return -errCode;
94 }
95 errCode = reply.ReadInt32();
96 if (errCode != E_OK) {
97 LOG_ERROR("GetRowCount Reply Error %{public}d", errCode);
98 return errCode;
99 }
100 count = reply.ReadInt32();
101 LOG_DEBUG("GetRowCount count %{public}d", count);
102 rowCount_ = count;
103 return E_OK;
104 }
105
OnGo(int oldRowIndex,int newRowIndex)106 bool ISharedResultSetProxy::OnGo(int oldRowIndex, int newRowIndex)
107 {
108 LOG_DEBUG("OnGo Begin");
109 MessageParcel request;
110 request.WriteInterfaceToken(GetDescriptor());
111 request.WriteInt32(oldRowIndex);
112 request.WriteInt32(newRowIndex);
113 MessageParcel reply;
114 MessageOption msgOption;
115 int errCode = Remote()->SendRequest(static_cast<uint32_t>(ResultSetCode::FUNC_ON_GO), request, reply, msgOption);
116 if (errCode != 0) {
117 LOG_ERROR("OnGo IPC Error %{public}x", errCode);
118 return -errCode;
119 }
120 return reply.ReadBool();
121 }
122
Close()123 int ISharedResultSetProxy::Close()
124 {
125 LOG_DEBUG("Close Begin");
126 AbsSharedResultSet::Close();
127 MessageParcel request;
128 request.WriteInterfaceToken(GetDescriptor());
129 MessageParcel reply;
130 MessageOption msgOption;
131 int errCode = Remote()->SendRequest(static_cast<uint32_t>(ResultSetCode::FUNC_CLOSE), request, reply, msgOption);
132 if (errCode != 0) {
133 LOG_ERROR("Close IPC Error %{public}x", errCode);
134 return -errCode;
135 }
136 return reply.ReadInt32();
137 }
138 } // namespace OHOS::NativeRdb