Skip to content

Commit d0f7f2f

Browse files
authored
Merge pull request #1475 from fnc12/in-ast-refactor
moved in to a dedicated file
2 parents 10572d8 + 1feb330 commit d0f7f2f

6 files changed

Lines changed: 238 additions & 114 deletions

File tree

dev/ast/in.h

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#pragma once
2+
3+
#ifndef SQLITE_ORM_IMPORT_STD_MODULE
4+
#include <initializer_list> // std::initializer_list
5+
#include <tuple> // std::tuple
6+
#include <utility> // std::move
7+
#include <vector> // std::vector
8+
#endif
9+
10+
#include "../tags.h"
11+
12+
namespace sqlite_orm::internal {
13+
14+
struct in_base {
15+
bool negative = false; // used in not_in
16+
};
17+
18+
/**
19+
* IN operator object.
20+
*/
21+
template<class L, class A>
22+
struct dynamic_in_t : condition_t, in_base, negatable_t {
23+
using self = dynamic_in_t<L, A>;
24+
25+
L left; // left expression
26+
A argument; // in arg
27+
28+
dynamic_in_t(L left_, A argument_, bool negative_) :
29+
in_base{negative_}, left(std::move(left_)), argument(std::move(argument_)) {}
30+
};
31+
32+
template<class L, class... Args>
33+
struct in_t : condition_t, in_base, negatable_t {
34+
L left;
35+
std::tuple<Args...> argument;
36+
37+
in_t(L left_, decltype(argument) argument_, bool negative_) :
38+
in_base{negative_}, left(std::move(left_)), argument(std::move(argument_)) {}
39+
};
40+
41+
}
42+
43+
SQLITE_ORM_EXPORT namespace sqlite_orm {
44+
/**
45+
* IN operator with vector of values.
46+
* Example: in(&User::id, std::vector<int>{1, 2, 3})
47+
* @param left Left expression (column or value to check).
48+
* @param values Vector of values to check against.
49+
* @return dynamic_in_t instance representing IN clause.
50+
*/
51+
template<class L, class E>
52+
internal::dynamic_in_t<L, std::vector<E>> in(L left, std::vector<E> values) {
53+
return {std::move(left), std::move(values), false};
54+
}
55+
56+
/**
57+
* IN operator with initializer list.
58+
* Example: in(&User::id, {1, 2, 3})
59+
* @param left Left expression (column or value to check).
60+
* @param values Initializer list of values to check against.
61+
* @return dynamic_in_t instance representing IN clause.
62+
*/
63+
template<class L, class E>
64+
internal::dynamic_in_t<L, std::vector<E>> in(L left, std::initializer_list<E> values) {
65+
return {std::move(left), std::move(values), false};
66+
}
67+
68+
/**
69+
* IN operator with a subquery or custom argument.
70+
* Example: in(&User::id, select(&Employee::managerId))
71+
* @param left Left expression (column or value to check).
72+
* @param argument Subquery or container to check against.
73+
* @return dynamic_in_t instance representing IN clause.
74+
*/
75+
template<class L, class A>
76+
internal::dynamic_in_t<L, A> in(L left, A argument) {
77+
return {std::move(left), std::move(argument), false};
78+
}
79+
80+
/**
81+
* NOT IN operator with vector of values.
82+
* Example: not_in(&User::id, std::vector<int>{1, 2, 3})
83+
* @param left Left expression (column or value to check).
84+
* @param values Vector of values to check against.
85+
* @return dynamic_in_t instance representing NOT IN clause.
86+
*/
87+
template<class L, class E>
88+
internal::dynamic_in_t<L, std::vector<E>> not_in(L left, std::vector<E> values) {
89+
return {std::move(left), std::move(values), true};
90+
}
91+
92+
/**
93+
* NOT IN operator with initializer list.
94+
* Example: not_in(&User::id, {1, 2, 3})
95+
* @param left Left expression (column or value to check).
96+
* @param values Initializer list of values to check against.
97+
* @return dynamic_in_t instance representing NOT IN clause.
98+
*/
99+
template<class L, class E>
100+
internal::dynamic_in_t<L, std::vector<E>> not_in(L left, std::initializer_list<E> values) {
101+
return {std::move(left), std::move(values), true};
102+
}
103+
104+
/**
105+
* NOT IN operator with a subquery or custom argument.
106+
* Example: not_in(&User::id, select(&Employee::managerId))
107+
* @param left Left expression (column or value to check).
108+
* @param argument Subquery or container to check against.
109+
* @return dynamic_in_t instance representing NOT IN clause.
110+
*/
111+
template<class L, class A>
112+
internal::dynamic_in_t<L, A> not_in(L left, A argument) {
113+
return {std::move(left), std::move(argument), true};
114+
}
115+
}

