aboutsummaryrefslogtreecommitdiff
path: root/odb/mssql/statement-processing.cxx
blob: f6f37aa7bdcb22a9250a21298958f615125d73a2 (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
// file      : odb/mssql/statement-processing.cxx
// copyright : Copyright (c) 2009-2018 Code Synthesis Tools CC
// license   : ODB NCUEL; see accompanying LICENSE file

#include <cassert>

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

#ifdef LIBODB_TRACE_STATEMENT_PROCESSING
#  include <iostream>
#endif

#include <odb/mssql/statement.hxx>

using namespace std;

namespace odb
{
  namespace mssql
  {
    typedef bind bind_type;

    void statement::
    process_select (std::string& r,
                    const char* s,
                    const bind_type* bind,
                    std::size_t bind_size,
#ifndef LIBODB_DEBUG_STATEMENT_PROCESSING
                    bool optimize
#else
                    bool
#endif
                    )
    {
      // This implementation is pretty much the same as the generic one
      // except for two things:
      //
      // 1. When checking for the fast case, take into account long data
      //    columns which we may have to re-arrange.
      //
      // 2. Create the column list in two passes, ordinary columns first
      //    followed by the long data columns.
      //

      bool empty (true); // Empty case (if none present).
      bool fast (true);  // Fast case (if all present and none are long data).
      for (size_t i (0); i != bind_size && (empty || fast); ++i)
      {
        const bind_type& b (bind[i]);

        if (b.buffer != 0)
          empty = false;
        else
          fast = false;

        if (b.type == bind_type::long_string ||
            b.type == bind_type::long_nstring ||
            b.type == bind_type::long_binary)
          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 && fuzzy_prefix (p, e, "JOIN ", 5))
      {
        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, "JOIN ", 5, true))
          ;

        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);
        bool need_second (false);

        // First pass: non-long data columns.
        //
        {
          size_t bi (0);
          for (const char *c (columns_begin), *ce (comma_begin (c, e));
               ce != 0; comma_next (c, ce, e))
          {
            const bind_type& b (bind[bi++]);

            // See if the column is present in the bind array and if it
            // is of the right kind.
            //
            if (b.buffer == 0)
              continue;

            if (b.type == bind_type::long_string ||
                b.type == bind_type::long_nstring ||
                b.type == bind_type::long_binary)
            {
              need_second = true;
              continue;
            }

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

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

        // Second pass: long data columns.
        //
        if (need_second)
        {
          size_t bi (0);
          for (const char *c (columns_begin), *ce (comma_begin (c, e));
               ce != 0; comma_next (c, ce, e))
          {
            const bind_type& b (bind[bi++]);

            // See if the column is present in the bind array and if it
            // is of the right kind.
            //
            if (b.buffer == 0 ||
                (b.type != bind_type::long_string &&
                 b.type != bind_type::long_nstring &&
                 b.type != bind_type::long_binary))
              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, je, "JOIN ", 5) + 5; // Skip past "JOIN ".
          const char* table_begin (p);
          p = find (p, je, ' '); // End of the table name.
          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 (p != je && // Not the end.
              (je - p < 4 || traits::compare (p, "ON ", 3) != 0))
          {
            // Something other than "ON ", so got to be an alias.
            //
            p += 3;
            alias_begin = p;
            p = find (p, je, ' '); // There might be no ON (CROSS JOIN).
            alias_size = (p != 0 ? p : je) - alias_begin;
          }
          else
          {
            // Just the table.
            //
            alias_begin = table_begin;
            alias_size = table_end - alias_begin;
          }

          // The alias must be quoted.
          //
          assert (*alias_begin == '[' &&
                  *(alias_begin + alias_size - 1) == ']');

          // 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 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] != '[')))
              continue;

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

            // Check that we are followed by a single identifier.
            //
            e = r.find (']', 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
    }
  }
}