a1525f5fa96f882b09e7d5c7521ac1228c433043
[platform/core/security/vist.git] / src / vist / query-builder / expression.hpp
1 /*
2  *  Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  */
16
17 #pragma once
18
19 #include "column.hpp"
20 #include "type.hpp"
21 #include "condition.hpp"
22
23 #include <type_traits>
24
25 namespace vist {
26 namespace tsqb {
27
28 template<typename Type>
29 struct Expression {
30         Type value;
31 };
32
33 template<typename O, typename F>
34 Expression<Column<O, F>> expr(F O::*field)
35 {
36         Column<O, F> anonymous = {"anonymous", field};
37         return {anonymous};
38 }
39
40 template<typename L, typename R>
41 struct Lesser : public condition::Binary<L, R> {
42         using condition::Binary<L, R>::Binary;
43
44         operator std::string() const
45         {
46                 return "<";
47         }
48 };
49
50 template<typename L, typename R>
51 Lesser<L, R> operator<(Expression<L> expr, R r)
52 {
53         return {expr.value, r};
54 }
55
56 template<typename L, typename R>
57 struct Equal : public condition::Binary<L, R>
58 {
59         using condition::Binary<L, R>::Binary;
60
61         operator std::string() const
62         {
63                 return "=";
64         }
65 };
66
67 template<typename L, typename R>
68 Equal<L, R> operator==(Expression<L> expr, R r)
69 {
70         return {expr.value, r};
71 }
72
73 namespace join {
74
75 template<typename L, typename R>
76 struct Equal
77 {
78         L l;
79         R r;
80
81         operator std::string() const
82         {
83                 return "=";
84         }
85 };
86
87 } // namespace join
88
89 template<typename L, typename R>
90 join::Equal<L, R> operator==(Expression<L> l, Expression<R> r)
91 {
92         return {l.value, r.value};
93 }
94
95 template<typename L, typename R>
96 struct Greater : public condition::Binary<L, R>
97 {
98         using condition::Binary<L, R>::Binary;
99
100         operator std::string() const
101         {
102                 return ">";
103         }
104 };
105
106 template<typename L, typename R>
107 Greater<L, R> operator>(Expression<L> expr, R r)
108 {
109         return {expr.value, r};
110 }
111
112 template<bool B, typename T = void>
113 using enable_if_t = typename std::enable_if<B, T>::type;
114
115 template<typename T>
116 using is_condition = typename std::is_base_of<condition::Base, T>;
117
118 template<typename L,
119                  typename R,
120                  typename = typename tsqb::enable_if_t<is_condition<L>::value
121                                                          && tsqb::is_condition<R>::value>>
122 condition::And<L, R> operator&&(const L& l, const R& r)
123 {
124         return {l, r};
125 }
126
127 template<typename L,
128                  typename R,
129                  typename = typename tsqb::enable_if_t<is_condition<L>::value
130                                                          && tsqb::is_condition<R>::value>>
131 condition::Or<L, R> operator||(const L& l, const R& r)
132 {
133         return {l, r};
134 }
135
136 } // namespace tsqb
137 } // namespace vist