summaryrefslogtreecommitdiff
path: root/odb/relational/oracle/schema.cxx
blob: 84697615f96215727194dd09d6531641b8039e4a (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// file      : odb/relational/oracle/schema.cxx
// author    : Constantin Michael <constantin@codesynthesis.com>
// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC
// license   : GNU GPL v3; see accompanying LICENSE file

#include <string>

#include <odb/relational/schema.hxx>

#include <odb/relational/oracle/common.hxx>
#include <odb/relational/oracle/context.hxx>

using namespace std;

namespace relational
{
  namespace oracle
  {
    namespace schema
    {
      namespace relational = relational::schema;

      struct schema_emitter: relational::schema_emitter
      {
        schema_emitter (const base& x): base (x) {}

        virtual void
        line (const std::string& l)
        {
          base::line (l);
          last_ = l;
        }

        virtual void
        post ()
        {
          if (!first_) // Ignore empty statements.
          {
            if (last_ == "END;")
              os << endl
                 << '/' << endl
                 << endl;

            else
              os << ';' << endl
                 << endl;
          }
        }

      private:
        string last_;
      };
      entry<schema_emitter> schema_emitter_;

      //
      // File.
      //

      struct schema_file: relational::schema_file, context
      {
        schema_file (const base& x): base (x) {}

        virtual void
        pre ()
        {
          os << "SET FEEDBACK OFF;" << endl
             << "WHENEVER SQLERROR EXIT FAILURE;" << endl
             << "WHENEVER OSERROR EXIT FAILURE;" << endl
             << endl;
        }

        virtual void
        post ()
        {
          os << "EXIT;" << endl;
        }
      };
      entry<schema_file> schema_file_;

      //
      // Drop.
      //

      struct drop_common: virtual relational::drop_common
      {
        virtual void
        drop_table (string const& table)
        {
          // Oracle has no IF EXISTS conditional for dropping objects. The
          // PL/SQL approach below seems to be the least error-prone and the
          // most widely used of the alternatives.
          //
          os << "BEGIN" << endl
             << "  BEGIN" << endl
             << "    EXECUTE IMMEDIATE 'DROP TABLE " << quote_id (table) <<
            " CASCADE CONSTRAINTS';" << endl
             << "  EXCEPTION" << endl
             << "    WHEN OTHERS THEN" << endl
             << "      IF SQLCODE != -942 THEN RAISE; END IF;" << endl
             << "  END;"
             << "  BEGIN" << endl
             << "    EXECUTE IMMEDIATE 'DROP SEQUENCE " <<
            quote_id (table + "_seq") << "';" << endl
             << "  EXCEPTION" << endl
             << "    WHEN OTHERS THEN" << endl
             << "      IF SQLCODE != -2289 THEN RAISE; END IF;" << endl
             << "  END;" << endl
             << "  BEGIN" << endl
             << "    EXECUTE IMMEDIATE 'DROP TRIGGER " <<
            quote_id (table + "_trig") << "';" << endl
             << "  EXCEPTION" << endl
             << "    WHEN OTHERS THEN" << endl
             << "      IF SQLCODE != -4080 THEN RAISE; END IF;" << endl
             << "  END;" << endl
             << "END;" << endl;
        }
      };

      struct member_drop: relational::member_drop, drop_common
      {
        member_drop (base const& x): base (x) {}
      };
      entry<member_drop> member_drop_;

      struct class_drop: relational::class_drop, drop_common
      {
        class_drop (base const& x): base (x) {}
      };
      entry<class_drop> class_drop_;

      //
      // Create.
      //

      struct object_columns: relational::object_columns, context
      {
        object_columns (base const& x): base (x) {}

        virtual void
        null (semantics::data_member& m)
        {
          sql_type::core_type t (column_sql_type (m, prefix_).type);

          // Oracle interprets empty VARCHAR2 and NVARCHAR2 strings as NULL. As
          // an empty string is always valid within the C++ context, VARCHAR2
          // and NVARCHAR2 columns are always specified as nullable.
          //
          if (t != sql_type::VARCHAR2 && t != sql_type::NVARCHAR2)
            base::null (m);
        }

        virtual void
        default_enum (semantics::data_member& m, tree en, string const&)
        {
          // Make sure the column is mapped to Oracle NUMBER.
          //
          sql_type t (column_sql_type (m));

          if (t.type != sql_type::NUMBER)
          {
            cerr << m.file () << ":" << m.line () << ":" << m.column ()
                 << ": error: column with default value specified as C++ "
                 << "enumerator must map to Oracle NUMBER" << endl;

            throw operation_failed ();
          }

          using semantics::enumerator;

          enumerator& e (dynamic_cast<enumerator&> (*unit.find (en)));

          if (e.enum_ ().unsigned_ ())
            os << " DEFAULT " << e.value ();
          else
            os << " DEFAULT " << static_cast<long long> (e.value ());
        }

        virtual void
        reference (semantics::data_member&)
        {
        }
      };
      entry<object_columns> object_columns_;

      struct object_columns_references:
        object_columns_base, relational::common, context
      {
        object_columns_references (emitter& e,
                                   ostream& os,
                                   string const& table,
                                   string const& prefix = string ())
            : relational::common (e, os),
              table_ (table),
              prefix_ (prefix)
        {
        }

        virtual bool
        traverse_column (semantics::data_member& m, string const& name, bool)
        {
          if (inverse (m))
            return false;

          if (semantics::class_* c =
              object_pointer (member_utype (m, prefix_)))
          {
            pre_statement ();

            os << "ALTER TABLE " << quote_id (table_) << endl
               << "  ADD FOREIGN KEY (" << quote_id (name) << ")" << endl
               << "  REFERENCES " << table_qname (*c) << endl
               << "  DEFERRABLE INITIALLY DEFERRED" << endl;

            post_statement ();
          }
          else if (prefix_ == "id")
          {
            semantics::class_& c (*context::top_object);

            pre_statement ();

            // We don't need INITIALLY DEFERRED here since the object row
            // must exist before any container row.
            //
            os << "ALTER TABLE " << quote_id (table_) << endl
               << "  ADD FOREIGN KEY (" << quote_id (name) << ")" << endl
               << "  REFERENCES " << table_qname (c) << endl
               << "  ON DELETE CASCADE" << endl;

            post_statement ();
          }

          return true;
        }

      private:
        string table_;
        string prefix_;
      };

      struct member_create: object_members_base, context
      {
        member_create (emitter& e, ostream& os, relational::tables& tables)
            : object_members_base (false, true, false),
              e_ (e),
              os_ (os),
              tables_ (tables)
        {
        }

        virtual void
        traverse_container (semantics::data_member& m, semantics::type& t)
        {
          using semantics::type;
          using semantics::data_member;

          // Ignore inverse containers of object pointers.
          //
          if (inverse (m, "value"))
            return;

          string const& name (table_name (m, table_prefix_));

          if (tables_.count (name))
            return;

          type& vt (container_vt (t));

          // object_id
          //
          {
            object_columns_references ocr (e_, os_, name, "id");
            string id_name (column_name (m, "id", "object_id"));
            ocr.traverse_column (m, id_name, true);
          }

          // value
          //
          if (semantics::class_* cvt = composite_wrapper (vt))
          {
            object_columns_references ocr (e_, os_, name);
            ocr.traverse (m, *cvt, "value", "value");
          }
          else
          {
            object_columns_references ocr (e_, os_, name, "value");
            string const& value_name (column_name (m, "value", "value"));
            ocr.traverse_column (m, value_name, true);
          }

          tables_.insert (name);
        }

      private:
        emitter& e_;
        ostream& os_;
        relational::tables& tables_;
      };

      struct class_create: relational::class_create, context
      {
        class_create (base const& x): base (x) {}

        virtual void
        traverse (type& c)
        {
          if (pass_ != 2)
          {
            base::traverse (c);
            return;
          }

          if (c.file () != unit.file ())
            return;

          if (!object (c) || abstract (c))
            return;

          string const& name (table_name (c));

          if (tables_[pass_].count (name))
            return;

          semantics::data_member* id (id_member (c));

          if (id->count ("auto"))
          {
            string seq_name (quote_id (name + "_seq"));

            pre_statement ();

            os_ << "CREATE SEQUENCE " << seq_name << endl
                << "  START WITH 1 INCREMENT BY 1" << endl
                << endl;

            post_statement ();

            pre_statement ();

            os_ << "CREATE TRIGGER " <<
              quote_id (name + "_trig") << endl
                << "  BEFORE INSERT ON " << quote_id (name) << endl
                << "  FOR EACH ROW" << endl
                << "BEGIN" << endl
                << "  SELECT " << seq_name << ".nextval INTO :new." <<
              column_qname (*id) << " FROM DUAL;" << endl
                << "END;" << endl;

            post_statement ();
          }

          object_columns_references ocr (e_, os_, name);
          ocr.traverse (c);

          tables_[pass_].insert (name);

          member_create mc (e_, os_, tables_[pass_]);
          mc.traverse (c);
        }
      };
      entry<class_create> class_create_;
    }
  }
}