[go: up one dir, main page]

File: use-type.cpp

package info (click to toggle)
soci 4.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,944 kB
  • sloc: ansic: 169,887; cpp: 54,198; javascript: 12,258; ada: 1,973; sh: 36; makefile: 12; xml: 2
file content (324 lines) | stat: -rw-r--r-- 8,249 bytes parent folder | download
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
//
// 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)
//

#define SOCI_SOURCE
#include "soci/soci-platform.h"
#include "soci/use-type.h"
#include "soci/statement.h"
#include "soci/soci-unicode.h"
#include "soci-exchange-cast.h"
#include "soci-mktime.h"
#include "soci-vector-helpers.h"

#include <cstdio>

using namespace soci;
using namespace soci::details;

namespace
{

// Helper returning pointer to a vector element at the given index.
//
// This is only used in this file currently but could be extracted into
// soci-vector-helpers.h if it turns out to be useful elsewhere.

void* get_vector_element(exchange_type e, void* data, int index)
{
    switch (e)
    {
        case x_char:
            return &exchange_vector_type_cast<x_char>(data).at(index);
        case x_stdstring:
            return &exchange_vector_type_cast<x_stdstring>(data).at(index);
        case x_stdwstring:
            return &exchange_vector_type_cast<x_stdwstring>(data).at(index);
        case x_int8:
            return &exchange_vector_type_cast<x_int8>(data).at(index);
        case x_uint8:
            return &exchange_vector_type_cast<x_uint8>(data).at(index);
        case x_int16:
            return &exchange_vector_type_cast<x_int16>(data).at(index);
        case x_uint16:
            return &exchange_vector_type_cast<x_uint16>(data).at(index);
        case x_int32:
            return &exchange_vector_type_cast<x_int32>(data).at(index);
        case x_uint32:
            return &exchange_vector_type_cast<x_uint32>(data).at(index);
        case x_int64:
            return &exchange_vector_type_cast<x_int64>(data).at(index);
        case x_uint64:
            return &exchange_vector_type_cast<x_uint64>(data).at(index);
        case x_double:
            return &exchange_vector_type_cast<x_double>(data).at(index);
        case x_stdtm:
            return &exchange_vector_type_cast<x_stdtm>(data).at(index);
        case x_xmltype:
            return &exchange_vector_type_cast<x_xmltype>(data).at(index);
        case x_longstring:
            return &exchange_vector_type_cast<x_longstring>(data).at(index);
        case x_statement:
            // There is no vector of statements, but this is fine because we
            // don't use this value in do_dump_value anyhow, so we may just
            // return null.
            return NULL;
        case x_rowid:
            // Same as for x_statement above.
            return NULL;
        case x_blob:
            return &exchange_vector_type_cast<x_blob>(data).at(index);
    }

    return NULL;
}

// Common part of scalar and vector use types.
void
do_dump_value(std::ostream& os,
              exchange_type type,
              void* data,
              indicator const* ind)
{
    if (ind && *ind == i_null)
    {
        os << "NULL";
        return;
    }

    switch (type)
    {
        case x_char:
            os << "'" << exchange_type_cast<x_char>(data) << "'";
            return;

        case x_stdstring:
            // TODO: Escape quotes?
            os << "\"" << exchange_type_cast<x_stdstring>(data) << "\"";
            return;

        case x_stdwstring:
            os << "\"" << wide_to_utf8(exchange_type_cast<x_stdwstring>(data)) << "\"";
            return;

        case x_int8:
            os << exchange_type_cast<x_int8>(data);
            return;

        case x_uint8:
            os << exchange_type_cast<x_uint8>(data);
            return;

        case x_int16:
            os << exchange_type_cast<x_int16>(data);
            return;

        case x_uint16:
            os << exchange_type_cast<x_uint16>(data);
            return;

        case x_int32:
            os << exchange_type_cast<x_int32>(data);
            return;

        case x_uint32:
            os << exchange_type_cast<x_uint32>(data);
            return;

        case x_int64:
            os << exchange_type_cast<x_int64>(data);
            return;

        case x_uint64:
            os << exchange_type_cast<x_uint64>(data);
            return;

        case x_double:
            os << exchange_type_cast<x_double>(data);
            return;

        case x_stdtm:
            {
                std::tm const& t = exchange_type_cast<x_stdtm>(data);

                char buf[80];
                format_std_tm(t, buf, sizeof(buf));

                os << buf;
            }
            return;

        case x_statement:
            os << "<statement>";
            return;

        case x_rowid:
            os << "<rowid>";
            return;

        case x_blob:
            os << "<blob>";
            return;

        case x_xmltype:
            os << "<xml>";
            return;

        case x_longstring:
            os << "<long string>";
            return;
    }

    // This is normally unreachable, but avoid throwing from here as we're
    // typically called from an exception handler.
    os << "<unknown>";
}

} // anonymous namespace