dev/ast_iterator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "ast/match.h"
2828
#include "ast/cast.h"
2929
#include "ast/limit.h"
30+
#include "ast/in.h"
3031

3132
namespace sqlite_orm::internal {
3233
/**

dev/column_result.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "function.h"
2828
#include "ast/special_keywords.h"
2929
#include "ast/cast.h"
30+
#include "ast/in.h"
3031

3132
namespace sqlite_orm::internal {
3233
/**

dev/conditions.h

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -322,33 +322,6 @@ namespace sqlite_orm::internal {
322322
}
323323
};
324324

325-
struct in_base {
326-
bool negative = false; // used in not_in
327-
};
328-
329-
/**
330-
* IN operator object.
331-
*/
332-
template<class L, class A>
333-
struct dynamic_in_t : condition_t, in_base, negatable_t {
334-
using self = dynamic_in_t<L, A>;
335-
336-
L left; // left expression
337-
A argument; // in arg
338-
339-
dynamic_in_t(L left_, A argument_, bool negative_) :
340-
in_base{negative_}, left(std::move(left_)), argument(std::move(argument_)) {}
341-
};
342-
343-
template<class L, class... Args>
344-
struct in_t : condition_t, in_base, negatable_t {
345-
L left;
346-
std::tuple<Args...> argument;
347-
348-
in_t(L left_, decltype(argument) argument_, bool negative_) :
349-
in_base{negative_}, left(std::move(left_)), argument(std::move(argument_)) {}
350-
};
351-
352325
struct is_null_string {
353326
operator std::string() const {
354327
return "IS NULL";
@@ -1012,36 +985,6 @@ SQLITE_ORM_EXPORT namespace sqlite_orm {
1012985
return {std::move(t)};
1013986
}
1014987

1015-
template<class L, class E>
1016-
internal::dynamic_in_t<L, std::vector<E>> in(L l, std::vector<E> values) {
1017-
return {std::move(l), std::move(values), false};
1018-
}
1019-
1020-
template<class L, class E>
1021-
internal::dynamic_in_t<L, std::vector<E>> in(L l, std::initializer_list<E> values) {
1022-
return {std::move(l), std::move(values), false};
1023-
}
1024-
1025-
template<class L, class A>
1026-
internal::dynamic_in_t<L, A> in(L l, A arg) {
1027-
return {std::move(l), std::move(arg), false};
1028-
}
1029-
1030-
template<class L, class E>
1031-
internal::dynamic_in_t<L, std::vector<E>> not_in(L l, std::vector<E> values) {
1032-
return {std::move(l), std::move(values), true};
1033-
}
1034-
1035-
template<class L, class E>
1036-
internal::dynamic_in_t<L, std::vector<E>> not_in(L l, std::initializer_list<E> values) {
1037-
return {std::move(l), std::move(values), true};
1038-
}
1039-
1040-
template<class L, class A>
1041-
internal::dynamic_in_t<L, A> not_in(L l, A arg) {
1042-
return {std::move(l), std::move(arg), true};
1043-
}
1044-
1045988
template<class L, class R>
1046989
constexpr internal::is_equal_t<L, R> is_equal(L l, R r) {
1047990
return {std::move(l), std::move(r)};

dev/node_tuple.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "ast/match.h"
2727
#include "ast/cast.h"
2828
#include "ast/limit.h"
29+
#include "ast/in.h"
2930

3031
namespace sqlite_orm::internal {
3132
template<class T, class SFINAE = void>

0 commit comments

Comments
 (0)