OZO 「お象」
Boost.Asio and libpq based asynchronous PostgreSQL unofficial header-only C++17 client library.
|
|
3 #include <ozo/detail/float.h>
4 #include <ozo/core/strong_typedef.h>
5 #include <ozo/core/nullable.h>
6 #include <ozo/core/unwrap.h>
7 #include <ozo/core/concept.h>
8 #include <ozo/optional.h>
12 #include <boost/hana/at_key.hpp>
13 #include <boost/hana/insert.hpp>
14 #include <boost/hana/string.hpp>
15 #include <boost/hana/map.hpp>
16 #include <boost/hana/pair.hpp>
17 #include <boost/hana/type.hpp>
18 #include <boost/hana/not_equal.hpp>
22 #include <string_view>
24 #include <type_traits>
62 namespace hana = boost::hana;
63 using namespace hana::literals;
65 namespace fusion = boost::fusion;
98 struct is_array : std::false_type {};
149 template <
typename T>
151 inline constexpr
auto Array = is_array<std::decay_t<T>>::value;
154 namespace definitions {
155 template <
typename T>
158 template <
typename T>
164 template <
typename Traits,
typename = std::
void_t<>>
165 struct is_type_traits_defined : std::false_type {};
167 template <
typename T>
168 struct is_type_traits_defined<T,
Require<
169 !std::is_void_v<typename T::name>
170 >> : std::true_type {};
172 template <
typename T>
173 inline auto get_type_traits(
const T&) {
174 using type = unwrap_type<T>;
176 using value_type = unwrap_type<typename type::value_type>;
177 if constexpr (is_type_traits_defined<definitions::array<value_type>>::value) {
178 return definitions::array<value_type>{};
183 if constexpr (is_type_traits_defined<definitions::type<type>>::value) {
184 return definitions::type<type>{};
206 #ifdef OZO_DOCUMENTATION
207 template <
typename T>
209 using name = <implementation defined>;
210 using oid = <implementation defined>;
211 using size = <implementation defined>;
215 template <
typename T>
216 using type_traits = decltype(detail::get_type_traits(std::declval<T>()));
224 template <
typename T>
226 inline constexpr
auto HasDefinition = !std::is_void_v<type_traits<T>>;
229 template <
typename T>
230 struct is_composite : std::bool_constant<
231 HanaStruct<T> || FusionAdaptedStruct<T>
256 template <
typename T>
258 inline constexpr
auto Composite = is_composite<std::decay_t<T>>::value;
277 template <
size_type n>
287 template <
size_type n>
291 template <
typename T,
typename = std::
void_t<>>
292 struct is_built_in : std::false_type {};
294 template <
typename T>
295 struct is_built_in<T, std::enable_if_t<
296 !std::is_void_v<typename type_traits<T>::oid>>
297 > : std::true_type {};
305 template <
typename T>
308 template <
typename T>
309 struct is_dynamic_size : std::is_same<typename type_traits<T>::size, dynamic_size> {};
317 template <
typename T>
326 template <
typename T>
335 template <
typename T>
338 "no type_traits found for the type");
340 constexpr
char const* retval = hana::to<char const*>(name);
350 template <
typename T>
351 constexpr
auto type_name(
const T&) noexcept {
return type_name<T>();}
355 template <
typename Name,
typename O
id =
void,
typename Size = dynamic_size>
356 struct type_definition {
362 template <
typename Type,
typename O
id>
363 using array_definition = type_definition<
371 #define OZO_PG_DEFINE_TYPE_(Type, Name, OidType, Size) \
372 namespace ozo::definitions {\
374 struct type<Type> : detail::type_definition<decltype(Name##_s), OidType, Size>{\
375 static_assert(std::is_same_v<Size, dynamic_size>\
376 || Size::value == null_state_size\
377 || static_cast<size_type>(sizeof(Type)) == Size::value,\
378 #Type " type size does not match to declared size");\
382 #define OZO_PG_DEFINE_TYPE(Type, Name, Oid, Size) \
383 OZO_PG_DEFINE_TYPE_(Type, Name, oid_constant<Oid>, Size)
385 #define OZO_PG_DEFINE_TYPE_ARRAY_(Type, OidType) \
386 namespace ozo::definitions {\
388 struct array<Type> : detail::array_definition<Type, OidType>{};\
391 #define OZO_PG_DEFINE_TYPE_AND_ARRAY_(Type, Name, OidType, ArrayOidType, Size) \
392 OZO_PG_DEFINE_TYPE_(Type, Name, OidType, Size) \
393 OZO_PG_DEFINE_TYPE_ARRAY_(Type, ArrayOidType)
414 #ifdef OZO_DOCUMENTATION
415 #define OZO_PG_DEFINE_TYPE_AND_ARRAY(Type, Name, Oid, ArrayOid, Size)
417 #define OZO_PG_DEFINE_TYPE_AND_ARRAY(Type, Name, Oid, ArrayOid, Size) \
418 OZO_PG_DEFINE_TYPE_AND_ARRAY_(Type, Name, oid_constant<Oid>, oid_constant<ArrayOid>, Size)
454 #ifdef OZO_DOCUMENTATION
455 #define OZO_PG_DEFINE_CUSTOM_TYPE(Type, Name [, Size])
457 #define OZO_PG_DEFINE_CUSTOM_TYPE_IMPL_3(Type, Name, Size) \
458 OZO_PG_DEFINE_TYPE_AND_ARRAY_(Type, Name, void, void, Size)
460 #define OZO_PG_DEFINE_CUSTOM_TYPE_IMPL_2(Type, Name) \
461 OZO_PG_DEFINE_CUSTOM_TYPE_IMPL_3(Type, Name, dynamic_size)
463 #define OZO_PG_DEFINE_CUSTOM_TYPE(...)\
464 BOOST_PP_OVERLOAD(OZO_PG_DEFINE_CUSTOM_TYPE_IMPL_,__VA_ARGS__)(__VA_ARGS__)
471 template <
typename ... T>
473 return hana::make_map(
474 hana::make_pair(hana::type_c<T>,
null_oid()) ...
486 template <
typename ...Ts>
488 using impl_type = decltype(detail::register_types<Ts...>());
494 template <
typename T>
495 struct is_oid_map : std::false_type {};
497 template <
typename ...Ts>
498 struct is_oid_map<oid_map_t<Ts...>> : std::true_type {};
533 template <
typename T>
561 template <
typename ...Ts>
581 template <
typename T,
typename ...Ts>
583 static_assert(!is_built_in<T>::value,
"type must not be built-in");
584 map.impl[hana::type_c<T>] = oid;
594 #ifdef OZO_DOCUMENTATION
595 template <
typename T,
typename O
idMap>
598 template <
typename T,
typename ...Ts>
601 constexpr
auto key = hana::type_c<unwrap_type<T>>;
602 static_assert(decltype(hana::find(map.impl, key) != hana::nothing)::
value,
603 "type OID for T can not be found in the OidMap, it should be registered via register_type()");
604 return map.impl[key];
607 template <
typename T,
typename O
idMap>
609 -> Require<BuiltIn<T>,
oid_t> {
610 static_assert(ozo::OidMap<OidMap>,
"map should model OidMap concept");
622 #ifdef OZO_DOCUMENTATION
623 template <
typename T,
typename O
idMap>
626 template <
typename T,
typename O
idMap>
628 static_assert(ozo::OidMap<OidMap>,
"map should model OidMap concept");
629 return type_oid<std::decay_t<T>>(map);
641 template <
typename T,
typename O
idMap>
643 static_assert(ozo::OidMap<OidMap>,
"map should model OidMap concept");
644 return type_oid<T>(map) == oid;
655 template <
typename T,
typename O
idMap>
657 return accepts_oid<std::decay_t<T>>(map, oid);
673 template <
typename ...Ts>
675 return hana::length(map.impl) == hana::size_c<0>;
Condition indicates if type has corresponding type traits for PostgreSQL.
constexpr bool BuiltIn
Condition indicates if the specified type is built-in for PG.
Definition: type_traits.h:306
Object size constant type.
Definition: type_traits.h:278
constexpr null_oid_t null_oid
Constant for empty OID.
Definition: type_traits.h:95
void set_type_oid(oid_map_t< Ts... > &map, oid_t oid) noexcept
Definition: type_traits.h:582
Array concept represents PostgreSQL array.
constexpr auto DynamicSize
Condition indicates if the specified type is has dynamic size.
Definition: type_traits.h:318
constexpr auto OidMap
Map of C++ types to corresponding PostgreSQL types OIDs.
Definition: type_traits.h:534
constexpr bool empty(const oid_map_t< Ts... > &map) noexcept
Definition: type_traits.h:674
constexpr oid_map_t< Ts... > register_types() noexcept
Provides #OidMap implementation for user-defined types.
Definition: type_traits.h:562
Composite concept represents PostgreSQL composite types.
< implementation defined > oid
ozo::oid_constant with Oid of the built-in type or null_oid_t for non built-in type
Definition: type_traits.h:210
< implementation defined > name
boost::hana::string with name of the type in a database
Definition: type_traits.h:209
Type Require
Concept requirement emulation.
Definition: concept.h:54
Type traits template forward declaration.
Definition: type_traits.h:208
std::int32_t size_type
PostgreSQL size type.
Definition: type_traits.h:265
Database request result value proxy.
Definition: result.h:25
OID constant type.
Definition: type_traits.h:83
bool accepts_oid(const OidMap &map, const T &, oid_t oid) noexcept
Definition: type_traits.h:656
< implementation defined > size
ozo::size_constant with size of the type object in bytes or ozo::dynamic_size in other case
Definition: type_traits.h:211
oid_t type_oid(const OidMap &map, const T &v) noexcept template< typename T
Function returns oid for type from #OidMap.
OidMap implementation type.
Definition: type_traits.h:487
std::decay_t< decltype(register_types<>())> empty_oid_map
Type alias for empty #OidMap.
Definition: type_traits.h:570
constexpr auto type_name(const T &) noexcept
Function returns type name in Postgre SQL.
Definition: type_traits.h:351
::Oid oid_t
PostgreSQL OID type - object identifier.
Definition: type_traits.h:70
constexpr auto StaticSize
Condition indicates if the specified type is has fixed size.
Definition: type_traits.h:327