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 "ModifyTimeCursor"
16 #include "modify_time_cursor.h"
17 #include "logger.h"
18 #include "relational_store_error_code.h"
19 #include "securec.h"
20 #include "traits.h"
21 namespace OHOS::RdbNdk {
ModifyTimeCursor(ModifyTimeCursor::ModifyTime && modifyTime)22 ModifyTimeCursor::ModifyTimeCursor(ModifyTimeCursor::ModifyTime &&modifyTime)
23     : RelationalCursor(modifyTime), modifyTime_(std::move(modifyTime))
24 {
25 }
26 
GetSize(int32_t columnIndex,size_t * size)27 int ModifyTimeCursor::GetSize(int32_t columnIndex, size_t *size)
28 {
29     if (size == nullptr) {
30         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
31     }
32     if (columnIndex == 0 && modifyTime_.NeedConvert()) {
33         *size = modifyTime_.GetMaxOriginKeySize();
34         return OH_Rdb_ErrCode::RDB_OK;
35     }
36     return RelationalCursor::GetSize(columnIndex, size);
37 }
38 
GetText(int32_t columnIndex,char * value,int length)39 int ModifyTimeCursor::GetText(int32_t columnIndex, char *value, int length)
40 {
41     if (value == nullptr || length <= 0) {
42         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
43     }
44     if (columnIndex == 0 && modifyTime_.NeedConvert()) {
45         auto priKey = ConvertPRIKey();
46         auto *val = Traits::get_if<std::string>(&priKey);
47         if (val == nullptr) {
48             return OH_Rdb_ErrCode::RDB_ERR;
49         }
50         errno_t result = strcpy_s(value, length, val->data());
51         if (result != EOK) {
52             LOG_ERROR("strcpy_s failed, result is %{public}d", result);
53             return OH_Rdb_ErrCode::RDB_ERR;
54         }
55         return OH_Rdb_ErrCode::RDB_OK;
56     }
57     return RelationalCursor::GetText(columnIndex, value, length);
58 }
59 
GetInt64(int32_t columnIndex,int64_t * value)60 int ModifyTimeCursor::GetInt64(int32_t columnIndex, int64_t *value)
61 {
62     if (value == nullptr) {
63         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
64     }
65     if (columnIndex == 0 && modifyTime_.NeedConvert()) {
66         auto priKey = ConvertPRIKey();
67         auto *val = Traits::get_if<int64_t>(&priKey);
68         if (val == nullptr) {
69             return OH_Rdb_ErrCode::RDB_ERR;
70         }
71         *value = *val;
72         return OH_Rdb_ErrCode::RDB_OK;
73     }
74     return RelationalCursor::GetInt64(columnIndex, value);
75 }
76 
GetReal(int32_t columnIndex,double * value)77 int ModifyTimeCursor::GetReal(int32_t columnIndex, double *value)
78 {
79     if (columnIndex == 0 && modifyTime_.NeedConvert()) {
80         auto priKey = ConvertPRIKey();
81         auto *val = Traits::get_if<double>(&priKey);
82         if (val == nullptr) {
83             return OH_Rdb_ErrCode::RDB_ERR;
84         }
85         *value = *val;
86         return OH_Rdb_ErrCode::RDB_OK;
87     }
88     return RelationalCursor::GetReal(columnIndex, value);
89 }
90 } // namespace OHOS::RdbNdk
91