standard_use_type::~standard_use_type()
{
    delete backEnd_;
}

void standard_use_type::bind(statement_impl & st, int & position)
{
    if (backEnd_ == NULL)
    {
        backEnd_ = st.make_use_type_backend();
    }

    if (name_.empty())
    {
        backEnd_->bind_by_pos(position, data_, type_, readOnly_);
    }
    else
    {
        backEnd_->bind_by_name(name_, data_, type_, readOnly_);
    }
}

void standard_use_type::dump_value(std::ostream& os, int /* index */) const
{
    do_dump_value(os, type_, data_, ind_);
}

void standard_use_type::pre_exec(int num)
{
    backEnd_->pre_exec(num);
}

void standard_use_type::pre_use()
{
    // Handle IN direction of parameters of SQL statements and procedures
    convert_to_base();
    backEnd_->pre_use(ind_);
}

void standard_use_type::post_use(bool gotData)
{
    // Handle OUT direction of IN/OUT parameters of stored procedures
    backEnd_->post_use(gotData, ind_);
    convert_from_base();

    // IMPORTANT:
    // This treatment of input ("use") parameter as output data sink may be
    // confusing, but it is necessary to store OUT data back in the same
    // object as IN, of IN/OUT parameter.
    // As there is no symmetry for IN/OUT in SQL and there are no OUT/IN
    // we do not perform convert_to_base() for output ("into") parameter.
    // See conversion_use_type<T>::convert_from_base() for more details.
}

void standard_use_type::clean_up()
{
    if (backEnd_ != NULL)
    {
        backEnd_->clean_up();
    }
}

vector_use_type::~vector_use_type()
{
    delete backEnd_;
}

void vector_use_type::bind(statement_impl & st, int & position)
{
    if (backEnd_ == NULL)
    {
        backEnd_ = st.make_vector_use_type_backend();
    }

    if (name_.empty())
    {
        if (end_ != NULL)
        {
            backEnd_->bind_by_pos_bulk(position, data_, type_, begin_, end_);
        }
        else
        {
            backEnd_->bind_by_pos(position, data_, type_);
        }
    }
    else
    {
        if (end_ != NULL)
        {
            backEnd_->bind_by_name_bulk(name_, data_, type_, begin_, end_);
        }
        else
        {
            backEnd_->bind_by_name(name_, data_, type_);
        }
    }
}

void vector_use_type::dump_value(std::ostream& os, int index) const
{
    if (index != -1)
    {
        do_dump_value(
            os,
            type_,
            get_vector_element(type_, data_, index),
            ind_ ? &ind_->at(index) : NULL
        );
    }
    else
    {
        // We can't dump the whole vector, which could be huge, and it would be
        // pretty useless to do it anyhow as it still wouldn't give any
        // information about which vector element corresponds to the row which
        // triggered the error.
        os << "<vector>";
    }
}

void vector_use_type::pre_exec(int num)
{
    backEnd_->pre_exec(num);
}

void vector_use_type::pre_use()
{
    convert_to_base();

    backEnd_->pre_use(ind_ ? &ind_->at(0) : NULL);
}

std::size_t vector_use_type::size() const
{
    return backEnd_->size();
}

void vector_use_type::clean_up()
{
    if (backEnd_ != NULL)
    {
        backEnd_->clean_up();
    }
}