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

#include <odb/context.hxx>

using namespace std;

namespace
{
  char const* keywords[] =
  {
    "NULL",
    "and",
    "asm",
    "auto",
    "bitand",
    "bitor",
    "bool",
    "break",
    "case",
    "catch",
    "char",
    "class",
    "compl",
    "const",
    "const_cast",
    "continue",
    "default",
    "delete",
    "do",
    "double",
    "dynamic_cast",
    "else",
    "end_eq",
    "enum",
    "explicit",
    "export",
    "extern",
    "false",
    "float",
    "for",
    "friend",
    "goto",
    "if",
    "inline",
    "int",
    "long",
    "mutable",
    "namespace",
    "new",
    "not",
    "not_eq",
    "operator",
    "or",
    "or_eq",
    "private",
    "protected",
    "public",
    "register",
    "reinterpret_cast",
    "return",
    "short",
    "signed",
    "sizeof",
    "static",
    "static_cast",
    "struct",
    "switch",
    "template",
    "this",
    "throw",
    "true",
    "try",
    "typedef",
    "typeid",
    "typename",
    "union",
    "unsigned",
    "using",
    "virtual",
    "void",
    "volatile",
    "wchar_t",
    "while",
    "xor",
    "xor_eq"
  };

  struct type_map_entry
  {
    const char* const cxx_type;
    const char* const db_type;
  };

  type_map_entry mysql_type_map[] =
  {
    {"bool", "TINYINT(1)"},

    {"char", "TINYINT"},
    {"signed char", "TINYINT"},
    {"unsigned char", "TINYINT UNSIGNED"},

    {"short int", "SMALLINT"},
    {"short unsigned int", "SMALLINT UNSIGNED"},

    {"int", "INT"},
    {"unsigned int", "INT UNSIGNED"},

    {"long int", "BIGINT"},
    {"long unsigned int", "BIGINT UNSIGNED"},

    {"long long int", "BIGINT"},
    {"long long unsigned int", "BIGINT UNSIGNED"},

    {"float", "FLOAT"},
    {"double", "DOUBLE"},

    {"::std::string", "TEXT"}
  };
}

context::
context (ostream& os_,
         semantics::unit& unit_,
         options_type const& ops)
    : data_ (new (shared) data),
      os (os_),
      unit (unit_),
      options (ops),
      keyword_set (data_->keyword_set_)
{
  for (size_t i (0); i < sizeof (keywords) / sizeof (char*); ++i)
    data_->keyword_set_.insert (keywords[i]);

  // Populate the C++ type to DB type map.
  //
  {
    size_t n;
    type_map_entry* p;

    switch (options.database ())
    {
    case database::mysql:
      {
        p = mysql_type_map;
        n = sizeof (mysql_type_map) / sizeof (type_map_entry);
        break;
      }
    default:
      {
        p = 0;
        n = 0;
        break;
      }
    }

    for (size_t i (0); i < n; ++i)
      data_->type_map_.insert (
        type_map_type::value_type (p[i].cxx_type, p[i].db_type));
  }
}

context::
context (context& c)
    : data_ (c.data_),
      os (c.os),
      unit (c.unit),
      options (c.options),
      keyword_set (c.keyword_set)
{
}

string context::
table_name (semantics::type& t) const
{
  if (t.count ("table"))
    return t.get<string> ("table");
  else
    return t.name ();
}

string context::
column_name (semantics::data_member& m) const
{
  if (m.count ("column"))
    return m.get<string> ("column");
  else
  {
    string s (m.name ());
    size_t n (s.size ());

    // Do basic processing: remove trailing and leading underscores
    // as well as the 'm_' prefix.
    //
    // @@ What if the resulting names conflict?
    //
    size_t b (0), e (n - 1);

    if (n > 2 && s[0] == 'm' && s[1] == '_')
      b += 2;

    for (; b <= e && s[b] == '_'; b++) ;
    for (; e >= b && s[e] == '_'; e--) ;

    return b > e ? s : string (s, b, e - b + 1);
  }
}

string context::
db_type (semantics::data_member& m) const
{
  if (m.count ("type"))
    return m.get<string> ("type");

  string const& name (m.type ().fq_name ());
  type_map_type::const_iterator i (data_->type_map_.find (name));

  if (i != data_->type_map_.end ())
    return i->second;

  cerr << m.file () << ":" << m.line () << ":" << m.column ()
       << " error: unable to map C++ type '" << name << "' used in "
       << "data member '" << m.name () << "' to a database type" << endl;

  cerr << m.file () << ":" << m.line () << ":" << m.column ()
       << " info: use '#pragma odb type' to specify the database type"
       << endl;

  throw generation_failed ();
}

string context::
escape (string const& name) const
{
  typedef string::size_type size;

  string r;
  size n (name.size ());

  // In most common cases we will have that many characters.
  //
  r.reserve (n);

  for (size i (0); i < n; ++i)
  {
    char c (name[i]);

    if (i == 0)
    {
      if (!((c >= 'a' && c <= 'z') ||
            (c >= 'A' && c <= 'Z') ||
            c == '_'))
        r = (c >= '0' && c <= '9') ? "cxx_" : "cxx";
    }

    if (!((c >= 'a' && c <= 'z') ||
          (c >= 'A' && c <= 'Z') ||
          (c >= '0' && c <= '9') ||
          c == '_'))
      r += '_';
    else
      r += c;
  }

  if (r.empty ())
    r = "cxx";

  // Custom reserved words.
  //
  /*
  reserved_name_map_type::const_iterator i (reserved_name_map.find (r));

  if (i != reserved_name_map.end ())
  {
    if (!i->second.empty ())
      return i->second;
    else
      r += L'_';
  }
  */

  // Keywords
  //
  if (keyword_set.find (r) != keyword_set.end ())
  {
    r += '_';

    // Re-run custom words.
    //
    /*
    i = reserved_name_map.find (r);

    if (i != reserved_name_map.end ())
    {
      if (!i->second.empty ())
        return i->second;
      else
        r += L'_';
    }
    */
  }

  return r;
}

// namespace
//

void namespace_::
traverse (type& ns)
{
  // Only traverse namespaces from the main file.
  //
  if (ns.file () == unit.file ())
  {
    string name (ns.name ());

    if (name.empty ())
      os << "namespace";
    else
      os << "namespace " << name;

    os << "{";

    traversal::namespace_::traverse (ns);

    os << "}";
  }
}