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 "convertor_error_code.h"
21 #include "logger.h"
22 #include "oh_cursor.h"
23 #include "rdb_errno.h"
24 #include "relational_asset.h"
25 #include "relational_store_error_code.h"
26 #include "securec.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
GetAssetsCount(OH_Cursor * cursor,int32_t columnIndex,uint32_t * count)156 int RelationalCursor::GetAssetsCount(OH_Cursor *cursor, int32_t columnIndex, uint32_t *count)
157 {
158 auto self = GetSelf(cursor);
159 if (self == nullptr) {
160 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
161 }
162 return self->GetAssetsCount(columnIndex, count);
163 }
164
Destroy(OH_Cursor * cursor)165 int RelationalCursor::Destroy(OH_Cursor *cursor)
166 {
167 auto self = GetSelf(cursor);
168 if (self == nullptr) {
169 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
170 }
171 int errCode = self->Destroy();
172 if (errCode != NativeRdb::E_OK) {
173 return errCode;
174 }
175 delete self;
176 return errCode;
177 }
178
RelationalCursor(std::shared_ptr<NativeRdb::ResultSet> resultSet)179 RelationalCursor::RelationalCursor(std::shared_ptr<NativeRdb::ResultSet> resultSet)
180 : resultSet_(std::move(resultSet))
181 {
182 id = RDB_CURSOR_CID;
183
184 getColumnCount = GetColumnCount;
185 getColumnType = GetColumnType;
186 getColumnIndex = GetColumnIndex;
187 getColumnName = GetColumnName;
188 getRowCount = GetRowCount;
189 goToNextRow = GoToNextRow;
190 getSize = GetSize;
191 getText = GetText;
192 getInt64 = GetInt64;
193 getReal = GetReal;
194 getBlob = GetBlob;
195 isNull = IsNull;
196 destroy = Destroy;
197 getAsset = GetAsset;
198 getAssets = GetAssets;
199 getAssetsCount = GetAssetsCount;
200 }
201
GetSelf(OH_Cursor * cursor)202 RelationalCursor *RelationalCursor::GetSelf(OH_Cursor *cursor)
203 {
204 if (cursor == nullptr || cursor->id != RdbNdk::RDB_CURSOR_CID) {
205 LOG_ERROR("cursor invalid. is null %{public}d", (cursor == nullptr));
206 return nullptr;
207 }
208 return static_cast<RdbNdk::RelationalCursor *>(cursor);
209 }
210
GetColumnCount(int * count)211 int RelationalCursor::GetColumnCount(int *count)
212 {
213 if (count == nullptr || resultSet_ == nullptr) {
214 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
215 }
216 return ConvertorErrorCode::NativeToNdk(resultSet_->GetColumnCount(*count));
217 }
218
GetColumnType(int32_t columnIndex,OH_ColumnType * columnType)219 int RelationalCursor::GetColumnType(int32_t columnIndex, OH_ColumnType *columnType)
220 {
221 if (columnType == nullptr || columnIndex < 0 || resultSet_ == nullptr) {
222 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
223 }
224 NativeRdb::ColumnType type;
225 int result = resultSet_->GetColumnType(columnIndex, type);
226 *columnType = static_cast<OH_ColumnType>(static_cast<int>(type));
227 return ConvertorErrorCode::NativeToNdk(result);
228 }
229
GetColumnIndex(const char * name,int * columnIndex)230 int RelationalCursor::GetColumnIndex(const char *name, int *columnIndex)
231 {
232 if (name == nullptr || columnIndex == nullptr || resultSet_ == nullptr) {
233 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
234 }
235 return ConvertorErrorCode::NativeToNdk(resultSet_->GetColumnIndex(name, *columnIndex));
236 }
237
GetColumnName(int32_t columnIndex,char * name,int length)238 int RelationalCursor::GetColumnName(int32_t columnIndex, char *name, int length)
239 {
240 if (name == nullptr || length <= 0 || resultSet_ == nullptr) {
241 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
242 }
243 std::string str;
244 int errCode = resultSet_->GetColumnName(columnIndex, str);
245 if (errCode != NativeRdb::E_OK) {
246 return ConvertorErrorCode::NativeToNdk(errCode);
247 }
248 errno_t result = strcpy_s(name, length, str.c_str());
249 if (result != EOK) {
250 LOG_ERROR("strcpy_s failed, result is %{public}d", result);
251 return OH_Rdb_ErrCode::RDB_ERR;
252 }
253 return OH_Rdb_ErrCode::RDB_OK;
254 }
255
GetRowCount(int * count)256 int RelationalCursor::GetRowCount(int *count)
257 {
258 if (count == nullptr || resultSet_ == nullptr) {
259 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
260 }
261 return ConvertorErrorCode::NativeToNdk(resultSet_->GetRowCount(*count));
262 }
263
GoToNextRow()264 int RelationalCursor::GoToNextRow()
265 {
266 if (resultSet_ == nullptr) {
267 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
268 }
269 return ConvertorErrorCode::NativeToNdk(resultSet_->GoToNextRow());
270 }
271
GetSize(int32_t columnIndex,size_t * size)272 int RelationalCursor::GetSize(int32_t columnIndex, size_t *size)
273 {
274 if (size == nullptr || resultSet_ == nullptr) {
275 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
276 }
277 return ConvertorErrorCode::NativeToNdk(resultSet_->GetSize(columnIndex, *size));
278 }
279
GetText(int32_t columnIndex,char * value,int length)280 int RelationalCursor::GetText(int32_t columnIndex, char *value, int length)
281 {
282 if (value == nullptr || length <= 0 || resultSet_ == nullptr) {
283 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
284 }
285 std::string str;
286 int errCode = resultSet_->GetString(columnIndex, str);
287 if (errCode != NativeRdb::E_OK) {
288 return ConvertorErrorCode::NativeToNdk(errCode);
289 }
290 errno_t result = strcpy_s(value, length, str.c_str());
291 if (result != EOK) {
292 LOG_ERROR("strcpy_s failed, result is %{public}d", result);
293 return OH_Rdb_ErrCode::RDB_ERR;
294 }
295 return OH_Rdb_ErrCode::RDB_OK;
296 }
297
GetInt64(int32_t columnIndex,int64_t * value)298 int RelationalCursor::GetInt64(int32_t columnIndex, int64_t *value)
299 {
300 if (value == nullptr || resultSet_ == nullptr) {
301 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
302 }
303 return ConvertorErrorCode::NativeToNdk(resultSet_->GetLong(columnIndex, *value));
304 }
305
GetReal(int32_t columnIndex,double * value)306 int RelationalCursor::GetReal(int32_t columnIndex, double *value)
307 {
308 if (value == nullptr || resultSet_ == nullptr) {
309 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
310 }
311 return ConvertorErrorCode::NativeToNdk(resultSet_->GetDouble(columnIndex, *value));
312 }
313
GetBlob(int32_t columnIndex,unsigned char * value,int length)314 int RelationalCursor::GetBlob(int32_t columnIndex, unsigned char *value, int length)
315 {
316 if (value == nullptr || length <= 0 || resultSet_ == nullptr) {
317 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
318 }
319 std::vector<uint8_t> vec;
320 int errCode = resultSet_->GetBlob(columnIndex, vec);
321 if (errCode != NativeRdb::E_OK) {
322 return ConvertorErrorCode::NativeToNdk(errCode);
323 }
324 errno_t result = memcpy_s(value, length, vec.data(), vec.size());
325 if (result != EOK) {
326 LOG_ERROR("memcpy_s failed, result is %{public}d", result);
327 return OH_Rdb_ErrCode::RDB_ERR;
328 }
329 return OH_Rdb_ErrCode::RDB_OK;
330 }
331
GetAsset(int32_t columnIndex,Data_Asset * value)332 int RelationalCursor::GetAsset(int32_t columnIndex, Data_Asset *value)
333 {
334 if (resultSet_ == nullptr || value == nullptr || columnIndex < 0 || value->cid != DATA_ASSET_V0) {
335 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
336 }
337 NativeRdb::AssetValue asset;
338 auto errCode = resultSet_->GetAsset(columnIndex, asset);
339 if (errCode != NativeRdb::E_OK) {
340 return ConvertorErrorCode::NativeToNdk(errCode);
341 }
342 value->cid = DATA_ASSET_V0;
343 value->asset_ = asset;
344 return ConvertorErrorCode::NativeToNdk(errCode);
345 }
346
GetAssets(int32_t columnIndex,Data_Asset ** value,uint32_t * length)347 int RelationalCursor::GetAssets(int32_t columnIndex, Data_Asset **value, uint32_t *length)
348 {
349 if (resultSet_ == nullptr || length == nullptr) {
350 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
351 }
352
353 std::vector<NativeRdb::AssetValue> assets;
354 auto errCode = resultSet_->GetAssets(columnIndex, assets);
355 if (errCode != NativeRdb::E_OK) {
356 return ConvertorErrorCode::NativeToNdk(errCode);
357 }
358 uint32_t inputLength = *length;
359 *length = assets.size();
360 if (value == nullptr) {
361 return OH_Rdb_ErrCode::RDB_OK;
362 }
363 if (*length != inputLength) {
364 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
365 }
366 for (uint32_t i = 0; i < *length; ++i) {
367 if (value[i] == nullptr || value[i]->cid != DATA_ASSET_V0) {
368 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
369 }
370 value[i]->cid = DATA_ASSET_V0;
371 value[i]->asset_ = assets[i];
372 }
373 return ConvertorErrorCode::NativeToNdk(errCode);
374 }
375
IsNull(int32_t columnIndex,bool * isNull)376 int RelationalCursor::IsNull(int32_t columnIndex, bool *isNull)
377 {
378 if (isNull == nullptr) {
379 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
380 }
381 return ConvertorErrorCode::NativeToNdk(resultSet_->IsColumnNull(columnIndex, *isNull));
382 }
383
Destroy()384 int RelationalCursor::Destroy()
385 {
386 if (resultSet_ == nullptr) {
387 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
388 }
389 return ConvertorErrorCode::NativeToNdk(resultSet_->Close());
390 }
391
GetAssetsCount(int32_t columnIndex,uint32_t * count)392 int RelationalCursor::GetAssetsCount(int32_t columnIndex, uint32_t *count)
393 {
394 if (count == nullptr) {
395 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
396 }
397 std::vector<NativeRdb::AssetValue> assets;
398 auto errCode = resultSet_->GetAssets(columnIndex, assets);
399 if (errCode != NativeRdb::E_OK) {
400 return ConvertorErrorCode::NativeToNdk(errCode);
401 }
402 *count = assets.size();
403 return OH_Rdb_ErrCode::RDB_OK;
404 }
405
GetFloatVectorCount(int32_t columnIndex,size_t * length)406 int RelationalCursor::GetFloatVectorCount(int32_t columnIndex, size_t *length)
407 {
408 if (length == nullptr || resultSet_ == nullptr) {
409 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
410 }
411 std::vector<float> result = {};
412 auto errCode = resultSet_->GetFloat32Array(columnIndex, result);
413 if (errCode != NativeRdb::E_OK) {
414 return ConvertorErrorCode::GetInterfaceCode(errCode);
415 }
416 *length = result.size();
417 return OH_Rdb_ErrCode::RDB_OK;
418 }
419
GetFloatVector(int32_t columnIndex,float * val,size_t inLen,size_t * outLen)420 int RelationalCursor::GetFloatVector(int32_t columnIndex, float *val, size_t inLen, size_t *outLen)
421 {
422 if (val == nullptr || inLen == 0 || outLen == nullptr || resultSet_ == nullptr) {
423 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
424 }
425 std::vector<float> result = {};
426 auto errCode = resultSet_->GetFloat32Array(columnIndex, result);
427 if (errCode != NativeRdb::E_OK) {
428 return ConvertorErrorCode::GetInterfaceCode(errCode);
429 }
430 if (inLen < result.size()) {
431 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
432 }
433 *outLen = result.size();
434 errCode = memcpy_s(val, inLen * sizeof(float), result.data(), (*outLen) * sizeof(float));
435 if (errCode != EOK) {
436 LOG_ERROR("memcpy_s failed, errCode is %{public}d", errCode);
437 *outLen = 0;
438 return OH_Rdb_ErrCode::RDB_E_ERROR;
439 }
440 return OH_Rdb_ErrCode::RDB_OK;
441 }
442
443 } // namespace RdbNdk
444 } // namespace OHOS
445
446 using namespace OHOS::RdbNdk;
447 using namespace OHOS::NativeRdb;
OH_Cursor_GetFloatVectorCount(OH_Cursor * cursor,int32_t columnIndex,size_t * length)448 int OH_Cursor_GetFloatVectorCount(OH_Cursor *cursor, int32_t columnIndex, size_t *length)
449 {
450 auto self = RelationalCursor::GetSelf(cursor);
451 if (self == nullptr) {
452 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
453 }
454 return self->GetFloatVectorCount(columnIndex, length);
455 }
456
OH_Cursor_GetFloatVector(OH_Cursor * cursor,int32_t columnIndex,float * val,size_t inLen,size_t * outLen)457 int OH_Cursor_GetFloatVector(OH_Cursor *cursor, int32_t columnIndex, float *val, size_t inLen, size_t *outLen)
458 {
459 auto self = RelationalCursor::GetSelf(cursor);
460 if (self == nullptr) {
461 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
462 }
463 return self->GetFloatVector(columnIndex, val, inLen, outLen);
464 }
465