aboutsummaryrefslogtreecommitdiff
path: root/odb/mysql/schema.cxx
blob: 686272c7a838e3dd05e1d8de1a86f77f96574844 (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
// file      : odb/mysql/schema.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC
// license   : GNU GPL v3; see accompanying LICENSE file

#include <set>

#include <odb/mysql/common.hxx>
#include <odb/mysql/schema.hxx>

using namespace std;

namespace mysql
{
  namespace
  {
    typedef set<string> tables;

    struct object_columns: object_columns_base, context
    {
      object_columns (context& c, string const& prefix = string ())
          : object_columns_base (c), context (c), prefix_ (prefix)
      {
      }

      virtual bool
      column (semantics::data_member& m, string const& name, bool first)
      {
        // Ignore inverse object pointers.
        //
        if (inverse (m))
          return false;

        if (!first)
          os << "," << endl;

        os << "  `" << name << "` " << column_type (m, prefix_);

        if (m.count ("id"))
          os << " PRIMARY KEY";

        using semantics::class_;
        if (class_* c = object_pointer (member_type (m, prefix_)))
        {
          os << " REFERENCES `" << table_name (*c) << "` (`" <<
            column_name (id_member (*c)) << "`)";
        }

        return true;
      }

    private:
      string prefix_;
    };

    struct member_create: object_members_base, context
    {
      member_create (context& c, semantics::class_& object, tables& t)
          : object_members_base (c, false, true),
            context (c),
            object_ (object),
            id_member_ (id_member (object)),
            tables_ (t)
      {
      }

      virtual void
      container (semantics::data_member& m)
      {
        using semantics::type;
        using semantics::data_member;

        // Ignore inverse containers of object pointers.
        //
        if (inverse (m, "value"))
          return;

        type& t (m.type ());
        container_kind_type ck (container_kind (t));
        type& vt (container_vt (t));

        string const& name (table_name (m, table_prefix_));

        if (tables_.count (name))
          return;

        os << "CREATE TABLE `" << name << "` (" << endl;

        // object_id (simple value)
        //
        string id_name (column_name (m, "id", "object_id"));
        os << "  `" << id_name << "` " << column_type (id_member_, "ref");

        // index (simple value)
        //
        string index_name;
        bool ordered (ck == ck_ordered && !unordered (m));
        if (ordered)
        {
          index_name = column_name (m, "index", "index");

          os << "," << endl
             << "  `" << index_name << "` " << column_type (m, "index");
        }

        // key (simple or composite value)
        //
        if (ck == ck_map || ck == ck_multimap)
        {
          type& kt (container_kt (t));

          os << "," << endl;

          if (semantics::class_* ckt = comp_value (kt))
          {
            object_columns oc (*this);
            oc.traverse_composite (m, *ckt, "key", "key");
          }
          else
          {
            object_columns oc (*this, "key");
            string const& name (column_name (m, "key", "key"));
            oc.column (m, name, true);
          }
        }

        // value (simple or composite value)
        //
        {
          os << "," << endl;

          if (semantics::class_* cvt = comp_value (vt))
          {
            object_columns oc (*this);
            oc.traverse_composite (m, *cvt, "value", "value");
          }
          else
          {
            object_columns oc (*this, "value");
            string const& name (column_name (m, "value", "value"));
            oc.column (m, name, true);
          }
        }

        // object_id index
        //
        os << "," << endl
           << "  INDEX (`" << id_name << "`)";

        // index index
        //
        if (ordered)
          os << "," << endl
             << "  INDEX (`" << index_name << "`)";

        os << ")";

        string const& engine (options.mysql_engine ());

        if (engine != "default")
          os << endl
             << "  ENGINE=" << engine;

        os << ";" << endl
           << endl;

        tables_.insert (name);
      }

    private:
      semantics::class_& object_;
      semantics::data_member& id_member_;
      tables& tables_;
    };

    struct class_create: traversal::class_, context
    {
      class_create (context& c)
          : context (c)
      {
      }

      virtual void
      traverse (type& c)
      {
        if (c.file () != unit.file ())
          return;

        if (!c.count ("object"))
          return;

        string const& name (table_name (c));

        // If the table with this name was already created, assume the
        // user knows what they are doing and skip it.
        //
        if (tables_.count (name))
          return;

        os << "CREATE TABLE `" << name << "` (" << endl;

        {
          object_columns oc (*this);
          oc.traverse (c);
        }

        os << ")";

        string const& engine (options.mysql_engine ());

        if (engine != "default")
          os << endl
             << "  ENGINE=" << engine;

        os << ";" << endl
           << endl;

        // Create tables for members.
        //
        {
          member_create mc (*this, c, tables_);
          mc.traverse (c);
        }

        tables_.insert (name);
      }

    private:
      tables tables_;
    };

    struct member_drop: object_members_base, context
    {
      member_drop (context& c, semantics::class_& object, tables& t)
          : object_members_base (c, false, true),
            context (c),
            object_ (object),
            tables_ (t)
      {
      }

      virtual void
      container (semantics::data_member& m)
      {
        // Ignore inverse containers of object pointers.
        //
        if (inverse (m, "value"))
          return;

        string const& name (table_name (m, table_prefix_));

        if (tables_.count (name))
          return;

        os << "DROP TABLE IF EXISTS `" << name << "`;" << endl;
        tables_.insert (name);
      }

    private:
      semantics::class_& object_;
      tables& tables_;
    };

    struct class_drop: traversal::class_, context
    {
      class_drop (context& c)
          : context (c)
      {
      }

      virtual void
      traverse (type& c)
      {
        if (c.file () != unit.file ())
          return;

        if (!c.count ("object"))
          return;

        string const& name (table_name (c));

        if (tables_.count (name))
          return;

        os << "DROP TABLE IF EXISTS `" << name << "`;" << endl;

        // Drop tables for members.
        //
        {
          member_drop mc (*this, c, tables_);
          mc.traverse (c);
        }

        tables_.insert (name);
      }

    private:
      tables tables_;
    };

    static char const file_header[] =
    "/* This file was generated by ODB, object-relational mapping (ORM)\n"
    " * compiler for C++.\n"
    " */\n\n";
  }

  void
  generate_schema (context& ctx)
  {
    ctx.os << file_header;

    // Drop.
    //
    {
      traversal::unit unit;
      traversal::defines unit_defines;
      traversal::namespace_ ns;
      class_drop c (ctx);

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

      traversal::defines ns_defines;

      ns >> ns_defines >> ns;
      ns_defines >> c;
      unit.dispatch (ctx.unit);
    }

    ctx.os << endl;

    // Create.
    //
    {
      traversal::unit unit;
      traversal::defines unit_defines;
      traversal::namespace_ ns;
      class_create c (ctx);

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

      traversal::defines ns_defines;

      ns >> ns_defines >> ns;
      ns_defines >> c;
      unit.dispatch (ctx.unit);
    }
  }
}