sqlite3/vector-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 "common.h"
11 // std
12 #include <cassert>
13 #include <cstddef>
14 #include <cstdlib>
15 #include <ctime>
16 #include <string>
17 #include <vector>
18 
19 using namespace soci;
20 using namespace soci::details;
21 using namespace soci::details::sqlite3;
22 
24  int& position, void* data, exchange_type type)
25 {
26  data_ = data;
27  type_ = type;
28  position_ = position++;
29 }
30 
32 {
33  // ...
34 }
35 
36 namespace // anonymous
37 {
38 
39 template <typename T>
40 void set_in_vector(void* p, int indx, T const& val)
41 {
42  assert(NULL != p);
43 
44  std::vector<T>* dest = static_cast<std::vector<T>*>(p);
45  std::vector<T>& v = *dest;
46  v[indx] = val;
47 }
48 
49 } // namespace anonymous
50 
52 {
53  if (!gotData)
54  {
55  // no data retrieved
56  return;
57  }
58 
59  int const endRow = static_cast<int>(statement_.dataCache_.size());
60  for (int i = 0; i < endRow; ++i)
61  {
62  sqlite3_column const& curCol =
63  statement_.dataCache_[i][position_-1];
64 
65  if (curCol.isNull_)
66  {
67  if (ind == NULL)
68  {
69  throw soci_error(
70  "Null value fetched and no indicator defined.");
71  }
72  ind[i] = i_null;
73 
74  // no need to convert data if it is null, go to next row
75  continue;
76  }
77  else
78  {
79  if (ind != NULL)
80  {
81  ind[i] = i_ok;
82  }
83  }
84 
85  const char * buf = curCol.data_.c_str();
86 
87  // set buf to a null string if a null pointer is returned
88  if (buf == NULL)
89  {
90  buf = "";
91  }
92 
93  switch (type_)
94  {
95  case x_char:
96  set_in_vector(data_, i, *buf);
97  break;
98  case x_stdstring:
99  set_in_vector<std::string>(data_, i, buf);
100  break;
101  case x_short:
102  {
103  short const val = string_to_integer<short>(buf);
104  set_in_vector(data_, i, val);
105  }
106  break;
107  case x_integer:
108  {
109  int const val = string_to_integer<int>(buf);
110  set_in_vector(data_, i, val);
111  }
112  break;
113  case x_long_long:
114  {
115  long long const val = string_to_integer<long long>(buf);
116  set_in_vector(data_, i, val);
117  }
118  break;
120  {
121  unsigned long long const val
122  = string_to_unsigned_integer<unsigned long long>(buf);
123  set_in_vector(data_, i, val);
124  }
125  break;
126  case x_double:
127  {
128  double const val = strtod(buf, NULL);
129  set_in_vector(data_, i, val);
130  }
131  break;
132  case x_stdtm:
133  {
134  // attempt to parse the string and convert to std::tm
135  std::tm t;
136  parse_std_tm(buf, t);
137 
138  set_in_vector(data_, i, t);
139  }
140  break;
141  default:
142  throw soci_error("Into element used with non-supported type.");
143  }
144  }
145 }
146 
148 {
149  switch (type_)
150  {
151  // simple cases
152  case x_char:
153  resize_vector<char>(data_, sz);
154  break;
155  case x_short:
156  resize_vector<short>(data_, sz);
157  break;
158  case x_integer:
159  resize_vector<int>(data_, sz);
160  break;
161  case x_long_long:
162  resize_vector<long long>(data_, sz);
163  break;
165  resize_vector<unsigned long long>(data_, sz);
166  break;
167  case x_double:
168  resize_vector<double>(data_, sz);
169  break;
170  case x_stdstring:
171  resize_vector<std::string>(data_, sz);
172  break;
173  case x_stdtm:
174  resize_vector<std::tm>(data_, sz);
175  break;
176  default:
177  throw soci_error("Into vector element used with non-supported type.");
178  }
179 }
180 
182 {
183  std::size_t sz = 0; // dummy initialization to please the compiler
184  switch (type_)
185  {
186  // simple cases
187  case x_char:
188  sz = get_vector_size<char>(data_);
189  break;
190  case x_short:
191  sz = get_vector_size<short>(data_);
192  break;
193  case x_integer:
194  sz = get_vector_size<int>(data_);
195  break;
196  case x_long_long:
197  sz = get_vector_size<long long>(data_);
198  break;
200  sz = get_vector_size<unsigned long long>(data_);
201  break;
202  case x_double:
203  sz = get_vector_size<double>(data_);
204  break;
205  case x_stdstring:
206  sz = get_vector_size<std::string>(data_);
207  break;
208  case x_stdtm:
209  sz = get_vector_size<std::tm>(data_);
210  break;
211  default:
212  throw soci_error("Into vector element used with non-supported type.");
213  }
214 
215  return sz;
216 }
217 
219 {
220  // ...
221 }
void parse_std_tm(char const *buf, std::tm &t)
void post_fetch(bool gotData, indicator *ind)
void define_by_pos(int &position, void *data, details::exchange_type type)


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