summaryrefslogtreecommitdiff
path: root/odb/relational/model.cxx
blob: 7c6638728f163c1821f6ee89d298bf36d7a482ed (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
// file      : odb/relational/model.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC
// license   : GNU GPL v3; see accompanying LICENSE file

#include <odb/gcc.hxx>

#include <cassert>
#include <limits>
#include <sstream>

#include <odb/relational/model.hxx>
#include <odb/relational/generate.hxx>

using namespace std;

namespace relational
{
  namespace model
  {
    // object_columns
    //
    string object_columns::
    default_ (semantics::data_member& m)
    {
      default_value* dv (0);

      semantics::type& t (utype (m));

      if (m.count ("default"))
        dv = &m.get<default_value> ("default");
      else if (t.count ("default"))
        dv = &t.get<default_value> ("default");
      else
        return ""; // No default value for this column.

      switch (dv->kind)
      {
      case default_value::reset:
        {
          // No default value.
          return "";
        }
      case default_value::null:
        {
          return default_null (m);
          break;
        }
      case default_value::boolean:
        {
          return default_bool (m, dv->value == "true");
          break;
        }
      case default_value::number:
        {
          tree n (dv->node);

          switch (TREE_CODE (n))
          {
          case INTEGER_CST:
            {
              HOST_WIDE_INT hwl (TREE_INT_CST_LOW (n));
              HOST_WIDE_INT hwh (TREE_INT_CST_HIGH (n));

              unsigned long long l (hwl);
              unsigned long long h (hwh);
              unsigned short width (HOST_BITS_PER_WIDE_INT);

              unsigned long long v ((h << width) + l);

              return default_integer (m, v, dv->value == "-");
              break;
            }
          case REAL_CST:
            {
              double v;

              REAL_VALUE_TYPE d (TREE_REAL_CST (n));

              if (REAL_VALUE_ISINF (d))
                v = numeric_limits<double>::infinity ();
              else if (REAL_VALUE_ISNAN (d))
                v = numeric_limits<double>::quiet_NaN ();
              else
              {
                char tmp[256];
                real_to_decimal (tmp, &d, sizeof (tmp), 0, true);
                istringstream is (tmp);
                is >> v;
              }

              if (dv->value == "-")
                v = -v;

              return default_float (m, v);
              break;
            }
          default:
            assert (false);
          }
          break;
        }
      case default_value::string:
        {
          return default_string (m, dv->value);
          break;
        }
      case default_value::enumerator:
        {
          return default_enum (m, dv->node, dv->value);
          break;
        }
      }

      return "";
    }

    cutl::shared_ptr<sema_rel::model>
    generate ()
    {
      context ctx;
      cutl::shared_ptr<sema_rel::model> m (new (shared) sema_rel::model);

      traversal::unit unit;
      traversal::defines unit_defines;
      typedefs unit_typedefs (false);
      traversal::namespace_ ns;
      instance<class_> c (*m);

      unit >> unit_defines >> ns;
      unit_defines >> c;
      unit >> unit_typedefs >> c;

      traversal::defines ns_defines;
      typedefs ns_typedefs (false);

      ns >> ns_defines >> ns;
      ns_defines >> c;
      ns >> ns_typedefs >> c;

      try
      {
        unit.dispatch (ctx.unit);
      }
      catch (sema_rel::duplicate_name const& e)
      {
        semantics::node& o (*e.orig.get<semantics::node*> ("cxx-node"));
        semantics::node& d (*e.dup.get<semantics::node*> ("cxx-node"));

        cerr << d.file () << ":" << d.line () << ":" << d.column ()
             << ": error: " << e.dup.kind () << " name '" << e.orig_name
             << "' conflicts with an already defined " << e.orig.kind ()
             << " name"
             << endl;

        cerr << o.file () << ":" << o.line () << ":" << o.column ()
             << ": info: conflicting " << e.orig.kind () << " is "
             << "defined here"
             << endl;

        cerr << d.file () << ":" << d.line () << ":" << d.column ()
             << ": error: use '#pragma db column' or '#pragma db table' "
             << "to change one of the names"
             << endl;

        throw operation_failed ();
      }

      return m;
    }
  }
}