aboutsummaryrefslogtreecommitdiff
path: root/odb/sqlite/statement.cxx
blob: 417a74660c1f5ca0499b60d428fde3f3fae60d1c (plain)
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// file      : odb/sqlite/statement.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#include <cstring> // std::memcpy
#include <cassert>

#include <odb/exceptions.hxx> // object_not_persistent

#include <odb/sqlite/statement.hxx>
#include <odb/sqlite/connection.hxx>
#include <odb/sqlite/error.hxx>

using namespace std;

namespace odb
{
  namespace sqlite
  {
    // statement
    //

    statement::
    ~statement ()
    {
      sqlite3_finalize (stmt_);
    }

    statement::
    statement (connection& conn, const string& s)
        : conn_ (conn)
    {
      if (int e = sqlite3_prepare_v2 (
            conn_.handle (),
            s.c_str (),
            static_cast<int> (s.size () + 1),
            &stmt_,
            0))
      {
        translate_error (e, conn_);
      }
    }

    statement::
    statement (connection& conn, const char* s, std::size_t n)
        : conn_ (conn)
    {
      if (int e = sqlite3_prepare_v2 (
            conn_.handle (),
            s,
            static_cast<int> (n),
            &stmt_,
            0))
      {
        translate_error (e, conn_);
      }
    }

    void statement::
    bind_param (const bind* p, size_t n, size_t start_param)
    {
      int e (SQLITE_OK);
      start_param++; // SQLite parameters are counted from 1.

      for (size_t i (0); e == SQLITE_OK && i < n; ++i)
      {
        const bind& b (p[i]);
        int j (static_cast<int> (i + start_param));

        if (*b.is_null)
        {
          e = sqlite3_bind_null (stmt_, j);
          continue;
        }

        switch (b.type)
        {
        case bind::integer:
          {
            long long v (*static_cast<long long*> (b.buffer));
            e = sqlite3_bind_int64 (stmt_, j, static_cast<sqlite3_int64> (v));
            break;
          }
        case bind::real:
          {
            double v (*static_cast<double*> (b.buffer));
            e = sqlite3_bind_double (stmt_, j, v);
            break;
          }
        case bind::text:
          {
            e = sqlite3_bind_text (stmt_,
                                   j,
                                   static_cast<const char*> (b.buffer),
                                   static_cast<int> (*b.size),
                                   SQLITE_STATIC);
            break;
          }
        case bind::blob:
          {
            e = sqlite3_bind_blob (stmt_,
                                   j,
                                   b.buffer,
                                   static_cast<int> (*b.size),
                                   SQLITE_STATIC);
            break;
          }
        }
      }

      if (e != SQLITE_OK)
        translate_error (e, conn_);
    }

    bool statement::
    bind_result (const bind* p, size_t n, bool truncated)
    {
      bool r (true);

      for (size_t i (0); i < n; ++i)
      {
        const bind& b (p[i]);
        int j (static_cast<int> (i));

        if (truncated && !*b.truncated)
          continue;

        *b.truncated = false;

        // Check for NULL unless we are reloading a truncated result.
        //
        if (!truncated)
        {
          *b.is_null = sqlite3_column_type (stmt_, j) != SQLITE_NULL;

          if (*b.is_null)
            continue;
        }

        switch (b.type)
        {
        case bind::integer:
          {
            *static_cast<long long*> (b.buffer) =
              static_cast<long long> (sqlite3_column_int64 (stmt_, j));
            break;
          }
        case bind::real:
          {
            *static_cast<double*> (b.buffer) =
              sqlite3_column_double (stmt_, j);
            break;
          }
        case bind::text:
        case bind::blob:
          {
            *b.size = static_cast<size_t> (sqlite3_column_bytes (stmt_, j));

            if (*b.size > b.capacity)
            {
              *b.truncated = true;
              r = false;
              continue;
            }

            const void* d (b.type == bind::text
                           ? sqlite3_column_text (stmt_, j)
                           : sqlite3_column_blob (stmt_, j));

            memcpy (b.buffer, d, *b.size);
            break;
          }
        }
      }

      return r;
    }

    // simple_statement
    //

    simple_statement::
    simple_statement (connection& conn, const string& s)
        : statement (conn, s),
          result_set_ (stmt_ ? sqlite3_column_count (stmt_) != 0: false)
    {
    }

