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 && microseconds == rhs.microseconds;
37 }
38 constexpr bool operator!=(const Interval& rhs) const { return !(*this == rhs); }
39
40 constexpr DurationType GetDuration() const {
41 if (months != 0) throw UnsupportedInterval{};
42 return std::chrono::duration_cast<DurationType>(
43 std::chrono::microseconds{microseconds} + std::chrono::hours{days * 24}
44 );
45 }
46};
47
48} // namespace detail
49
50template <>
51struct BufferParser<detail::Interval> : detail::BufferParserBase<detail::Interval> {
52 using BaseType = detail::BufferParserBase<detail::Interval>;
53 using ValueType = typename BaseType::ValueType;
54
55 using BaseType::BaseType;
56
57 void operator()(const FieldBuffer& buffer) {
58 ValueType tmp;
59 std::size_t offset = 0;
60 io::ReadBuffer(buffer.GetSubBuffer(offset, sizeof(Bigint), BufferCategory::kPlainBuffer), tmp.microseconds);
61 offset += sizeof(Bigint);
62 io::ReadBuffer(buffer.GetSubBuffer(offset, sizeof(Integer), BufferCategory::kPlainBuffer), tmp.days);
63 offset += sizeof(Integer);
64 io::ReadBuffer(buffer.GetSubBuffer(offset, sizeof(Integer), BufferCategory::kPlainBuffer), tmp.months);
65
66 std::swap(this->value, tmp);
67 }
68};
69
70template <>
71struct BufferFormatter<detail::Interval> : detail::BufferFormatterBase<detail::Interval> {
72 using BaseType = detail::BufferFormatterBase<detail::Interval>;
73
74 using BaseType::BaseType;
75
76 template <typename Buffer>
77 void operator()(const UserTypes& types, Buffer& buffer) const {
78 io::WriteBuffer(types, buffer, this->value.microseconds);
79 io::WriteBuffer(types, buffer, this->value.days);
80 io::WriteBuffer(types, buffer, this->value.months);
81 }
82};
83
84template <>
85struct CppToSystemPg<detail::Interval> : PredefinedOid<PredefinedOids::kInterval> {};
86
87} // namespace storages::postgres::io
88
89USERVER_NAMESPACE_END