sqlite3/standard-into-type.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2006 Maciej Sobczak, Stephen Hutton, David Courtney
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 #include <soci-platform.h>
9 #include "soci-sqlite3.h"
10 #include "rowid.h"
11 #include "common.h"
12 #include "blob.h"
13 // std
14 #include <cstdlib>
15 #include <ctime>
16 #include <string>
17 
18 using namespace soci;
19 using namespace soci::details;
20 using namespace soci::details::sqlite3;
21 
22 void sqlite3_standard_into_type_backend::define_by_pos(int & position, void * data,
23  exchange_type type)
24 {
25  data_ = data;
26  type_ = type;
27  position_ = position++;
28 }
29 
31 {
32  // ...
33 }
34 
36  bool calledFromFetch,
37  indicator * ind)
38 {
39  if (calledFromFetch == true && gotData == false)
40  {
41  // this is a normal end-of-rowset condition,
42  // no need to do anything (fetch() will return false)
43  return;
44  }
45 
46  // sqlite columns start at 0
47  int const pos = position_ - 1;
48 
49  if (gotData)
50  {
51  // first, deal with indicators
52  if (sqlite3_column_type(statement_.stmt_, pos) == SQLITE_NULL)
53  {
54  if (ind == NULL)
55  {
56  throw soci_error(
57  "Null value fetched and no indicator defined.");
58  }
59 
60  *ind = i_null;
61  return;
62  }
63  else
64  {
65  if (ind != NULL)
66  {
67  *ind = i_ok;
68  }
69  }
70 
71  const char *buf = reinterpret_cast<const char*>(
72  sqlite3_column_text(statement_.stmt_,pos));
73 
74  if (buf == NULL)
75  {
76  buf = "";
77  }
78 
79  switch (type_)
80  {
81  case x_char:
82  {
83  char *dest = static_cast<char*>(data_);
84  *dest = *buf;
85  }
86  break;
87  case x_stdstring:
88  {
89  std::string *dest = static_cast<std::string *>(data_);
90  dest->assign(buf);
91  }
92  break;
93  case x_short:
94  {
95  short *dest = static_cast<short*>(data_);
96  long val = std::strtol(buf, NULL, 10);
97  *dest = static_cast<short>(val);
98  }
99  break;
100  case x_integer:
101  {
102  int *dest = static_cast<int*>(data_);
103  long val = std::strtol(buf, NULL, 10);
104  *dest = static_cast<int>(val);
105  }
106  break;
107  case x_long_long:
108  {
109  long long* dest = static_cast<long long*>(data_);
110  *dest = std::strtoll(buf, NULL, 10);
111  }
112  break;
114  {
115  unsigned long long* dest = static_cast<unsigned long long*>(data_);
116  *dest = string_to_unsigned_integer<unsigned long long>(buf);
117  }
118  break;
119  case x_double:
120  {
121  double *dest = static_cast<double*>(data_);
122  double val = strtod(buf, NULL);
123  *dest = static_cast<double>(val);
124  }
125  break;
126  case x_stdtm:
127  {
128  // attempt to parse the string and convert to std::tm
129  std::tm *dest = static_cast<std::tm *>(data_);
130  parse_std_tm(buf, *dest);
131  }
132  break;
133  case x_rowid:
134  {
135  // RowID is internally identical to unsigned long
136 
137  rowid *rid = static_cast<rowid *>(data_);
138  sqlite3_rowid_backend *rbe = static_cast<sqlite3_rowid_backend *>(rid->get_backend());
139  long long val = std::strtoll(buf, NULL, 10);
140  rbe->value_ = static_cast<unsigned long>(val);
141  }
142  break;
143  case x_blob:
144  {
145  blob *b = static_cast<blob *>(data_);
146  sqlite3_blob_backend *bbe =
147  static_cast<sqlite3_blob_backend *>(b->get_backend());
148 
149  buf = reinterpret_cast<const char*>(sqlite3_column_blob(
150  statement_.stmt_,
151  pos));
152 
153  int len = sqlite3_column_bytes(statement_.stmt_, pos);
154  bbe->set_data(buf, len);
155  }
156  break;
157  default:
158  throw soci_error("Into element used with non-supported type.");
159  }
160  }
161 }
162 
164 {
165  // ...
166 }
details::rowid_backend * get_backend()
Definition: rowid.h:33
virtual void define_by_pos(int &position, void *data, details::exchange_type type)
void parse_std_tm(char const *buf, std::tm &t)
virtual void post_fetch(bool gotData, bool calledFromFetch, indicator *ind)
details::blob_backend * get_backend()
Definition: blob.h:39
std::size_t set_data(char const *buf, std::size_t toWrite)


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