File: validator.cxx

package info (click to toggle)
odb 2.4.0-5
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 10,716 kB
  • ctags: 6,755
  • sloc: cpp: 41,102; sh: 11,329; makefile: 35
file content (641 lines) | stat: -rw-r--r-- 17,481 bytes parent folder | download | duplicates (2)
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
// file      : odb/relational/validator.cxx
// copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC
// license   : GNU GPL v3; see accompanying LICENSE file

#include <map>
#include <iostream>

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

using namespace std;

namespace relational
{
  namespace
  {
    //
    // Pass 2.
    //

    struct data_member2: traversal::data_member, context
    {
      data_member2 (bool& valid)
          : valid_ (valid)
      {
      }

      virtual void
      traverse (type& m)
      {
        if (transient (m))
          return;

        if (null (m))
        {
          if (semantics::class_* c = composite_wrapper (utype (m)))
          {
            if (has_a (*c, test_container))
            {
              os << m.file () << ":" << m.line () << ":" << m.column () << ":"
                 << " error: composite member containing containers cannot "
                 << "be null" << endl;

              os << c->file () << ":" << c->line () << ":" << c->column ()
                 << ": info: composite value type is defined here" << endl;

              valid_ = false;
            }
          }
        }

        // Check on-delete.
        //
        if (m.count ("on-delete"))
        {
          const char* kp (container (m) ? "value" : "");
          location l (m.location ());

          // Make sure it is a pointer.
          //
          if (!object_pointer (member_utype (m, kp)))
          {
            error (l) << "on_delete specified for non-object pointer" << endl;
            valid_ = false;
          }

          // Make sure it is not inverse.
          //
          if (inverse (m, kp))
          {
            error (l) << "on_delete specified for inverse object " <<
              "pointer" << endl;
            valid_ = false;
          }

          // Make sure the pointer is nullable if asked to set it to NULL.
          //
          using sema_rel::foreign_key;

          if (m.get<foreign_key::action_type> ("on-delete") ==
              foreign_key::set_null &&
              !null (m, kp))
          {
            error (l) << "set_null specified for non-nullable object "
              "pointer" << endl;
            valid_ = false;
          }
        }
      }

      bool& valid_;
    };

    struct object_no_id_members: object_members_base
    {
      object_no_id_members (bool& valid)
          : object_members_base (false, false, true), valid_ (valid), dm_ (0)
      {
      }

      virtual void
      traverse_pointer (semantics::data_member& m, semantics::class_&)
      {
        if (inverse (m))
        {
          semantics::data_member& dm (dm_ != 0 ? *dm_ : m);

          os << dm.file () << ":" << dm.line () << ":" << dm.column () << ":"
             << " error: inverse object pointer member '" << member_prefix_
             << m.name () << "' in an object without an object id" << endl;

          valid_ = false;
        }
      }

      virtual void
      traverse_container (semantics::data_member& m, semantics::type&)
      {
        semantics::data_member& dm (dm_ != 0 ? *dm_ : m);

        os << dm.file () << ":" << dm.line () << ":" << dm.column () << ":"
           << " error: container member '" << member_prefix_ << m.name ()
           << "' in an object without an object id" << endl;

        valid_ = false;
      }

      virtual void
      traverse_composite (semantics::data_member* m, semantics::class_& c)
      {
        semantics::data_member* old_dm (dm_);

        if (dm_ == 0)
          dm_ = m;

        object_members_base::traverse_composite (m, c);

        dm_ = old_dm;
      }

    private:
      bool& valid_;
      semantics::data_member* dm_; // Direct object data member.
    };

    struct composite_id_members: object_members_base
    {
      composite_id_members (bool& valid)
          : object_members_base (false, false, true), valid_ (valid), dm_ (0)
      {
      }

      virtual void
      traverse_pointer (semantics::data_member& m, semantics::class_&)
      {
        semantics::data_member& dm (dm_ != 0 ? *dm_ : m);

        os << dm.file () << ":" << dm.line () << ":" << dm.column () << ":"
           << " error: object pointer member '" << member_prefix_ << m.name ()
           << "' in a composite value type that is used as an object id"
           << endl;

        valid_ = false;
      }

      virtual void
      traverse_simple (semantics::data_member& m)
      {
        if (readonly (member_path_, member_scope_))
        {
          semantics::data_member& dm (dm_ != 0 ? *dm_ : m);

          os << dm.file () << ":" << dm.line () << ":" << dm.column () << ":"
             << " error: readonly member '" << member_prefix_ << m.name ()
             << "' in a composite value type that is used as an object id"
             << endl;

          valid_ = false;
        }
      }

      virtual void
      traverse_container (semantics::data_member& m, semantics::type&)
      {
        semantics::data_member& dm (dm_ != 0 ? *dm_ : m);

        os << dm.file () << ":" << dm.line () << ":" << dm.column () << ":"
           << " error: container member '" << member_prefix_ << m.name ()
           << "' in a composite value type that is used as an object id"
           << endl;

        valid_ = false;
      }

