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 "RelationalPredicatesObjects"
16 #include "logger.h"
17 #include "oh_value_object.h"
18 #include "relational_store_error_code.h"
19 #include "relational_predicates_objects.h"
20 
21 namespace OHOS {
22 namespace RdbNdk {
23 // The class id used to uniquely identify the OH_VObject class.
24 constexpr int RDB_PREDICATES_OBJECTS_CID = 1234565;
PutInt64(OH_VObject * objects,int64_t * value,uint32_t count)25 int RelationalPredicatesObjects::PutInt64(OH_VObject *objects, int64_t *value, uint32_t count)
26 {
27     auto self = GetSelf(objects);
28     if (self == nullptr || value == nullptr || count == 0) {
29         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
30     }
31     self->values_.clear();
32     self->values_.reserve(count);
33     for (uint32_t i = 0; i < count; i++) {
34         self->values_.push_back(value[i]);
35     }
36     return OH_Rdb_ErrCode::RDB_OK;
37 }
38 
PutDouble(OH_VObject * objects,double * value,uint32_t count)39 int RelationalPredicatesObjects::PutDouble(OH_VObject *objects, double *value, uint32_t count)
40 {
41     auto self = GetSelf(objects);
42     if (self == nullptr || value == nullptr || count == 0) {
43         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
44     }
45     self->values_.clear();
46     self->values_.reserve(count);
47     for (uint32_t i = 0; i < count; i++) {
48         self->values_.push_back(value[i]);
49     }
50     return OH_Rdb_ErrCode::RDB_OK;
51 }
52 
PutText(OH_VObject * objects,const char * value)53 int RelationalPredicatesObjects::PutText(OH_VObject *objects, const char *value)
54 {
55     auto self = GetSelf(objects);
56     if (self == nullptr || value == nullptr) {
57         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
58     }
59 
60     self->values_.clear();
61     self->values_.push_back(value);
62     return OH_Rdb_ErrCode::RDB_OK;
63 }
64 
PutTexts(OH_VObject * objects,const char ** value,uint32_t count)65 int RelationalPredicatesObjects::PutTexts(OH_VObject *objects, const char **value, uint32_t count)
66 {
67     auto self = GetSelf(objects);
68     if (self == nullptr || value == nullptr || count == 0) {
69         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
70     }
71 
72     self->values_.clear();
73     self->values_.reserve(count);
74     for (uint32_t i = 0; i < count; i++) {
75         self->values_.push_back(value[i]);
76     }
77     return OH_Rdb_ErrCode::RDB_OK;
78 }
79 
Destroy(OH_VObject * objects)80 int RelationalPredicatesObjects::Destroy(OH_VObject *objects)
81 {
82     auto self = GetSelf(objects);
83     if (self == nullptr) {
84         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
85     }
86     delete self;
87     return OH_Rdb_ErrCode::RDB_OK;
88 }
89 
RelationalPredicatesObjects()90 RelationalPredicatesObjects::RelationalPredicatesObjects()
91 {
92     id = RDB_PREDICATES_OBJECTS_CID;
93     putInt64 = PutInt64;
94     putDouble = PutDouble;
95     putText = PutText;
96     putTexts = PutTexts;
97     destroy = Destroy;
98 }
99 
GetSelf(OH_VObject * valueObject)100 RelationalPredicatesObjects *RelationalPredicatesObjects::GetSelf(OH_VObject *valueObject)
101 {
102     if (valueObject == nullptr || valueObject->id != OHOS::RdbNdk::RDB_PREDICATES_OBJECTS_CID) {
103         LOG_ERROR("predicates objects invalid. is null %{public}d", (valueObject == nullptr));
104         return nullptr;
105     }
106     return static_cast<OHOS::RdbNdk::RelationalPredicatesObjects *>(valueObject);
107 }
108 
Get()109 std::vector<ValueObject> &RelationalPredicatesObjects::Get()
110 {
111     return values_;
112 }
113 } // namespace RdbNdk
114 } // namespace OHOS