1 /*
2  * Copyright (c) 2022 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 
16 #ifndef OHOS_DISTRIBUTED_DATA_FRAMEWORKS_INNERKITSIMPL_NATIVE_PREFERENCES_INCLUDE_COV_UTIL_H
17 #define OHOS_DISTRIBUTED_DATA_FRAMEWORKS_INNERKITSIMPL_NATIVE_PREFERENCES_INCLUDE_COV_UTIL_H
18 
19 #include <string>
20 #include <variant>
21 #include <set>
22 #include "data_query.h"
23 
24 namespace OHOS {
25 namespace DistributedKv {
26 class CovUtil final {
27 public:
28     template<typename _VTp, typename _TTp, typename _First>
FillField(const std::string & field,const _VTp & data,_TTp & target)29     static auto FillField(const std::string &field, const _VTp &data, _TTp &target)
30     {
31         return target();
32     }
33 
34     template<typename _VTp, typename _TTp, typename _First, typename _Second, typename ..._Rest>
FillField(const std::string & field,const _VTp & data,_TTp & target)35     static auto FillField(const std::string &field, const _VTp &data, _TTp &target)
36     {
37         if ((sizeof ...(_Rest) + 1) == data.index()) {
38             return target(field, std::get<(sizeof ...(_Rest) + 1)>(data));
39         }
40         return FillField<_VTp, _TTp, _Second, _Rest...>(field, data, target);
41     }
42 
43     template<typename _TTp, typename ..._Types>
FillField(const std::string & field,const std::variant<_Types...> & data,_TTp & target)44     static auto FillField(const std::string &field, const std::variant<_Types...> &data, _TTp &target)
45     {
46         return FillField<decltype(data), _TTp, _Types...>(field, data, target);
47     }
48 };
49 
50 enum class QueryType {
51     EQUAL = 0,
52     NOT_EQUAL = 1,
53     GREATER = 2,
54     LESS = 3,
55     GREATER_OR_EQUAL = 4,
56     LESS_OR_EQUAL = 5,
57     IN = 6,
58     NOT_IN = 7
59 };
60 
61 class Querys {
62 public:
Querys(OHOS::DistributedKv::DataQuery * dataQuery,QueryType type)63     Querys(OHOS::DistributedKv::DataQuery *dataQuery, QueryType type) : dataQuery_(dataQuery), type_(type) {};
64     template<typename T>
operator()65     int operator()(const std::string &field, const T &value)
66     {
67         if (type_ == QueryType::EQUAL) {
68             dataQuery_->EqualTo(field, value);
69         } else if (type_ == QueryType::NOT_EQUAL) {
70             dataQuery_->NotEqualTo(field, value);
71         } else if (type_ == QueryType::GREATER) {
72             dataQuery_->GreaterThan(field, value);
73         } else if (type_ == QueryType::LESS) {
74             dataQuery_->LessThan(field, value);
75         } else if (type_ == QueryType::GREATER_OR_EQUAL) {
76             dataQuery_->GreaterThanOrEqualTo(field, value);
77         } else if (type_ == QueryType::LESS_OR_EQUAL) {
78             dataQuery_->LessThanOrEqualTo(field, value);
79         }
80         return 0;
81     }
82 
operator()83     int operator()()
84     {
85         return 0;
86     }
87 
88 private:
89     OHOS::DistributedKv::DataQuery *dataQuery_;
90     QueryType type_;
91 };
92 
93 class InOrNotIn {
94 public:
InOrNotIn(OHOS::DistributedKv::DataQuery * dataQuery,QueryType type)95     InOrNotIn(OHOS::DistributedKv::DataQuery *dataQuery, QueryType type) : dataQuery_(dataQuery), type_(type)  {};
96     template<typename T>
operator()97     int operator()(const std::string &field, const std::vector<T> &value)
98     {
99         if (type_ == QueryType::IN) {
100             dataQuery_->In(field, value);
101         } else if (type_ == QueryType::NOT_IN) {
102             dataQuery_->NotIn(field, value);
103         }
104         return 0;
105     }
106 
operator()107     int operator()()
108     {
109         return 0;
110     }
111 
112 private:
113     OHOS::DistributedKv::DataQuery *dataQuery_;
114     QueryType type_;
115 };
116 }
117 }
118 #endif // OHOS_DISTRIBUTED_DATA_FRAMEWORKS_INNERKITSIMPL_NATIVE_PREFERENCES_INCLUDE_COV_UTIL_H
119