aboutsummaryrefslogtreecommitdiff
path: root/odb/relational/mssql/schema.cxx
blob: c7e9aa73e1ff9bbf93cfe0c268b47a8b2573eede (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
// file      : odb/relational/mssql/schema.cxx
// copyright : Copyright (c) 2009-2018 Code Synthesis Tools CC
// license   : GNU GPL v3; see accompanying LICENSE file

#include <odb/relational/schema.hxx>

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

using namespace std;

namespace relational
{
  namespace mssql
  {
    namespace schema
    {
      namespace relational = relational::schema;
      using relational::table_set;

      struct sql_emitter: relational::sql_emitter
      {
        sql_emitter (const base& x): base (x) {}

        virtual void
        post ()
        {
          if (!first_) // Ignore empty statements.
          {
            os << ';' << endl
               << "GO" << endl
               << endl;
          }
        }
      };
      entry<sql_emitter> sql_emitter_;

      //
      // File.
      //

      struct sql_file: relational::sql_file, context
      {
        sql_file (const base& x): base (x) {}

        virtual void
        prologue ()
        {
          // Suppress the (x rows affected) messages from sqlcmd for DML
          // statements. We only use DML for schema version management.
          //
          if ((model == 0 || model->version () != 0) &&
              !options.suppress_schema_version ())
            os << "SET NOCOUNT ON;" << endl
               << endl;
        }
      };
      entry<sql_file> sql_file_;

      //
      // Drop.
      //

      struct drop_column: relational::drop_column, context
      {
        drop_column (base const& x): base (x) {}

        virtual void
        traverse (sema_rel::drop_column& dc)
        {
          if (first_)
            first_ = false;
          else
            os << "," << endl
               << "              ";

          os << quote_id (dc.name ());
        }
      };
      entry<drop_column> drop_column_;

      struct drop_foreign_key: relational::drop_foreign_key, context
      {
        drop_foreign_key (base const& x): base (x) {}

        virtual void
        drop (sema_rel::table& t, sema_rel::foreign_key& fk)
        {
          bool migration (dropped_ == 0);

          if (migration)
          {
            if (fk.not_deferrable ())
              pre_statement ();
            else
            {
              if (format_ != schema_format::sql)
                return;

              os << "/*" << endl;
            }
          }
          else
          {
            // Here we drop potentially deferrable keys and also need to
            // test if the key exists.
            //
            pre_statement ();

            os << "IF OBJECT_ID(" << quote_string (fk.name ()) << ", " <<
              quote_string ("F") << ") IS NOT NULL" << endl
               << "  ";
          }

          os << "ALTER TABLE " << quote_id (t.name ()) << endl
             << (migration ? "  " : "    ") << "DROP CONSTRAINT " <<
            quote_id (fk.name ()) << endl;


          if (!migration || fk.not_deferrable ())
            post_statement ();
          else
            os << "*/" << endl
               << endl;
        }

        virtual void
        traverse (sema_rel::drop_foreign_key& dfk)
        {
          // Find the foreign key we are dropping in the base model.
          //
          sema_rel::foreign_key& fk (find<sema_rel::foreign_key> (dfk));

          bool c (!fk.not_deferrable () && !in_comment);

          if (c && format_ != schema_format::sql)
            return;

          if (!first_)
            os << (c ? "" : ",") << endl
               << "                  ";

          if (c)
            os << "/* ";

          os << quote_id (fk.name ());

          if (c)
            os << " */";

          if (first_)
          {
            if (c)
              // There has to be a real name otherwise the whole statement
              // would have been commented out.
              //
              os << endl
                 << "                  ";
            else
              first_ = false;
          }
        }
      };
      entry<drop_foreign_key> drop_foreign_key_;

      struct drop_index: relational::drop_index, context
      {
        drop_index (base const& x): base (x) {}

        virtual void
        drop (sema_rel::index& in)
        {
          sema_rel::table& t (static_cast<sema_rel::table&> (in.scope ()));

          os << "DROP INDEX " << name (in) << " ON " <<
            quote_id (t.name ()) << endl;
        }
      };
      entry<drop_index> drop_index_;

      struct drop_table: relational::drop_table, context
      {
        drop_table (base const& x): base (x) {}

        virtual void
        drop (sema_rel::table& t, bool migration)
        {
          // SQL Server has no IF EXISTS conditional for dropping tables.
          // The following approach appears to be the recommended way to
          // drop a table if it exists.
          //
          sema_rel::qname const& name (t.name ());

          pre_statement ();

          if (!migration)
            os << "IF OBJECT_ID(" << quote_string (name.string ()) <<
              ", " << quote_string ("U") << ") IS NOT NULL" << endl
               << "  ";

          os << "DROP TABLE " << quote_id (name) << endl;

          post_statement ();
        }
      };
      entry<drop_table> drop_table_;

      //
      // Create.
      //

      struct create_column: relational::create_column, context
      {
        create_column (base const& x): base (x) {}

        virtual void
        traverse (sema_rel::add_column& ac)
        {
          if (first_)
            first_ = false;
          else
            os << "," << endl
               << "      ";

          create (ac);
        }

        virtual void
        auto_ (sema_rel::primary_key&)
        {
          os << " IDENTITY";
        }
      };
      entry<create_column> create_column_;

      struct create_foreign_key: relational::create_foreign_key, context
      {
        create_foreign_key (base const& x): base (x) {}

        void
        diagnose (sema_rel::foreign_key& fk)
        {
          if (fk.on_delete () != sema_rel::foreign_key::no_action)
          {
            cerr << "warning: foreign key '" << fk.name () << "' has " <<
              "ON DELETE clause but is disabled in SQL Server due to lack "
              "of deferrable constraint support" << endl;

            cerr << "info: consider using non-deferrable foreign keys (" <<
              "--fkeys-deferrable-mode)" << endl;
          }
        }

        virtual void
        traverse_create (sema_rel::foreign_key& fk)
        {
          // SQL Server does not support deferrable constraint checking.
          // Output such foreign keys as comments, for documentation,
          // unless we are generating embedded schema.
          //
          if (fk.not_deferrable ())
            base::traverse_create (fk);
          else
          {
            diagnose (fk);

            // Don't bloat C++ code with comment strings if we are
            // generating embedded schema.
            //
            if (format_ != schema_format::sql)
              return;

            os << endl
               << "  /*" << endl
               << "  CONSTRAINT ";
            create (fk);
            os << endl
               << "  */";
          }
        }

        virtual void
        traverse_add (sema_rel::foreign_key& fk)
        {
          bool c (!fk.not_deferrable () && !in_comment);

          if (c)
            diagnose (fk);

          if (c && format_ != schema_format::sql)
            return;

          if (!first_)
            os << (c ? "" : ",") << endl
               << "      ";

          if (c)
            os << "/*" << endl
               << "      ";

          os << "CONSTRAINT ";
          create (fk);

          if (c)
            os << endl
               << "      */";

          if (first_)
          {
            if (c)
              // There has to be a real key otherwise the whole statement
              // would have been commented out.
              //
              os << endl
                 << "      ";
            else
              first_ = false;
          }
        }

        virtual void
        deferrable (sema_rel::deferrable)
        {
          // This will still be called to output the comment.
        }
      };
      entry<create_foreign_key> create_foreign_key_;

      struct create_table: relational::create_table, context
      {
        create_table (base const& x): base (x) {}

        // See if there are any undefined foreign keys that are not
        // deferrable.
        //
        bool
        check_undefined_fk_deferrable_only (sema_rel::table& t)
        {
          for (sema_rel::table::names_iterator i (t.names_begin ());
               i != t.names_end (); ++i)
          {
            using sema_rel::foreign_key;

            if (foreign_key* fk = dynamic_cast<foreign_key*> (&i->nameable ()))
            {
              if (!fk->count ("mssql-fk-defined") &&
                  fk->not_deferrable ())
                return false;
            }
          }
          return true;
        }

        virtual void
        traverse (sema_rel::table& t)
        {
          if (pass_ == 1)
            base::traverse (t);
          else
          {
            // Add undefined foreign keys.
            //
            if (check_undefined_fk (t))
            {
              bool deferrable (check_undefined_fk_deferrable_only (t));

              if (!deferrable || format_ == schema_format::sql)
              {
                if (deferrable)
                {
                  os << "/*" << endl;
                  in_comment = true;
                }
                else
                  pre_statement ();

                os << "ALTER TABLE " << quote_id (t.name ()) << endl
                   << "  ADD ";

                instance<create_foreign_key> cfk (*this);
                trav_rel::unames n (*cfk);
                names (t, n);
                os << endl;

                if (deferrable)
                {
                  in_comment = false;
                  os << "*/" << endl
                     << endl;
                }
                else
                  post_statement ();
              }
            }
          }
        }
      };
      entry<create_table> create_table_;

      //
      // Alter.
      //

      struct alter_column: relational::alter_column, context
      {
        alter_column (base const& x): base (x) {}

        virtual void
        traverse (sema_rel::column& c)
        {
          // Relax (NULL) in pre and tighten (NOT NULL) in post.
          //
          if (pre_ != c.null ())
            return;

          using sema_rel::table;
          table& at (static_cast<table&> (c.scope ()));

          pre_statement ();

          os << "ALTER TABLE " << quote_id (at.name ()) << endl
             << "  ALTER COLUMN ";
          alter (c);
          os << endl;

          post_statement ();
        }
      };
      entry<alter_column> alter_column_;

      struct alter_table_pre: relational::alter_table_pre, context
      {
        alter_table_pre (base const& x): base (x) {}

        // Check if we are only dropping deferrable foreign keys.
        //
        bool
        check_drop_deferrable_only (sema_rel::alter_table& at)
        {
          for (sema_rel::alter_table::names_iterator i (at.names_begin ());
               i != at.names_end (); ++i)
          {
            using sema_rel::foreign_key;
            using sema_rel::drop_foreign_key;

            if (drop_foreign_key* dfk =
                dynamic_cast<drop_foreign_key*> (&i->nameable ()))
            {
              foreign_key& fk (find<foreign_key> (*dfk));

              if (fk.not_deferrable ())
                return false;
            }
          }
          return true;
        }

        virtual void
        alter (sema_rel::alter_table& at)
        {
          // SQL Server can only alter one kind of thing at a time.
          //
          if (check<sema_rel::drop_foreign_key> (at))
          {
            bool deferrable (check_drop_deferrable_only (at));

            if (!deferrable || format_ == schema_format::sql)
            {
              if (deferrable)
              {
                os << "/*" << endl;
                in_comment = true;
              }
              else
                pre_statement ();

              os << "ALTER TABLE " << quote_id (at.name ()) << endl
                 << "  DROP CONSTRAINT ";

              instance<drop_foreign_key> dfc (*this);
              trav_rel::unames n (*dfc);
              names (at, n);
              os << endl;

              if (deferrable)
              {
                in_comment = false;
                os << "*/" << endl
                   << endl;
              }
              else
                post_statement ();
            }
          }

          if (check<sema_rel::add_column> (at))
          {
            pre_statement ();

            os << "ALTER TABLE " << quote_id (at.name ()) << endl
               << "  ADD ";

            instance<create_column> cc (*this);
            trav_rel::unames n (*cc);
            names (at, n);
            os << endl;

            post_statement ();
          }

          // For ALTER COLUMN, SQL Server can only have one per ALTER TABLE.
          //
          {
            bool tl (true); // (Im)perfect forwarding.
            instance<alter_column> ac (*this, tl);
            trav_rel::unames n (*ac);
            names (at, n);
          }
        }
      };
      entry<alter_table_pre> alter_table_pre_;

      struct alter_table_post: relational::alter_table_post, context
      {
        alter_table_post (base const& x): base (x) {}

        // Check if we are only adding deferrable foreign keys.
        //
        bool
        check_add_deferrable_only (sema_rel::alter_table& at)
        {
          for (sema_rel::alter_table::names_iterator i (at.names_begin ());
               i != at.names_end (); ++i)
          {
            using sema_rel::add_foreign_key;

            if (add_foreign_key* afk =
                dynamic_cast<add_foreign_key*> (&i->nameable ()))
            {
              if (afk->not_deferrable ())
                return false;
            }
          }
          return true;
        }

        virtual void
        alter (sema_rel::alter_table& at)
        {
          // SQL Server can only alter one kind of thing at a time.
          //
          if (check<sema_rel::drop_column> (at))
          {
            pre_statement ();

            os << "ALTER TABLE " << quote_id (at.name ()) << endl
               << "  DROP COLUMN ";

            instance<drop_column> dc (*this);
            trav_rel::unames n (*dc);
            names (at, n);
            os << endl;

            post_statement ();
          }

          // For ALTER COLUMN, SQL Server can only have one per ALTER TABLE.
          //
          {
            bool fl (false); // (Im)perfect forwarding.
            instance<alter_column> ac (*this, fl);
            trav_rel::unames n (*ac);
            names (at, n);
          }

          if (check<sema_rel::add_foreign_key> (at))
          {
            bool deferrable (check_add_deferrable_only (at));

            if (!deferrable || format_ == schema_format::sql)
            {
              if (deferrable)
              {
                os << "/*" << endl;
                in_comment = true;
              }
              else
                pre_statement ();

              os << "ALTER TABLE " << quote_id (at.name ()) << endl
                 << "  ADD ";

              instance<create_foreign_key> cfc (*this);
              trav_rel::unames n (*cfc);
              names (at, n);
              os << endl;

              if (deferrable)
              {
                in_comment = false;
                os << "*/" << endl
                   << endl;
              }
              else
                post_statement ();
            }
          }
        }
      };
      entry<alter_table_post> alter_table_post_;

      //
      // Schema version table.
      //

      struct version_table: relational::version_table, context
      {
        version_table (base const& x): base (x) {}

        virtual void
        create_table ()
        {
          pre_statement ();

          os << "IF OBJECT_ID(" << quote_string (table_.string ()) <<
            ", " << quote_string ("U") << ") IS NULL" << endl
             << "  CREATE TABLE " << qt_ << " (" << endl
             << "    " << qn_ << " VARCHAR(256) NOT NULL PRIMARY KEY," << endl
             << "    " << qv_ << " BIGINT NOT NULL," << endl
             << "    " << qm_ << " BIT NOT NULL)" << endl;

          post_statement ();
        }

        virtual void
        create (sema_rel::version v)
        {
          pre_statement ();

          os << "IF NOT EXISTS (SELECT 1 FROM " << qt_ << " WHERE " << qn_ <<
            " = " << qs_ << ")" << endl
             << "  INSERT INTO " << qt_ << " (" << endl
             << "    " << qn_ << ", " << qv_ << ", " << qm_ << ")" << endl
             << "    VALUES (" << qs_ << ", " << v << ", 0)" << endl;

          post_statement ();
        }
      };
      entry<version_table> version_table_;
    }
  }
}