aboutsummaryrefslogtreecommitdiff
path: root/odb/statement-processing.cxx
blob: 3920995d5905e5d1dad85de55c3948199f6f2c7c (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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
// file      : odb/statement-processing.cxx
// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#include <cassert>

#include <odb/statement-processing-common.hxx>

#ifdef LIBODB_TRACE_STATEMENT_PROCESSING
#  include <iostream>
#endif

#include <odb/statement.hxx>

using namespace std;

namespace odb
{
  typedef const void* const* bind_type;

  static inline const void*
  bind_at (size_t i, bind_type bind, size_t bind_skip)
  {
    const char* b (reinterpret_cast<const char*> (bind));
    return *reinterpret_cast<bind_type> (b + i * bind_skip);
  }

  void statement::
  process_insert (const char* s,
                  bind_type bind,
                  size_t bind_size,
                  size_t bind_skip,
                  char param_symbol,
                  string& r)
  {
#ifndef LIBODB_DEBUG_STATEMENT_PROCESSING
    assert (bind_size != 0); // Cannot be versioned.
#endif

    bool fast (true); // Fast case (if all present).
    for (size_t i (0); i != bind_size && fast; ++i)
    {
      if (bind_at (i, bind, bind_skip) == 0)
        fast = false;
    }

    // Fast path: just remove the "structure".
    //
#ifndef LIBODB_DEBUG_STATEMENT_PROCESSING
    if (fast)
    {
      process_fast (s, r);
      return;
    }
#endif

    // Scan the statement and store the positions of various parts.
    //
    size_t n (traits::length (s));
    const char* e (s + n);

    // Header.
    //
    const char* p (find (s, e, '\n'));
    assert (p != 0);
    size_t header_size (p - s);
    p++;

    // Column list.
    //
    const char* columns_begin (0);
    if (*p == '(')
    {
      columns_begin = p;

      // Find the end of the column list.
      //
      for (const char* ce (paren_begin (p, e)); ce != 0; paren_next (p, ce, e))
        ;
    }

    // OUTPUT
    //
    const char* output_begin (0);
    size_t output_size (0);
    if (e - p > 7 && traits::compare (p, "OUTPUT ", 7) == 0)
    {
      output_begin = p;
      p += 7;
      p = find (p, e, '\n');
      assert (p != 0);
      output_size = p - output_begin;
      p++;
    }

    // VALUES or DEFAULT VALUES
    //
    bool empty (true); // DEFAULT VALUES case (if none present).
    const char* values_begin (0);
    if (e - p > 7 && traits::compare (p, "VALUES\n", 7) == 0)
    {
      p += 7;
      values_begin = p;

      size_t bi (0);
      for (const char* ve (paren_begin (p, e)); ve != 0; paren_next (p, ve, e))
      {
        // We cannot be empty if we have a non-parameterized value, e.g.,
        // INSERT ... VALUES(1,?). We also cannot be empty if this value
        // is present in the bind array.
        //
        if (find (p, ve, param_symbol) == 0 ||
            bind_at (bi++, bind, bind_skip) != 0)
          empty = false;
      }
    }
    else
    {
      // Must be DEFAULT VALUES.
      //
      assert (traits::compare (p, "DEFAULT VALUES", 14) == 0);
      p += 14;

      if (*p == '\n')
        p++;
    }

    // Trailer.
    //
    const char* trailer_begin (0);
    size_t trailer_size (0);
    if (e - p != 0)
    {
      trailer_begin = p;
      trailer_size = e - p;
    }

    // Empty case.
    //
    if (empty)
    {
      r.reserve (header_size +
                 (output_size == 0 ? 0 : output_size + 1) +
                 15 + // For " DEFAULT VALUES"
                 (trailer_size == 0 ? 0 : trailer_size + 1));

      r.assign (s, header_size);

      if (output_size != 0)
      {
        r += ' ';
        r.append (output_begin, output_size);
      }

      r += " DEFAULT VALUES";

      if (trailer_size != 0)
      {
        r += ' ';
        r.append (trailer_begin, trailer_size);
      }

#ifdef LIBODB_TRACE_STATEMENT_PROCESSING
      if (r.size () != n)
        cerr << endl
             << "old: '" << s << "'" << endl << endl
             << "new: '" << r << "'" << endl << endl;
#endif

      return;
    }

    // Assume the same size as the original. It can only shrink, and in
    // most cases only slightly. So this is a good approximation.
    //
    r.reserve (n);
    r.assign (s, header_size);

    // Column list.
    //
    {
      r += ' ';

      size_t i (0), bi (0);

      for (const char *c (columns_begin), *ce (paren_begin (c, e)),
                      *v (values_begin), *ve (paren_begin (v, e));
           ce != 0; paren_next (c, ce, e), paren_next (v, ve, e))
      {
        // See if the value contains the parameter symbol and, if so,
        // whether it is present in the bind array.
        //
        if (find (v, ve, param_symbol) != 0 &&
            bind_at (bi++, bind, bind_skip) == 0)
          continue;

        // Append the column.
        //
        if (i++ == 0)
          r += '(';
        else
          r += ", "; // Add the space for consistency with the fast path.

        r.append (c, ce - c);
      }

      r += ')';
    }

    // OUTPUT
    //
    if (output_size != 0)
    {
      r += ' ';
      r.append (output_begin, output_size);
    }

    // Value list.
    //
    {
      r += " VALUES ";

      size_t i (0), bi (0);

      for (const char* v (values_begin), *ve (paren_begin (v, e));
           ve != 0; paren_next (v, ve, e))
      {
        // See if the value contains the parameter symbol and, if so,
        // whether it is present in the bind array.
        //
        if (find (v, ve, param_symbol) != 0 &&
            bind_at (bi++, bind, bind_skip) == 0)
          continue;

        // Append the value.
        //
        if (i++ == 0)
          r += '(';
        else
          r += ", "; // Add the space for consistency with the fast path.

        r.append (v, ve - v);
      }

      r += ')';
    }

    // Trailer.
    //
    if (trailer_size != 0)
    {
      r += ' ';
      r.append (trailer_begin, trailer_size);
    }

#ifdef LIBODB_TRACE_STATEMENT_PROCESSING
    if (r.size () != n)
      cerr << endl
           << "old: '" << s << "'" << endl << endl
           << "new: '" << r << "'" << endl << endl;
#endif
  }

  void statement::
  process_update (const char* s,
                  bind_type bind,
                  size_t bind_size,
                  size_t bind_skip,
                  char param_symbol,
                  string& r)
  {
    bool fast (true); // Fast case (if all present).
    for (size_t i (0); i != bind_size && fast; ++i)
    {
      if (bind_at (i, bind, bind_skip) == 0)
        fast = false;
    }

    // Fast path: just remove the "structure".
    //
#ifndef LIBODB_DEBUG_STATEMENT_PROCESSING
    if (fast)
    {
      process_fast (s, r);
      return;
    }
#endif

    // Scan the statement and store the positions of various parts.
    //
    size_t n (traits::length (s));
    const char* e (s + n);

    // Header.
    //
    const char* p (find (s, e, '\n'));
    assert (p != 0);
    size_t header_size (p - s);
    p++;

    // SET
    //
    bool empty (true); // Empty SET case.
    const char* set_begin (0);
    if (e - p > 4 && traits::compare (p, "SET\n", 4) == 0)
    {
      p += 4;
      set_begin = p;

      // Scan the SET list.
      //
      size_t bi (0);
      for (const char* pe (comma_begin (p, e)); pe != 0; comma_next (p, pe, e))
      {
        if (empty)
        {
          // We cannot be empty if we have a non-parameterized set expression,
          // e.g., UPDATE ... SET ver=ver+1 ... We also cannot be empty if
          // this expression is present in the bind array.
          //
          if (find (p, pe, param_symbol) == 0 ||
              bind_at (bi++, bind, bind_skip) != 0)
            empty = false;
        }
      }
    }

    // Empty case.
    //
    if (empty)
    {
      r.clear ();

#ifdef LIBODB_TRACE_STATEMENT_PROCESSING
      if (n != 0)
        cerr << endl
             << "old: '" << s << "'" << endl << endl
             << "new: '" << r << "'" << endl << endl;
#endif

      return;
    }

    // Trailer.
    //
    const char* trailer_begin (0);
    size_t trailer_size (0);
    if (e - p != 0)
    {
      trailer_begin = p;
      trailer_size = e - p;
    }

    // Assume the same size as the original. It can only shrink, and in
    // most cases only slightly. So this is a good approximation.
    //
    r.reserve (n);
    r.assign (s, header_size);

    // SET list.
    //
    {
      r += " SET ";

      size_t i (0), bi (0);

      for (const char* p (set_begin), *pe (comma_begin (p, e));
           pe != 0; comma_next (p, pe, e))
      {
        // See if the value contains the parameter symbol and, if so,
        // whether it is present in the bind array.
        //
        if (find (p, pe, param_symbol) != 0 &&
            bind_at (bi++, bind, bind_skip) == 0)
          continue;

        // Append the expression.
        //
        if (i++ != 0)
          r += ", "; // Add the space for consistency with the fast path.

        r.append (p, pe - p);
      }
    }

    // Trailer.
    //
    if (trailer_size != 0)
    {
      r += ' ';
      r.append (trailer_begin, trailer_size);
    }

#ifdef LIBODB_TRACE_STATEMENT_PROCESSING
    if (r.size () != n)
      cerr << endl
           << "old: '" << s << "'" << endl << endl
           << "new: '" << r << "'" << endl << endl;
#endif
  }

  void statement::
  process_select (const char* s,
                  bind_type bind,
                  size_t bind_size,
                  size_t bind_skip,
                  char quote_open,
                  char quote_close,
#ifndef LIBODB_DEBUG_STATEMENT_PROCESSING
                  bool optimize,
#else
                  bool,
#endif
                  string& r,
                  bool as)
  {
    bool empty (true); // Empty case (if none present).
    bool fast (true);  // Fast case (if all present).
    for (size_t i (0); i != bind_size && (empty || fast); ++i)
    {
      if (bind_at (i, bind, bind_skip) != 0)
        empty = false;
      else
        fast = false;
    }

    // Empty.
    //
    if (empty)
    {
      r.clear ();

#ifdef LIBODB_TRACE_STATEMENT_PROCESSING
      if (*s != '\0')
        cerr << endl
             << "old: '" << s << "'" << endl << endl
             << "new: '" << r << "'" << endl << endl;
#endif
      return;
    }

    // Fast path: just remove the "structure".
    //
#ifndef LIBODB_DEBUG_STATEMENT_PROCESSING
    if (fast && !optimize)
    {
      process_fast (s, r);
      return;
    }
#endif

    // Scan the statement and store the positions of various parts.
    //
    size_t n (traits::length (s));
    const char* e (s + n);

    // Header.
    //
    const char* p (find (s, e, '\n'));
    assert (p != 0);
    size_t header_size (p - s);
    p++;

    // Column list.
    //
    const char* columns_begin (p);
    for (const char* ce (comma_begin (p, e)); ce != 0; comma_next (p, ce, e))
      ;

    // FROM.
    assert (traits::compare (p, "FROM ", 5) == 0);
    const char* from_begin (p);
    p = find (p, e, '\n'); // May not end with '\n'.
    if (p == 0)
      p = e;
    size_t from_size (p - from_begin);
    if (p != e)
      p++;

    // JOIN list.
    //
    const char* joins_begin (0), *joins_end (0);
    if (e - p > 5 && traits::compare (p, "LEFT JOIN ", 10) == 0)
    {
      joins_begin = p;

      // Find the end of the JOIN list.
      //
      for (const char* je (newline_begin (p, e));
           je != 0; newline_next (p, je, e, "LEFT JOIN ", 10))
        ;

      joins_end = (p != e ? p - 1 : p);
    }

#ifndef LIBODB_DEBUG_STATEMENT_PROCESSING
    if (fast && joins_begin == 0)
    {
      // No JOINs to optimize so can still take the fast path.
      //
      process_fast (s, r);
      return;
    }
#endif

    // Trailer (WHERE, ORDER BY, etc).
    //
    const char* trailer_begin (0);
    size_t trailer_size (0);
    if (e - p != 0)
    {
      trailer_begin = p;
      trailer_size = e - p;
    }

    // Assume the same size as the original. It can only shrink, and in
    // most cases only slightly. So this is a good approximation.
    //
    r.reserve (n);
    r.assign (s, header_size);

    // Column list.
    //
    {
      r += ' ';

      size_t i (0), bi (0);

      for (const char *c (columns_begin), *ce (comma_begin (c, e));
           ce != 0; comma_next (c, ce, e))
      {
        // See if the column is present in the bind array.
        //
        if (bind_at (bi++, bind, bind_skip) == 0)
          continue;

        // Append the column.
        //
        if (i++ != 0)
          r += ", "; // Add the space for consistency with the fast path.

        r.append (c, ce - c);
      }
    }

    // From.
    //
    r += ' ';
    r.append (from_begin, from_size);

    // JOIN list, pass 1.
    //
    size_t join_pos (0);
    if (joins_begin != 0)
    {
      // Fill in the JOIN "area" with spaces.
      //
      r.resize (r.size () + joins_end - joins_begin + 1, ' ');
      join_pos = r.size () + 1; // End of the last JOIN.
    }

    // Trailer.
    //
    if (trailer_size != 0)
    {
      r += ' ';
      r.append (trailer_begin, trailer_size);
    }

    // JOIN list, pass 2.
    //
    if (joins_begin != 0)
    {
      // Splice the JOINs into the pre-allocated area.
      //
      for (const char* je (joins_end), *j (newline_rbegin (je, joins_begin));
           j != 0; newline_rnext (j, je, joins_begin))
      {
        size_t n (je - j);

        // Get the alias or, if none used, the table name.
        //
        p = find (j + 10, je, ' '); // 10 for "LEFT JOIN ".
        const char* table_end (p);
        p++; // Skip space.

        // We may or may not have the AS keyword.
        //
        const char* alias_begin (0);
        size_t alias_size (0);
        if (je - p > 3 && traits::compare (p, "ON ", 3) != 0)
        {
          if (as)
            p += 3;

          alias_begin = p;
          alias_size = find (p, je, ' ') - alias_begin;
        }
        else
        {
          alias_begin = j + 10;
          alias_size = table_end - alias_begin;
        }

        // The alias must be quoted.
        //
        assert (*alias_begin == quote_open &&
                *(alias_begin + alias_size - 1) == quote_close);

        // We now need to see if the alias is used in either the SELECT
        // list, the WHERE conditions, or the ON condition of any of the
        // JOINs that we have already processed and decided to keep.
        //
        // Instead of re-parsing the whole thing again, we are going to
        // take a shortcut and simply search for the alias in the statement
        // we have constructed so far (that's why we have have added the
        // trailer before filling in the JOINs). To make it more robust,
        // we are going to do a few extra sanity checks, specifically,
        // that the alias is a top level identifier and is followed by
        // only a single identifer (column). This will catch cases like
        // [s].[t].[c] where [s] is also used as an alias or LEFT JOIN [t]
        // where [t] is also used as an alias in another JOIN.
        //
        bool found (false);
        for (size_t p (r.find (alias_begin, 0, alias_size));
             p != string::npos;
             p = r.find (alias_begin, p + alias_size, alias_size))
        {
          size_t e (p + alias_size);

          // If we are not a top-level qualifier or not a bottom-level,
          // then we are done (3 is for at least "[a]").
          //
          if ((p != 0 && r[p - 1] == '.') ||
              (e + 3 >= r.size () || (r[e] != '.' || r[e + 1] != quote_open)))
            continue;

          // The only way to distinguish the [a].[c] from FROM [a].[c] or
          // LEFT JOIN [a].[c] is by checking the prefix.
          //
          if ((p > 5 && r.compare (p - 5, 5, "FROM ") == 0) ||
              (p > 10 && r.compare (p - 10, 10, "LEFT JOIN ") == 0))
            continue;

          // Check that we are followed by a single identifier.
          //
          e = r.find (quote_close, e + 2);
          if (e == string::npos || (e + 1 != r.size () && r[e + 1] == '.'))
            continue;

          found = true;
          break;
        }

        join_pos -= n + 1; // Extra one for space.
        if (found)
          r.replace (join_pos, n, j, n);
        else
          r.erase (join_pos - 1, n + 1); // Extra one for space.
      }
    }

#ifdef LIBODB_TRACE_STATEMENT_PROCESSING
    if (r.size () != n)
      cerr << endl
           << "old: '" << s << "'" << endl << endl
           << "new: '" << r << "'" << endl << endl;
#endif
  }
}