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

#include <cassert>
#include <limits>
#include <sstream>

#include <odb/emitter.hxx>

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

using namespace std;

namespace relational
{
  namespace schema
  {
    // cxx_object
    //
    schema_format cxx_object::format_embedded (schema_format::embedded);

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

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

      os << file_header;

      instance<sql_file> file;
      file->prologue ();

      instance<sql_emitter> em;
      emitter_ostream emos (*em);

      schema_format f (schema_format::sql);

      // Drop.
      //
      {
        instance<drop_model> model (*em, emos, f);
        instance<drop_table> table (*em, emos, f);
        trav_rel::qnames names;

        model >> names >> table;

        // Pass 1 and 2.
        //
        for (unsigned short pass (1); pass < 3; ++pass)
        {
          model->pass (pass);
          table->pass (pass);

          model->traverse (*ctx.model);
        }
      }

      os << endl;

      // Create.
      //
      {
        instance<create_model> model (*em, emos, f);
        instance<create_table> table (*em, emos, f);
        trav_rel::qnames names;

        model >> names >> table;

        // Pass 1 and 2.
        //
        for (unsigned short pass (1); pass < 3; ++pass)
        {
          model->pass (pass);
          table->pass (pass);

          model->traverse (*ctx.model);
        }
      }

      file->epilogue ();
    }
  }
}