mysql/standard-use-type.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2006 Maciej Sobczak, Stephen Hutton
3 // MySQL backend copyright (C) 2006 Pawel Aleksander Fedorynski
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_MYSQL_SOURCE
10 #include "soci-mysql.h"
11 #include "common.h"
12 #include <soci-platform.h>
13 // std
14 #include <ciso646>
15 #include <cstdio>
16 #include <cstring>
17 #include <limits>
18 
19 #ifdef _MSC_VER
20 #pragma warning(disable:4355)
21 #endif
22 
23 using namespace soci;
24 using namespace soci::details;
25 using namespace soci::details::mysql;
26 
27 
29  int &position, void *data, exchange_type type, bool /* readOnly */)
30 {
31  data_ = data;
32  type_ = type;
33  position_ = position++;
34 }
35 
37  std::string const &name, void *data, exchange_type type, bool /* readOnly */)
38 {
39  data_ = data;
40  type_ = type;
41  name_ = name;
42 }
43 
45 {
46  if (ind != NULL && *ind == i_null)
47  {
48  buf_ = new char[5];
49  std::strcpy(buf_, "NULL");
50  }
51  else
52  {
53  // allocate and fill the buffer with text-formatted client data
54  switch (type_)
55  {
56  case x_char:
57  {
58  char buf[] = { *static_cast<char*>(data_), '\0' };
59  buf_ = quote(statement_.session_.conn_, buf, 1);
60  }
61  break;
62  case x_stdstring:
63  {
64  std::string *s = static_cast<std::string *>(data_);
65  buf_ = quote(statement_.session_.conn_,
66  s->c_str(), s->size());
67  }
68  break;
69  case x_short:
70  {
71  std::size_t const bufSize
72  = std::numeric_limits<short>::digits10 + 3;
73  buf_ = new char[bufSize];
74  snprintf(buf_, bufSize, "%d",
75  static_cast<int>(*static_cast<short*>(data_)));
76  }
77  break;
78  case x_integer:
79  {
80  std::size_t const bufSize
81  = std::numeric_limits<int>::digits10 + 3;
82  buf_ = new char[bufSize];
83  snprintf(buf_, bufSize, "%d", *static_cast<int*>(data_));
84  }
85  break;
86  case x_long_long:
87  {
88  std::size_t const bufSize
89  = std::numeric_limits<long long>::digits10 + 3;
90  buf_ = new char[bufSize];
91  snprintf(buf_, bufSize, "%" LL_FMT_FLAGS "d", *static_cast<long long *>(data_));
92  }
93  break;
95  {
96  std::size_t const bufSize
97  = std::numeric_limits<unsigned long long>::digits10 + 3;
98  buf_ = new char[bufSize];
99  snprintf(buf_, bufSize, "%" LL_FMT_FLAGS "u",
100  *static_cast<unsigned long long *>(data_));
101  }
102  break;
103 
104  case x_double:
105  {
106  if (is_infinity_or_nan(*static_cast<double*>(data_))) {
107  throw soci_error(
108  "Use element used with infinity or NaN, which are "
109  "not supported by the MySQL server.");
110  }
111 
112  std::size_t const bufSize = 100;
113  buf_ = new char[bufSize];
114 
115  snprintf(buf_, bufSize, "%.20g",
116  *static_cast<double*>(data_));
117  }
118  break;
119  case x_stdtm:
120  {
121  std::size_t const bufSize = 22;
122  buf_ = new char[bufSize];
123 
124  std::tm *t = static_cast<std::tm *>(data_);
125  snprintf(buf_, bufSize,
126  "\'%d-%02d-%02d %02d:%02d:%02d\'",
127  t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
128  t->tm_hour, t->tm_min, t->tm_sec);
129  }
130  break;
131  default:
132  throw soci_error("Use element used with non-supported type.");
133  }
134  }
135 
136  if (position_ > 0)
137  {
138  // binding by position
139  statement_.useByPosBuffers_[position_] = &buf_;
140  }
141  else
142  {
143  // binding by name
144  statement_.useByNameBuffers_[name_] = &buf_;
145  }
146 }
147 
148 void mysql_standard_use_type_backend::post_use(bool /*gotData*/, indicator* /*ind*/)
149 {
150  // TODO: Is it possible to have the bound element being overwritten
151  // by the database?
152  // If not, then nothing to do here, please remove this comment.
153  // If yes, then use the value of the readOnly parameter:
154  // - true: the given object should not be modified and the backend
155  // should detect if the modification was performed on the
156  // isolated buffer and throw an exception if the buffer was modified
157  // (this indicates logic error, because the user used const object
158  // and executed a query that attempted to modified it)
159  // - false: the modification should be propagated to the given object.
160  // ...
161 
162  clean_up();
163 }
164 
166 {
167  if (buf_ != NULL)
168  {
169  delete [] buf_;
170  buf_ = NULL;
171  }
172 }
bool is_infinity_or_nan(T x)
Definition: mysql/common.h:39
virtual void post_use(bool gotData, indicator *ind)
virtual void bind_by_name(std::string const &name, void *data, details::exchange_type type, bool readOnly)
virtual void bind_by_pos(int &position, void *data, details::exchange_type type, bool readOnly)
char * quote(MYSQL *conn, const char *s, int len)
virtual void pre_use(indicator const *ind)
#define LL_FMT_FLAGS
Definition: soci-platform.h:14


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