aboutsummaryrefslogtreecommitdiff
path: root/odb/relational/header.cxx
blob: 099dc33272542c7a8f71322ab16474ace8af8e79 (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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
// file      : odb/relational/header.cxx
// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC
// license   : GNU GPL v3; see accompanying LICENSE file

#include <odb/relational/header.hxx>
#include <odb/relational/generate.hxx>

using namespace std;

void relational::header::class1::
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));

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

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

  string const& type (class_fq_name (c));
  column_count_type const& cc (column_count (c));

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

  // pointer_query_columns & query_columns
  //
  if (options.generate_query ())
  {
    // If we don't have object pointers, then also generate
    // query_columns (in this case pointer_query_columns and
    // query_columns are the same and the former inherits from
    // the latter). Otherwise we have to postpone query_columns
    // generation until the second pass to deal with forward-
    // declared objects.
    //
    if (!has_a (c, test_pointer | include_base))
      query_columns_type_->traverse (c);

    pointer_query_columns_type_->traverse (c);
  }

  // object_traits_impl
  //
  os << "template <>" << endl
     << "class access::object_traits_impl< " << type << ", id_" << db <<
    " >:"  << endl
     << "  public access::object_traits< " << type << " >"
     << "{"
     << "public:" << endl;

  object_public_extra_pre (c);

  // Polymorphic root_traits, base_traits, and discriminator_image_type.
  //
  if (poly)
  {
    if (!abst)
      os << "typedef polymorphic_entry<object_type, id_" << db <<
        "> entry_type;";

    os << "typedef object_traits_impl<root_type, id_" << db << "> " <<
      "root_traits;";

    if (poly_derived)
    {
      os << "typedef object_traits_impl<base_type, id_" << db << "> " <<
        "base_traits;"
         << endl;
    }
    else
    {
      os << endl
         << "struct discriminator_image_type"
         << "{";

      discriminator_image_member_->traverse (*discriminator);

      if (optimistic != 0)
        version_image_member_->traverse (*optimistic);

      os << "std::size_t version;"
         << "};";
    }
  }

  // id_image_type
  //
  if (id != 0)
  {
    if (base_id)
    {
      semantics::class_& b (
        dynamic_cast<semantics::class_&> (id->scope ()));
      string const& type ();

      os << "typedef object_traits_impl< " << class_fq_name (b) << ", " <<
        "id_" << db << " >::id_image_type id_image_type;"
         << endl;
    }
    else
    {
      os << "struct id_image_type"
         << "{";

      id_image_member_->traverse (*id);

      if (optimistic != 0)
        version_image_member_->traverse (*optimistic);

      os << "std::size_t version;"
         << "};";
    }
  }

  // Polymorphic map.
  //
  if (poly)
  {
    if (!poly_derived)
      os << "static map_type* map;";

    os << "static const " << (abst ? "abstract_" : "") << "info_type info;"
       << endl;
  }

  // image_type
  //
  image_type_->traverse (c);

  //
  // Containers (abstract and concrete).
  //

  {
    instance<container_traits> t (c);
    t->traverse (c);
  }

  //
  // Query (abstract and concrete).
  //

  if (options.generate_query ())
  {
    // Generate object pointer tags here unless we are generating dynamic
    // multi-database support, in which case they generated in object_traits.
    //
    if (options.multi_database () != multi_database::dynamic &&
        has_a (c, test_pointer | exclude_base))
    {
      query_tags t;   // Not customizable.
      t.traverse (c);
    }
  }

  //
  // Functions (abstract and concrete).
  //

  if (id != 0 || !reuse_abst)
    os << "using object_traits<object_type>::id;"
       << endl;

  if (!poly_derived && id != 0)
  {
    if (options.generate_query ())
      os << "static id_type" << endl
         << "id (const image_type&);"
         << endl;

    if (optimistic != 0)
      os << "static version_type" << endl
         << "version (const image_type&);"
         << endl;
  }

  // discriminator()
  //
  if (poly && !poly_derived)
    os << "static discriminator_type" << endl
       << "discriminator (const image_type&);"
       << endl;

  // grow ()
  //
  if (generate_grow)
  {
    // For derived classes in a polymorphic hierarchy, grow() will
    // check bases up to the specified depth. If one of the base
    // images has grown, then it will increment its version. But
    // the return value only indicates the state of this image,
    // excluding polymorphic bases (in other words, it is possible
    // that one of the bases has grown but this function returns
    // false).
    //
    os << "static bool" << endl
       << "grow (image_type&, " << truncated_vector;

    if (poly_derived)
      os << ", std::size_t = depth";

    os << ");"
       << endl;
  }

  // bind (image_type)
  //
  os << "static void" << endl
     << "bind (" << bind_vector << "," << endl;

  // If we are a derived type in a polymorphic hierarchy, then
  // we get the the external id binding.
  //
  if (poly_derived)
    os << "const " << bind_vector << " id," << endl
       << "std::size_t id_size," << endl;

  os << "image_type&," << endl
     << db << "::statement_kind);"
     << endl;

  // bind (id_image_type)
  //
  if (id != 0)
  {
    os << "static void" << endl
       << "bind (" << bind_vector << ", id_image_type&" <<
      (optimistic != 0 ? ", bool bind_version = true" : "") << ");"
       << endl;
  }

  // init (image, object)
  //
  os << "static " << (generate_grow ? "bool" : "void") << endl
     << "init (image_type&, const object_type&, " << db << "::statement_kind);"
     << endl;

  // init (object, image)
  //
  os << "static void" << endl
     << "init (object_type&, const image_type&, database*";

  if (poly_derived)
    os << ", std::size_t = depth";

  os << ");"
     << endl;

  // init (id_image, id)
  //
  if (id != 0)
  {
    os << "static void" << endl
       << "init (id_image_type&, const id_type&" <<
      (optimistic != 0 ? ", const version_type* = 0" : "") << ");"
       << endl;
  }

  if (poly_derived)
  {
    // check_version
    //
    os << "static bool" << endl
       << "check_version (const std::size_t*, const image_type&);"
       << endl;

    // update_version
    //
    os << "static void" << endl
       << "update_version (std::size_t*, const image_type&, " <<
      db << "::binding*);"
       << endl;
  }

  // The rest does not apply to reuse-abstract objects.
  //
  if (reuse_abst)
  {
    object_public_extra_post (c);
    os << "};";
    return;
  }

  // Statements typedefs.
  //
  if (poly)
  {
    if (poly_derived)
      os << "typedef" << endl
         << db << "::polymorphic_derived_object_statements" <<
        "<object_type>" << endl
         << "statements_type;"
         << endl
         << "typedef" << endl
         << db << "::polymorphic_root_object_statements<root_type>" << endl
         << "root_statements_type;"
         << endl;
    else
      os << "typedef" << endl
         << db << "::polymorphic_root_object_statements<object_type>" << endl
         << "statements_type;"
         << endl
         << "typedef statements_type root_statements_type;"
         << "typedef " << db << "::object_statements<object_type> " <<
        "base_statements_type;"
         << endl;
  }
  else
  {
    if (id != 0)
      os << "typedef " << db << "::object_statements<object_type> " <<
        "statements_type;"
         << endl;
    else
      os << "typedef " << db << "::no_id_object_statements<object_type> " <<
        "statements_type;"
         << endl;
  }

  //
  // Query (concrete).
  //

  if (options.generate_query ())
  {
    // query_base_type
    //
    os << "typedef " << db << "::query_base query_base_type;"
       << endl;
  }

  //
  // Containers (concrete).
  //

  // Statement cache (forward declaration).
  //
  if (id != 0)
    os << "struct container_statement_cache_type;"
       << endl;

  // column_count
  //
  os << "static const std::size_t column_count = " << cc.total << "UL;"
     << "static const std::size_t id_column_count = " << cc.id << "UL;"
     << "static const std::size_t inverse_column_count = " <<
    cc.inverse << "UL;"
     << "static const std::size_t readonly_column_count = " <<
    cc.readonly << "UL;"
     << "static const std::size_t managed_optimistic_column_count = " <<
    cc.optimistic_managed << "UL;";

  if (poly && !poly_derived)
    os << "static const std::size_t discriminator_column_count = " <<
      cc.discriminator << "UL;";

  os << endl;

  // Statements.
  //
  os << "static const char persist_statement[];";

  if (id != 0)
  {
    if (poly_derived)
    {
      char const* n (abst ? "1" : "depth");

      os << "static const char* const find_statements[" << n << "];"
         << "static const std::size_t find_column_counts[" << n << "];";
    }
    else
    {
      os << "static const char find_statement[];";

      if (poly)
        os << "static const char find_discriminator_statement[];";
    }

    if (cc.total != cc.id + cc.inverse + cc.readonly)
      os << "static const char update_statement[];";

    os << "static const char erase_statement[];";

    if (optimistic != 0 && !poly_derived)
      os << "static const char optimistic_erase_statement[];";
  }

  if (options.generate_query ())
  {
    os << "static const char query_statement[];"
       << "static const char erase_query_statement[];"
       << endl
       << "static const char table_name[];";
  }

  os << endl;

  //
  // Functions (concrete).
  //

  // persist ()
  //
  os << "static void" << endl
     << "persist (database&, " << (auto_id ? "" : "const ") << "object_type&";

  if (poly)
    os << ", bool top = true, bool dyn = true";

  os << ");"
     << endl;

  if (id != 0)
  {
    // find (id)
    //
    if (c.default_ctor ())
      os << "static pointer_type" << endl
         << "find (database&, const id_type&);"
         << endl;

    // find (id, obj)
    //
    os << "static bool" << endl
       << "find (database&, const id_type&, object_type&";

    if (poly)
      os << ", bool dyn = true";

    os << ");"
       << endl;

    // reload ()
    //
    os << "static bool" << endl
       << "reload (database&, object_type&";

    if (poly)
      os << ", bool dyn = true";

    os << ");"
       << endl;

    // update ()
    //
    // In case of a polymorphic object, we generate update() even if it is
    // readonly since the potentially-readwrite base will rely on it to
    // initialize the id image.
    //
    //
    if (!readonly (c) || poly)
    {
      os << "static void" << endl
         << "update (database&, const object_type&";

      if (poly)
        os << ", bool top = true, bool dyn = true";

      os << ");"
         << endl;
    }

    // erase ()
    //
    os << "static void" << endl
       << "erase (database&, const id_type&";

    if (poly)
      os << ", bool top = true, bool dyn = true";

    os << ");"
       << endl;

    os << "static void" << endl
       << "erase (database&, const object_type&";

    if (poly)
      os << ", bool top = true, bool dyn = true";

    os << ");"
       << endl;
  }

  // query ()
  //
  if (options.generate_query ())
  {
    if (!options.omit_unprepared ())
    {
      os << "static result<object_type>" << endl
         << "query (database&, const query_base_type&);"
         << endl;
    }

    os << "static unsigned long long" << endl
       << "erase_query (database&, const query_base_type&);"
       << endl;

    if (options.generate_prepared ())
    {
      os << "static odb::details::shared_ptr<prepared_query_impl>" << endl
         << "prepare_query (connection&, const char*, const query_base_type&);"
         << endl;

      os << "static odb::details::shared_ptr<result_impl>" << endl
         << "execute_query (prepared_query_impl&);"
         << endl;
    }
  }

  // create_schema ()
  //
  if (embedded_schema || separate_schema)
  {
    os << "static bool" << endl
       << "create_schema (database&, unsigned short pass, bool drop);"
       << endl;
  }

  object_public_extra_post (c);

  // Implementation details.
  //
  os << "public:" << endl;

  if (id != 0)
  {
    // Load the object image.
    //
    os << "static bool" << endl
       << "find_ (";

    if (poly && !poly_derived)
      os << "base_statements_type&, ";
    else
      os << "statements_type&, ";

    os << "const id_type*";

    if (poly_derived && !abst)
      os << ", std::size_t = depth";

    os << ");"
       << endl;

    // Load the rest of the object (containers, etc). Expects the id
    // image in the object statements to be initialized to the object
    // id.
    //
    os << "static void" << endl
       << "load_ (";

    if (poly && !poly_derived)
      os << "base_statements_type&, ";
    else
      os << "statements_type&, ";

    os << "object_type&";

    if (poly_derived)
      os << ", std::size_t = depth";

    os << ");"
       << endl;
  }

  // discriminator_ ()
  //
  if (poly && !poly_derived)
  {
    os << "static void" << endl
       << "discriminator_ (statements_type&," << endl
       << "const id_type&," << endl
       << "discriminator_type*";

    if (optimistic != 0)
      os << "," << endl
         << "version_type* = 0";

    os << ");"
       << endl;
  }

  // Load the dynamic part of the object. Depth inidicates where
  // the dynamic part starts. Expects the id image in the object
  // statements to be initialized to the object id. We don't need
  // it if we are poly-abstract.
  //
  if (poly_derived && !abst)
    os << "static void" << endl
       << "load_ (database&, root_type&, std::size_t);"
       << endl;

  // Image chain manipulation.
  //
  if (poly && need_image_clone && options.generate_query ())
  {
    os << "static root_traits::image_type&" << endl
       << "root_image (image_type&);"
       << endl;

    os << "static image_type*" << endl
       << "clone_image (const image_type&);"
       << endl;

    os << "static void" << endl
       << "copy_image (image_type&, const image_type&);"
       << endl;

    os << "static void" << endl
       << "free_image (image_type*);"
       << endl;
  }

  os << "};";


  // object_traits_impl< , id_default>
  //
  // Note that it is not generated for reuse-abstract classes.
  //
  if (options.default_database_specified () &&
      options.default_database () == db)
  {
    os << "template <>" << endl
       << "class access::object_traits_impl< " << type << ", " <<
      "id_default >:" << endl
       << "  public access::object_traits_impl< " << type << ", " <<
      "id_" << db << " >"
       << "{"
       << "};";
  }
}

