aboutsummaryrefslogtreecommitdiff
path: root/common/prepared/driver.cxx
blob: 0467f7062f5167da6521f6e0f61340460d50a42b (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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// file      : common/prepared/driver.cxx
// copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

// Test prepared query functionality.
//

#include <memory>   // std::auto_ptr, std::unique_ptr
#include <utility>  // std::move
#include <cassert>
#include <iostream>

#include <odb/database.hxx>
#include <odb/transaction.hxx>

#include <common/common.hxx>
#include <common/config.hxx> // HAVE_CXX11

#include "test.hxx"
#include "test-odb.hxx"

using namespace std;
using namespace odb::core;

struct params
{
  unsigned short age;
  std::string name;
};

static void
query_factory (const char* name, connection& c)
{
  typedef odb::query<person> query;

#ifdef HAVE_CXX11
  unique_ptr<params> p (new params);
#else
  auto_ptr<params> p (new params);
#endif
  prepared_query<person> pq (
    c.prepare_query<person> (
      name,
      query::age > query::_ref (p->age) &&
      query::name != query::_ref (p->name)));
#ifdef HAVE_CXX11
  c.cache_query (pq, move (p));
#else
  c.cache_query (pq, p);
#endif
}

int
main (int argc, char* argv[])
{
  try
  {
    auto_ptr<database> db (create_database (argc, argv));

    {
      person p1 ("John First",  91);
      person p2 ("John Second", 81);
      person p3 ("John Third",  71);
      person p4 ("John Fourth", 61);
      person p5 ("John Fifth",  51);

      transaction t (db->begin ());
      db->persist (p1);
      db->persist (p2);
      db->persist (p3);
      db->persist (p4);
      db->persist (p5);
      t.commit ();
    }

    typedef odb::query<person> query;
    typedef odb::prepared_query<person> prep_query;
    typedef odb::result<person> result;

    // Uncached query in the same transaction.
    //
    {
      transaction t (db->begin ());

      unsigned short age (90);
      prep_query pq (
        db->prepare_query<person> (
          "person-age-query",
          query::age > query::_ref (age)));

      for (unsigned short i (1); i < 6; ++i, age -= 10)
      {
        result r (pq.execute ());
        assert (size (r) == i);
      }

      age = 90;
      result r (pq.execute ());
      result::iterator i (r.begin ());
      assert (i != r.end () && i->name_ == "John First" && i->age_ == 91);
      assert (++i == r.end ());

      t.commit ();
    }

    // Uncached query in multiple transaction.
    //
    {
      connection_ptr c (db->connection ());

      unsigned short age (90);
      prep_query pq (
        c->prepare_query<person> (
          "person-age-query",
          query::age > query::_ref (age)));

      for (unsigned short i (1); i < 6; ++i, age -= 10)
      {
        transaction t (c->begin ());

        result r (pq.execute ());
        assert (size (r) == i);

        t.commit ();
      }

      transaction t (c->begin ());

      age = 90;
      result r (pq.execute ());
      result::iterator i (r.begin ());
      assert (i != r.end () && i->name_ == "John First" && i->age_ == 91);
      assert (++i == r.end ());

      t.commit ();
    }

    // Cached query without parameters.
    //
    {
      for (unsigned short i (1); i < 6; ++i)
      {
        transaction t (db->begin ());

        prep_query pq (db->lookup_query<person> ("person-val-age-query"));

        if (!pq)
        {
          assert (i == 1);
          pq = db->prepare_query<person> (
            "person-val-age-query",
            query::age > 90);
          db->cache_query (pq);
        }
        else if (i == 2)
        {
          try
          {
            db->cache_query (pq);
            assert (false);
          }
          catch (const odb::prepared_already_cached&)
          {
          }
        }

        result r (pq.execute ());
        assert (size (r) == 1);

        t.commit ();
      }
    }

    // Cached query with parameters.
    //
    {
      for (unsigned short i (1); i < 6; ++i)
      {
        transaction t (db->begin ());

        unsigned short* age;
        prep_query pq (db->lookup_query<person> ("person-ref-age-query", age));

        if (!pq)
        {
          assert (i == 1);

#ifdef HAVE_CXX11
          unique_ptr<unsigned short> p (new unsigned short);
#else
          auto_ptr<unsigned short> p (new unsigned short);
#endif
          age = p.get ();
          pq = db->prepare_query<person> (
            "person-ref-age-query",
            query::age > query::_ref (*age));

#ifdef HAVE_CXX11
          db->cache_query (pq, move (p));
#else
          db->cache_query (pq, p);
#endif
        }
        else if (i == 2)
        {
          // Object type mismatch.
          //
          try
          {
            db->lookup_query<int> ("person-ref-age-query", age);
            assert (false);
          }
          catch (const odb::prepared_type_mismatch&)
          {
          }

          // Parameters type mismatch.
          //
          try
          {
            int* age;
            db->lookup_query<person> ("person-ref-age-query", age);
            assert (false);
          }
          catch (const odb::prepared_type_mismatch&)
          {
          }
        }

        *age = 100 - i * 10;
        result r (pq.execute ());
        assert (size (r) == i);

        t.commit ();
      }
    }

    // Cached query with factory.
    //
    {
      db->query_factory ("person-params-query", &query_factory);

      for (unsigned int i (1); i < 6; ++i)
      {
        transaction t (db->begin ());

        params* p;
        prep_query pq (db->lookup_query<person> ("person-params-query", p));
        assert (pq);

        p->age = 100 - i * 10;
        p->name = "John First";
        result r (pq.execute ());
        assert (size (r) == i - 1);

        t.commit ();
      }

      db->query_factory ("person-params-query",
                         database::query_factory_ptr ());
    }

    // Cached query with wildcard factory.
    //
    {
      db->query_factory ("", &query_factory);

      for (unsigned int i (1); i < 6; ++i)
      {
        transaction t (db->begin ());

        params* p;
        prep_query pq (db->lookup_query<person> ("person-params-query-1", p));
        assert (pq);

        p->age = 100 - i * 10;
        p->name = "John First";
        result r (pq.execute ());
        assert (size (r) == i - 1);

        t.commit ();
      }

      db->query_factory ("", database::query_factory_ptr ());
    }

    // Cached query with lambda factory.
    //
#ifdef HAVE_CXX11
    {
      db->query_factory (
        "person-params-query-2",
        [] (const char* name, connection& c)
        {
          typedef odb::query<person> query;

          unique_ptr<params> p (new params);
          prepared_query<person> pq (
            c.prepare_query<person> (
              name,
              query::age > query::_ref (p->age) &&
              query::name != query::_ref (p->name)));
          c.cache_query (pq, move (p));
        });

      for (unsigned int i (1); i < 6; ++i)
      {
        transaction t (db->begin ());

        params* p;
        prep_query pq (db->lookup_query<person> ("person-params-query-2", p));
        assert (pq);

        p->age = 100 - i * 10;
        p->name = "John First";
        result r (pq.execute ());
        assert (size (r) == i - 1);

        t.commit ();
      }

      db->query_factory ("person-params-query-2",
                         database::query_factory_ptr ());
    }

    // Cached query with lambda factory using closure. Forces nonoptimized
    // representation of std::function.
    //
    {
      const std::string person_name ("John First");

      db->query_factory (
        "person-params-query-3",
        [person_name] (const char* name, connection& c)
        {
          typedef odb::query<person> query;

          prepared_query<person> pq (
            c.prepare_query<person> (
              name,
              query::age > 50 && query::name != person_name));
          c.cache_query (pq);
        });

      {
        transaction t (db->begin ());

        prep_query pq (db->lookup_query<person> ("person-params-query-3"));
        assert (pq);

        result r (pq.execute ());
        assert (size (r) == 4);

        t.commit ();
      }

      db->query_factory ("person-params-query-3",
#ifdef HAVE_CXX11_NULLPTR
                         nullptr
#else
                         database::query_factory_ptr ()
#endif
      );
    }
#endif

    // View prepared query.
    //
    {
      typedef odb::query<person_view> query;
      typedef odb::prepared_query<person_view> prep_query;
      typedef odb::result<person_view> result;

      transaction t (db->begin ());

      unsigned short age (90);
      prep_query pq (
        db->prepare_query<person_view> (
          "person-view-age-query",
          query::age > query::_ref (age)));

      for (unsigned short i (1); i < 6; ++i, age -= 10)
      {
        result r (pq.execute ());
        assert (size (r) == i);
      }

      age = 90;
      result r (pq.execute ());
      result::iterator i (r.begin ());
      assert (i != r.end () && i->name == "John First" && i->age == 91);
      assert (++i == r.end ());

      t.commit ();
    }

    // By-ref parameter image growth.
    //
    {
      transaction t (db->begin ());

      string name;
      prep_query pq (
        db->prepare_query<person> (
          "person-name-query",
          query::name != query::_ref (name)));

      {
        name = "John First";
        result r (pq.execute ());
        assert (size (r) == 4);
      }

      {
        name.assign (2048, 'x');
        result r (pq.execute ());
        assert (size (r) == 5);
      }

      t.commit ();
    }

    // Test execute_one() and execute_value().
    //
    {
      transaction t (db->begin ());

      person p ("John Doe", 23);
      db->persist (p);

      prep_query pq1 (
        db->prepare_query<person> ("query-1", query::id == p.id_));
      prep_query pq0 (
        db->prepare_query<person> ("query-0", query::id == p.id_ + 1));

      {
        auto_ptr<person> p (pq1.execute_one ());
        assert (p.get () != 0 && p->name_ == "John Doe");
      }

      {
        auto_ptr<person> p (pq0.execute_one ());
        assert (p.get () == 0);
      }

      {
        person p;
        assert (pq1.execute_one (p) && p.name_ == "John Doe");
      }

      {
        person p ("", 0);
        assert (!pq0.execute_one (p) &&
                p.id_ == 0 && p.name_.empty () && p.age_ == 0);
      }

      {
        person p (pq1.execute_value ());
        assert (p.name_ == "John Doe");
      }
    }
  }
  catch (const odb::exception& e)
  {
    cerr << e.what () << endl;
    return 1;
  }
}