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

#ifndef ODB_SEMANTICS_RELATIONAL_FOREIGN_KEY_HXX
#define ODB_SEMANTICS_RELATIONAL_FOREIGN_KEY_HXX

#include <iosfwd>

#include <odb/semantics/relational/deferrable.hxx>
#include <odb/semantics/relational/elements.hxx>
#include <odb/semantics/relational/key.hxx>

namespace semantics
{
  namespace relational
  {
    class foreign_key: public key
    {
    public:
      qname const&
      referenced_table () const
      {
        return referenced_table_;
      }

      typedef std::vector<string> columns;

      columns const&
      referenced_columns () const
      {
        return referenced_columns_;
      }

      columns&
      referenced_columns ()
      {
        return referenced_columns_;
      }

    public:
      typedef relational::deferrable deferrable_type;

      deferrable_type
      deferrable () const {return deferrable_;}

      bool
      not_deferrable () const
      {
        return deferrable_ == deferrable_type::not_deferrable;
      }

      enum action_type
      {
        no_action,
        cascade,
        set_null
      };

      action_type
      on_delete () const {return on_delete_;}

    public:
      foreign_key (string const& id,
                   qname const& referenced_table,
                   deferrable_type deferrable,
                   action_type on_delete = no_action)
          : key (id),
            referenced_table_ (referenced_table),
            deferrable_ (deferrable),
            on_delete_ (on_delete)
      {
      }

      foreign_key (foreign_key const&, uscope&, graph&);
      foreign_key (xml::parser&, uscope&, graph&);

      virtual foreign_key&
      clone (uscope&, graph&) const;

      virtual string
      kind () const
      {
        return "foreign key";
      }

      virtual void
      serialize (xml::serializer&) const;

    protected:
      void
      serialize_attributes (xml::serializer&) const;

      void
      serialize_content (xml::serializer&) const;

    private:
      qname referenced_table_;
      columns referenced_columns_;
      deferrable_type deferrable_;
      action_type on_delete_;
    };

    std::ostream&
    operator<< (std::ostream&, foreign_key::action_type);

    std::istream&
    operator>> (std::istream&, foreign_key::action_type&);

    class add_foreign_key: public foreign_key
    {
    public:
      add_foreign_key (string const& id,
                       qname const& rt,
                       deferrable_type d,
                       action_type od = no_action)
          : foreign_key (id, rt, d, od) {}
      add_foreign_key (foreign_key const& fk, uscope& s, graph& g)
          : foreign_key (fk, s, g) {}
      add_foreign_key (xml::parser& p, uscope& s, graph& g)
          : foreign_key (p, s, g) {}

      virtual add_foreign_key&
      clone (uscope&, graph&) const;

      virtual string
      kind () const {return "add foreign key";}

      virtual void
      serialize (xml::serializer&) const;
    };

    class drop_foreign_key: public unameable
    {
    public:
      drop_foreign_key (string const& id): unameable (id) {}
      drop_foreign_key (drop_foreign_key const& dfk, uscope&, graph& g)
          : unameable (dfk, g) {}
      drop_foreign_key (xml::parser&, uscope&, graph&);

      virtual drop_foreign_key&
      clone (uscope&, graph&) const;

      virtual string
      kind () const {return "drop foreign key";}

      virtual void
      serialize (xml::serializer&) const;
    };
  }
}

#endif // ODB_SEMANTICS_RELATIONAL_FOREIGN_KEY_HXX