void relational::header::class1::
traverse_view (type& c)
{
  string const& type (class_fq_name (c));
  size_t obj_count (c.get<size_t> ("object-count"));

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

  // view_traits_impl
  //
  os << "template <>" << endl
     << "class access::view_traits_impl< " << type << ", id_" <<
    db << " >:" << endl
     << "  public access::view_traits< " << type << " >"
     << "{"
     << "public:" << endl;

  view_public_extra_pre (c);

  // image_type
  //
  image_type_->traverse (c);

  os << "typedef " << db << "::view_statements<view_type> statements_type;"
     << endl;

  //
  // Query.
  //

  // Generate associated object tags here unless we are generating dynamic
  // multi-database support, in which case they generated in object_traits.
  //
  if (options.multi_database () != multi_database::dynamic)
  {
    query_tags t;   // Not customizable.
    t.traverse (c);
  }

  // query_base_type and query_columns (definition generated by class2).
  //
  os << "typedef " << db << "::query_base query_base_type;"
     << "struct query_columns";

  if (obj_count == 0)
    os << "{"
       << "};";
  else
    os << ";"
       << endl;

  //
  // Functions.
  //

  // grow ()
  //
  if (generate_grow)
  {
    os << "static bool" << endl
       << "grow (image_type&, " << truncated_vector << ");"
       << endl;
  }

  // bind (image_type)
  //
  os << "static void" << endl
     << "bind (" << bind_vector << ", image_type&);"
     << endl;

  // init (view, image)
  //
  os << "static void" << endl
     << "init (view_type&, const image_type&, database*);"
     << endl;

  // column_count
  //
  os << "static const std::size_t column_count = " <<
    column_count (c).total << "UL;"
     << endl;

  // Statements.
  //
  view_query& vq (c.get<view_query> ("query"));

  if (vq.kind != view_query::runtime)
  {
    os << "static query_base_type" << endl
       << "query_statement (const query_base_type&);"
       << endl;
  }

  //
  // Functions.
  //

  // query ()
  //
  if (!options.omit_unprepared ())
    os << "static result<view_type>" << endl
       << "query (database&, const query_base_type&);"
       << endl;

  if (options.generate_prepared ())
  {
    os << "static odb::details::shared_ptr<prepared_query_impl>" << endl
       << "prepare_query (connection&, const char*, const query_base_type&);"
       << endl;

    os << "static odb::details::shared_ptr<result_impl>" << endl
       << "execute_query (prepared_query_impl&);"
       << endl;
  }

  view_public_extra_post (c);

  os << "};";

  // view_traits_impl< , id_default>
  //
  if (options.default_database_specified () &&
      options.default_database () == db)
  {
    os << "template <>" << endl
       << "class access::view_traits_impl< " << type << ", " <<
      "id_default >:" << endl
       << "  public access::view_traits_impl< " << type << ", " <<
      "id_" << db << " >"
       << "{"
       << "};";
  }
}

