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

#include <vector>
#include <sstream>

#include <cutl/compiler/type-info.hxx>

#include <odb/semantics/relational/changelog.hxx>
#include <odb/semantics/relational/model.hxx>
#include <odb/semantics/relational/changeset.hxx>

using namespace std;

namespace semantics
{
  namespace relational
  {
    changelog::
    changelog (xml::parser& p)
        : contains_model_ (0)
    {
      using namespace xml;

      p.next_expect (parser::start_element, xmlns, "changelog");
      p.content (parser::complex);

      if (p.attribute<unsigned int> ("version") != 1)
        throw parsing (p, "unsupported changelog format version");

      database_ = p.attribute ("database");
      schema_name_ = p.attribute ("schema-name", "");

      // Because things are stored in the reverse order, first save the
      // changesets as XML chunks and then re-parse them in the reverse
      // order. We have to do it this way so that we can do lookups along
      // the alters edges.
      //
      typedef vector<string> changesets;
      changesets cs;

      for (parser::event_type e (p.peek ());
           e == parser::start_element;
           e = p.peek ())
      {
        if (p.qname () != xml::qname (xmlns, "changeset"))
          break; // Not our elements.

        ostringstream os;
        os.exceptions (ios_base::badbit | ios_base::failbit);
        serializer s (os, "changeset", 0); // No pretty-printing.
        size_t depth (0);

        do
        {
          switch (p.next ())
          {
          case parser::start_element:
            {
              s.start_element (p.qname ());

              if (depth == 0)
                s.namespace_decl (xmlns, "");

              typedef parser::attribute_map_type attr_map;
              attr_map const& am (p.attribute_map ());

              for (attr_map::const_iterator i (am.begin ());
                   i != am.end (); ++i)
                s.attribute (i->first, i->second.value);

              depth++;
              break;
            }
          case parser::end_element:
            {
              depth--;
              s.end_element ();
              break;
            }
          case parser::characters:
            {
              s.characters (p.value ());
              break;
            }
          default:
            {
              depth = 0;
              break;
            }
          }
        } while (depth != 0);

        cs.push_back (os.str ());
      }

      // Get the model.
      //
      p.next_expect (parser::start_element, xmlns, "model");
      model_type& m (new_node<model_type> (p, *this));
      new_edge<contains_model_type> (*this, m);
      p.next_expect (parser::end_element);

      // Re-parse the changesets in reverse order.
      //
      qscope* base (&m);
      for (changesets::reverse_iterator i (cs.rbegin ()); i != cs.rend (); ++i)
      {
        istringstream is (*i);
        is.exceptions (ios_base::badbit | ios_base::failbit);
        parser ip (is, p.input_name ());

        ip.next_expect (parser::start_element, xmlns, "changeset");

        changeset& c (new_node<changeset> (ip, *base, *this));
        new_edge<contains_changeset> (*this, c);
        base = &c;

        ip.next_expect (parser::end_element);
      }

      p.next_expect (parser::end_element);
    }

    void changelog::
    serialize (xml::serializer& s) const
    {
      s.start_element (xmlns, "changelog");
      s.namespace_decl (xmlns, "");
      s.attribute ("database", database_);
      if (!schema_name_.empty ())
        s.attribute ("schema-name", schema_name_);
      s.attribute ("version", 1); // Format version.

      // For better readability serialize things in reverse order so that
      // the most recent changeset appears first.
      //
      for (contains_changeset_list::const_reverse_iterator i (
             contains_changeset_.rbegin ());
           i != contains_changeset_.rend (); ++i)
      {
        (*i)->changeset ().serialize (s);
        s.characters ("\n");
      }

      model ().serialize (s);
      s.end_element ();
    }

    // type info
    //
    namespace
    {
      struct init
      {
        init ()
        {
          using compiler::type_info;

          // contains_model
          //
          {
            type_info ti (typeid (contains_model));
            ti.add_base (typeid (edge));
            insert (ti);
          }

          // contains_changeset
          //
          {
            type_info ti (typeid (contains_changeset));
            ti.add_base (typeid (edge));
            insert (ti);
          }

          // changelog
          //
          {
            type_info ti (typeid (changelog));
            ti.add_base (typeid (node));
            insert (ti);
          }
        }
      } init_;
    }
  }
}