aboutsummaryrefslogtreecommitdiff
path: root/odb/mysql/context.cxx
blob: 2c7452979872afc942cf784fc2fafd3e99ee35d5 (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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// file      : odb/mysql/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 <sstream>

#include <odb/sql-token.hxx>
#include <odb/sql-lexer.hxx>

#include <odb/mysql/context.hxx>

using namespace std;

namespace mysql
{
  namespace
  {
    struct type_map_entry
    {
      const char* const cxx_type;
      const char* const db_type;
      const char* const db_id_type;
    };

    type_map_entry type_map[] =
    {
      {"bool", "TINYINT(1) NOT NULL", 0},

      {"char", "TINYINT NOT NULL", 0},
      {"signed char", "TINYINT NOT NULL", 0},
      {"unsigned char", "TINYINT UNSIGNED NOT NULL", 0},

      {"short int", "SMALLINT NOT NULL", 0},
      {"short unsigned int", "SMALLINT UNSIGNED NOT NULL", 0},

      {"int", "INT NOT NULL", 0},
      {"unsigned int", "INT UNSIGNED NOT NULL", 0},

      {"long int", "BIGINT NOT NULL", 0},
      {"long unsigned int", "BIGINT UNSIGNED NOT NULL", 0},

      {"long long int", "BIGINT NOT NULL", 0},
      {"long long unsigned int", "BIGINT UNSIGNED NOT NULL", 0},

      {"float", "FLOAT NOT NULL", 0},
      {"double", "DOUBLE NOT NULL", 0},

      {"::std::string", "TEXT NOT NULL", "VARCHAR(255) NOT NULL"}
    };
  }

  context::
  context (ostream& os, semantics::unit& u, options_type const& ops)
      : base_context (os, u, ops, data_ptr (new (shared) data)),
        data_ (static_cast<data*> (base_context::data_.get ()))
  {
    // Populate the C++ type to DB type map.
    //
    for (size_t i (0); i < sizeof (type_map) / sizeof (type_map_entry); ++i)
    {
      type_map_entry const& e (type_map[i]);

      type_map_type::value_type v (
        e.cxx_type,
        db_type_type (e.db_type, e.db_id_type ? e.db_id_type : e.db_type));

      data_->type_map_.insert (v);
    }
  }

  context::
  context (context& c)
      : base_context (c),
        data_ (c.data_)
  {
  }

  string context::
  column_type (semantics::data_member& m) const
  {
    string r (::context::column_type (m));

    if (m.count ("auto"))
      r += " AUTO_INCREMENT";

    return r;
  }

  static sql_type
  parse_sql_type (semantics::data_member& m, std::string const& sql);

  sql_type const& context::
  db_type (semantics::data_member& m)
  {
    if (!m.count ("db-type"))
      m.set ("db-type", parse_sql_type (m, column_type (m)));

    return m.get<sql_type> ("db-type");
  }

  static sql_type
  parse_sql_type (semantics::data_member& m, string const& sql)
  {
    try
    {
      sql_type r;
      sql_lexer l (sql);

      // While most type names use single identifier, there are
      // a couple of exceptions to this rule:
      //
      // NATIONAL CHAR|VARCHAR
      // CHAR BYTE             (BINARY)
      // CHARACTER VARYING     (VARCHAR)
      // LONG VARBINARY        (MEDIUMBLOB)
      // LONG VARCHAR          (MEDIUMTEXT)
      //
      //
      enum state
      {
        parse_prefix,
        parse_name,
        parse_range,
        parse_sign,
        parse_done
      };

      state s (parse_prefix);
      string prefix;

      for (sql_token t (l.next ());
           s != parse_done && t.type () != sql_token::t_eos;
           t = l.next ())
      {
        sql_token::token_type tt (t.type ());

        switch (s)
        {
        case parse_prefix:
          {
            if (tt == sql_token::t_identifier)
            {
              string const& id (t.identifier ());

              if (id == "NATIONAL" ||
                  id == "CHAR" ||
                  id == "CHARACTER" ||
                  id == "LONG")
              {
                prefix = id;
                s = parse_name;
                continue;
              }
            }

            // Fall through.
            //
            s = parse_name;
          }
        case parse_name:
          {
            if (tt == sql_token::t_identifier)
            {
              bool match (true);
              string const& id (t.identifier ());

              // Numeric types.
              //
              if (id == "BIT")
              {
                r.type = sql_type::BIT;
              }
              else if (id == "TINYINT" || id == "INT1")
              {
                r.type = sql_type::TINYINT;
              }
              else if (id == "BOOL" || id == "BOOLEAN")
              {
                r.type = sql_type::TINYINT;
                r.range = true;
                r.range_value = 1;
              }
              else if (id == "SMALLINT" || id == "INT2")
              {
                r.type = sql_type::SMALLINT;
              }
              else if (id == "MEDIUMINT" || id == "INT3" || id == "MIDDLEINT")
              {
                r.type = sql_type::MEDIUMINT;
              }
              else if (id == "INT" || id == "INTEGER" || id == "INT4")
              {
                r.type = sql_type::INT;
              }
              else if (id == "BIGINT" || id == "INT8")
              {
                r.type = sql_type::BIGINT;
              }
              else if (id == "SERIAL")
              {
                r.type = sql_type::BIGINT;
                r.unsign = true;
              }
              else if (id == "FLOAT" || id == "FLOAT4")
              {
                r.type = sql_type::FLOAT;
              }
              else if (id == "DOUBLE" || id == "FLOAT8")
              {
                r.type = sql_type::DOUBLE;
              }
              else if (id == "DECIMAL" ||
                       id == "DEC" ||
                       id == "NUMERIC" ||
                       id == "FIXED")
              {
                r.type = sql_type::DECIMAL;
              }
              //
              // Date-time types.
              //
              else if (id == "DATE")
              {
                r.type = sql_type::DATE;
              }
              else if (id == "TIME")
              {
                r.type = sql_type::TIME;
              }
              else if (id == "DATETIME")
              {
                r.type = sql_type::DATETIME;
              }
              else if (id == "TIMESTAMP")
              {
                r.type = sql_type::TIMESTAMP;
              }
              else if (id == "YEAR")
              {
                r.type = sql_type::YEAR;
              }
              //
              // String and binary types.
              //
              else if (id == "NCHAR")
              {
                r.type = sql_type::CHAR;
              }
              else if (id == "VARCHAR")
              {
                r.type = prefix == "LONG"
                  ? sql_type::MEDIUMTEXT
                  : sql_type::VARCHAR;
              }
              else if (id == "NVARCHAR")
              {
                r.type = sql_type::VARCHAR;
              }
              else if (id == "VARYING" && prefix == "CHARACTER")
              {
                r.type = sql_type::VARCHAR;
              }
              else if (id == "BINARY")
              {
                r.type = sql_type::BINARY;
              }
              else if (id == "BYTE" && prefix == "CHAR")
              {
                r.type = sql_type::BINARY;
              }
              else if (id == "VARBINARY")
              {
                r.type = prefix == "LONG"
                  ? sql_type::MEDIUMBLOB
                  : sql_type::VARBINARY;
              }
              else if (id == "TINYBLOB")
              {
                r.type = sql_type::TINYBLOB;
              }
              else if (id == "TINYTEXT")
              {
                r.type = sql_type::TINYTEXT;
              }
              else if (id == "BLOB")
              {
                r.type = sql_type::BLOB;
              }
              else if (id == "TEXT")
              {
                r.type = sql_type::TEXT;
              }
              else if (id == "MEDIUMBLOB")
              {
                r.type = sql_type::MEDIUMBLOB;
              }
              else if (id == "MEDIUMTEXT")
              {
                r.type = sql_type::MEDIUMTEXT;
              }
              else if (id == "LONGBLOB")
              {
                r.type = sql_type::LONGBLOB;
              }
              else if (id == "LONGTEXT")
              {
                r.type = sql_type::LONGTEXT;
              }
              else if (id == "ENUM")
              {
                r.type = sql_type::ENUM;
              }
              else if (id == "SET")
              {
                r.type = sql_type::SET;
              }
              else
                match = false;

              if (match)
              {
                s = parse_range;
                continue;
              }
            }

            // Some prefixes can also be type names if not followed
            // by the actual type name.
            //
            if (!prefix.empty ())
            {
              if (prefix == "CHAR" || prefix == "CHARACTER")
              {
                r.type = sql_type::CHAR;
              }
              else if (prefix == "LONG")
              {
                r.type = sql_type::MEDIUMTEXT;
              }
            }

            if (r.type == sql_type::invalid)
            {
              cerr << m.file () << ":" << m.line () << ":" <<
                m.column () << ":";

              if (tt == sql_token::t_identifier)
                cerr << " error: unknown MySQL type '" <<
                  t.identifier () << "'" << endl;
              else
                cerr << " error: expected MySQL type name" << endl;

              throw generation_failed ();
            }

            // Fall through.
            //
            s = parse_range;
          }
        case parse_range:
          {
            if (t.punctuation () == sql_token::p_lparen)
            {
              t = l.next ();

              // ENUM and SET have a list of members instead of the range.
              //
              if (r.type == sql_type::ENUM || r.type == sql_type::SET)
              {
                // Skip tokens until we get the closing paren.
                //
                while (t.type () != sql_token::t_eos &&
                       t.punctuation () != sql_token::p_rparen)
                  t = l.next ();
              }
              else
              {
                if (t.type () != sql_token::t_int_lit)
                {
                  cerr << m.file () << ":" << m.line () << ":" << m.column ()
                       << ": error: integer range expected in MySQL type "
                       << "declaration" << endl;

                  throw generation_failed ();
                }

                unsigned int v;
                istringstream is (t.literal ());

                if (!(is >> v && is.eof ()))
                {
                  cerr << m.file () << ":" << m.line () << ":" << m.column ()
                       << ": error: invalid range value '" << t.literal ()
                       << "'in MySQL type declaration" << endl;

                  throw generation_failed ();
                }

                r.range = true;
                r.range_value = v;

                t = l.next ();

                if (t.punctuation () == sql_token::p_comma)
                {
                  // We have the second range value. Skip it.
                  //
                  l.next ();
                  t = l.next ();
                }
              }

              if (t.punctuation () != sql_token::p_rparen)
              {
                cerr << m.file () << ":" << m.line () << ":" << m.column ()
                     << ": error: expected ')' in MySQL type declaration"
                     << endl;

                throw generation_failed ();
              }

              s = parse_sign;
              continue;
            }

            // Fall through.
            //
            s = parse_sign;
          }
        case parse_sign:
          {
            if (tt == sql_token::t_identifier && t.identifier () == "UNSIGNED")
            {
              r.unsign = true;
            }

            s = parse_done;
            break;
          }
        }
      }

      if (s == parse_name && !prefix.empty ())
      {
        // Some prefixes can also be type names if not followed
        // by the actual type name.
        //
        if (prefix == "CHAR" || prefix == "CHARACTER")
        {
          r.type = sql_type::CHAR;
        }
        else if (prefix == "LONG")
        {
          r.type = sql_type::MEDIUMTEXT;
        }
      }

      if (r.type == sql_type::invalid)
      {
        cerr << m.file () << ":" << m.line () << ":" << m.column ()
             << ": error: incomplete MySQL type declaration" << endl;

        throw generation_failed ();
      }

      // If range is omitted for CHAR or BIT types, it defaults to 1.
      //
      if ((r.type == sql_type::CHAR || r.type == sql_type::BIT) && !r.range)
      {
        r.range = true;
        r.range_value = 1;
      }

      return r;
    }
    catch (sql_lexer::invalid_input const& e)
    {
      cerr << m.file () << ":" << m.line () << ":" << m.column ()
           << ": error: invalid MySQL type declaration: " << e.message << endl;

      throw generation_failed ();
    }
  }
}