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 #define LOG_TAG "ISharedResultSetProxy"
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 *block = nullptr;
45 AppDataFwk::SharedBlock::ReadMessageParcel(parcel, block);
46 result->SetBlock(block);
47 }
48 return std::shared_ptr<AbsSharedResultSet>(result.GetRefPtr(), [keep = result](AbsSharedResultSet *) {});
49 }
50
GetRowCount(int & count)51 int ISharedResultSetProxy::GetRowCount(int &count)
52 {
53 LOG_DEBUG("GetRowCount Begin.");
54 if (rowCount_ >= 0) {
55 count = rowCount_;
56 return E_OK;
57 }
58 MessageParcel request;
59 request.WriteInterfaceToken(GetDescriptor());
60 MessageParcel reply;
61 MessageOption msgOption;
62 int errCode =
63 Remote()->SendRequest(static_cast<uint32_t>(ResultSetCode::FUNC_GET_ROW_COUNT), request, reply, msgOption);
64 if (errCode != 0) {
65 LOG_ERROR("GetRowCount IPC Error %{public}x", errCode);
66 return -errCode;
67 }
68 errCode = reply.ReadInt32();
69 if (errCode != E_OK) {
70 LOG_ERROR("GetRowCount Reply Error %{public}d", errCode);
71 return errCode;
72 }
73 count = reply.ReadInt32();
74 LOG_DEBUG("GetRowCount count %{public}d", count);
75 rowCount_ = count;
76 return E_OK;
77 }
78
OnGo(int oldRowIndex,int newRowIndex)79 int ISharedResultSetProxy::OnGo(int oldRowIndex, int newRowIndex)
80 {
81 LOG_DEBUG("OnGo Begin.");
82 MessageParcel request;
83 request.WriteInterfaceToken(GetDescriptor());
84 request.WriteInt32(oldRowIndex);
85 request.WriteInt32(newRowIndex);
86 MessageParcel reply;
87 MessageOption msgOption;
88 int errCode = Remote()->SendRequest(static_cast<uint32_t>(ResultSetCode::FUNC_ON_GO), request, reply, msgOption);
89 if (errCode != 0) {
90 LOG_ERROR("OnGo IPC Error %{public}x", errCode);
91 return -errCode;
92 }
93 return reply.ReadInt32();
94 }
95
Close()96 int ISharedResultSetProxy::Close()
97 {
98 LOG_DEBUG("Close Begin.");
99 AbsSharedResultSet::Close();
100 MessageParcel request;
101 request.WriteInterfaceToken(GetDescriptor());
102 MessageParcel reply;
103 MessageOption msgOption;
104 int errCode = Remote()->SendRequest(static_cast<uint32_t>(ResultSetCode::FUNC_CLOSE), request, reply, msgOption);
105 if (errCode != 0) {
106 LOG_ERROR("Close IPC Error %{public}x", errCode);
107 return -errCode;
108 }
109 return reply.ReadInt32();
110 }
111
GetColumnNames()112 std::pair<int, std::vector<std::string>> ISharedResultSetProxy::GetColumnNames()
113 {
114 MessageParcel request;
115 request.WriteInterfaceToken(GetDescriptor());
116 MessageParcel reply;
117 MessageOption msgOption;
118 int errCode = Remote()->SendRequest(
119 static_cast<uint32_t>(ResultSetCode::FUNC_GET_ALL_COLUMN_NAMES), request, reply, msgOption);
120 if (errCode != 0) {
121 LOG_ERROR("GetAllColumnNames IPC Error %{public}x", errCode);
122 return { -errCode, {} };
123 }
124 errCode = reply.ReadInt32();
125 if (errCode != E_OK) {
126 LOG_ERROR("GetAllColumnNames Reply Error %{public}d", errCode);
127 return { errCode, {} };
128 }
129 std::vector<std::string> colNames;
130 if (!reply.ReadStringVector(&colNames)) {
131 return { E_INVALID_PARCEL, {} };
132 }
133 return { errCode, std::move(colNames) };
134 }
135 } // namespace OHOS::NativeRdb
136