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

namespace semantics
{
  namespace relational
  {
    // nameable
    //
    template <typename N>
    typename nameable<N>::parser_map nameable<N>::parser_map_;

    template <typename N>
    template <typename T>
    void nameable<N>::
    parser_impl (xml::parser& p, scope_type& s, graph& g)
    {
      name_type n (p.attribute ("name", name_type ()));
      T& x (g.new_node<T> (p, s, g));
      g.new_edge<names_type> (s, x, n);
    }

    template <typename N>
    nameable<N>::
    nameable (nameable const& n, graph&)
        : id_ (n.id_), named_ (0)
    {
    }

    template <typename N>
    nameable<N>::
    nameable (xml::parser&, graph&)
      // : id_ (p.attribute<string> ("id"))
        : named_ (0)
    {
      // The name attribute is handled in parser_impl().
    }

    template <typename N>
    void nameable<N>::
    serialize_attributes (xml::serializer& s) const
    {
      // Omit empty names (e.g., a primary key).
      //
      name_type const& n (name ());
      if (!n.empty ())
        s.attribute ("name", n);

      //s.attribute ("id", id_);
    }

    // scope
    //

    template <typename N>
    template <typename T, typename S>
    T* scope<N>::
    lookup (name_type const& name)
    {
      if (T* r = find<T> (name))
        return r;

      if (scope* b = base ())
      {
        if (find<S> (name) == 0)
          return b->lookup<T, S> (name);
      }

      return 0;
    }

    template <typename N>
    template <typename T>
    T* scope<N>::
    find (name_type const& name)
    {
      typename names_map::iterator i (names_map_.find (name));
      return i != names_map_.end ()
        ? dynamic_cast<T*> (&(*i->second)->nameable ())
        : 0;
    }

    template <typename N>
    typename scope<N>::names_iterator scope<N>::
    find (name_type const& name)
    {
      typename names_map::iterator i (names_map_.find (name));

      if (i == names_map_.end ())
        return names_.end ();
      else
        return i->second;
    }

    template <typename N>
    typename scope<N>::names_const_iterator scope<N>::
    find (name_type const& name) const
    {
      typename names_map::const_iterator i (names_map_.find (name));

      if (i == names_map_.end ())
        return names_.end ();
      else
        return names_const_iterator (i->second);
    }

    template <typename N>
    typename scope<N>::names_iterator scope<N>::
    find (names_type const& e)
    {
      typename names_iterator_map::iterator i (iterator_map_.find (&e));
      return i != iterator_map_.end () ? i->second : names_.end ();
    }

    template <typename N>
    typename scope<N>::names_const_iterator scope<N>::
    find (names_type const& e) const
    {
      typename names_iterator_map::const_iterator i (iterator_map_.find (&e));
      return i != iterator_map_.end () ? i->second : names_.end ();
    }

    template <typename N>
    scope<N>::
    scope (scope const& s, scope* base, graph& g)
        : first_key_ (names_.end ()),
          first_drop_column_ (names_.end ()),
          alters_ (0)
    {
      // Set the alters edge for lookup.
      //
      if (base != 0)
        g.new_edge<alters> (*this, *base);

      for (names_const_iterator i (s.names_begin ());
           i != s.names_end (); ++i)
      {
        nameable_type& n (i->nameable ().clone (*this, g));
        g.new_edge<names_type> (*this, n, i->name ());
      }
    }

    template <typename N>
    scope<N>::
    scope (xml::parser& p, scope* base, graph& g)
        : first_key_ (names_.end ()),
          first_drop_column_ (names_.end ()),
          alters_ (0)
    {
      // Set the alters edge for lookup.
      //
      if (base != 0)
        g.new_edge<alters> (*this, *base);

      using namespace xml;
      p.content (parser::complex);

      for (parser::event_type e (p.peek ());
           e == parser::start_element;
           e = p.peek ())
      {
        typename nameable_type::parser_map::iterator i (
          nameable_type::parser_map_.find (p.name ()));

        if (p.namespace_ () != xmlns || i == nameable_type::parser_map_.end ())
          break; // Not one of our elements.

        p.next ();
        i->second (p, *this, g);
        p.next_expect (parser::end_element);
      }
    }

    template <typename N>
    void scope<N>::
    serialize_content (xml::serializer& s) const
    {
      for (names_const_iterator i (names_begin ()); i != names_end (); ++i)
        i->nameable ().serialize (s);
    }

    class column;
    class primary_key;

    template <typename N>
    void scope<N>::
    add_edge_left (names_type& e)
    {
      name_type const& name (e.name ());

      typename names_map::iterator i (names_map_.find (name));

      if (i == names_map_.end ())
      {
        typename names_list::iterator i (names_.insert (names_.end (), &e));
        names_map_[name] = i;
        iterator_map_[&e] = i;
      }
      else
        throw duplicate_name (*this, (*i->second)->nameable (), e.nameable ());
    }

    template <typename N>
    void scope<N>::
    remove_edge_left (names_type& e)
    {
      typename names_iterator_map::iterator i (iterator_map_.find (&e));
      assert (i != iterator_map_.end ());

      names_.erase (i->second);
      names_map_.erase (e.name ());
      iterator_map_.erase (i);
    }
  }
}