Skip to content

Commit baa6b41

Browse files
committed
moved where_t to a dedicated file
1 parent 3f18ed6 commit baa6b41

File tree

9 files changed

+162
-78
lines changed

9 files changed

+162
-78
lines changed

dev/ast/where.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#pragma once
2+
3+
#include "../serialize_result_type.h"
4+
5+
namespace sqlite_orm {
6+
namespace internal {
7+
8+
struct where_string {
9+
serialize_result_type serialize() const {
10+
return "WHERE";
11+
}
12+
};
13+
14+
/**
15+
* WHERE argument holder.
16+
* C is expression type. Can be any expression like: is_equal_t, is_null_t, exists_t etc
17+
* Don't construct it manually. Call `where(...)` function instead.
18+
*/
19+
template<class C>
20+
struct where_t : where_string {
21+
using expression_type = C;
22+
23+
expression_type expression;
24+
25+
where_t(expression_type expression_) : expression(std::move(expression_)) {}
26+
};
27+
28+
template<class T>
29+
struct is_where : std::false_type {};
30+
31+
template<class T>
32+
struct is_where<where_t<T>> : std::true_type {};
33+
}
34+
35+
/**
36+
* WHERE clause. Use it to add WHERE conditions wherever you like.
37+
* C is expression type. Can be any expression like: is_equal_t, is_null_t, exists_t etc
38+
* @example
39+
* // SELECT name
40+
* // FROM letters
41+
* // WHERE id > 3
42+
* auto rows = storage.select(&Letter::name, where(greater_than(&Letter::id, 3)));
43+
*/
44+
template<class C>
45+
internal::where_t<C> where(C expression) {
46+
return {std::move(expression)};
47+
}
48+
}

dev/ast_iterator.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "function.h"
1414
#include "ast/excluded.h"
1515
#include "ast/upsert_clause.h"
16+
#include "ast/where.h"
1617

