postgresql/common.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2008 Maciej Sobczak, Stephen Hutton
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7 
8 #ifndef SOCI_POSTGRESQL_COMMON_H_INCLUDED
9 #define SOCI_POSTGRESQL_COMMON_H_INCLUDED
10 
11 #include "soci-postgresql.h"
12 #include <limits>
13 #include <cstdio>
14 #include <cstring>
15 #include <ctime>
16 #include <vector>
17 
18 namespace soci
19 {
20 
21 namespace details
22 {
23 
24 namespace postgresql
25 {
26 
27 // helper function for parsing integers
28 template <typename T>
29 T string_to_integer(char const * buf)
30 {
31  long long t(0);
32  int n(0);
33  int const converted = std::sscanf(buf, "%" LL_FMT_FLAGS "d%n", &t, &n);
34  if (converted == 1 && static_cast<std::size_t>(n) == std::strlen(buf))
35  {
36  // successfully converted to long long
37  // and no other characters were found in the buffer
38 
39  const T max = (std::numeric_limits<T>::max)();
40  const T min = (std::numeric_limits<T>::min)();
41  if (t <= static_cast<long long>(max) &&
42  t >= static_cast<long long>(min))
43  {
44  return static_cast<T>(t);
45  }
46  else
47  {
48  // value out of target range
49  throw soci_error("Cannot convert data.");
50  }
51  }
52  else
53  {
54  // try additional conversion from boolean
55  // (PostgreSQL gives 't' or 'f' for boolean results)
56 
57  if (buf[0] == 't' && buf[1] == '\0')
58  {
59  return static_cast<T>(1);
60  }
61  else if (buf[0] == 'f' && buf[1] == '\0')
62  {
63  return static_cast<T>(0);
64  }
65  else
66  {
67  throw soci_error("Cannot convert data.");
68  }
69  }
70 }
71 
72 // helper function for parsing unsigned integers
73 template <typename T>
74 T string_to_unsigned_integer(char const * buf)
75 {
76  unsigned long long t(0);
77  int n(0);
78  int const converted = std::sscanf(buf, "%" LL_FMT_FLAGS "u%n", &t, &n);
79  if (converted == 1 && static_cast<std::size_t>(n) == std::strlen(buf))
80  {
81  // successfully converted to unsigned long long
82  // and no other characters were found in the buffer
83 
84  const T max = (std::numeric_limits<T>::max)();
85  if (t <= static_cast<unsigned long long>(max))
86  {
87  return static_cast<T>(t);
88  }
89  else
90  {
91  // value out of target range
92  throw soci_error("Cannot convert data.");
93  }
94  }
95  else
96  {
97  // try additional conversion from boolean
98  // (PostgreSQL gives 't' or 'f' for boolean results)
99 
100  if (buf[0] == 't' && buf[1] == '\0')
101  {
102  return static_cast<T>(1);
103  }
104  else if (buf[0] == 'f' && buf[1] == '\0')
105  {
106  return static_cast<T>(0);
107  }
108  else
109  {
110  throw soci_error("Cannot convert data.");
111  }
112  }
113 }
114 
115 // helper function for parsing doubles
116 double string_to_double(char const * buf);
117 
118 // helper function for parsing datetime values
119 void parse_std_tm(char const * buf, std::tm & t);
120 
121 // helper for vector operations
122 template <typename T>
123 std::size_t get_vector_size(void * p)
124 {
125  std::vector<T> * v = static_cast<std::vector<T> *>(p);
126  return v->size();
127 }
128 
129 } // namespace postgresql
130 
131 } // namespace details
132 
133 } // namespace soci
134 
135 #endif // SOCI_POSTGRESQL_COMMON_H_INCLUDED
std::size_t get_vector_size(void *p)
T string_to_integer(char const *buf)
T string_to_unsigned_integer(char const *buf)
#define LL_FMT_FLAGS
Definition: soci-platform.h:14
void parse_std_tm(char const *buf, std::tm &t)
double string_to_double(char const *buf)
SOCI_POSTGRESQL_DECL postgresql_backend_factory const postgresql


asr_lib_ism
Author(s): Hanselmann Fabian, Heller Florian, Heizmann Heinrich, Kübler Marcel, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Reckling Reno, Stroh Daniel
autogenerated on Wed Jan 8 2020 04:02:40