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 #define LOG_TAG "RelationalCursor"
16 #include "relational_cursor.h"
17 
18 #include <string>
19 
20 #include "logger.h"
21 #include "oh_cursor.h"
22 #include "rdb_errno.h"
23 #include "relational_asset.h"
24 #include "relational_store_error_code.h"
25 #include "securec.h"
26 #include "convertor_error_code.h"
27 
28 namespace OHOS {
29 namespace RdbNdk {
GetColumnCount(OH_Cursor * cursor,int * count)30 int RelationalCursor::GetColumnCount(OH_Cursor *cursor, int *count)
31 {
32     auto self = GetSelf(cursor);
33     if (self == nullptr) {
34         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
35     }
36     return self->GetColumnCount(count);
37 }
38 
GetColumnType(OH_Cursor * cursor,int32_t columnIndex,OH_ColumnType * columnType)39 int RelationalCursor::GetColumnType(OH_Cursor *cursor, int32_t columnIndex, OH_ColumnType *columnType)
40 {
41     auto self = GetSelf(cursor);
42     if (self == nullptr) {
43         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
44     }
45     return self->GetColumnType(columnIndex, columnType);
46 }
47 
GetColumnIndex(OH_Cursor * cursor,const char * name,int * columnIndex)48 int RelationalCursor::GetColumnIndex(OH_Cursor *cursor, const char *name, int *columnIndex)
49 {
50     auto self = GetSelf(cursor);
51     if (self == nullptr) {
52         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
53     }
54     return self->GetColumnIndex(name, columnIndex);
55 }
56 
GetColumnName(OH_Cursor * cursor,int32_t columnIndex,char * name,int length)57 int RelationalCursor::GetColumnName(OH_Cursor *cursor, int32_t columnIndex, char *name, int length)
58 {
59     auto self = GetSelf(cursor);
60     if (self == nullptr) {
61         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
62     }
63     return self->GetColumnName(columnIndex, name, length);
64 }
65 
GetRowCount(OH_Cursor * cursor,int * count)66 int RelationalCursor::GetRowCount(OH_Cursor *cursor, int *count)
67 {
68     auto self = GetSelf(cursor);
69     if (self == nullptr) {
70         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
71     }
72     return self->GetRowCount(count);
73 }
74 
GoToNextRow(OH_Cursor * cursor)75 int RelationalCursor::GoToNextRow(OH_Cursor *cursor)
76 {
77     auto self = GetSelf(cursor);
78     if (self == nullptr) {
79         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
80     }
81     return self->GoToNextRow();
82 }
83 
GetSize(OH_Cursor * cursor,int32_t columnIndex,size_t * size)84 int RelationalCursor::GetSize(OH_Cursor *cursor, int32_t columnIndex, size_t *size)
85 {
86     auto self = GetSelf(cursor);
87     if (self == nullptr) {
88         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
89     }
90     return self->GetSize(columnIndex, size);
91 }
92 
GetText(OH_Cursor * cursor,int32_t columnIndex,char * value,int length)93 int RelationalCursor::GetText(OH_Cursor *cursor, int32_t columnIndex, char *value, int length)
94 {
95     auto self = GetSelf(cursor);
96     if (self == nullptr) {
97         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
98     }
99     return self->GetText(columnIndex, value, length);
100 }
101 
GetInt64(OH_Cursor * cursor,int32_t columnIndex,int64_t * value)102 int RelationalCursor::GetInt64(OH_Cursor *cursor, int32_t columnIndex, int64_t *value)
103 {
104     auto self = GetSelf(cursor);
105     if (self == nullptr) {
106         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
107     }
108     return self->GetInt64(columnIndex, value);
109 }
110 
GetReal(OH_Cursor * cursor,int32_t columnIndex,double * value)111 int RelationalCursor::GetReal(OH_Cursor *cursor, int32_t columnIndex, double *value)
112 {
113     auto self = GetSelf(cursor);
114     if (self == nullptr) {
115         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
116     }
117     return self->GetReal(columnIndex, value);
118 }
119 
GetBlob(OH_Cursor * cursor,int32_t columnIndex,unsigned char * value,int length)120 int RelationalCursor::GetBlob(OH_Cursor *cursor, int32_t columnIndex, unsigned char *value, int length)
121 {
122     auto self = GetSelf(cursor);
123     if (self == nullptr) {
124         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
125     }
126     return self->GetBlob(columnIndex, value, length);
127 }
128 
GetAsset(OH_Cursor * cursor,int32_t columnIndex,Data_Asset * value)129 int RelationalCursor::GetAsset(OH_Cursor *cursor, int32_t columnIndex, Data_Asset *value)
130 {
131     auto self = GetSelf(cursor);
132     if (self == nullptr) {
133         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
134     }
135     return self->GetAsset(columnIndex, value);
136 }
137 
GetAssets(OH_Cursor * cursor,int32_t columnIndex,Data_Asset ** value,uint32_t * length)138 int RelationalCursor::GetAssets(OH_Cursor *cursor, int32_t columnIndex, Data_Asset **value, uint32_t *length)
139 {
140     auto self = GetSelf(cursor);
141     if (self == nullptr) {
142         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
143     }
144     return self->GetAssets(columnIndex, value, length);
145 }
146 
IsNull(OH_Cursor * cursor,int32_t columnIndex,bool * isNull)147 int RelationalCursor::IsNull(OH_Cursor *cursor, int32_t columnIndex, bool *isNull)
148 {
149     auto self = GetSelf(cursor);
150     if (self == nullptr) {
151         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
152     }
153     return self->IsNull(columnIndex, isNull);
154 }
155 
156 
GetAssetsCount(OH_Cursor * cursor,int32_t columnIndex,uint32_t * count)157 int RelationalCursor::GetAssetsCount(OH_Cursor *cursor, int32_t columnIndex, uint32_t *count)
158 {
159     auto self = GetSelf(cursor);
160     if (self == nullptr) {
161         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
162     }
163     return self->GetAssetsCount(columnIndex, count);
164 }
165 
Destroy(OH_Cursor * cursor)166 int RelationalCursor::Destroy(OH_Cursor *cursor)
167 {
168     auto self = GetSelf(cursor);
169     if (self == nullptr) {
170         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
171     }
172     int errCode = self->Destroy();
173     if (errCode != NativeRdb::E_OK) {
174         return errCode;
175     }
176     delete self;
177     return errCode;
178 }
179 
RelationalCursor(std::shared_ptr<NativeRdb::ResultSet> resultSet)180 RelationalCursor::RelationalCursor(std::shared_ptr<NativeRdb::ResultSet> resultSet)
181     : resultSet_(std::move(resultSet))
182 {
183     id = RDB_CURSOR_CID;
184 
185     getColumnCount = GetColumnCount;
186     getColumnType = GetColumnType;
187     getColumnIndex = GetColumnIndex;
188     getColumnName = GetColumnName;
189     getRowCount = GetRowCount;
190     goToNextRow = GoToNextRow;
191     getSize = GetSize;
192     getText = GetText;
193     getInt64 = GetInt64;
194     getReal = GetReal;
195     getBlob = GetBlob;
196     isNull = IsNull;
197     destroy = Destroy;
198     getAsset = GetAsset;
199     getAssets = GetAssets;
200     getAssetsCount = GetAssetsCount;
201 }
202 
GetSelf(OH_Cursor * cursor)203 RelationalCursor *RelationalCursor::GetSelf(OH_Cursor *cursor)
204 {
205     if (cursor == nullptr || cursor->id != RdbNdk::RDB_CURSOR_CID) {
206         LOG_ERROR("cursor invalid. is null %{public}d", (cursor == nullptr));
207         return nullptr;
208     }
209     return static_cast<RdbNdk::RelationalCursor *>(cursor);
210 }
211 
GetColumnCount(int * count)212 int RelationalCursor::GetColumnCount(int *count)
213 {
214     if (count == nullptr || resultSet_ == nullptr) {
215         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
216     }
217     return ConvertorErrorCode::NativeToNdk(resultSet_->GetColumnCount(*count));
218 }
219 
GetColumnType(int32_t columnIndex,OH_ColumnType * columnType)220 int RelationalCursor::GetColumnType(int32_t columnIndex, OH_ColumnType *columnType)
221 {
222     if (columnType == nullptr || columnIndex < 0 || resultSet_ == nullptr) {
223         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
224     }
225     NativeRdb::ColumnType type;
226     int result = resultSet_->GetColumnType(columnIndex, type);
227     *columnType = static_cast<OH_ColumnType>(static_cast<int>(type));
228     return ConvertorErrorCode::NativeToNdk(result);
229 }
230 
GetColumnIndex(const char * name,int * columnIndex)231 int RelationalCursor::GetColumnIndex(const char *name, int *columnIndex)
232 {
233     if (name == nullptr || columnIndex == nullptr || resultSet_ == nullptr) {
234         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
235     }
236     return ConvertorErrorCode::NativeToNdk(resultSet_->GetColumnIndex(name, *columnIndex));
237 }
238 
GetColumnName(int32_t columnIndex,char * name,int length)239 int RelationalCursor::GetColumnName(int32_t columnIndex, char *name, int length)
240 {
241     if (name == nullptr || length <= 0 || resultSet_ == nullptr) {
242         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
243     }
244     std::string str;
245     int errCode = resultSet_->GetColumnName(columnIndex, str);
246     if (errCode != NativeRdb::E_OK) {
247         return ConvertorErrorCode::NativeToNdk(errCode);
248     }
249     errno_t result = strcpy_s(name, length, str.c_str());
250     if (result != EOK) {
251         LOG_ERROR("strcpy_s failed, result is %{public}d", result);
252         return OH_Rdb_ErrCode::RDB_ERR;
253     }
254     return OH_Rdb_ErrCode::RDB_OK;
255 }
256 
GetRowCount(int * count)257 int RelationalCursor::GetRowCount(int *count)
258 {
259     if (count == nullptr || resultSet_ == nullptr) {
260         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
261     }
262     return ConvertorErrorCode::NativeToNdk(resultSet_->GetRowCount(*count));
263 }
264 
GoToNextRow()265 int RelationalCursor::GoToNextRow()
266 {
267     if (resultSet_ == nullptr) {
268         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
269     }
270     return ConvertorErrorCode::NativeToNdk(resultSet_->GoToNextRow());
271 }
272 
GetSize(int32_t columnIndex,size_t * size)273 int RelationalCursor::GetSize(int32_t columnIndex, size_t *size)
274 {
275     if (size == nullptr || resultSet_ == nullptr) {
276         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
277     }
278     return ConvertorErrorCode::NativeToNdk(resultSet_->GetSize(columnIndex, *size));
279 }
280 
GetText(int32_t columnIndex,char * value,int length)281 int RelationalCursor::GetText(int32_t columnIndex, char *value, int length)
282 {
283     if (value == nullptr || length <= 0 || resultSet_ == nullptr) {
284         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
285     }
286     std::string str;
287     int errCode = resultSet_->GetString(columnIndex, str);
288     if (errCode != NativeRdb::E_OK) {
289         return ConvertorErrorCode::NativeToNdk(errCode);
290     }
291     errno_t result = strcpy_s(value, length, str.c_str());
292     if (result != EOK) {
293         LOG_ERROR("strcpy_s failed, result is %{public}d", result);
294         return OH_Rdb_ErrCode::RDB_ERR;
295     }
296     return OH_Rdb_ErrCode::RDB_OK;
297 }
298 
GetInt64(int32_t columnIndex,int64_t * value)299 int RelationalCursor::GetInt64(int32_t columnIndex, int64_t *value)
300 {
301     if (value == nullptr || resultSet_ == nullptr) {
302         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
303     }
304     return ConvertorErrorCode::NativeToNdk(resultSet_->GetLong(columnIndex, *value));
305 }
306 
GetReal(int32_t columnIndex,double * value)307 int RelationalCursor::GetReal(int32_t columnIndex, double *value)
308 {
309     if (value == nullptr || resultSet_ == nullptr) {
310         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
311     }
312     return ConvertorErrorCode::NativeToNdk(resultSet_->GetDouble(columnIndex, *value));
313 }
314 
GetBlob(int32_t columnIndex,unsigned char * value,int length)315 int RelationalCursor::GetBlob(int32_t columnIndex, unsigned char *value, int length)
316 {
317     if (value == nullptr || length <= 0 || resultSet_ == nullptr) {
318         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
319     }
320     std::vector<uint8_t> vec;
321     int errCode = resultSet_->GetBlob(columnIndex, vec);
322     if (errCode != NativeRdb::E_OK) {
323         return ConvertorErrorCode::NativeToNdk(errCode);
324     }
325     errno_t result = memcpy_s(value, length, vec.data(), vec.size());
326     if (result != EOK) {
327         LOG_ERROR("memcpy_s failed, result is %{public}d", result);
328         return OH_Rdb_ErrCode::RDB_ERR;
329     }
330     return OH_Rdb_ErrCode::RDB_OK;
331 }
332 
GetAsset(int32_t columnIndex,Data_Asset * value)333 int RelationalCursor::GetAsset(int32_t columnIndex, Data_Asset *value)
334 {
335     if (resultSet_ == nullptr || value == nullptr || columnIndex < 0 || value->cid != DATA_ASSET_V0) {
336         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
337     }
338     NativeRdb::AssetValue asset;
339     auto errCode = resultSet_->GetAsset(columnIndex, asset);
340     if (errCode != NativeRdb::E_OK) {
341         return ConvertorErrorCode::NativeToNdk(errCode);
342     }
343     value->cid = DATA_ASSET_V0;
344     value->asset_ = asset;
345     return ConvertorErrorCode::NativeToNdk(errCode);
346 }
347 
GetAssets(int32_t columnIndex,Data_Asset ** value,uint32_t * length)348 int RelationalCursor::GetAssets(int32_t columnIndex, Data_Asset **value, uint32_t *length)
349 {
350     if (resultSet_ == nullptr || length == nullptr) {
351         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
352     }
353 
354     std::vector<NativeRdb::AssetValue> assets;
355     auto errCode = resultSet_->GetAssets(columnIndex, assets);
356     if (errCode != NativeRdb::E_OK) {
357         return ConvertorErrorCode::NativeToNdk(errCode);
358     }
359     uint32_t inputLength = *length;
360     *length = assets.size();
361     if (value == nullptr) {
362         return OH_Rdb_ErrCode::RDB_OK;
363     }
364     if (*length != inputLength) {
365         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
366     }
367     for (uint32_t i = 0; i < *length; ++i) {
368         if (value[i] == nullptr || value[i]->cid != DATA_ASSET_V0) {
369             return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
370         }
371         value[i]->cid = DATA_ASSET_V0;
372         value[i]->asset_ = assets[i];
373     }
374     return ConvertorErrorCode::NativeToNdk(errCode);
375 }
376 
IsNull(int32_t columnIndex,bool * isNull)377 int RelationalCursor::IsNull(int32_t columnIndex, bool *isNull)
378 {
379     if (isNull == nullptr) {
380         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
381     }
382     return ConvertorErrorCode::NativeToNdk(resultSet_->IsColumnNull(columnIndex, *isNull));
383 }
384 
Destroy()385 int RelationalCursor::Destroy()
386 {
387     if (resultSet_ == nullptr) {
388         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
389     }
390     return ConvertorErrorCode::NativeToNdk(resultSet_->Close());
391 }
392 
GetAssetsCount(int32_t columnIndex,uint32_t * count)393 int RelationalCursor::GetAssetsCount(int32_t columnIndex, uint32_t *count)
394 {
395     if (count == nullptr) {
396         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
397     }
398     std::vector<NativeRdb::AssetValue> assets;
399     auto errCode = resultSet_->GetAssets(columnIndex, assets);
400     if (errCode != NativeRdb::E_OK) {
401         return ConvertorErrorCode::NativeToNdk(errCode);
402     }
403     *count = assets.size();
404     return OH_Rdb_ErrCode::RDB_OK;
405 }
406 
407 } // namespace RdbNdk
408 } // namespace OHOS
409