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

#include <odb/relational/schema-source.hxx>
#include <odb/relational/generate.hxx>

using namespace std;

namespace relational
{
  namespace schema
  {
    void
    generate_source ()
    {
      context ctx;
      ostream& os (ctx.os);
      database db (ctx.db);
      options const& ops (ctx.options);
      sema_rel::model& model (*ctx.model);
      string const& schema_name (ops.schema_name ()[db]);

      instance<cxx_emitter> emitter;
      emitter_ostream emitter_os (*emitter);
      schema_format format (schema_format::embedded);

      if (!model.names_empty ())
      {
        os << "namespace odb"
           << "{"
           << "static bool" << endl
           << "create_schema (database& db, unsigned short pass, bool drop)"
           << "{"
           << "ODB_POTENTIALLY_UNUSED (db);"
           << "ODB_POTENTIALLY_UNUSED (pass);"
           << "ODB_POTENTIALLY_UNUSED (drop);"
           << endl;

        // Drop.
        //
        if (!ops.omit_drop ())
        {
          bool close (false);

          os << "if (drop)"
             << "{";

          instance<drop_model> dmodel (*emitter, emitter_os, format);
          instance<drop_table> dtable (*emitter, emitter_os, format);
          trav_rel::qnames names;
          dmodel >> names >> dtable;

          for (unsigned short pass (1); pass < 3; ++pass)
          {
            emitter->pass (pass);
            dmodel->pass (pass);
            dtable->pass (pass);

            dmodel->traverse (model);

            close = close || !emitter->empty ();
          }

          if (close) // Close the last case and the switch block.
            os << "return false;"
               << "}"  // case
               << "}";  // switch

          os << "}";
        }

        // Create.
        //
        if (!ops.omit_create ())
        {
          bool close (false);

          if (ops.omit_drop ())
            os << "if (!drop)";
          else
            os << "else";

          os << "{";

          instance<create_model> cmodel (*emitter, emitter_os, format);
          instance<create_table> ctable (*emitter, emitter_os, format);
          trav_rel::qnames names;
          cmodel >> names >> ctable;

          for (unsigned short pass (1); pass < 3; ++pass)
          {
            emitter->pass (pass);
            cmodel->pass (pass);
            ctable->pass (pass);

            cmodel->traverse (model);

            close = close || !emitter->empty ();
          }

          if (close) // Close the last case and the switch block.
            os << "return false;"
               << "}"  // case
               << "}"; // switch

          os << "}";
        }

        os << "return false;"
           << "}";

        os << "static const schema_catalog_entry" << endl
           << "create_schema_entry_ (" << endl
           << "id_" << db << "," << endl
           << context::strlit (schema_name) << "," << endl
           << "&create_schema);"
           << endl;

        os << "}";
      }
    }
  }
}