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

#include <ostream>
#include <istream>

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

using namespace std;

namespace semantics
{
  namespace relational
  {
    static const char* action_str[] = {"NO ACTION", "CASCADE"};

    ostream&
    operator<< (ostream& os, foreign_key::action_type v)
    {
      return os << action_str[v];
    }

    istream&
    operator>> (istream& is, foreign_key::action_type& v)
    {
      string s;
      getline (is, s);

      if (!is.eof ())
        is.setstate (istream::failbit);

      if (!is.fail ())
      {
        if (s == "NO ACTION")
          v = foreign_key::no_action;
        else if (s == "CASCADE")
          v = foreign_key::cascade;
        else
          is.setstate (istream::failbit);
      }

      return is;
    }

    foreign_key::
    foreign_key (xml::parser& p, uscope& s, graph& g)
        : key (p, s, g),
          deferred_ (p.attribute ("deferred", false)),
          on_delete_ (p.attribute ("on-delete", no_action))
    {
      using namespace xml;

      p.next_expect (parser::start_element, xmlns, "references");
      referenced_table_ = p.attribute<qname> ("table");
      p.content (parser::complex);

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

        p.next ();
        referenced_columns_.push_back (p.attribute<uname> ("name"));
        p.content (parser::empty);
        p.next_expect (parser::end_element);
      }

      p.next_expect (parser::end_element);
    }

    void foreign_key::
    serialize (xml::serializer& s) const
    {
      s.start_element (xmlns, "foreign-key");
      key::serialize_attributes (s);

      if (deferred ())
        s.attribute ("deferred", true);

      if (on_delete () != no_action)
        s.attribute ("on-delete", on_delete ());

      key::serialize_content (s);

      // Referenced columns.
      //
      s.start_element (xmlns, "references");
      s.attribute ("table", referenced_table ());

      for (columns::const_iterator i (referenced_columns_.begin ());
           i != referenced_columns_.end (); ++i)
      {
        s.start_element (xmlns, "column");
        s.attribute ("name", *i);
        s.end_element ();
      }

      s.end_element (); // references
      s.end_element (); // foreign-key
    }

    // type info
    //
    namespace
    {
      struct init
      {
        init ()
        {
          unameable::parser_map_["foreign-key"] =
            &unameable::parser_impl<foreign_key>;

          using compiler::type_info;

          {
            type_info ti (typeid (foreign_key));
            ti.add_base (typeid (unameable));
            ti.add_base (typeid (key));
            insert (ti);
          }
        }
      } init_;
    }
  }
}