aboutsummaryrefslogtreecommitdiff
path: root/odb/sqlite/query.txx
blob: 409e3dba0f7b09230cb13e17e0526791881b130f (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
// file      : odb/sqlite/query.txx
// copyright : Copyright (c) 2009-2017 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

namespace odb
{
  namespace sqlite
  {
    //
    // query_base
    //

    template <database_type_id ID>
    query_base::
    query_base (const query_column<bool, ID>& c)
        : parameters_ (new (details::shared) query_params)
    {
      // Cannot use IS TRUE here since database type can be a non-
      // integral type.
      //
      append (c.table (), c.column ());
      append ("=");
      append<bool, ID> (val_bind<bool> (true), c.conversion ());
    }

    //
    // query_column
    //

    // in
    //
    template <typename T, database_type_id ID>
    query_base query_column<T, ID>::
    in (decayed_type v1, decayed_type v2) const
    {
      query_base q (table_, column_);
      q += "IN (";
      q.append<T, ID> (val_bind<T> (v1), conversion_);
      q += ",";
      q.append<T, ID> (val_bind<T> (v2), conversion_);
      q += ")";
      return q;
    }

    template <typename T, database_type_id ID>
    query_base query_column<T, ID>::
    in (decayed_type v1, decayed_type v2, decayed_type v3) const
    {
      query_base q (table_, column_);
      q += "IN (";
      q.append<T, ID> (val_bind<T> (v1), conversion_);
      q += ",";
      q.append<T, ID> (val_bind<T> (v2), conversion_);
      q += ",";
      q.append<T, ID> (val_bind<T> (v3), conversion_);
      q += ")";
      return q;
    }

    template <typename T, database_type_id ID>
    query_base query_column<T, ID>::
    in (decayed_type v1, decayed_type v2, decayed_type v3,
        decayed_type v4) const
    {
      query_base q (table_, column_);
      q += "IN (";
      q.append<T, ID> (val_bind<T> (v1), conversion_);
      q += ",";
      q.append<T, ID> (val_bind<T> (v2), conversion_);
      q += ",";
      q.append<T, ID> (val_bind<T> (v3), conversion_);
      q += ",";
      q.append<T, ID> (val_bind<T> (v4), conversion_);
      q += ")";
      return q;
    }

    template <typename T, database_type_id ID>
    query_base query_column<T, ID>::
    in (decayed_type v1, decayed_type v2, decayed_type v3, decayed_type v4,
        decayed_type v5) const
    {
      query_base q (table_, column_);
      q += "IN (";
      q.append<T, ID> (val_bind<T> (v1), conversion_);
      q += ",";
      q.append<T, ID> (val_bind<T> (v2), conversion_);
      q += ",";
      q.append<T, ID> (val_bind<T> (v3), conversion_);
      q += ",";
      q.append<T, ID> (val_bind<T> (v4), conversion_);
      q += ",";
      q.append<T, ID> (val_bind<T> (v5), conversion_);
      q += ")";
      return q;
    }

    template <typename T, database_type_id ID>
    template <typename I>
    query_base query_column<T, ID>::
    in_range (I begin, I end) const
    {
      if (begin != end)
      {
        query_base q (table_, column_);
        q += "IN (";

        for (I i (begin); i != end; ++i)
        {
          if (i != begin)
            q += ",";

          q.append<T, ID> (val_bind<T> (*i), conversion_);
        }

        q += ")";
        return q;
      }
      else
        return query_base (false);
    }

    // like
    //
    template <typename T, database_type_id ID>
    query_base query_column<T, ID>::
    like (val_bind<T> p) const
    {
      query_base q (table_, column_);
      q += "LIKE";
      q.append<T, ID> (p, conversion_);
      return q;
    }

    template <typename T, database_type_id ID>
    query_base query_column<T, ID>::
    like (ref_bind<T> p) const
    {
      query_base q (table_, column_);
      q += "LIKE";
      q.append<T, ID> (p, conversion_);
      return q;
    }

    template <typename T, database_type_id ID>
    query_base query_column<T, ID>::
    like (val_bind<T> p, decayed_type e) const
    {
      query_base q (table_, column_);
      q += "LIKE";
      q.append<T, ID> (p, conversion_);
      q += "ESCAPE";
      q.append<T, ID> (val_bind<T> (e), conversion_);
      return q;
    }

    template <typename T, database_type_id ID>
    query_base query_column<T, ID>::
    like (ref_bind<T> p, decayed_type e) const
    {
      query_base q (table_, column_);
      q += "LIKE";
      q.append<T, ID> (p, conversion_);
      q += "ESCAPE";
      q.append<T, ID> (val_bind<T> (e), conversion_);
      return q;
    }
  }
}