void relational::header::class1::
traverse_composite (type& c)
{
  string const& type (class_fq_name (c));

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

  os << "template <>" << endl
     << "class access::composite_value_traits< " << type << ", " <<
    "id_" << db << " >"
     << "{"
     << "public:" << endl;

  // value_type
  //
  os << "typedef " << type << " value_type;"
     << endl;

  // image_type
  //
  image_type_->traverse (c);

  // Containers.
  //
  {
    instance<container_traits> t (c);
    t->traverse (c);
  }

  // grow ()
  //
  if (generate_grow)
  {
    os << "static bool" << endl
       << "grow (image_type&, " << truncated_vector << ");"
       << endl;
  }

  // bind (image_type)
  //
  os << "static void" << endl
     << "bind (" << bind_vector << ", image_type&, " <<
    db << "::statement_kind);"
     << endl;

  // init (image, value)
  //
  os << "static " << (generate_grow ? "bool" : "void") << endl
     << "init (image_type&, const value_type&, " << db << "::statement_kind);"
     << endl;

  // init (value, image)
  //
  os << "static void" << endl
     << "init (value_type&, const image_type&, database*);"
     << endl;

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

    // set_null (image)
    //
    os << "static void" << endl
       << "set_null (image_type&, " << db << "::statement_kind);"
       << endl;
  }

  os << "};";
}

void relational::header::
generate ()
{
  context ctx;
  ostream& os (ctx.os);

  instance<include> i;
  i->generate ();

  os << "namespace odb"
     << "{";

  {
    traversal::unit unit;
    traversal::defines unit_defines;
    typedefs unit_typedefs (false);
    traversal::namespace_ ns;
    instance<class1> c;

    unit >> unit_defines >> ns;
    unit_defines >> c;
    unit >> unit_typedefs >> c;

    traversal::defines ns_defines;
    typedefs ns_typedefs (false);

    ns >> ns_defines >> ns;
    ns_defines >> c;
    ns >> ns_typedefs >> c;

    unit.dispatch (ctx.unit);
  }

  {
    traversal::unit unit;
    traversal::defines unit_defines;
    typedefs unit_typedefs (false);
    traversal::namespace_ ns;
    instance<class2> c;

    unit >> unit_defines >> ns;
    unit_defines >> c;
    unit >> unit_typedefs >> c;

    traversal::defines ns_defines;
    typedefs ns_typedefs (false);

    ns >> ns_defines >> ns;
    ns_defines >> c;
    ns >> ns_typedefs >> c;

    unit.dispatch (ctx.unit);
  }

  os << "}";
}