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