userver: userver/storages/postgres/io/interval.hpp Source File
Loading...
Searching...
No Matches
interval.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/postgres/io/interval.hpp
4/// @brief Time intervals I/O support
5/// @ingroup userver_postgres_parse_and_format
6
7#include <chrono>
8
9#include <userver/storages/postgres/exceptions.hpp>
10#include <userver/storages/postgres/io/buffer_io.hpp>
11#include <userver/storages/postgres/io/buffer_io_base.hpp>
12#include <userver/storages/postgres/io/integral_types.hpp>
13#include <userver/storages/postgres/io/traits.hpp>
14#include <userver/storages/postgres/io/type_mapping.hpp>
15
16USERVER_NAMESPACE_BEGIN
17
18namespace storages::postgres::io {
19
20namespace detail {
21
22/// Internal structure for representing PostgreSQL interval type
23struct Interval {
24 using DurationType = std::chrono::microseconds;
25
26 Integer months = 0;
27 Integer days = 0;
28 Bigint microseconds = 0;
29
30 constexpr Interval() = default;
31 constexpr Interval(Integer months, Integer days, Bigint microseconds)
32 : months{months}, days{days}, microseconds{microseconds} {}
33 constexpr explicit Interval(DurationType ms) : microseconds{ms.count()} {}
34
35 constexpr bool operator==(const Interval& rhs) const {
36 return months == rhs.months && days == rhs.days &&
37 microseconds == rhs.microseconds;
38 }
39 constexpr bool operator!=(const Interval& rhs) const {
40 return !(*this == rhs);
41 }
42
43 constexpr DurationType GetDuration() const {
44 if (months != 0) throw UnsupportedInterval{};
45 return std::chrono::duration_cast<DurationType>(
46 std::chrono::microseconds{microseconds} +
47 std::chrono::hours{days * 24});
48 }
49};
50
51} // namespace detail
52
53template <>
57 using ValueType = typename BaseType::ValueType;
58
59 using BaseType::BaseType;
60
61 void operator()(const FieldBuffer& buffer) {
63 std::size_t offset = 0;
67 offset += sizeof(Bigint);
70 tmp.days);
71 offset += sizeof(Integer);
74 tmp.months);
75
76 std::swap(this->value, tmp);
77 }
78};
79
80template <>
84
85 using BaseType::BaseType;
86
87 template <typename Buffer>
88 void operator()(const UserTypes& types, Buffer& buffer) const {
92 }
93};
94
95template <>
96struct CppToSystemPg<detail::Interval>
97 : PredefinedOid<PredefinedOids::kInterval> {};
98
99} // namespace storages::postgres::io
100
101USERVER_NAMESPACE_END