• 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 #define LOG_TAG "ResultSetProxy"
16 
17 #include "logger.h"
18 #include "message_parcel.h"
19 #include "rdb_errno.h"
20 #include "result_set_proxy.h"
21 
22 namespace OHOS::NativeRdb {
ResultSetProxy(const sptr<IRemoteObject> & impl)23 ResultSetProxy::ResultSetProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IResultSet>(impl)
24 {
25     LOG_INFO("Init result set proxy.");
26     remote_ = Remote();
27 }
28 
~ResultSetProxy()29 ResultSetProxy::~ResultSetProxy()
30 {
31     LOG_INFO("Result set destroy, close result.");
32     Close();
33 }
34 
GetAllColumnNames(std::vector<std::string> & columnNames)35 int ResultSetProxy::GetAllColumnNames(std::vector<std::string> &columnNames)
36 {
37     MessageParcel data, reply;
38     if (!data.WriteInterfaceToken(ResultSetProxy::GetDescriptor())) {
39         LOG_ERROR("Write descriptor failed, code is %{public}d.", CMD_GET_ALL_COLUMN_NAMES);
40         return E_ERROR;
41     }
42     MessageOption mo { MessageOption::TF_SYNC };
43     int32_t error = remote_->SendRequest(CMD_GET_ALL_COLUMN_NAMES, data, reply, mo);
44     if (error != 0) {
45         LOG_ERROR("SendRequest failed, error is %{public}d, code is %{public}d.", error, CMD_GET_ALL_COLUMN_NAMES);
46         return E_ERROR;
47     }
48     int status = reply.ReadInt32();
49     if (status != E_OK) {
50         LOG_ERROR("Reply status error, status is %{public}d, code is %{public}d.", status, CMD_GET_ALL_COLUMN_NAMES);
51         return status;
52     }
53     if (!reply.ReadStringVector(&columnNames)) {
54         LOG_ERROR("Read columnNames failed.");
55         return E_ERROR;
56     }
57     return E_OK;
58 }
59 
GetColumnCount(int & count)60 int ResultSetProxy::GetColumnCount(int &count)
61 {
62     return SendRequestRetInt(CMD_GET_COLUMN_COUNT, count);
63 }
64 
GetColumnType(int columnIndex,ColumnType & columnType)65 int ResultSetProxy::GetColumnType(int columnIndex, ColumnType &columnType)
66 {
67     MessageParcel reply;
68     int status = SendRequestRetReply(CMD_GET_COLUMN_TYPE, columnIndex, reply);
69     if (status != E_OK) {
70         return status;
71     }
72     columnType = static_cast<ColumnType>(reply.ReadInt32());
73     return E_OK;
74 }
75 
GetColumnIndex(const std::string & columnName,int & columnIndex)76 int ResultSetProxy::GetColumnIndex(const std::string &columnName, int &columnIndex)
77 {
78     MessageParcel data, reply;
79     if (!data.WriteInterfaceToken(ResultSetProxy::GetDescriptor())) {
80         LOG_ERROR("Write descriptor failed, code is %{public}d.", CMD_GET_COLUMN_INDEX);
81         return E_ERROR;
82     }
83     if (!reply.SetMaxCapacity(MAX_IPC_CAPACITY) || !data.WriteString(columnName)) {
84         LOG_ERROR("Set max capacity failed or write parcel failed, code is %{public}d.", CMD_GET_COLUMN_INDEX);
85         return E_ERROR;
86     }
87     MessageOption mo { MessageOption::TF_SYNC };
88     int32_t error = remote_->SendRequest(CMD_GET_COLUMN_INDEX, data, reply, mo);
89     if (error != 0) {
90         LOG_ERROR("SendRequest failed, error is %{public}d, code is %{public}d.", error, CMD_GET_COLUMN_INDEX);
91         return E_ERROR;
92     }
93     int status = reply.ReadInt32();
94     if (status != E_OK) {
95         LOG_ERROR("Reply status error, status is %{public}d, code is %{public}d.", status, CMD_GET_COLUMN_INDEX);
96         return status;
97     }
98     columnIndex = reply.ReadInt32();
99     return E_OK;
100 }
101 
GetColumnName(int columnIndex,std::string & columnName)102 int ResultSetProxy::GetColumnName(int columnIndex, std::string &columnName)
103 {
104     MessageParcel reply;
105     int status = SendRequestRetReply(CMD_GET_COLUMN_NAME, columnIndex, reply);
106     if (status != E_OK) {
107         return status;
108     }
109     columnName = reply.ReadString();
110     return E_OK;
111 }
112 
GetRowCount(int & count)113 int ResultSetProxy::GetRowCount(int &count)
114 {
115     return SendRequestRetInt(CMD_GET_ROW_COUNT, count);
116 }
117 
GetRowIndex(int & position) const118 int ResultSetProxy::GetRowIndex(int &position) const
119 {
120     return SendRequestRetInt(CMD_GET_ROW_INDEX, position);
121 }
122 
GoTo(int offset)123 int ResultSetProxy::GoTo(int offset)
124 {
125     return SendIntRequest(CMD_GO_TO, offset);
126 }
127 
GoToRow(int position)128 int ResultSetProxy::GoToRow(int position)
129 {
130     return SendIntRequest(CMD_GO_TO_ROW, position);
131 }
132 
GoToFirstRow()133 int ResultSetProxy::GoToFirstRow()
134 {
135     return SendRequest(CMD_GO_TO_FIRST_ROW);
136 }
137 
GoToLastRow()138 int ResultSetProxy::GoToLastRow()
139 {
140     return SendRequest(CMD_GO_TO_LAST_ROW);
141 }
142 
GoToNextRow()143 int ResultSetProxy::GoToNextRow()
144 {
145     return SendRequest(CMD_GO_TO_NEXT_ROW);
146 }
147 
GoToPreviousRow()148 int ResultSetProxy::GoToPreviousRow()
149 {
150     return SendRequest(CMD_GO_TO_PREV_ROW);
151 }
152 
IsEnded(bool & result)153 int ResultSetProxy::IsEnded(bool &result)
154 {
155     return SendRequestRetBool(CMD_IS_ENDED_ROW, result);
156 }
157 
IsStarted(bool & result) const158 int ResultSetProxy::IsStarted(bool &result) const
159 {
160     return SendRequestRetBool(CMD_IS_STARTED_ROW, result);
161 }
162 
IsAtFirstRow(bool & result) const163 int ResultSetProxy::IsAtFirstRow(bool &result) const
164 {
165     return SendRequestRetBool(CMD_IS_AT_FIRST_ROW, result);
166 }
167 
IsAtLastRow(bool & result)168 int ResultSetProxy::IsAtLastRow(bool &result)
169 {
170     return SendRequestRetBool(CMD_IS_AT_LAST_ROW, result);
171 }
172 
GetBlob(int columnIndex,std::vector<uint8_t> & blob)173 int ResultSetProxy::GetBlob(int columnIndex, std::vector<uint8_t> &blob)
174 {
175     MessageParcel reply;
176     int status = SendRequestRetReply(CMD_GET_BLOB, columnIndex, reply);
177     if (status != E_OK) {
178         return status;
179     }
180     if (!reply.ReadUInt8Vector(&blob)) {
181         LOG_ERROR("Read blob failed.");
182         return E_ERROR;
183     }
184     return E_OK;
185 }
186 
GetString(int columnIndex,std::string & value)187 int ResultSetProxy::GetString(int columnIndex, std::string &value)
188 {
189     MessageParcel reply;
190     int status = SendRequestRetReply(CMD_GET_STRING, columnIndex, reply);
191     if (status != E_OK) {
192         return status;
193     }
194     value = reply.ReadString();
195     return E_OK;
196 }
197 
GetInt(int columnIndex,int & value)198 int ResultSetProxy::GetInt(int columnIndex, int &value)
199 {
200     MessageParcel reply;
201     int status = SendRequestRetReply(CMD_GET_INT, columnIndex, reply);
202     if (status != E_OK) {
203         return status;
204     }
205     value = reply.ReadInt32();
206     return E_OK;
207 }
208 
GetLong(int columnIndex,int64_t & value)209 int ResultSetProxy::GetLong(int columnIndex, int64_t &value)
210 {
211     MessageParcel reply;
212     int status = SendRequestRetReply(CMD_GET_LONG, columnIndex, reply);
213     if (status != E_OK) {
214         return status;
215     }
216     value = reply.ReadInt64();
217     return E_OK;
218 }
219 
GetDouble(int columnIndex,double & value)220 int ResultSetProxy::GetDouble(int columnIndex, double &value)
221 {
222     MessageParcel reply;
223     int status = SendRequestRetReply(CMD_GET_DOUBLE, columnIndex, reply);
224     if (status != E_OK) {
225         return status;
226     }
227     value = reply.ReadDouble();
228     return E_OK;
229 }
230 
IsColumnNull(int columnIndex,bool & isNull)231 int ResultSetProxy::IsColumnNull(int columnIndex, bool &isNull)
232 {
233     MessageParcel reply;
234     int status = SendRequestRetReply(CMD_IS_COLUMN_NULL, columnIndex, reply);
235     if (status != E_OK) {
236         return status;
237     }
238     isNull = reply.ReadBool();
239     return E_OK;
240 }
241 
IsClosed() const242 bool ResultSetProxy::IsClosed() const
243 {
244     MessageParcel data, reply;
245     if (!data.WriteInterfaceToken(ResultSetProxy::GetDescriptor())) {
246         LOG_ERROR("Write descriptor failed, code is %{public}d.", CMD_IS_CLOSED);
247         return false;
248     }
249     MessageOption mo { MessageOption::TF_SYNC };
250     int32_t error = remote_->SendRequest(CMD_IS_CLOSED, data, reply, mo);
251     if (error != 0) {
252         LOG_ERROR("SendRequest failed, error is %{public}d, code is %{public}d.", error, CMD_IS_CLOSED);
253         return false;
254     }
255     return reply.ReadBool();
256 }
257 
Close()258 int ResultSetProxy::Close()
259 {
260     return SendRequest(CMD_CLOSE);
261 }
262 
SendRequest(uint32_t code)263 int ResultSetProxy::SendRequest(uint32_t code)
264 {
265     MessageParcel data, reply;
266     if (!data.WriteInterfaceToken(ResultSetProxy::GetDescriptor())) {
267         LOG_ERROR("Write descriptor failed, code is %{public}d.", code);
268         return E_ERROR;
269     }
270     MessageOption mo { MessageOption::TF_SYNC };
271     int32_t error = remote_->SendRequest(code, data, reply, mo);
272     if (error != 0) {
273         LOG_ERROR("SendRequest failed, error is %{public}d, code is %{public}d.", error, code);
274         return E_ERROR;
275     }
276     int status = reply.ReadInt32();
277     if (status != E_OK) {
278         LOG_ERROR("Reply status error, status is %{public}d, code is %{public}d.", status, code);
279         return status;
280     }
281     return E_OK;
282 }
283 
SendIntRequest(uint32_t code,int value)284 int ResultSetProxy::SendIntRequest(uint32_t code, int value)
285 {
286     MessageParcel data, reply;
287     if (!data.WriteInterfaceToken(ResultSetProxy::GetDescriptor())) {
288         LOG_ERROR("Write descriptor failed, code is %{public}d.", code);
289         return E_ERROR;
290     }
291     if (!reply.SetMaxCapacity(MAX_IPC_CAPACITY) || !data.WriteInt32(value)) {
292         LOG_ERROR("Set max capacity failed or write parcel failed, code is %{public}d.", code);
293         return E_ERROR;
294     }
295     MessageOption mo { MessageOption::TF_SYNC };
296     int32_t error = remote_->SendRequest(code, data, reply, mo);
297     if (error != 0) {
298         LOG_ERROR("SendRequest failed, error is %{public}d, code is %{public}d.", error, code);
299         return E_ERROR;
300     }
301     int status = reply.ReadInt32();
302     if (status != E_OK) {
303         LOG_ERROR("Reply status error, status is %{public}d, code is %{public}d.", status, code);
304         return status;
305     }
306     return E_OK;
307 }
308 
SendRequestRetBool(uint32_t code,bool & result) const309 int ResultSetProxy::SendRequestRetBool(uint32_t code, bool &result) const
310 {
311     MessageParcel data, reply;
312     if (!data.WriteInterfaceToken(ResultSetProxy::GetDescriptor())) {
313         LOG_ERROR("Write descriptor failed, code is %{public}d.", code);
314         return E_ERROR;
315     }
316     MessageOption mo { MessageOption::TF_SYNC };
317     int32_t error = remote_->SendRequest(code, data, reply, mo);
318     if (error != 0) {
319         LOG_ERROR("SendRequest failed, error is %{public}d, code is %{public}d.", error, code);
320         return E_ERROR;
321     }
322     int status = reply.ReadInt32();
323     if (status != E_OK) {
324         LOG_ERROR("Reply status error, status is %{public}d, code is %{public}d.", status, code);
325         return status;
326     }
327     result = reply.ReadBool();
328     return E_OK;
329 }
330 
SendRequestRetInt(uint32_t code,int & result) const331 int ResultSetProxy::SendRequestRetInt(uint32_t code, int &result) const
332 {
333     MessageParcel data, reply;
334     if (!data.WriteInterfaceToken(ResultSetProxy::GetDescriptor())) {
335         LOG_ERROR("Write descriptor failed, code is %{public}d.", code);
336         return E_ERROR;
337     }
338     MessageOption mo { MessageOption::TF_SYNC };
339     int32_t error = remote_->SendRequest(code, data, reply, mo);
340     if (error != 0) {
341         LOG_ERROR("SendRequest failed, error is %{public}d, code is %{public}d.", error, code);
342         return E_ERROR;
343     }
344     int status = reply.ReadInt32();
345     if (status != E_OK) {
346         LOG_ERROR("Reply status error, status is %{public}d, code is %{public}d.", status, code);
347         return status;
348     }
349     result = reply.ReadInt32();
350     return E_OK;
351 }
352 
SendRequestRetReply(uint32_t code,int columnIndex,MessageParcel & reply)353 int ResultSetProxy::SendRequestRetReply(uint32_t code, int columnIndex, MessageParcel &reply)
354 {
355     MessageParcel data;
356     if (!data.WriteInterfaceToken(ResultSetProxy::GetDescriptor())) {
357         LOG_ERROR("Write descriptor failed, code is %{public}d.", code);
358         return E_ERROR;
359     }
360     if (!reply.SetMaxCapacity(MAX_IPC_CAPACITY) || !data.WriteInt32(columnIndex)) {
361         LOG_ERROR("Set max capacity failed or write parcel failed, code is %{public}d.", code);
362         return E_ERROR;
363     }
364     MessageOption mo { MessageOption::TF_SYNC };
365     int32_t error = remote_->SendRequest(code, data, reply, mo);
366     if (error != 0) {
367         LOG_ERROR("SendRequest failed, error is %{public}d, code is %{public}d.", error, code);
368         return E_ERROR;
369     }
370     int status = reply.ReadInt32();
371     if (status != E_OK) {
372         LOG_ERROR("Reply status error, status is %{public}d, code is %{public}d.", status, code);
373         return status;
374     }
375     return E_OK;
376 }
377 } // namespace OHOS::NativeRdb