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