      virtual void
      traverse_composite (semantics::data_member* m, semantics::class_& c)
      {
        semantics::data_member* old_dm (dm_);

        if (dm_ == 0)
          dm_ = m;

        object_members_base::traverse_composite (m, c);

        dm_ = old_dm;
      }

    private:
      bool& valid_;
      semantics::data_member* dm_; // Direct composite member.
    };

    struct view_members: object_members_base
    {
      view_members (bool& valid)
          : object_members_base (false, false, true), valid_ (valid), dm_ (0)
      {
      }

      virtual void
      traverse_pointer (semantics::data_member& m, semantics::class_&)
      {
        if (dm_ != 0 && object_pointer (utype (m)))
        {
          location const& l (dm_->location ());

          error (l) << "nested view data member '" << member_prefix_
                    << m.name () << "' is an object pointer" << endl;
          info (l) << "views can only contain direct object pointer members"
                   << endl;

          valid_ = false;
        }
      }

      virtual void
      traverse_container (semantics::data_member& m, semantics::type&)
      {
        semantics::data_member& dm (dm_ != 0 ? *dm_ : m);

        os << dm.file () << ":" << dm.line () << ":" << dm.column () << ":"
           << " error: view data member '" << member_prefix_ << m.name ()
           << "' is a container" << endl;

        os << dm.file () << ":" << dm.line () << ":" << dm.column () << ":"
           << ": info: views cannot contain containers" << endl;

        valid_ = false;
      }

      virtual void
      traverse_composite (semantics::data_member* m, semantics::class_& c)
      {
        semantics::data_member* old_dm (dm_);

        if (dm_ == 0)
          dm_ = m;

        object_members_base::traverse_composite (m, c);

        dm_ = old_dm;
      }

    private:
      bool& valid_;
      semantics::data_member* dm_; // Direct view data member.
    };

    struct class2: traversal::class_, context
    {
      class2 (bool& valid)
          : valid_ (valid),
            typedefs_ (true),
            data_member_ (valid),
            object_no_id_members_ (valid),
            composite_id_members_ (valid),
            view_members_ (valid)
      {
        *this >> defines_ >> *this;
        *this >> typedefs_ >> *this;

        data_member_names_ >> data_member_;
      }

      virtual void
      traverse (type& c)
      {
        class_kind_type ck (class_kind (c));
        switch (ck)
        {
        case class_object:
          names (c);
          traverse_object (c);
          break;
        case class_view:
          names (c);
          traverse_view (c);
          break;
        case class_composite:
          names (c);
          traverse_composite (c);
          break;
        case class_other:
          break;
        }

        // Make sure indexes are not defined for anything other than objects.
        //
        if (c.count ("index") && ck != class_object)
        {
          indexes& ins (c.get<indexes> ("index"));

          for (indexes::iterator i (ins.begin ()); i != ins.end (); ++i)
          {
            error (i->loc) << "index definition on a non-persistent class"
                           << endl;
            valid_ = false;
          }
        }
      }

      virtual void
      traverse_object (type& c)
      {
        semantics::data_member* id (id_member (c));

        if (id != 0)
        {
          if (semantics::class_* cm = composite_wrapper (utype (*id)))
          {
            // Composite id cannot be auto.
            //
            if (auto_ (*id))
            {
              os << id->file () << ":" << id->line () << ":" << id->column ()
                 << ": error: composite id cannot be automatically assigned"
                 << endl;

              valid_ = false;
            }

            // Make sure we don't have any containers or pointers in this
            // composite value type.
            //
            if (valid_)
            {
              composite_id_members_.traverse (*cm);

              if (!valid_)
                os << id->file () << ":" << id->line () << ":" << id->column ()
                   << ": info: composite id is defined here" << endl;
            }

            // Check that the composite value type is default-constructible.
            //
            if (!cm->default_ctor ())
            {
              os << cm->file () << ":" << cm->line () << ":" << cm->column ()
                 << ": error: composite value type that is used as object id "
                 << "is not default-constructible" << endl;

              os << cm->file () << ":" << cm->line () << ":" << cm->column ()
                 << ": info: provide default constructor for this value type"
                 << endl;

              os << id->file () << ":" << id->line () << ":" << id->column ()
                 << ": info: composite id is defined here" << endl;

              valid_ = false;
            }
          }
        }
        else
        {
          if (!abstract (c))
          {
            // Make sure we don't have any containers or inverse pointers.
            //
            object_no_id_members_.traverse (c);
          }
        }

        names (c, data_member_names_);

        // Validate bulk operation support.
        //
        for (bool i (true); i && c.count ("bulk"); i = false)
        {
          location_t l (c.get<location_t> ("bulk-location"));

          if (polymorphic (c))
          {
            error (l) << "bulk operations on polymorphic objects are "
              "not supported" << endl;
            valid_ = false;
            break;
          }

          if (has_a (c, test_straight_container))
          {
            error (l) << "bulk operations on objects with containers are "
              "not supported" << endl;
            valid_ = false;
            break;
          }

          bool update (true);

          // Unless we only have manually-updated sections, we cannot generate
          // the bulk update operation.
          //
          user_sections& uss (c.get<user_sections> ("user-sections"));

          for (user_sections::iterator i (uss.begin ());
               update && i != uss.end ();
               ++i)
          {
            const user_section& s (*i);

            // Skip special sections.
            //
            if (s.special != user_section::special_ordinary)
              continue;

            // Always-updated section still needs a separate statement
            // (since it may not be loaded).
            //
            if (!s.update_empty () && s.update != user_section::update_manual)
              update = false;
          }

          c.set ("bulk-persist", true);
          if (update) c.set ("bulk-update", true);
          c.set ("bulk-erase", true);
        }

        // Validate indexes.
        //
        {
          indexes& ins (c.get<indexes> ("index"));

          // Make sure index members are not transient, inverse, or
          // containers.
          //
          for (indexes::iterator i (ins.begin ()); i != ins.end (); ++i)
          {
            index& in (*i);

            for (index::members_type::iterator i (in.members.begin ());
                 i != in.members.end (); ++i)
            {
              index::member& im (*i);
              semantics::data_member& m (*im.path.back ());

              if (transient (m))
              {
                error (im.loc) << "index member is transient" << endl;
                valid_ = false;
              }

              if (inverse (m))
              {
                error (im.loc) << "index member is an inverse object " <<
                  "pointer" << endl;
                valid_ = false;
              }

              if (container (m))
              {
                error (im.loc) << "index member is a container" << endl;
                valid_ = false;
              }
            }
          }
        }
      }

