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

#ifndef ODB_CONNECTION_HXX
#define ODB_CONNECTION_HXX

#include <odb/pre.hxx>

#include <map>
#include <string>
#include <memory>   // std::auto_ptr, std::unique_ptr
#include <cstddef>  // std::size_t
#include <typeinfo>

#include <odb/forward.hxx>
#include <odb/traits.hxx>
#include <odb/query.hxx>
#include <odb/prepared-query.hxx>

#include <odb/details/config.hxx> // ODB_CXX11
#include <odb/details/export.hxx>
#include <odb/details/c-string.hxx>
#include <odb/details/shared-ptr.hxx>

namespace odb
{
  class transaction_impl;

  class connection;
  typedef details::shared_ptr<connection> connection_ptr;

  class LIBODB_EXPORT connection: public details::shared_base
  {
  public:
    typedef odb::database database_type;

    database_type&
    database ();

    // Transactions.
    //
  public:
    virtual transaction_impl*
    begin () = 0;

    // Native database statement execution. Note that unlike the
    // versions in the database class, these can be executed
    // without a transaction.
    //
  public:
    unsigned long long
    execute (const char* statement);

    unsigned long long
    execute (const std::string& statement);

    virtual unsigned long long
    execute (const char* statement, std::size_t length) = 0;

    // Query preparation.
    //
  public:
    template <typename T>
    prepared_query<T>
    prepare_query (const char* name, const char*);

    template <typename T>
    prepared_query<T>
    prepare_query (const char* name, const std::string&);

    template <typename T>
    prepared_query<T>
    prepare_query (const char* name, const query<T>&);

    template <typename T>
    void
    cache_query (const prepared_query<T>&);

    template <typename T, typename P>
    void
    cache_query (const prepared_query<T>&, std::auto_ptr<P> params);

#ifdef ODB_CXX11
    template <typename T, typename P>
    void
    cache_query (const prepared_query<T>&, std::unique_ptr<P> params);
#endif

    template <typename T>
    prepared_query<T>
    lookup_query (const char* name) const;

    template <typename T, typename P>
    prepared_query<T>
    lookup_query (const char* name, P*& params) const;

    // SQL statement tracing.
    //
  public:
    typedef odb::tracer tracer_type;

    void
    tracer (tracer_type&);

    void
    tracer (tracer_type*);

    tracer_type*
    tracer () const;

   public:
    // Store the transaction-spacific tracer in the connection. If we
    // were to store it in the transaction, then in order to check if
    // it was set, we would need to get the transaction instance using
    // the current() API. But that requires a TLS lookup, which can be
    // slow.
    //
    tracer_type*
    transaction_tracer () const;

  public:
    virtual
    ~connection ();

    // Recycle the connection to be used by another thread. This call
    // invalidates uncached prepared queries.
    //
    void
    recycle ();

  protected:
    connection (database_type&);

    template <typename T,
              database_id DB,
              class_kind kind = class_traits<T>::kind>
    struct query_;

    virtual void
    cache_query_ (prepared_query_impl* pq,
                  const std::type_info& ti,
                  void* params,
                  const std::type_info* params_info,
                  void (*params_deleter) (void*));

    prepared_query_impl*
    lookup_query_ (const char* name,
                   const std::type_info& ti,
                   void** params, // out
                   const std::type_info* params_info) const;

    template <typename P>
    static void
    params_deleter (void*);

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

    // Prepared query cache.
    //
  protected:
    struct prepared_entry_type
    {
      details::shared_ptr<prepared_query_impl> prep_query;
      const std::type_info* type_info;
      void* params;
      const std::type_info* params_info;
      void (*params_deleter) (void*);
    };

    typedef
    std::map<const char*, prepared_entry_type, details::c_string_comparator>
    prepared_map_type;

    prepared_map_type prepared_map_;

    void
    clear_prepared_map ();

  protected:
    database_type& database_;
    tracer_type* tracer_;

    // Active query result list.
    //
  protected:
    friend class result_impl;
    result_impl* results_;

    void
    invalidate_results ();

    // Prepared but uncached query list (cached ones are stored in
    // prepared_map_).
    //
  protected:
    friend class prepared_query_impl;
    prepared_query_impl* prepared_queries_;

  protected:
    friend class transaction;
    tracer_type* transaction_tracer_;
  };
}

#include <odb/connection.ixx>
#include <odb/connection.txx>

#include <odb/post.hxx>

#endif // ODB_MYSQL_CONNECTION_HXX