aboutsummaryrefslogtreecommitdiff
path: root/odb/oracle/query-dynamic.cxx
blob: 5d8a2b7a2d78194f504050a6dd441e93cd199bbc (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
// file      : odb/oracle/query-dynamic.cxx
// copyright : Copyright (c) 2005-2012 Code Synthesis Tools CC
// license   : ODB NCUEL; see accompanying LICENSE file

#include <cstddef> // std::size_t

#include <odb/oracle/query-dynamic.hxx>
#include <odb/oracle/exceptions.hxx>    // lob_comparison

using namespace std;

namespace odb
{
  namespace oracle
  {
    static const char* logic_operators[] = {") AND (", ") OR ("};
    static const char* comp_operators[] = {"=", "!=", "<", ">", "<=", ">="};

    static void
    translate (query_base& q, const odb::query_base& s, size_t p)
    {
      typedef odb::query_base::clause_part part;

      const part& x (s.clause ()[p]);

      switch (x.kind)
      {
      case part::kind_column:
        {
          const query_column_base* c (
            static_cast<const query_column_base*> (
              x.native_info[id_oracle].column));

          q.append (c->table (), c->column ());
          break;
        }
      case part::kind_param_val:
      case part::kind_param_ref:
        {
          const query_column_base& qc (
            *static_cast<const query_column_base*> (
              x.native_info[id_oracle].column));

          query_param_factory f (
            reinterpret_cast<query_param_factory> (
              x.native_info[id_oracle].param_factory));

          // If factory is NULL, then this is a LOB value.
          //
          if (f == 0)
            throw lob_comparison ();

          const odb::query_param* p (
            reinterpret_cast<const odb::query_param*> (x.data));

          q.append (f (p->value, qc, x.kind == part::kind_param_ref),
                    qc.conversion ());
          break;
        }
      case part::kind_native:
        {
          q.append (s.strings ()[x.data]);
          break;
        }
      case part::kind_true:
      case part::kind_false:
        {
          q.append (x.kind == part::kind_true);
          break;
        }
      case part::op_add:
        {
          translate (q, s, x.data);
          translate (q, s, p - 1);
          break;
        }
      case part::op_and:
      case part::op_or:
        {
          q += "(";
          translate (q, s, x.data);
          q += logic_operators[x.kind - part::op_and];
          translate (q, s, p - 1);
          q += ")";
          break;
        }
      case part::op_not:
        {
          q += "NOT (";
          translate (q, s, p - 1);
          q += ")";
          break;
        }
      case part::op_null:
      case part::op_not_null:
        {
          translate (q, s, p - 1);
          q += (x.kind == part::op_null ? "IS NULL" : "IS NOT NULL");
          break;
        }
      case part::op_in:
        {
          size_t b (p - x.data);

          translate (q, s, b - 1); // column
          q += "IN (";

          for (size_t i (b); i != p; ++i)
          {
            if (i != b)
              q += ",";

            translate (q, s, i);
          }

          q += ")";
          break;
        }
      case part::op_eq:
      case part::op_ne:
      case part::op_lt:
      case part::op_gt:
      case part::op_le:
      case part::op_ge:
        {
          translate (q, s, x.data);
          q += comp_operators[x.kind - part::op_eq];
          translate (q, s, p - 1);
          break;
        }
      }
    }

    query_base::
    query_base (const odb::query_base& q)
        : binding_ (0, 0)
    {
      if (!q.empty ())
        translate (*this, q, q.clause ().size () - 1);
    }
  }
}