      virtual void
      traverse_view (type& c)
      {
        const view_query& vq (c.get<view_query> ("query"));

        // Make sure we don't have any containers or object pointers.
        //
        view_members_.traverse (c);

        names (c, data_member_names_);

        // Allow certain kinds of empty views.
        //
        if (vq.kind != view_query::runtime &&
            vq.kind != view_query::complete_execute)
        {
          // Allow all the members to be deleted.
          //
          column_count_type const& cc (column_count (c));

          if (cc.total == 0)
          {
            os << c.file () << ":" << c.line () << ":" << c.column () << ":"
               << " error: no persistent data members in the class" << endl;
            valid_ = false;
          }
        }
      }

      virtual void
      traverse_composite (type& c)
      {
        names (c, data_member_names_);
      }

    public:
      bool& valid_;

      traversal::defines defines_;
      typedefs typedefs_;

      data_member2 data_member_;
      traversal::names data_member_names_;

      object_no_id_members object_no_id_members_;
      composite_id_members composite_id_members_;
      view_members view_members_;
    };
  }

  void
  validate (options const&,
            features&,
            semantics::unit& u,
            semantics::path const&,
            unsigned short pass)
  {
    bool valid (true);

    // Validate custom type mapping.
    //
    if (pass == 1)
    {
      // Create an empty list if we don't have one. This makes the
      // rest of the code simpler.
      //
      if (!u.count ("custom-db-types"))
        u.set ("custom-db-types", custom_db_types ());

      custom_db_types & cts (u.get<custom_db_types> ("custom-db-types"));

      for (custom_db_types::iterator i (cts.begin ()); i != cts.end (); ++i)
      {
        custom_db_type& ct (*i);

        if (ct.type.empty ())
        {
          error (ct.loc) << "'type' clause expected in db pragma map" << endl;
          valid = false;
        }

        if (ct.as.empty ())
        {
          error (ct.loc) << "'as' clause expected in db pragma map" << endl;
          valid = false;
        }

        if (ct.to.empty ())
          ct.to = "(?)";
        else
        {
          size_t p (ct.to.find ("(?)"));

          if (p == string::npos)
          {
            error (ct.loc) << "no '(?)' expression in the 'to' clause "
                           << "of db pragma map" << endl;
            valid = false;
          }
          else if (ct.to.find ("(?)", p + 3) != string::npos)
          {
            error (ct.loc) << "multiple '(?)' expressions in the 'to' "
                           << "clause of db pragma map" << endl;
            valid = false;
          }
        }

        if (ct.from.empty ())
          ct.from = "(?)";
        else
        {
          size_t p (ct.from.find ("(?)"));

          if (p == string::npos)
          {
            error (ct.loc) << "no '(?)' expression in the 'from' clause "
                           << "of db pragma map" << endl;
            valid = false;
          }
          else if (ct.from.find ("(?)", p + 3) != string::npos)
          {
            error (ct.loc) << "multiple '(?)' expressions in the 'from' "
                           << "clause of db pragma map" << endl;
            valid = false;
          }
        }
      }
    }

    if (!valid)
      throw operation_failed ();

    if (pass == 1)
    {
    }
    else
    {
      traversal::unit unit;
      traversal::defines unit_defines;
      typedefs unit_typedefs (true);
      traversal::namespace_ ns;
      class2 c (valid);

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

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

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

      unit.dispatch (u);
    }

    if (!valid)
      throw operation_failed ();
  }
}