aboutsummaryrefslogtreecommitdiff
path: root/odb/sqlite/statement.cxx
blob: 9071f68bf0f047c7fe37491dc21efa8647e4cd49 (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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// 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 ()
    {
      finilize ();
    }

    void statement::
    init (const char* s, std::size_t n)
    {
      int e;
      sqlite3_stmt* stmt (0);
      while ((e = sqlite3_prepare_v2 (conn_.handle (),
                                      s,
                                      static_cast<int> (n),
                                      &stmt,
                                      0)) == SQLITE_LOCKED)
      {
        conn_.wait ();
      }

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

      stmt_.reset (stmt);

      active_ = false;
      cached_ = false;

      prev_ = 0;
      next_ = this;

      list_add (); // Add to the list because we are uncached.
    }

    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 != 0 && *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)
    {
      // Make sure that the number of columns in the result returned by
      // the database matches the number that we expect. A common cause
      // of this assertion is a native view with a number of data members
      // not matching the number of columns in the SELECT-list.
      //
      assert (static_cast<size_t> (sqlite3_data_count (stmt_)) == n);

      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 == 0 || !*b.truncated))
          continue;

        if (b.truncated != 0)
          *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)
            {
              if (b.truncated != 0)
                *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;

      unsigned long long r (0);

      // Only the first call to sqlite3_step() can return SQLITE_LOCKED.
      //
      int e;
      sqlite3* h (conn_.handle ());
      while ((e = sqlite3_step (stmt_)) == SQLITE_LOCKED)
      {
        if (sqlite3_extended_errcode (h) != SQLITE_LOCKED_SHAREDCACHE)
          break;

        sqlite3_reset (stmt_);
        conn_.wait ();
      }

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

      sqlite3_reset (stmt_);

      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)
    {
    }

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

    void select_statement::
    execute ()
    {
      if (active ())
        reset ();

      done_ = false;

      if (cond_ != 0)
        bind_param (cond_->bind, cond_->count);

      active (true);
    }

    void select_statement::
    free_result ()
    {
      reset ();
      done_ = true;
    }

    bool select_statement::
    next ()
    {
      if (!done_)
      {
        int e;
        sqlite3* h (conn_.handle ());
        while ((e = sqlite3_step (stmt_)) == SQLITE_LOCKED)
        {
          if (sqlite3_extended_errcode (h) != SQLITE_LOCKED_SHAREDCACHE)
            break;

          sqlite3_reset (stmt_);
          conn_.wait ();
        }

        if (e != SQLITE_ROW)
        {
          reset ();
          done_ = true;

          if (e != SQLITE_DONE)
            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 ()
    {
      bind_param (data_.bind, data_.count);

      int e;
      sqlite3* h (conn_.handle ());
      while ((e = sqlite3_step (stmt_)) == SQLITE_LOCKED)
      {
        if (sqlite3_extended_errcode (h) != SQLITE_LOCKED_SHAREDCACHE)
          break;

        sqlite3_reset (stmt_);
        conn_.wait ();
      }

      sqlite3_reset (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 ()
    {
      bind_param (data_.bind, data_.count);
      bind_param (cond_.bind, cond_.count, data_.count);

      int e;
      sqlite3* h (conn_.handle ());
      while ((e = sqlite3_step (stmt_)) == SQLITE_LOCKED)
      {
        if (sqlite3_extended_errcode (h) != SQLITE_LOCKED_SHAREDCACHE)
          break;

        sqlite3_reset (stmt_);
        conn_.wait ();
      }

      sqlite3_reset (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 ()
    {
      bind_param (cond_.bind, cond_.count);

      int e;
      sqlite3* h (conn_.handle ());
      while ((e = sqlite3_step (stmt_)) == SQLITE_LOCKED)
      {
        if (sqlite3_extended_errcode (h) != SQLITE_LOCKED_SHAREDCACHE)
          break;

        sqlite3_reset (stmt_);
        conn_.wait ();
      }

      sqlite3_reset (stmt_);

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

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