db2/standard-into-type.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2011-2013 Denis Chapligin
3 // Copyright (C) 2004-2006 Maciej Sobczak, Stephen Hutton, David Courtney
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 
9 #define SOCI_DB2_SOURCE
10 #include "soci-db2.h"
11 #include "common.h"
12 #include <ctime>
13 
14 using namespace soci;
15 using namespace soci::details;
16 
17 
19  int & position, void * data, exchange_type type)
20 {
21  this->data = data;
22  this->type = type;
23  this->position = position;
24  position++;
25 
26  SQLUINTEGER size = 0;
27 
28  switch (type)
29  {
30  case x_char:
31  cType = SQL_C_CHAR;
32  size = sizeof(char) + 1;
33  buf = new char[size];
34  data = buf;
35  break;
36  case x_stdstring:
37  cType = SQL_C_CHAR;
38  // Patch: set to min between column size and 100MB (used ot be 32769)
39  // Column size for text data type can be too large for buffer allocation
40  size = statement_.column_size(this->position);
42  size++;
43  buf = new char[size];
44  data = buf;
45  break;
46  case x_short:
47  cType = SQL_C_SSHORT;
48  size = sizeof(short);
49  break;
50  case x_integer:
51  cType = SQL_C_SLONG;
52  size = sizeof(SQLINTEGER);
53  break;
54  case x_long_long:
55  cType = SQL_C_SBIGINT;
56  size = sizeof(long long);
57  break;
59  cType = SQL_C_UBIGINT;
60  size = sizeof(unsigned long long);
61  break;
62  case x_double:
63  cType = SQL_C_DOUBLE;
64  size = sizeof(double);
65  break;
66  case x_stdtm:
67  cType = SQL_C_TYPE_TIMESTAMP;
68  size = sizeof(TIMESTAMP_STRUCT);
69  buf = new char[size];
70  data = buf;
71  break;
72  case x_rowid:
73  cType = SQL_C_UBIGINT;
74  size = sizeof(unsigned long long);
75  break;
76  default:
77  throw soci_error("Into element used with non-supported type.");
78  }
79 
80  valueLen = 0;
81 
82  SQLRETURN cliRC = SQLBindCol(statement_.hStmt, static_cast<SQLUSMALLINT>(this->position),
83  static_cast<SQLUSMALLINT>(cType), data, size, &valueLen);
84  if (cliRC != SQL_SUCCESS)
85  {
86  throw db2_soci_error("Error while pre-fething into type",cliRC);
87  }
88 }
89 
91 {
92  //...
93 }
94 
96  bool gotData, bool calledFromFetch, indicator * ind)
97 {
98  if (calledFromFetch == true && gotData == false)
99  {
100  // this is a normal end-of-rowset condition,
101  // no need to do anything (fetch() will return false)
102  return;
103  }
104 
105  if (gotData)
106  {
107  // first, deal with indicators
108  if (SQL_NULL_DATA == valueLen)
109  {
110  if (ind == NULL)
111  {
112  throw soci_error(
113  "Null value fetched and no indicator defined.");
114  }
115 
116  *ind = i_null;
117  return;
118  }
119  else
120  {
121  if (ind != NULL)
122  {
123  *ind = i_ok;
124  }
125  }
126 
127  // only std::string and std::tm need special handling
128  if (type == x_char)
129  {
130  char *c = static_cast<char*>(data);
131  *c = buf[0];
132  }
133  if (type == x_stdstring)
134  {
135  std::string *s = static_cast<std::string *>(data);
136  *s = buf;
137  if (s->size() >= (details::db2::cli_max_buffer - 1))
138  {
139  throw soci_error("Buffer size overflow; maybe got too large string");
140  }
141  }
142  else if (type == x_stdtm)
143  {
144  std::tm *t = static_cast<std::tm *>(data);
145 
146  TIMESTAMP_STRUCT * ts = reinterpret_cast<TIMESTAMP_STRUCT*>(buf);
147  t->tm_isdst = -1;
148  t->tm_year = ts->year - 1900;
149  t->tm_mon = ts->month - 1;
150  t->tm_mday = ts->day;
151  t->tm_hour = ts->hour;
152  t->tm_min = ts->minute;
153  t->tm_sec = ts->second;
154 
155  // normalize and compute the remaining fields
156  std::mktime(t);
157  }
158  }
159 }
160 
162 {
163  if (buf)
164  {
165  delete [] buf;
166  buf = 0;
167  }
168 }
void define_by_pos(int &position, void *data, details::exchange_type type)
const std::size_t cli_max_buffer
Definition: db2/common.h:17
void post_fetch(bool gotData, bool calledFromFetch, indicator *ind)


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