include/sqlon/order.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /****************************************************************************** | ||
| 2 | ** | ||
| 3 | ** Copyright (C) 2026 Ivan Pinezhaninov <ivan.pinezhaninov@gmail.com> | ||
| 4 | ** | ||
| 5 | ** This file is part of the SQLon - which can be found at | ||
| 6 | ** https://github.com/IvanPinezhaninov/sqlon/. | ||
| 7 | ** | ||
| 8 | ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 9 | ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 10 | ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
| 11 | ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
| 12 | ** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
| 13 | ** OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR | ||
| 14 | ** THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 15 | ** | ||
| 16 | ******************************************************************************/ | ||
| 17 | |||
| 18 | #ifndef SQLON_ORDER_H | ||
| 19 | #define SQLON_ORDER_H | ||
| 20 | |||
| 21 | #include <sqlon/export.h> | ||
| 22 | #include <sqlon/expression.h> | ||
| 23 | |||
| 24 | #include <utility> | ||
| 25 | |||
| 26 | namespace sqlon { | ||
| 27 | |||
| 28 | namespace detail { | ||
| 29 | struct ordering_access; | ||
| 30 | } | ||
| 31 | |||
| 32 | /** @brief ORDER BY expression with direction and optional NULL placement. */ | ||
| 33 | class ordering final { | ||
| 34 | public: | ||
| 35 | /** @brief Returns the ordering with NULL values placed first. */ | ||
| 36 | SQLON_API ordering nulls_first() const; | ||
| 37 | |||
| 38 | /** @brief Returns the ordering with NULL values placed last. */ | ||
| 39 | SQLON_API ordering nulls_last() const; | ||
| 40 | |||
| 41 | private: | ||
| 42 | friend struct detail::ordering_access; | ||
| 43 | |||
| 44 | 17 | explicit ordering(detail::order_by_node node) | |
| 45 | 17 | : m_node(std::move(node)) | |
| 46 | 17 | {} | |
| 47 | |||
| 48 | detail::order_by_node m_node; | ||
| 49 | }; | ||
| 50 | |||
| 51 | namespace detail { | ||
| 52 | |||
| 53 | struct ordering_access final { | ||
| 54 | 17 | static ordering make(order_by_node node) | |
| 55 | { | ||
| 56 | 17 | return ordering{std::move(node)}; | |
| 57 | } | ||
| 58 | |||
| 59 | 17 | static const order_by_node& node(const ordering& value) noexcept | |
| 60 | { | ||
| 61 | 17 | return value.m_node; | |
| 62 | } | ||
| 63 | |||
| 64 | 4 | static order_by_node& node(ordering& value) noexcept | |
| 65 | { | ||
| 66 | 4 | return value.m_node; | |
| 67 | } | ||
| 68 | }; | ||
| 69 | |||
| 70 | } // namespace detail | ||
| 71 | |||
| 72 | /** @brief Creates an ascending ordering for an expression. */ | ||
| 73 | template<typename T> | ||
| 74 | 10 | ordering asc(const expression<T>& value) | |
| 75 | { | ||
| 76 | return detail::ordering_access::make( | ||
| 77 | 10 | {detail::expression_access::node(value), detail::order_direction::ascending, detail::nulls_order::unspecified}); | |
| 78 | 10 | } | |
| 79 | |||
| 80 | /** @brief Creates a descending ordering for an expression. */ | ||
| 81 | template<typename T> | ||
| 82 | 7 | ordering desc(const expression<T>& value) | |
| 83 | { | ||
| 84 | return detail::ordering_access::make( | ||
| 85 | 7 | {detail::expression_access::node(value), detail::order_direction::descending, detail::nulls_order::unspecified}); | |
| 86 | 7 | } | |
| 87 | |||
| 88 | template<typename T> | ||
| 89 | 1 | ordering expression<T>::asc() const | |
| 90 | { | ||
| 91 | 1 | return sqlon::asc(*this); | |
| 92 | } | ||
| 93 | |||
| 94 | template<typename T> | ||
| 95 | 1 | ordering expression<T>::desc() const | |
| 96 | { | ||
| 97 | 1 | return sqlon::desc(*this); | |
| 98 | } | ||
| 99 | |||
| 100 | } // namespace sqlon | ||
| 101 | |||
| 102 | #endif // SQLON_ORDER_H | ||
| 103 |