postgresql/standard-into-type.cpp
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 #define SOCI_POSTGRESQL_SOURCE
9 #include <soci-platform.h>
10 #include "soci-postgresql.h"
11 #include "common.h"
12 #include "rowid.h"
13 #include "blob.h"
14 #include <libpq/libpq-fs.h> // libpq
15 #include <cctype>
16 #include <cstdio>
17 #include <cstring>
18 #include <cstdlib>
19 #include <ctime>
20 #include <sstream>
21 
22 #ifdef SOCI_POSTGRESQL_NOPARAMS
23 #ifndef SOCI_POSTGRESQL_NOBINDBYNAME
24 #define SOCI_POSTGRESQL_NOBINDBYNAME
25 #endif // SOCI_POSTGRESQL_NOBINDBYNAME
26 #endif // SOCI_POSTGRESQL_NOPARAMS
27 
28 using namespace soci;
29 using namespace soci::details;
30 using namespace soci::details::postgresql;
31 
32 
34  int & position, void * data, exchange_type type)
35 {
36  data_ = data;
37  type_ = type;
38  position_ = position++;
39 }
40 
42 {
43  // nothing to do here
44 }
45 
47  bool gotData, bool calledFromFetch, indicator * ind)
48 {
49  if (calledFromFetch == true && gotData == false)
50  {
51  // this is a normal end-of-rowset condition,
52  // no need to do anything (fetch() will return false)
53  return;
54  }
55 
56  if (gotData)
57  {
58  // postgresql_ positions start at 0
59  int const pos = position_ - 1;
60 
61  // first, deal with indicators
62  if (PQgetisnull(statement_.result_, statement_.currentRow_, pos) != 0)
63  {
64  if (ind == NULL)
65  {
66  throw soci_error(
67  "Null value fetched and no indicator defined.");
68  }
69 
70  *ind = i_null;
71 
72  // no need to convert data if it is null
73  return;
74  }
75  else
76  {
77  if (ind != NULL)
78  {
79  *ind = i_ok;
80  }
81  }
82 
83  // raw data, in text format
84  char const * buf = PQgetvalue(statement_.result_,
85  statement_.currentRow_, pos);
86 
87  switch (type_)
88  {
89  case x_char:
90  {
91  char * dest = static_cast<char *>(data_);
92  *dest = *buf;
93  }
94  break;
95  case x_stdstring:
96  {
97  std::string * dest = static_cast<std::string *>(data_);
98  dest->assign(buf);
99  }
100  break;
101  case x_short:
102  {
103  short * dest = static_cast<short *>(data_);
104  *dest = string_to_integer<short>(buf);
105  }
106  break;
107  case x_integer:
108  {
109  int * dest = static_cast<int *>(data_);
110  *dest = string_to_integer<int>(buf);
111  }
112  break;
113  case x_long_long:
114  {
115  long long * dest = static_cast<long long *>(data_);
116  *dest = string_to_integer<long long>(buf);
117  }
118  break;
120  {
121  unsigned long long * dest = static_cast<unsigned long long *>(data_);
122  *dest = string_to_unsigned_integer<unsigned long long>(buf);
123  }
124  break;
125  case x_double:
126  {
127  double * dest = static_cast<double *>(data_);
128  *dest = string_to_double(buf);
129  }
130  break;
131  case x_stdtm:
132  {
133  // attempt to parse the string and convert to std::tm
134  std::tm * dest = static_cast<std::tm *>(data_);
135  parse_std_tm(buf, *dest);
136  }
137  break;
138  case x_rowid:
139  {
140  // RowID is internally identical to unsigned long
141 
142  rowid * rid = static_cast<rowid *>(data_);
144  = static_cast<postgresql_rowid_backend *>(
145  rid->get_backend());
146 
147  rbe->value_ = string_to_unsigned_integer<unsigned long>(buf);
148  }
149  break;
150  case x_blob:
151  {
152  unsigned long oid =
153  string_to_unsigned_integer<unsigned long>(buf);
154 
155  int fd = lo_open(statement_.session_.conn_, oid,
156  INV_READ | INV_WRITE);
157  if (fd == -1)
158  {
159  throw soci_error("Cannot open the blob object.");
160  }
161 
162  blob * b = static_cast<blob *>(data_);
164  = static_cast<postgresql_blob_backend *>(b->get_backend());
165 
166  if (bbe->fd_ != -1)
167  {
168  lo_close(statement_.session_.conn_, bbe->fd_);
169  }
170 
171  bbe->fd_ = fd;
172  bbe->oid_ = oid;
173  }
174  break;
175 
176  default:
177  throw soci_error("Into element used with non-supported type.");
178  }
179  }
180 }
181 
183 {
184  // nothing to do here
185 }
virtual void post_fetch(bool gotData, bool calledFromFetch, indicator *ind)
details::rowid_backend * get_backend()
Definition: rowid.h:33
virtual void define_by_pos(int &position, void *data, details::exchange_type type)
details::blob_backend * get_backend()
Definition: blob.h:39
void parse_std_tm(char const *buf, std::tm &t)
double string_to_double(char const *buf)


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:41