aboutsummaryrefslogtreecommitdiff
path: root/odb/sqlite/connection-factory.hxx
blob: 0d01b85885173501eb934230c59fb3391caa61b6 (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
// file      : odb/sqlite/connection-factory.hxx
// license   : GNU GPL v2; see accompanying LICENSE file

#ifndef ODB_SQLITE_CONNECTION_FACTORY_HXX
#define ODB_SQLITE_CONNECTION_FACTORY_HXX

#include <odb/pre.hxx>

#include <vector>
#include <cstddef> // std::size_t
#include <cassert>

#include <odb/details/mutex.hxx>
#include <odb/details/condition.hxx>
#include <odb/details/shared-ptr.hxx>

#include <odb/sqlite/version.hxx>
#include <odb/sqlite/forward.hxx>
#include <odb/sqlite/connection.hxx>
#include <odb/sqlite/details/export.hxx>

namespace odb
{
  namespace sqlite
  {
    // Share a single connection in a guaranteed serial database access.
    //
    // For example, a single-threaded application that executes all the
    // operations via the database instance without messing with multiple
    // connections/transactions would qualify.
    //
    class LIBODB_SQLITE_EXPORT serial_connection_factory:
      public connection_factory
    {
    public:
      serial_connection_factory () {}

      virtual connection_ptr
      connect ();

      virtual void
      database (database_type&);

      virtual
      ~serial_connection_factory ();

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

    protected:
      // This function is called when the factory needs to create the
      // connection.
      //
      virtual connection_ptr
      create ();

      connection_ptr connection_;
    };

    // Share a single connection potentially between multiple threads.
    //
    class LIBODB_SQLITE_EXPORT single_connection_factory:
      public connection_factory
    {
    public:
      single_connection_factory () {}

      virtual connection_ptr
      connect ();

      virtual void
      database (database_type&);

      virtual
      ~single_connection_factory ();

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

    protected:
      class LIBODB_SQLITE_EXPORT single_connection: public connection
      {
      public:
        single_connection (single_connection_factory&, int extra_flags = 0);
        single_connection (single_connection_factory&, sqlite3*);

      private:
        static bool
        zero_counter (void*);

      private:
        friend class single_connection_factory;
        shared_base::refcount_callback cb_;
      };

      friend class single_connection;

      typedef details::shared_ptr<single_connection> single_connection_ptr;

      // This function is called when the factory needs to create the
      // connection.
      //
      virtual single_connection_ptr
      create ();

    protected:
      // Return true if the connection should be deleted, false otherwise.
      //
      bool
      release (single_connection*);

    protected:
      details::mutex mutex_;
      single_connection_ptr connection_;
    };

    // Create a new connection every time one is requested.
    //
    class LIBODB_SQLITE_EXPORT new_connection_factory:
      public connection_factory
    {
    public:
      new_connection_factory (): extra_flags_ (0) {}

      virtual connection_ptr
      connect ();

      virtual void
      database (database_type&);

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

    private:
      int extra_flags_;
    };

    // Pool a number of connections.
    //
    class LIBODB_SQLITE_EXPORT connection_pool_factory:
      public connection_factory
    {
    public:
      // The max_connections argument specifies the maximum number of
      // concurrent connections this pool will maintain. If this value
      // is 0 then the pool will create a new connection every time all
      // of the existing connections are in use.
      //
      // The min_connections argument specifies the minimum number of
      // connections that should be maintained by the pool. If the
      // number of connections maintained by the pool exceeds this
      // number and there are no active waiters for a new connection,
      // then the pool will release the excess connections. If this
      // value is 0 then the pool will maintain all the connections
      // that were ever created.
      //
      // The ping argument specifies whether to ping the connection to
      // make sure it is still alive before returning it to the caller.
      //
      connection_pool_factory (std::size_t max_connections = 0,
                               std::size_t min_connections = 0)
          : max_ (max_connections),
            min_ (min_connections),
            extra_flags_ (0),
            in_use_ (0),
            waiters_ (0),
            cond_ (mutex_)
      {
        // max_connections == 0 means unlimited.
        //
        assert (max_connections == 0 || max_connections >= min_connections);
      }

      virtual connection_ptr
      connect ();

      virtual void
      database (database_type&);

      virtual
      ~connection_pool_factory ();

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

    protected:
      class LIBODB_SQLITE_EXPORT pooled_connection: public connection
      {
      public:
        pooled_connection (connection_pool_factory&, int extra_flags = 0);
        pooled_connection (connection_pool_factory&, sqlite3*);

      private:
        static bool
        zero_counter (void*);

      private:
        friend class connection_pool_factory;
        shared_base::refcount_callback cb_;
      };

      friend class pooled_connection;

      typedef details::shared_ptr<pooled_connection> pooled_connection_ptr;
      typedef std::vector<pooled_connection_ptr> connections;

      // This function is called whenever the pool needs to create a new
      // connection.
      //
      virtual pooled_connection_ptr
      create ();

    protected:
      // Return true if the connection should be deleted, false otherwise.
      //
      bool
      release (pooled_connection*);

    protected:
      const std::size_t max_;
      const std::size_t min_;
      int extra_flags_;

      std::size_t in_use_;  // Number of connections currently in use.
      std::size_t waiters_; // Number of threads waiting for a connection.

      connections connections_;

      details::mutex mutex_;
      details::condition cond_;
    };

    class LIBODB_SQLITE_EXPORT default_attached_connection_factory:
      public attached_connection_factory
    {
    public:
      explicit
      default_attached_connection_factory (const connection_ptr& main)
          : attached_connection_factory (main) {}

      using attached_connection_factory::database; // Accessor.

      virtual void
      database (database_type&);

      virtual connection_ptr
      connect ();

      // Active object interface.
      //
      virtual void
      clear ();

      virtual void
      detach ();

      virtual
      ~default_attached_connection_factory ();

    protected:
      static void
      translate_statement (std::string&, const char*, std::size_t, connection&);
    };
  }
}

#include <odb/post.hxx>

#endif // ODB_SQLITE_CONNECTION_FACTORY_HXX