|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#ifndef SQLITE_ORM_IMPORT_STD_MODULE |
| 4 | +#include <string> // std::string |
| 5 | +#include <tuple> // std::tuple |
| 6 | +#include <type_traits> // std::forward, std::move |
| 7 | +#include <utility> // std::forward, std::move |
| 8 | +#endif |
| 9 | + |
| 10 | +#include "../functional/cxx_type_traits_polyfill.h" |
| 11 | + |
| 12 | +namespace sqlite_orm::internal { |
| 13 | + |
| 14 | + struct unbounded_preceding_t {}; |
| 15 | + |
| 16 | + template<class E> |
| 17 | + struct preceding_t { |
| 18 | + E expression; |
| 19 | + }; |
| 20 | + |
| 21 | + struct current_row_t {}; |
| 22 | + |
| 23 | + template<class E> |
| 24 | + struct following_t { |
| 25 | + E expression; |
| 26 | + }; |
| 27 | + |
| 28 | + struct unbounded_following_t {}; |
| 29 | + |
| 30 | + enum class frame_type_t { rows, range, groups }; |
| 31 | + enum class frame_exclude_t { no_others, current_row, group, ties }; |
| 32 | + |
| 33 | + template<class Start, class End> |
| 34 | + struct frame_spec_t { |
| 35 | + frame_type_t type; |
| 36 | + Start start; |
| 37 | + End end; |
| 38 | + frame_exclude_t exclude = frame_exclude_t::no_others; |
| 39 | + |
| 40 | + frame_spec_t exclude_current_row() const { |
| 41 | + auto res = *this; |
| 42 | + res.exclude = frame_exclude_t::current_row; |
| 43 | + return res; |
| 44 | + } |
| 45 | + |
| 46 | + frame_spec_t exclude_group() const { |
| 47 | + auto res = *this; |
| 48 | + res.exclude = frame_exclude_t::group; |
| 49 | + return res; |
| 50 | + } |
| 51 | + |
| 52 | + frame_spec_t exclude_ties() const { |
| 53 | + auto res = *this; |
| 54 | + res.exclude = frame_exclude_t::ties; |
| 55 | + return res; |
| 56 | + } |
| 57 | + |
| 58 | + frame_spec_t exclude_no_others() const { |
| 59 | + auto res = *this; |
| 60 | + res.exclude = frame_exclude_t::no_others; |
| 61 | + return res; |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + template<class... Args> |
| 66 | + struct partition_by_t { |
| 67 | + using arguments_type = std::tuple<Args...>; |
| 68 | + arguments_type arguments; |
| 69 | + }; |
| 70 | + |
| 71 | + template<class T> |
| 72 | + inline constexpr bool is_partition_by_v = polyfill::is_specialization_of_v<T, partition_by_t>; |
| 73 | + |
| 74 | + template<class T> |
| 75 | + using is_partition_by = polyfill::bool_constant<is_partition_by_v<T>>; |
| 76 | + |
| 77 | + struct window_ref_t { |
| 78 | + std::string name; |
| 79 | + }; |
| 80 | + |
| 81 | + template<class F, class... Args> |
| 82 | + struct over_t { |
| 83 | + using function_type = F; |
| 84 | + using arguments_type = std::tuple<Args...>; |
| 85 | + |
| 86 | + function_type function; |
| 87 | + arguments_type arguments; |
| 88 | + }; |
| 89 | + |
| 90 | + template<class T> |
| 91 | + inline constexpr bool is_over_v = polyfill::is_specialization_of_v<T, over_t>; |
| 92 | + |
| 93 | + template<class T> |
| 94 | + using is_over = polyfill::bool_constant<is_over_v<T>>; |
| 95 | + |
| 96 | + template<class... Args> |
| 97 | + struct window_defn_t { |
| 98 | + std::string name; |
| 99 | + using arguments_type = std::tuple<Args...>; |
| 100 | + arguments_type arguments; |
| 101 | + }; |
| 102 | + |
| 103 | + template<class T> |
| 104 | + inline constexpr bool is_window_defn_v = polyfill::is_specialization_of_v<T, window_defn_t>; |
| 105 | + |
| 106 | + template<class T> |
| 107 | + using is_window_defn = polyfill::bool_constant<is_window_defn_v<T>>; |
| 108 | +} |
| 109 | + |
| 110 | +SQLITE_ORM_EXPORT namespace sqlite_orm { |
| 111 | + |
| 112 | + /** |
| 113 | + * UNBOUNDED PRECEDING frame boundary. |
| 114 | + * https://sqlite.org/windowfunctions.html |
| 115 | + */ |
| 116 | + inline internal::unbounded_preceding_t unbounded_preceding() { |
| 117 | + return {}; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * expr PRECEDING frame boundary. |
| 122 | + * https://sqlite.org/windowfunctions.html |
| 123 | + */ |
| 124 | + template<class E> |
| 125 | + internal::preceding_t<E> preceding(E expression) { |
| 126 | + return {std::move(expression)}; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * CURRENT ROW frame boundary. |
| 131 | + * https://sqlite.org/windowfunctions.html |
| 132 | + */ |
| 133 | + inline internal::current_row_t current_row() { |
| 134 | + return {}; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * expr FOLLOWING frame boundary. |
| 139 | + * https://sqlite.org/windowfunctions.html |
| 140 | + */ |
| 141 | + template<class E> |
| 142 | + internal::following_t<E> following(E expression) { |
| 143 | + return {std::move(expression)}; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * UNBOUNDED FOLLOWING frame boundary. |
| 148 | + * https://sqlite.org/windowfunctions.html |
| 149 | + */ |
| 150 | + inline internal::unbounded_following_t unbounded_following() { |
| 151 | + return {}; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * ROWS BETWEEN start AND end frame specification. |
| 156 | + * Example: rows(unbounded_preceding(), current_row()) |
| 157 | + */ |
| 158 | + template<class Start, class End> |
| 159 | + internal::frame_spec_t<Start, End> rows(Start start, End end) { |
| 160 | + return {internal::frame_type_t::rows, std::move(start), std::move(end)}; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * RANGE BETWEEN start AND end frame specification. |
| 165 | + * Example: range(current_row(), unbounded_following()) |
| 166 | + */ |
| 167 | + template<class Start, class End> |
| 168 | + internal::frame_spec_t<Start, End> range(Start start, End end) { |
| 169 | + return {internal::frame_type_t::range, std::move(start), std::move(end)}; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * GROUPS BETWEEN start AND end frame specification. |
| 174 | + * Example: groups(unbounded_preceding(), current_row()) |
| 175 | + */ |
| 176 | + template<class Start, class End> |
| 177 | + internal::frame_spec_t<Start, End> groups(Start start, End end) { |
| 178 | + return {internal::frame_type_t::groups, std::move(start), std::move(end)}; |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * PARTITION BY expression list for window functions. |
| 183 | + * Example: partition_by(&Employee::departmentId) |
| 184 | + */ |
| 185 | + template<class... Args> |
| 186 | + internal::partition_by_t<Args...> partition_by(Args... args) { |
| 187 | + return {{std::forward<Args>(args)...}}; |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Reference to a named window definition (OVER window_name). |
| 192 | + * Example: row_number().over(window_ref("win")) |
| 193 | + */ |
| 194 | + inline internal::window_ref_t window_ref(std::string name) { |
| 195 | + return {std::move(name)}; |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Named window definition (WINDOW name AS (...)). |
| 200 | + * Passed as a condition to select(). |
| 201 | + * Example: window("win", order_by(&Employee::salary)) |
| 202 | + */ |
| 203 | + template<class... Args> |
| 204 | + internal::window_defn_t<Args...> window(std::string name, Args... args) { |
| 205 | + return {std::move(name), {std::forward<Args>(args)...}}; |
| 206 | + } |
| 207 | +} |
0 commit comments