OZO 「お象」
Boost.Asio and libpq based asynchronous PostgreSQL unofficial header-only C++17 client library.
query.h
1 #pragma once
2 
3 #include <ozo/detail/functional.h>
4 #include <ozo/core/concept.h>
5 #include <ozo/type_traits.h>
6 
29 namespace ozo {
30 
31 template <typename T, typename = hana::when<true>>
32 struct to_const_char_impl {};
33 
34 template <typename T>
35 struct to_const_char_impl<T, hana::when<HanaString<T>>> {
36  static constexpr const char* apply(const T& v) noexcept {
37  return hana::to<const char*>(v);
38  }
39 };
40 
41 template <>
42 struct to_const_char_impl<std::string> {
43  static const char* apply(const std::string& v) noexcept {
44  return v.data();
45  }
46 };
47 
48 template <>
49 struct to_const_char_impl<std::string_view> {
50  static constexpr const char* apply(std::string_view v) noexcept {
51  return v.data();
52  }
53 };
54 
55 template <>
56 struct to_const_char_impl<const char*> {
57  static constexpr const char* apply(const char* v) noexcept {
58  return v;
59  }
60 };
61 
62 #ifdef OZO_DOCUMENTATION
63 
97 template <typename T>
98 constexpr auto to_const_char(const T& text) noexcept;
99 #else
100 template <typename T>
101 inline constexpr detail::result_of<to_const_char_impl, T> to_const_char(const T& v) noexcept {
102  static_assert(noexcept(detail::apply<to_const_char_impl>(v)),
103  "to_const_char_impl::apply() should be noexcept");
104  static_assert(std::is_same_v<const char*, detail::result_of<to_const_char_impl, T>>,
105  "to_const_char_impl::apply() should return const char*");
106  return detail::apply<to_const_char_impl>(v);
107 }
108 #endif
109 
110 template <class, class = std::void_t<>>
111 struct is_query_text : std::false_type {};
112 
113 template <class T>
114 struct is_query_text<T, std::void_t<
115  decltype(ozo::to_const_char(std::declval<const T&>()))
116 >> : std::true_type {};
117 
137 template <typename T>
139 inline constexpr auto QueryText = is_query_text<std::decay_t<T>>::value;
141 
142 template <typename T, typename = hana::when<true>>
143 struct get_query_text_impl;
144 
145 #ifdef OZO_DOCUMENTATION
146 
176 template <typename Query>
177 constexpr auto get_query_text(Query&& query);
178 #else
179 template <typename T>
180 inline constexpr detail::result_of<get_query_text_impl, T> get_query_text(T&& query) {
181  return detail::apply<get_query_text_impl>(std::forward<T>(query));
182 }
183 #endif
184 
185 template <typename T, typename = hana::when<true>>
186 struct get_query_params_impl;
187 
188 #ifdef OZO_DOCUMENTATION
189 
218 template <typename T>
219 constexpr auto get_query_params(Query&& query);
220 #else
221 template <typename T>
222 inline constexpr detail::result_of<get_query_params_impl, T> get_query_params(T&& query) {
223  return detail::apply<get_query_params_impl>(std::forward<T>(query));
224 }
225 #endif
226 
227 template <class, class = std::void_t<>>
228 struct is_query : std::false_type {};
229 
230 template <class T>
231 struct is_query<T, std::void_t<
232  decltype(get_query_text(std::declval<const T&>())),
233  decltype(get_query_params(std::declval<const T&>()))
234 >> : std::bool_constant<
235  QueryText<decltype(get_query_text(std::declval<const T&>()))>
236  && HanaSequence<decltype(get_query_params(std::declval<const T&>()))>
237 > {};
238 
266 template <typename T>
268 inline constexpr auto Query = is_query<std::decay_t<T>>::value;
270 
295 template <class QueryText , class ... Params>
296 inline constexpr auto make_query(QueryText&& text, Params&& ...params);
297 
298 template <class ... ParamsT>
299 inline constexpr auto make_query(const char* text, ParamsT&& ... params) {
300  return make_query(std::string_view(text), std::forward<ParamsT>(params)...);
301 }
302 
303 template <class T, class = Require<Query<T>>>
304 decltype(auto) get_text(const T& query) {
305  return get_query_text(query);
306 }
307 
308 template <class T, class = Require<Query<T>>>
309 decltype(auto) get_params(const T& query) {
310  return get_query_params(query);
311 }
312 
313 } // namespace ozo
314 
315 #include <ozo/impl/query.h>
ozo::make_query
constexpr auto make_query(QueryText &&text, Params &&...params)
Construct built-in Query implementation object.
ozo::get_query_params
constexpr auto get_query_params(Query &&query)
Get the query parameters.
Query
Query concept
ozo::get_query_text
constexpr auto get_query_text(Query &&query)
Get the query text object.
ozo::to_const_char
constexpr auto to_const_char(const T &text) noexcept
Convert QueryText to const char*.
QueryText
Query text concept