aboutsummaryrefslogtreecommitdiff
path: root/odb/query-dynamic.cxx
blob: 9c3493f31611ba0836061c74250ea273a25a42c6 (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
// file      : odb/query-dynamic.cxx
// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#include <odb/query-dynamic.hxx>

using namespace std;

namespace odb
{
  // query_param
  //
  query_param::
  ~query_param ()
  {
  }

  // query_base
  //
  void query_base::
  clear ()
  {
    for (clause_type::iterator i (clause_.begin ()); i != clause_.end (); ++i)
    {
      if (i->kind == clause_part::kind_param_val ||
          i->kind == clause_part::kind_param_ref)
      {
        query_param* qp (reinterpret_cast<query_param*> (i->data));

        if (qp != 0 && qp->_dec_ref ())
          delete qp;
      }
    }

    clause_.clear ();
    strings_.clear ();
  }

  void query_base::
  append (const string& native)
  {
    strings_.push_back (native);
    clause_.push_back (clause_part ());
    clause_.back ().kind = clause_part::kind_native;
    clause_.back ().data = strings_.size () - 1;
  }

  void query_base::
  append (const query_base& x)
  {
    size_t i (clause_.size ()), delta (i);
    size_t n (i + x.clause_.size ());
    clause_.resize (n);

    for (size_t j (0); i < n; ++i, ++j)
    {
      const clause_part& s (x.clause_[j]);
      clause_part& d (clause_[i]);

      d = s;

      // We need to increment the param references, update pointers
      // to strings and update argument positions.
      //
      switch (s.kind)
      {
      case clause_part::kind_param_val:
      case clause_part::kind_param_ref:
        {
          reinterpret_cast<query_param*> (d.data)->_inc_ref ();
          break;
        }
      case clause_part::kind_native:
        {
          strings_.push_back (x.strings_[s.data]);
          d.data = strings_.size () - 1;
          break;
        }
      case clause_part::op_eq:
      case clause_part::op_and:
        {
          d.data += delta;
          break;
        }
      default:
        break;
      }
    }
  }

  void query_base::
  append_ref (const void* ref, const native_column_info* c)
  {
    clause_.push_back (clause_part ());
    clause_part& p (clause_.back ());

    p.kind = clause_part::kind_param_ref;
    p.data = 0; // In case new below throws.
    p.native_info = c;

    p.data = reinterpret_cast<std::size_t> (
      new (details::shared) query_param (ref));
  }

  query_base& query_base::
  operator+= (const std::string& native)
  {
    if (!native.empty ())
    {
      size_t p (clause_.size ());
      append (native);

      if (p != 0)
        append (clause_part::op_add, p - 1);
    }

    return *this;
  }

  query_base& query_base::
  operator+= (const query_base& x)
  {
    if (!x.empty ())
    {
      size_t p (clause_.size ());
      append (x);

      if (p != 0)
        append (clause_part::op_add, p - 1);
    }

    return *this;
  }

  query_base
  operator&& (const query_base& x, const query_base& y)
  {
    // Optimize cases where one or both sides are constant truth.
    //
    bool xt (x.const_true ()), yt (y.const_true ());

    if (xt && yt)
      return x;

    if (xt || x.empty ())
      return y;

    if (yt || y.empty ())
      return x;

    query_base r (x);
    r.append (y);
    r.append (query_base::clause_part::op_and, x.clause ().size () - 1);
    return r;
  }

  query_base
  operator|| (const query_base& x, const query_base& y)
  {
    if (x.empty ())
      return y;

    if (y.empty ())
      return x;

    query_base r (x);
    r.append (y);
    r.append (query_base::clause_part::op_or, x.clause ().size () - 1);
    return r;
  }

  query_base
  operator! (const query_base& x)
  {
    if (x.empty ())
      return x;

    query_base r (x);
    r.append (query_base::clause_part::op_not, 0);
    return r;
  }
}