1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
//
// Copyright (C) 2004-2016 Maciej Sobczak, Stephen Hutton
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
//
#ifndef SOCI_USE_TYPE_H_INCLUDED
#define SOCI_USE_TYPE_H_INCLUDED
#include "soci/soci-platform.h"
#include "soci/soci-backend.h"
#include "soci/type-ptr.h"
#include "soci/exchange-traits.h"
// std
#include <cstddef>
#include <ostream>
#include <string>
#include <vector>
namespace soci { namespace details {
class statement_impl;
// this is intended to be a base class for all classes that deal with
// binding input data (and OUT PL/SQL variables)
class SOCI_DECL use_type_base
{
public:
virtual ~use_type_base() {}
virtual void bind(statement_impl & st, int & position) = 0;
virtual std::string get_name() const = 0;
virtual void dump_value(std::ostream& os, int index) const = 0;
virtual void pre_exec(int num) = 0;
virtual void pre_use() = 0;
virtual void post_use(bool gotData) = 0;
virtual void clean_up() = 0;
virtual std::size_t size() const = 0; // returns the number of elements
};
typedef type_ptr<use_type_base> use_type_ptr;
class SOCI_DECL standard_use_type : public use_type_base
{
public:
standard_use_type(void* data, exchange_type type,
bool readOnly, std::string const& name = std::string())
: data_(data)
, type_(type)
, ind_(NULL)
, readOnly_(readOnly)
, name_(name)
, backEnd_(NULL)
{
// FIXME: This was added with Ilia's patch
// https://github.com/SOCI/soci/commit/c166625a28f7c907318134f625ff5acea7d9a1f8
// but it seems to be a troublemaker, causing duplicated conversions
//convert_to_base();
}
standard_use_type(void* data, exchange_type type, indicator& ind,
bool readOnly, std::string const& name = std::string())
: data_(data)
, type_(type)
, ind_(&ind)
, readOnly_(readOnly)
, name_(name)
, backEnd_(NULL)
{
// FIXME
//convert_to_base();
}
~standard_use_type() override;
void bind(statement_impl & st, int & position) override;
std::string get_name() const override { return name_; }
void dump_value(std::ostream& os, int index) const override;
virtual void * get_data() { return data_; }
// conversion hook (from arbitrary user type to base type)
virtual void convert_to_base() {}
virtual void convert_from_base() {}
protected:
void pre_use() override;
private:
void pre_exec(int num) override;
void post_use(bool gotData) override;
void clean_up() override;
std::size_t size() const override { return 1; }
void* data_;
exchange_type type_;
indicator* ind_;
bool readOnly_;
std::string name_;
standard_use_type_backend* backEnd_;
};
class SOCI_DECL vector_use_type : public use_type_base
{
public:
vector_use_type(void* data, exchange_type type,
std::string const& name = std::string())
: data_(data)
, type_(type)
, ind_(NULL)
, begin_(0)
, end_(NULL)
, name_(name)
, backEnd_(NULL)
{}
vector_use_type(void* data, exchange_type type,
std::size_t begin, std::size_t * end,
std::string const& name = std::string())
: data_(data)
, type_(type)
, ind_(NULL)
, begin_(begin)
, end_(end)
, name_(name)
, backEnd_(NULL)
{}
vector_use_type(void* data, exchange_type type,
std::vector<indicator> const& ind,
std::string const& name = std::string())
: data_(data)
, type_(type)
, ind_(&ind)
, begin_(0)
, end_(NULL)
, name_(name)
, backEnd_(NULL)
{}
vector_use_type(void* data, exchange_type type,
std::vector<indicator> const& ind,
std::size_t begin, std::size_t * end,
std::string const& name = std::string())
: data_(data)
, type_(type)
, ind_(&ind)
, begin_(begin)
, end_(end)
, name_(name)
, backEnd_(NULL)
{}
~vector_use_type() override;
private:
void bind(statement_impl& st, int & position) override;
std::string get_name() const override { return name_; }
void dump_value(std::ostream& os, int index) const override;
void pre_exec(int num) override;
void pre_use() override;
void post_use(bool) override { /* nothing to do */ }
void clean_up() override;
std::size_t size() const override;
void* data_;
exchange_type type_;
std::vector<indicator> const* ind_;
std::size_t begin_;
std::size_t * end_;
std::string name_;
vector_use_type_backend * backEnd_;
virtual void convert_to_base() {}
};
// implementation for the basic types (those which are supported by the library
// out of the box without user-provided conversions)
template <typename T>
class use_type : public standard_use_type
{
public:
use_type(T& t, std::string const& name = std::string())
: standard_use_type(&t,
static_cast<exchange_type>(exchange_traits<T>::x_type), false, name)
{}
use_type(T const& t, std::string const& name = std::string())
: standard_use_type(const_cast<T*>(&t),
static_cast<exchange_type>(exchange_traits<T>::x_type), true, name)
{}
use_type(T& t, indicator& ind, std::string const& name = std::string())
: standard_use_type(&t,
static_cast<exchange_type>(exchange_traits<T>::x_type), ind, false, name)
{}
use_type(T const& t, indicator& ind, std::string const& name = std::string())
: standard_use_type(const_cast<T*>(&t),
static_cast<exchange_type>(exchange_traits<T>::x_type), ind, true, name)
{}
};
template <typename T>
class use_type<std::vector<T> > : public vector_use_type
{
public:
use_type(std::vector<T>& v, std::string const& name = std::string())
: vector_use_type(&v,
static_cast<exchange_type>(exchange_traits<T>::x_type), name)
{}
use_type(std::vector<T>& v, std::size_t begin, std::size_t * end,
std::string const& name = std::string())
: vector_use_type(&v,
static_cast<exchange_type>(exchange_traits<T>::x_type), begin, end, name)
{}
use_type(std::vector<T> const& v, std::string const& name = std::string())
: vector_use_type(const_cast<std::vector<T>*>(&v),
static_cast<exchange_type>(exchange_traits<T>::x_type), name)
{}
use_type(std::vector<T> const& v, std::size_t begin, std::size_t * end,
std::string const& name = std::string())
: vector_use_type(const_cast<std::vector<T>*>(&v),
static_cast<exchange_type>(exchange_traits<T>::x_type), begin, end, name)
{}
use_type(std::vector<T>& v, std::vector<indicator> const& ind,
std::string const& name = std::string())
: vector_use_type(&v,
static_cast<exchange_type>(exchange_traits<T>::x_type), ind, name)
{}
use_type(std::vector<T>& v, std::vector<indicator> const& ind,
std::size_t begin, std::size_t * end, std::string const& name = std::string())
: vector_use_type(&v,
static_cast<exchange_type>(exchange_traits<T>::x_type), ind, begin, end, name)
{}
use_type(std::vector<T> const& v, std::vector<indicator> const& ind,
std::string const& name = std::string())
: vector_use_type(const_cast<std::vector<T> *>(&v),
static_cast<exchange_type>(exchange_traits<T>::x_type), ind, name)
{}
use_type(std::vector<T> const& v, std::vector<indicator> const& ind,
std::size_t begin, std::size_t * end, std::string const& name = std::string())
: vector_use_type(const_cast<std::vector<T> *>(&v),
static_cast<exchange_type>(exchange_traits<T>::x_type), ind, begin, end, name)
{}
};
// helper dispatchers for basic types
template <typename T>
use_type_ptr do_use(T & t, std::string const & name, basic_type_tag)
{
return use_type_ptr(new use_type<T>(t, name));
}
template <typename T>
use_type_ptr do_use(T const & t, std::string const & name, basic_type_tag)
{
return use_type_ptr(new use_type<T>(t, name));
}
template <typename T>
use_type_ptr do_use(T & t, indicator & ind,
std::string const & name, basic_type_tag)
{
return use_type_ptr(new use_type<T>(t, ind, name));
}
template <typename T>
use_type_ptr do_use(T const & t, indicator & ind,
std::string const & name, basic_type_tag)
{
return use_type_ptr(new use_type<T>(t, ind, name));
}
template <typename T>
use_type_ptr do_use(T & t, std::vector<indicator> & ind,
std::string const & name, basic_type_tag)
{
return use_type_ptr(new use_type<T>(t, ind, name));
}
template <typename T>
use_type_ptr do_use(T const & t, std::vector<indicator> & ind,
std::string const & name, basic_type_tag)
{
return use_type_ptr(new use_type<T>(t, ind, name));
}
template <typename T>
use_type_ptr do_use(std::vector<T> & t,
std::size_t begin, std::size_t * end,
std::string const & name, basic_type_tag)
{
return use_type_ptr(
new use_type<std::vector<T> >(t, begin, end, name));
}
template <typename T>
use_type_ptr do_use(const std::vector<T> & t,
std::size_t begin, std::size_t * end,
std::string const & name, basic_type_tag)
{
return use_type_ptr(
new use_type<std::vector<T> >(t, begin, end, name));
}
template <typename T>
use_type_ptr do_use(std::vector<T> & t, std::vector<indicator> & ind,
std::size_t begin, std::size_t * end,
std::string const & name, basic_type_tag)
{
return use_type_ptr(
new use_type<std::vector<T> >(t, ind, begin, end, name));
}
template <typename T>
use_type_ptr do_use(const std::vector<T> & t, std::vector<indicator> & ind,
std::size_t begin, std::size_t * end,
std::string const & name, basic_type_tag)
{
return use_type_ptr(
new use_type<std::vector<T> >(t, ind, begin, end, name));
}
} // namespace details
} // namesapce soci
#endif // SOCI_USE_TYPE_H_INCLUDED
|