aboutsummaryrefslogtreecommitdiff
path: root/odb/relational/inline.hxx
blob: 2bed1daf53d94fae167ce8df54ba4feeb2878432 (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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
// file      : odb/relational/inline.hxx
// copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC
// license   : GNU GPL v3; see accompanying LICENSE file

#ifndef ODB_RELATIONAL_INLINE_HXX
#define ODB_RELATIONAL_INLINE_HXX

#include <odb/diagnostics.hxx>
#include <odb/relational/context.hxx>
#include <odb/relational/common.hxx>

namespace relational
{
  namespace inline_
  {
    //
    // get/set null (composite value only)
    //

    struct null_member: virtual member_base
    {
      typedef null_member base;

      null_member (bool get)
          : member_base (string (), 0, string (), string ()), get_ (get)
      {
      }

    protected:
      bool get_;
    };

    template <typename T>
    struct null_member_impl: null_member, virtual member_base_impl<T>
    {
      typedef null_member_impl base_impl;

      null_member_impl (base const& x): base (x) {}

      typedef typename member_base_impl<T>::member_info member_info;

      virtual bool
      pre (member_info& mi)
      {
        // If the member is soft- added or deleted, check the version.
        //
        unsigned long long av (added (mi.m));
        unsigned long long dv (deleted (mi.m));
        if (av != 0 || dv != 0)
        {
          os << "if (";

          if (av != 0)
            os << "svm >= schema_version_migration (" << av << "ULL, true)";

          if (av != 0 && dv != 0)
            os << " &&" << endl;

          if (dv != 0)
            os << "svm <= schema_version_migration (" << dv << "ULL, true)";

          os << ")"
             << "{";
        }

        // If the whole value type is readonly, then set will never be
        // called with sk == statement_update.
        //
        if (!get_ && !readonly (*context::top_object))
        {
          semantics::class_* c;

          if (readonly (mi.m) || ((c = composite (mi.t)) && readonly (*c)))
            os << "if (sk == statement_insert)" << endl;
        }

        return true;
      }

      virtual void
      post (member_info& mi)
      {
        if (added (mi.m) || deleted (mi.m))
          os << "}";
      }

      virtual void
      traverse_composite (member_info& mi)
      {
        string traits ("composite_value_traits< " + mi.fq_type () + ", id_" +
                       db.string () + " >");

        if (get_)
          os << "r = r && " << traits << "::get_null (" <<
            "i." << mi.var << "value";
        else
          os << traits << "::set_null (i." << mi.var << "value, sk";

        if (versioned (*composite (mi.t)))
          os << ", svm";

        os << ");";
      }
    };

    struct null_base: traversal::class_, virtual context
    {
      typedef null_base base;

      null_base (bool get): get_ (get) {}

      virtual void
      traverse (type& c)
      {
        // Ignore transient bases.
        //
        if (!composite (c))
          return;

        string traits ("composite_value_traits< " + class_fq_name (c) +
                       ", id_" + db.string () + " >");

        // If the derived value type is readonly, then set will never be
        // called with sk == statement_update.
        //
        if (!get_ && readonly (c) && !readonly (*context::top_object))
          os << "if (sk == statement_insert)" << endl;

        if (get_)
          os << "r = r && " << traits << "::get_null (i";
        else
          os << traits << "::set_null (i, sk";

        if (versioned (c))
          os << ", svm";

        os << ");";
      }

    protected:
      bool get_;
    };

    //
    //
    struct class_: traversal::class_, virtual context
    {
      typedef class_ base;

      class_ ()
          : typedefs_ (false),
            get_null_base_ (true),
            get_null_member_ (true),
            set_null_base_ (false),
            set_null_member_ (false)
      {
        init ();
      }

      class_ (class_ const&)
          : root_context (), //@@ -Wextra
            context (),
            typedefs_ (false),
            get_null_base_ (true),
            get_null_member_ (true),
            set_null_base_ (false),
            set_null_member_ (false)
      {
        init ();
      }

      void
      init ()
      {
        *this >> defines_ >> *this;
        *this >> typedefs_ >> *this;

        get_null_base_inherits_ >> get_null_base_;
        get_null_member_names_ >> get_null_member_;

        set_null_base_inherits_ >> set_null_base_;
        set_null_member_names_ >> set_null_member_;
      }

      virtual void
      traverse (type& c)
      {
        class_kind_type ck (class_kind (c));

        if (ck == class_other ||
            (!options.at_once () && class_file (c) != unit.file ()))
          return;

        names (c);

        context::top_object = context::cur_object = &c;

        switch (ck)
        {
        case class_object: traverse_object (c); break;
        case class_view: traverse_view (c); break;
        case class_composite: traverse_composite (c); break;
        default: break;
        }

        context::top_object = context::cur_object = 0;
      }

      virtual void
      object_extra (type&)
      {
      }

      virtual void
      traverse_object (type& c)
      {
        using semantics::data_member;

        data_member* id (id_member (c));
        bool auto_id (id && auto_ (*id));
        bool base_id (id && &id->scope () != &c); // Comes from base.
        data_member* optimistic (context::optimistic (c));

        // Base class that contains the object id and version for optimistic
        // concurrency.
        //
        type* base (
          id != 0 && base_id ? dynamic_cast<type*> (&id->scope ()) : 0);

        type* poly_root (context::polymorphic (c));
        bool poly (poly_root != 0);
        bool poly_derived (poly && poly_root != &c);

        bool abst (abstract (c));
        bool reuse_abst (abst && !poly);

        bool versioned (context::versioned (c));

        string const& type (class_fq_name (c));
        string traits ("access::object_traits_impl< " + type + ", id_" +
                       db.string () + " >");

        user_sections& uss (c.get<user_sections> ("user-sections"));

        os << "// " << class_name (c) << endl
           << "//" << endl
           << endl;

        object_extra (c);

        if (id != 0 && base_id)
        {
          if (!poly_derived)
          {
            // id (id_image_type)
            //
            if (auto_id)
            {
              os << "inline" << endl
                 << traits << "::id_type" << endl
                 << traits << "::" << endl
                 << "id (const id_image_type& i)"
                 << "{"
                 << "return object_traits_impl< " << class_fq_name (*base) <<
                ", id_" << db << " >::id (i);"
                 << "}";
            }

            // id (image_type)
            //
            if (options.generate_query ())
            {
              os << "inline" << endl
                 << traits << "::id_type" << endl
                 << traits << "::" << endl
                 << "id (const image_type& i)"
                 << "{"
                 << "return object_traits_impl< " << class_fq_name (*base) <<
                ", id_" << db << " >::id (i);"
                 << "}";
            }

            // version (image_type)
            //
            if (optimistic != 0)
            {
              os << "inline" << endl
                 << traits << "::version_type" << endl
                 << traits << "::" << endl
                 << "version (const image_type& i)"
                 << "{"
                 << "return object_traits_impl< " << class_fq_name (*base) <<
                ", id_" << db << " >::version (i);"
                 << "}";
            }
          }

          // bind (id_image_type)
          //
          os << "inline" << endl
             << "void " << traits << "::" << endl
             << "bind (" << bind_vector << " b, id_image_type& i" <<
            (optimistic != 0 ? ", bool bv" : "") << ")"
             << "{"
             << "object_traits_impl< " << class_fq_name (*base) << ", id_" <<
            db << " >::bind (b, i" << (optimistic != 0 ? ", bv" : "") << ");"
             << "}";

          os << "inline" << endl
             << "void " << traits << "::" << endl
             << "init (id_image_type& i, const id_type& id" <<
            (optimistic != 0 ? ", const version_type* v" : "") << ")"
             << "{"
             << "object_traits_impl< " << class_fq_name (*base) << ", id_" <<
            db << " >::init (i, id" << (optimistic != 0 ? ", v" : "") << ");"
             << "}";
        }

        if (poly_derived)
        {
          size_t depth (polymorphic_depth (c));

          // check_version
          //
          os << "inline" << endl
             << "bool " << traits << "::" << endl
             << "check_version (const std::size_t* v, const image_type& i)"
             << "{"
             << "return ";

          string image ("i.");
          for (size_t i (0); i < depth; ++i)
          {
            os << (i == 0 ? "" : " ||") << endl
               << "  v[" << i << "UL] != " << image << "version";

            image += "base->";
          }

          os << ";"
             << "}";

          // update_version
          //
          os << "inline" << endl
             << "void " << traits << "::" << endl
             << "update_version (std::size_t* v, const image_type& i, " <<
            db << "::binding* b)"
             << "{";

          image = "i.";
          for (size_t i (0); i < depth; ++i)
          {
            os << "v[" << i << "UL] = " << image << "version;";
            image += "base->";
          }

          // A poly-abstract class always has only one entry in the
          // bindings array.
          //
          if (abst)
            os << "b[0].version++;";
          else
            for (size_t i (0); i < depth; ++i)
              os << "b[" << i << "UL].version++;";

          os << "}";
        }

        // The rest does not apply to reuse-abstract objects.
        //
        if (reuse_abst)
          return;

        // erase (object_type)
        //
        if (id != 0 && !poly && optimistic == 0 &&
            !has_a (c, test_smart_container))
        {
          os << "inline" << endl
             << "void " << traits << "::" << endl
             << "erase (database& db, const object_type& obj)"
             << "{"
             << "callback (db, obj, callback_event::pre_erase);"
             << "erase (db, id (obj));";

          // Note that we don't reset sections since the object is now
          // transient and the state of a section in a transient object
          // is undefined.

          os << "callback (db, obj, callback_event::post_erase);"
             << "}";
        }

        // load (section) [thunk version; poly_derived is true]
        //
        if (uss.count (user_sections::count_total |
                       user_sections::count_load  |
                       (poly ? user_sections::count_load_empty : 0)) != 0 &&
            uss.count (user_sections::count_new   |
                       user_sections::count_load  |
                       (poly ? user_sections::count_load_empty : 0)) == 0)
        {
          os << "inline" << endl
             << "bool " << traits << "::" << endl
             << "load (connection& conn, object_type& obj, section& s, " <<
            "const info_type* pi)"
             << "{"
             << "return base_traits::load (conn, obj, s, pi);"
             << "}";
        }

        // update (section) [thunk version; poly_derived is true]
        //
        if (uss.count (user_sections::count_total  |
                       user_sections::count_update |
                       (poly ? user_sections::count_update_empty : 0)) != 0 &&
            uss.count (user_sections::count_new    |
                       user_sections::count_update |
                       (poly ? user_sections::count_update_empty : 0)) == 0)
        {
          os << "inline" << endl
             << "bool " << traits << "::" << endl
             << "update (connection& conn, const object_type& obj, " <<
            "const section& s, const info_type* pi)"
             << "{"
             << "return base_traits::update (conn, obj, s, pi);"
             << "}";
        }

        // load_()
        //
        if (id != 0 &&
            !(poly_derived ||
              has_a (c, test_container | include_eager_load, &main_section) ||
              uss.count (user_sections::count_new  |
                         user_sections::count_load |
                         (poly ? user_sections::count_load_empty : 0)) != 0))
        {
          os << "inline" << endl
             << "void " << traits << "::" << endl
             << "load_ (statements_type& sts," << endl
             << "object_type& obj," << endl
             << "bool";

          if (versioned)
            os << "," << endl
               << "const schema_version_migration& svm";

          os << ")"
             << "{"
             << "ODB_POTENTIALLY_UNUSED (sts);"
             << "ODB_POTENTIALLY_UNUSED (obj);";

          if (versioned)
            os << "ODB_POTENTIALLY_UNUSED (svm);";

          os << endl;

          // Mark eager sections as loaded.
          //
          for (user_sections::iterator i (uss.begin ()); i != uss.end (); ++i)
          {
            // Skip special sections.
            //
            if (i->special == user_section::special_version)
              continue;

            data_member& m (*i->member);

            // If the section is soft- added or deleted, check the version.
            // We can only end up here if the object itself is versioned
            // (simple value section).
            //
            unsigned long long av (added (m));
            unsigned long long dv (deleted (m));
            if (av != 0 || dv != 0)
            {
              os << "if (";

              if (av != 0)
                os << "svm >= schema_version_migration (" << av << "ULL, true)";

              if (av != 0 && dv != 0)
                os << " &&" << endl;

              if (dv != 0)
                os << "svm <= schema_version_migration (" << dv << "ULL, true)";

              os << ")" << endl;
            }

            // Section access is always by reference.
            //
            member_access& ma (m.get<member_access> ("get"));
            if (!ma.synthesized)
              os << "// From " << location_string (ma.loc, true) << endl;

            os << ma.translate ("obj") << ".reset (true, false);"
               << endl;
          }

          os << "}";
        }

        if (poly && need_image_clone && options.generate_query ())
        {
          // root_image ()
          //
          os << "inline" << endl
             << traits << "::root_traits::image_type&" << endl
             << traits << "::" << endl
             << "root_image (image_type& i)"
             << "{";

          if (poly_derived)
            os << "return base_traits::root_image (*i.base);";
          else
            os << "return i;";

          os << "}";

          // clone_image ()
          //
          os << "inline" << endl
             << traits << "::image_type*" << endl
             << traits << "::" << endl
             << "clone_image (image_type& i)"
             << "{";

          if (poly_derived)
            os << "details::unique_ptr<base_traits::image_type> p (" << endl
               << "base_traits::clone_image (*i.base));"
               << "image_type* c (new image_type (i));"
               << "c->base = p.release ();"
               << "return c;";
          else
            os << "return new image_type (i);";

          os << "}";

          // copy_image ()
          //
          os << "inline" << endl
             << "void " << traits << "::" << endl
             << "copy_image (image_type& d, image_type& s)"
             << "{";

          if (poly_derived)
            os << "base_traits::image_type* b (d.base);"
               << "base_traits::copy_image (*b, *s.base);"
               << "d = s;" // Overwrites the base pointer.
               << "d.base = b;";
          else
            os << "d = s;";

          os << "}";

          // free_image ()
          //
          os << "inline" << endl
             << "void " << traits << "::" << endl
             << "free_image (image_type* i)"
             << "{";

          if (poly_derived)
            os << "base_traits::free_image (i->base);";

          os << "delete i;"
             << "}";
        }
      }

      virtual void
      view_extra (type&)
      {
      }

      virtual void
      traverse_view (type& c)
      {
        string const& type (class_fq_name (c));
        string traits ("access::view_traits_impl< " + type + ", id_" +
                       db.string () + " >");

        os << "// " << class_name (c) << endl
           << "//" << endl
           << endl;

        view_extra (c);
      }

      virtual void
      traverse_composite (type& c)
      {
        bool versioned (context::versioned (c));

        string const& type (class_fq_name (c));
        string traits ("access::composite_value_traits< " + type + ", id_" +
                       db.string () + " >");

        os << "// " << class_name (c) << endl
           << "//" << endl
           << endl;

        if (!has_a (c, test_container))
        {
          // get_null (image)
          //
          os << "inline" << endl
             << "bool " << traits << "::" << endl
             << "get_null (const image_type& i";

          if (versioned)
            os << "," << endl
               << "const schema_version_migration& svm";

          os << ")"
             << "{";

          if (versioned)
            os << "ODB_POTENTIALLY_UNUSED (svm);"
               << endl;

          os << "bool r (true);";

          inherits (c, get_null_base_inherits_);
          names (c, get_null_member_names_);

          os << "return r;"
             << "}";

          // set_null (image)
          //
          os << "inline" << endl
             << "void " << traits << "::" << endl
             << "set_null (image_type& i," << endl
             << db << "::statement_kind sk";

          if (versioned)
            os << "," << endl
               << "const schema_version_migration& svm";

          os << ")"
             << "{"
             << "ODB_POTENTIALLY_UNUSED (sk);";

          if (versioned)
            os << "ODB_POTENTIALLY_UNUSED (svm);";

          os << endl
             << "using namespace " << db << ";"
             << endl;

          inherits (c, set_null_base_inherits_);
          names (c, set_null_member_names_);

          os << "}";
        }
      }

    private:
      traversal::defines defines_;
      typedefs typedefs_;

      instance<null_base> get_null_base_;
      traversal::inherits get_null_base_inherits_;
      instance<null_member> get_null_member_;
      traversal::names get_null_member_names_;

      instance<null_base> set_null_base_;
      traversal::inherits set_null_base_inherits_;
      instance<null_member> set_null_member_;
      traversal::names set_null_member_names_;
    };

    struct include: virtual context
    {
      typedef include base;

      virtual void
      generate ()
      {
        if (features.polymorphic_object && options.generate_query ())
          os << "#include <odb/details/unique-ptr.hxx>" << endl
             << endl;
      }
    };
  }
}

#endif // ODB_RELATIONAL_INLINE_HXX