    simple_statement::
    simple_statement (connection& conn, const char* s, std::size_t n)
        : statement (conn, s, n),
          result_set_ (stmt_ ? sqlite3_column_count (stmt_) != 0: false)
    {
    }

    unsigned long long simple_statement::
    execute ()
    {
      if (stmt_ == 0) // Empty statement or comment.
        return 0;

      if (int e = sqlite3_reset (stmt_))
        translate_error (e, conn_);

      unsigned long long r (0);

      int e;
      for (e = sqlite3_step (stmt_); e == SQLITE_ROW; e = sqlite3_step (stmt_))
        r++;

      if (e != SQLITE_DONE)
        translate_error (e, conn_);

      if (!result_set_)
        r = static_cast<unsigned long long> (
          sqlite3_changes (conn_.handle ()));

      return r;
    }

    // select_statement
    //

    select_statement::
    select_statement (connection& conn,
                      const string& s,
                      binding& cond,
                      binding& data)
        : statement (conn, s), cond_ (cond), data_ (data)
    {
    }

    void select_statement::
    execute ()
    {
      done_ = false;

      if (int e = sqlite3_reset (stmt_))
        translate_error (e, conn_);

      bind_param (cond_.bind, cond_.count);
    }

    bool select_statement::
    next ()
    {
      if (!done_)
      {
        int e (sqlite3_step (stmt_));

        switch (e)
        {
        case SQLITE_DONE:
          {
            done_ = true;
            break;
          }
        case SQLITE_ROW:
          {
            break;
          }
        default:
          {
            translate_error (e, conn_);
          }
        }
      }

      return !done_;
    }

    select_statement::result select_statement::
    load ()
    {
      if (done_)
        return no_data;

      return bind_result (data_.bind, data_.count) ? success : truncated;
    }

    void select_statement::
    reload ()
    {
      assert (!done_);

      if (!bind_result (data_.bind, data_.count, true))
        assert (false);
    }

    // insert_statement
    //

    insert_statement::
    insert_statement (connection& conn, const string& s, binding& data)
        : statement (conn, s), data_ (data)
    {
    }

    bool insert_statement::
    execute ()
    {
      if (int e = sqlite3_reset (stmt_))
        translate_error (e, conn_);

      bind_param (data_.bind, data_.count);

      int e (sqlite3_step (stmt_));

      if (e != SQLITE_DONE)
      {
        // SQLITE_CONSTRAINT error code covers more than just a duplicate
        // primary key. Unfortunately, there is nothing more precise that
        // we can use (even sqlite3_errmsg() returns generic "constraint
        // failed").
        //
        if (e == SQLITE_CONSTRAINT)
          return false;
        else
          translate_error (e, conn_);
      }

      return true;
    }

    unsigned long long insert_statement::
    id ()
    {
      return static_cast<unsigned long long> (
        sqlite3_last_insert_rowid (conn_.handle ()));
    }

    // update_statement
    //

    update_statement::
    update_statement (connection& conn,
                      const string& s,
                      binding& cond,
                      binding& data)
        : statement (conn, s), cond_ (cond), data_ (data)
    {
    }

    void update_statement::
    execute ()
    {
      if (int e = sqlite3_reset (stmt_))
        translate_error (e, conn_);

      bind_param (data_.bind, data_.count);
      bind_param (cond_.bind, cond_.count, data_.count);

      int e (sqlite3_step (stmt_));

      if (e != SQLITE_DONE)
        translate_error (e, conn_);

      if (sqlite3_changes (conn_.handle ()) == 0)
        throw object_not_persistent ();
    }

    // delete_statement
    //

    delete_statement::
    delete_statement (connection& conn, const string& s, binding& cond)
        : statement (conn, s), cond_ (cond)
    {
    }

    unsigned long long delete_statement::
    execute ()
    {
      if (int e = sqlite3_reset (stmt_))
        translate_error (e, conn_);

      bind_param (cond_.bind, cond_.count);

      int e (sqlite3_step (stmt_));

      if (e != SQLITE_DONE)
        translate_error (e, conn_);

      return static_cast<unsigned long long> (
        sqlite3_changes (conn_.handle ()));
    }
  }
}