aboutsummaryrefslogtreecommitdiff
path: root/odb/sqlite/container-statements.hxx
blob: a7b39bf380b044b3ec35469af2ad3e171a6876c1 (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
// file      : odb/sqlite/container-statements.hxx
// copyright : Copyright (c) 2005-2012 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#ifndef ODB_SQLITE_CONTAINER_STATEMENTS_HXX
#define ODB_SQLITE_CONTAINER_STATEMENTS_HXX

#include <odb/pre.hxx>

#include <cstddef> // std::size_t

#include <odb/forward.hxx>
#include <odb/traits.hxx>

#include <odb/sqlite/version.hxx>
#include <odb/sqlite/binding.hxx>
#include <odb/sqlite/statement.hxx>
#include <odb/sqlite/details/export.hxx>

namespace odb
{
  namespace sqlite
  {
    class connection;

    // Template argument is the generated abstract container traits type.
    // That is, it doesn't need to provide column counts and statements.
    //
    template <typename T>
    class container_statements
    {
    public:
      typedef T traits;

      typedef typename traits::data_image_type data_image_type;
      typedef typename traits::cond_image_type cond_image_type;

      typedef typename traits::functions_type functions_type;

      typedef sqlite::insert_statement insert_statement_type;
      typedef sqlite::select_statement select_statement_type;
      typedef sqlite::delete_statement delete_statement_type;

      typedef sqlite::connection connection_type;

      container_statements (connection_type&);

      connection_type&
      connection ()
      {
        return conn_;
      }

      // Functions.
      //
      functions_type&
      functions ()
      {
        return functions_;
      }

      // Id image binding (external).
      //
      const binding&
      id_binding ()
      {
        return *id_binding_;
      }

      void
      id_binding (const binding& b)
      {
        id_binding_ = &b;
      }

      // Condition image.
      //
      cond_image_type&
      cond_image ()
      {
        return cond_image_;
      }

      std::size_t
      cond_image_version () const
      {
        return cond_image_version_;
      }

      void
      cond_image_version (std::size_t v)
      {
        cond_image_version_ = v;
      }

      std::size_t
      cond_id_binding_version () const
      {
        return cond_id_binding_version_;
      }

      void
      cond_id_binding_version (std::size_t v)
      {
        cond_id_binding_version_ = v;
      }

      binding&
      cond_image_binding ()
      {
        return cond_image_binding_;
      }

      // Data image.
      //
      data_image_type&
      data_image ()
      {
        return data_image_;
      }

            std::size_t
      data_image_version () const
      {
        return data_image_version_;
      }

      void
      data_image_version (std::size_t v)
      {
        data_image_version_ = v;
      }

      std::size_t
      data_id_binding_version () const
      {
        return data_id_binding_version_;
      }

      void
      data_id_binding_version (std::size_t v)
      {
        data_id_binding_version_ = v;
      }

      binding&
      data_image_binding ()
      {
        return data_image_binding_;
      }

      binding&
      select_image_binding ()
      {
        return select_image_binding_;
      }

      bool*
      select_image_truncated ()
      {
        return select_image_truncated_;
      }

      //
      // Statements.
      //

      insert_statement_type&
      insert_one_statement ()
      {
        if (insert_one_ == 0)
        {
          insert_one_.reset (
            new (details::shared) insert_statement_type (
              conn_, insert_one_text_, data_image_binding_));
        }

        return *insert_one_;
      }

      select_statement_type&
      select_all_statement ()
      {
        if (select_all_ == 0)
        {
          select_all_.reset (
            new (details::shared) select_statement_type (
              conn_,
              select_all_text_,
              cond_image_binding_,
              select_image_binding_));
        }

        return *select_all_;
      }

      delete_statement_type&
      delete_all_statement ()
      {
        if (delete_all_ == 0)
        {
          delete_all_.reset (
            new (details::shared) delete_statement_type (
              conn_, delete_all_text_, cond_image_binding_));
        }

        return *delete_all_;
      }

    private:
      container_statements (const container_statements&);
      container_statements& operator= (const container_statements&);

    protected:
      connection_type& conn_;
      functions_type functions_;

      const binding* id_binding_;

      cond_image_type cond_image_;
      std::size_t cond_image_version_;
      std::size_t cond_id_binding_version_;
      binding cond_image_binding_;

      data_image_type data_image_;
      std::size_t data_image_version_;
      std::size_t data_id_binding_version_;

      binding data_image_binding_;

      // Skips the id from data_image_binding.
      //
      binding select_image_binding_;
      bool* select_image_truncated_;

      const char* insert_one_text_;
      const char* select_all_text_;
      const char* delete_all_text_;

      details::shared_ptr<insert_statement_type> insert_one_;
      details::shared_ptr<select_statement_type> select_all_;
      details::shared_ptr<delete_statement_type> delete_all_;
    };

    // Template argument is the generated concrete container traits type.
    //
    template <typename T>
    class container_statements_impl: public T::statements_type
    {
    public:
      typedef T traits;
      typedef typename T::statements_type base;
      typedef sqlite::connection connection_type;

      container_statements_impl (connection_type&);

    private:
      container_statements_impl (const container_statements_impl&);
      container_statements_impl& operator= (const container_statements_impl&);

    private:
      bind cond_image_bind_[traits::cond_column_count];
      bind data_image_bind_[traits::data_column_count];
      bool select_image_truncated_array_[traits::data_column_count-
                                         traits::id_column_count];
    };
  }
}

#include <odb/sqlite/container-statements.txx>

#include <odb/post.hxx>

#endif // ODB_SQLITE_CONTAINER_STATEMENTS_HXX