Skip to content

Commit 8e174bc

Browse files
committed
We add a new struct to be able to express that a sort order
like asc or desc can be more spefied via nulls first or last.
1 parent f3f344d commit 8e174bc

3 files changed

Lines changed: 98 additions & 5 deletions

File tree

include/sqlpp23/core/operator/comparison_functions.h

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,19 +169,42 @@ enum class sort_type {
169169
template <typename L>
170170
requires(values_are_comparable<L, L>::value)
171171
constexpr auto asc(L l) -> sort_order_expression<L> {
172-
return {l, sort_type::asc};
172+
return {std::move(l), sort_type::asc};
173173
}
174174

175175
template <typename L>
176176
requires(values_are_comparable<L, L>::value)
177177
constexpr auto desc(L l) -> sort_order_expression<L> {
178-
return {l, sort_type::desc};
178+
return {std::move(l), sort_type::desc};
179179
}
180180

181181
template <typename L>
182182
requires(values_are_comparable<L, L>::value)
183183
constexpr auto order(L l, sort_type order) -> sort_order_expression<L> {
184-
return {l, order};
184+
return {std::move(l), order};
185+
}
186+
187+
enum class nulls_pos {
188+
first,
189+
last,
190+
};
191+
192+
template <typename L>
193+
requires(values_are_comparable<L, L>::value)
194+
constexpr auto nulls_first(L l) -> sort_order_expression<L> {
195+
return {std::move(l), {std::nullopt , nulls_pos::first}};
196+
}
197+
198+
template <typename L>
199+
requires(values_are_comparable<L, L>::value)
200+
constexpr auto nulls_last(L l) -> sort_order_expression<L> {
201+
return {std::move(l), {std::nullopt , nulls_pos::last}};
202+
}
203+
204+
template <typename L>
205+
requires(values_are_comparable<L, L>::value)
206+
constexpr auto nulls_order(L l, nulls_pos pos) -> sort_order_expression<L> {
207+
return {std::move(l), {std::nullopt , std::move(pos)}};
185208
}
186209

187210
} // namespace sqlpp

include/sqlpp23/core/operator/enable_comparison.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,24 @@ class enable_comparison {
125125
return ::sqlpp::order(std::forward<Expr>(self), t);
126126
}
127127

128+
template <typename Expr>
129+
constexpr auto nulls_first(this Expr&& self)
130+
-> decltype(::sqlpp::nulls_first(std::forward<Expr>(self))) {
131+
return ::sqlpp::nulls_first(std::forward<Expr>(self));
132+
}
133+
134+
template <typename Expr>
135+
constexpr auto nulls_last(this Expr&& self)
136+
-> decltype(::sqlpp::nulls_last(std::forward<Expr>(self))) {
137+
return ::sqlpp::nulls_last(std::forward<Expr>(self));
138+
}
139+
140+
template <typename Expr>
141+
constexpr auto nulls_order(this Expr&& self, ::sqlpp::nulls_pos t)
142+
-> decltype(::sqlpp::nulls_order(std::forward<Expr>(self), t)) {
143+
return ::sqlpp::nulls_order(std::forward<Expr>(self), t);
144+
}
145+
128146
template <typename Expr, typename R>
129147
constexpr auto like(this Expr&& self, R r)
130148
-> decltype(::sqlpp::like(std::forward<Expr>(self), std::move(r))) {

include/sqlpp23/core/operator/sort_order_expression.h

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,60 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3232
#include <sqlpp23/core/reader.h>
3333

3434
namespace sqlpp {
35+
36+
template <typename L>
37+
struct sort_order_with_nulls_expression {
38+
constexpr sort_order_with_nulls_expression(sort_order_expression<L> lhs, nulls_pos rhs)
39+
: _lhs(std::move(lhs)), _rhs(std::move(rhs)) {}
40+
sort_order_with_nulls_expression(const sort_order_with_nulls_expression&) = default;
41+
sort_order_with_nulls_expression(sort_order_with_nulls_expression&&) = default;
42+
sort_order_with_nulls_expression& operator=(const sort_order_with_nulls_expression&) = default;
43+
sort_order_with_nulls_expression& operator=(sort_order_with_nulls_expression&&) = default;
44+
~sort_order_with_nulls_expression() = default;
45+
46+
private:
47+
friend reader_t;
48+
const sort_order_expression<L> _lhs;
49+
const nulls_pos _rhs;
50+
};
51+
3552
template <typename L>
3653
struct sort_order_expression {
37-
constexpr sort_order_expression(L l, sort_type r)
38-
: _lhs(std::move(l)), _rhs(std::move(r)) {}
54+
constexpr sort_order_expression(L lhs, sort_type rhs)
55+
: _lhs(std::move(lhs)), _rhs(std::move(rhs)) {}
3956
sort_order_expression(const sort_order_expression&) = default;
4057
sort_order_expression(sort_order_expression&&) = default;
4158
sort_order_expression& operator=(const sort_order_expression&) = default;
4259
sort_order_expression& operator=(sort_order_expression&&) = default;
4360
~sort_order_expression() = default;
4461

62+
constexpr auto nulls_first() -> sort_order_with_nulls_expression<L>;
63+
constexpr auto nulls_last() -> sort_order_with_nulls_expression<L>;
64+
constexpr auto nulls_order(::sqlpp::nulls_pos t) -> sort_order_with_nulls_expression<L>;
65+
4566
private:
4667
friend reader_t;
4768
L _lhs;
4869
sort_type _rhs;
4970
};
5071

72+
73+
74+
template <typename L>
75+
constexpr auto sort_order_expression<L>::nulls_first() -> sort_order_with_nulls_expression<L> {
76+
return {std::move(_lhs), ::sqlpp::nulls_pos::first};
77+
}
78+
79+
template <typename L>
80+
constexpr auto sort_order_expression<L>::nulls_last() -> sort_order_with_nulls_expression<L> {
81+
return {std::move(_lhs), ::sqlpp::nulls_pos::last};
82+
};
83+
84+
template <typename L>
85+
constexpr auto sort_order_expression<L>::nulls_order(::sqlpp::nulls_pos t) -> sort_order_with_nulls_expression<L> {
86+
return {std::move(_lhs), std::move(t)};
87+
}
88+
5189
template <typename L>
5290
struct nodes_of<sort_order_expression<L>> {
5391
using type = detail::type_vector<L>;
@@ -64,6 +102,20 @@ auto to_sql_string(Context&, const sort_type& t) -> std::string {
64102
return " DESC";
65103
}
66104

105+
template <typename Context>
106+
auto to_sql_string(Context&, const nulls_pos& t) -> std::string {
107+
if (t == nulls_pos::first) {
108+
return " NULLS FIRST";
109+
}
110+
return " NULLS LAST";
111+
}
112+
113+
template <typename Context>
114+
auto to_sql_string(Context& context, const sort_order& t)
115+
-> std::string {
116+
return to_sql_string(context, read.lhs(t)) + to_sql_string(context, read.rhs(t));
117+
}
118+
67119
template <typename Context, typename L>
68120
auto to_sql_string(Context& context, const sort_order_expression<L>& t)
69121
-> std::string {

0 commit comments

Comments
 (0)