1718
namespace sqlite_orm {
1819

@@ -96,8 +97,8 @@ namespace sqlite_orm {
9697
using node_type = where_t<C>;
9798

9899
template<class L>
99-
void operator()(const node_type& where, const L& l) const {
100-
iterate_ast(where.c, l);
100+
void operator()(const node_type& expression, const L& lambda) const {
101+
iterate_ast(expression.expression, lambda);
101102
}
102103
};
103104

dev/conditions.h

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -421,29 +421,6 @@ namespace sqlite_orm {
421421
is_not_null_t(T t_) : t(std::move(t_)) {}
422422
};
423423

424-
struct where_string {
425-
operator std::string() const {
426-
return "WHERE";
427-
}
428-
};
429-
430-
/**
431-
* WHERE argument holder.
432-
* C is conditions type. Can be any condition like: is_equal_t, is_null_t, exists_t etc
433-
*/
434-
template<class C>
435-
struct where_t : where_string {
436-
C c;
437-
438-
where_t(C c_) : c(std::move(c_)) {}
439-
};
440-
441-
template<class T>
442-
struct is_where : std::false_type {};
443-
444-
template<class T>
445-
struct is_where<where_t<T>> : std::true_type {};
446-
447424
struct order_by_base {
448425
int asc_desc = 0; // 1: asc, -1: desc
449426
std::string _collate_argument;
@@ -1261,11 +1238,6 @@ namespace sqlite_orm {
12611238
return {std::move(l), std::move(r)};
12621239
}
12631240

1264-
template<class C>
1265-
internal::where_t<C> where(C c) {
1266-
return {std::move(c)};
1267-
}
1268-
12691241
/**
12701242
* ORDER BY column
12711243
* Example: storage.select(&User::name, order_by(&User::id))

dev/core_functions.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,18 @@
55
#include <type_traits> // std::forward, std::is_base_of, std::enable_if
66
#include <memory> // std::unique_ptr
77
#include <vector> // std::vector
8-
#ifdef SQLITE_ORM_STRING_VIEW_SUPPORTED
9-
#include <string_view>
10-
#endif
118

129
#include "conditions.h"
1310
#include "operators.h"
1411
#include "is_base_of_template.h"
12+
#include "serialize_result_type.h"
1513

1614
namespace sqlite_orm {
1715

1816
using int64 = sqlite_int64;
1917
using uint64 = sqlite_uint64;
2018

2119
namespace internal {
22-
#ifdef SQLITE_ORM_STRING_VIEW_SUPPORTED
23-
using serialize_result_type = std::string_view;
24-
#else
25-
using serialize_result_type = std::string;
26-
#endif
2720

2821
template<class T>
2922
struct is_into;

dev/node_tuple.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "function.h"
1717
#include "ast/excluded.h"
1818
#include "ast/upsert_clause.h"
19+
#include "ast/where.h"
1920

2021
namespace sqlite_orm {
2122

dev/select_constraints.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "is_base_of_template.h"
1111
#include "tuple_helper/tuple_helper.h"
1212
#include "optional_container.h"
13+
#include "ast/where.h"
1314

1415
namespace sqlite_orm {
1516

dev/serialize_result_type.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#ifdef SQLITE_ORM_STRING_VIEW_SUPPORTED
4+
#include <string_view> // string_view
5+
#else
6+
#include <string> // std::string
7+
#endif
8+
9+
namespace sqlite_orm {
10+
namespace internal {
11+
#ifdef SQLITE_ORM_STRING_VIEW_SUPPORTED
12+
using serialize_result_type = std::string_view;
13+
#else
14+
using serialize_result_type = std::string;
15+
#endif
16+
}
17+
}

dev/statement_serializator.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,8 +1658,8 @@ namespace sqlite_orm {
16581658
template<class C>
16591659
std::string operator()(const statement_type& statement, const C& context) const {
16601660
std::stringstream ss;
1661-
ss << static_cast<std::string>(statement) << " ";
1662-
auto whereString = serialize(statement.c, context);
1661+
ss << statement.serialize() << " ";
1662+
auto whereString = serialize(statement.expression, context);
16631663
ss << "( " << whereString << ") ";
16641664
return ss.str();
16651665
}

include/sqlite_orm/sqlite_orm.h

Lines changed: 89 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2865,29 +2865,6 @@ namespace sqlite_orm {
28652865
is_not_null_t(T t_) : t(std::move(t_)) {}
28662866
};
28672867

2868-
struct where_string {
2869-
operator std::string() const {
2870-
return "WHERE";
2871-
}
2872-
};
2873-
2874-
/**
2875-
* WHERE argument holder.
2876-
* C is conditions type. Can be any condition like: is_equal_t, is_null_t, exists_t etc
2877-
*/
2878-
template<class C>
2879-
struct where_t : where_string {
2880-
C c;
2881-
2882-
where_t(C c_) : c(std::move(c_)) {}
2883-
};
2884-
2885-
template<class T>
2886-
struct is_where : std::false_type {};
2887-
2888-
template<class T>
2889-
struct is_where<where_t<T>> : std::true_type {};
2890-
28912868
struct order_by_base {
28922869
int asc_desc = 0; // 1: asc, -1: desc
28932870
std::string _collate_argument;
@@ -3705,11 +3682,6 @@ namespace sqlite_orm {
37053682
return {std::move(l), std::move(r)};
37063683
}
37073684

3708-
template<class C>
3709-
internal::where_t<C> where(C c) {
3710-
return {std::move(c)};
3711-
}
3712-
37133685
/**
37143686
* ORDER BY column
37153687
* Example: storage.select(&User::name, order_by(&User::id))
@@ -4087,9 +4059,6 @@ namespace sqlite_orm {
40874059
#include <type_traits> // std::forward, std::is_base_of, std::enable_if
40884060
#include <memory> // std::unique_ptr
40894061
#include <vector> // std::vector
4090-
#ifdef SQLITE_ORM_STRING_VIEW_SUPPORTED
4091-
#include <string_view>
4092-
#endif
40934062

40944063
// #include "conditions.h"
40954064

@@ -4134,17 +4103,30 @@ namespace sqlite_orm {
41344103
}
41354104
}
41364105

4137-
namespace sqlite_orm {
4106+
// #include "serialize_result_type.h"
41384107

4139-
using int64 = sqlite_int64;
4140-
using uint64 = sqlite_uint64;
4108+
#ifdef SQLITE_ORM_STRING_VIEW_SUPPORTED
4109+
#include <string_view> // string_view
4110+
#else
4111+
#include <string> // std::string
4112+
#endif
41414113

4114+
namespace sqlite_orm {
41424115
namespace internal {
41434116
#ifdef SQLITE_ORM_STRING_VIEW_SUPPORTED
41444117
using serialize_result_type = std::string_view;
41454118
#else
41464119
using serialize_result_type = std::string;
41474120
#endif
4121+
}
4122+
}
4123+
4124+
namespace sqlite_orm {
4125+
4126+
using int64 = sqlite_int64;
4127+
using uint64 = sqlite_uint64;
4128+
4129+
namespace internal {
41484130

41494131
template<class T>
41504132
struct is_into;
@@ -6163,6 +6145,71 @@ namespace sqlite_orm {
61636145

61646146
// #include "optional_container.h"
61656147

6148+
// #include "ast/where.h"
6149+
6150+
// #include "../serialize_result_type.h"
6151+
6152+
#ifdef SQLITE_ORM_STRING_VIEW_SUPPORTED
6153+
#include <string_view> // string_view
6154+
#else
6155+
#include <string> // std::string
6156+
#endif
6157+
6158+
namespace sqlite_orm {
6159+
namespace internal {
6160+
#ifdef SQLITE_ORM_STRING_VIEW_SUPPORTED
6161+
using serialize_result_type = std::string_view;
6162+
#else
6163+
using serialize_result_type = std::string;
6164+
#endif
6165+
}
6166+
}
6167+
6168+
namespace sqlite_orm {
6169+
namespace internal {
6170+
6171+
struct where_string {
6172+
serialize_result_type serialize() const {
6173+
return "WHERE";
6174+
}
6175+
};
6176+
6177+
/**
6178+
* WHERE argument holder.
6179+
* C is expression type. Can be any expression like: is_equal_t, is_null_t, exists_t etc
6180+
* Don't construct it manually. Call `where(...)` function instead.
6181+
*/
6182+
template<class C>
6183+
struct where_t : where_string {
6184+
using expression_type = C;
6185+
6186+
expression_type expression;
6187+
6188+
where_t(expression_type expression_) : expression(std::move(expression_)) {}
6189+
};
6190+
6191+
template<class T>
6192+
struct is_where : std::false_type {};
6193+
6194+
template<class T>
6195+
struct is_where<where_t<T>> : std::true_type {};
6196+
}
6197+
6198+
/**
6199+
* WHERE clause. Use it to add WHERE conditions wherever you like.
6200+
* C is expression type. Can be any expression like: is_equal_t, is_null_t, exists_t etc
6201+
* @example
6202+
* // SELECT name
6203+
* // FROM letters
6204+
* // WHERE id > 3
6205+
* auto rows = storage.select(&Letter::name, where(greater_than(&Letter::id, 3)));
6206+
*/
6207+
template<class C>
6208+
internal::where_t<C> where(C expression) {
6209+
return {std::move(expression)};
6210+
}
6211+
}
6212+
61666213
namespace sqlite_orm {
61676214

61686215
namespace internal {
@@ -10526,6 +10573,8 @@ namespace sqlite_orm {
1052610573
}
1052710574
}
1052810575

10576+
// #include "ast/where.h"
10577+
1052910578
namespace sqlite_orm {
1053010579

1053110580
namespace internal {
@@ -10608,8 +10657,8 @@ namespace sqlite_orm {
1060810657
using node_type = where_t<C>;
1060910658

1061010659
template<class L>
10611-
void operator()(const node_type& where, const L& l) const {
10612-
iterate_ast(where.c, l);
10660+
void operator()(const node_type& expression, const L& lambda) const {
10661+
iterate_ast(expression.expression, lambda);
1061310662
}
1061410663
};
1061510664

@@ -14678,8 +14727,8 @@ namespace sqlite_orm {
1467814727
template<class C>
1467914728
std::string operator()(const statement_type& statement, const C& context) const {
1468014729
std::stringstream ss;
14681-
ss << static_cast<std::string>(statement) << " ";
14682-
auto whereString = serialize(statement.c, context);
14730+
ss << statement.serialize() << " ";
14731+
auto whereString = serialize(statement.expression, context);
1468314732
ss << "( " << whereString << ") ";
1468414733
return ss.str();
1468514734
}
@@ -16667,6 +16716,8 @@ __pragma(pop_macro("min"))
1666716716

1666816717
// #include "ast/upsert_clause.h"
1666916718

16719+
// #include "ast/where.h"
16720+
1667016721
namespace sqlite_orm {
1667116722

1667216723
namespace internal {

0 commit comments

Comments
 (0)