Common ODB Runtime Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
connection.hxx
Go to the documentation of this file.
1 // file : odb/connection.hxx
2 // copyright : Copyright (c) 2005-2013 Code Synthesis Tools CC
3 // license : GNU GPL v2; see accompanying LICENSE file
4 
5 #ifndef ODB_CONNECTION_HXX
6 #define ODB_CONNECTION_HXX
7 
8 #include <odb/pre.hxx>
9 
10 #include <map>
11 #include <string>
12 #include <memory> // std::auto_ptr, std::unique_ptr
13 #include <cstddef> // std::size_t
14 #include <typeinfo>
15 
16 #include <odb/forward.hxx>
17 #include <odb/traits.hxx>
18 #include <odb/query.hxx>
19 #include <odb/prepared-query.hxx>
20 
21 #include <odb/details/config.hxx> // ODB_CXX11
22 #include <odb/details/export.hxx>
23 #include <odb/details/c-string.hxx>
24 #include <odb/details/shared-ptr.hxx>
25 
26 namespace odb
27 {
28  class transaction_impl;
29 
30  class connection;
31  typedef details::shared_ptr<connection> connection_ptr;
32 
33  class LIBODB_EXPORT connection: public details::shared_base
34  {
35  public:
37 
39  database ();
40 
41  // Transactions.
42  //
43  public:
44  virtual transaction_impl*
45  begin () = 0;
46 
47  // Native database statement execution. Note that unlike the
48  // versions in the database class, these can be executed
49  // without a transaction.
50  //
51  public:
52  unsigned long long
53  execute (const char* statement);
54 
55  unsigned long long
56  execute (const std::string& statement);
57 
58  virtual unsigned long long
59  execute (const char* statement, std::size_t length) = 0;
60 
61  // Query preparation.
62  //
63  public:
64  template <typename T>
66  prepare_query (const char* name, const char*);
67 
68  template <typename T>
70  prepare_query (const char* name, const std::string&);
71 
72  template <typename T>
74  prepare_query (const char* name, const query<T>&);
75 
76  template <typename T>
77  void
78  cache_query (const prepared_query<T>&);
79 
80  template <typename T, typename P>
81  void
82  cache_query (const prepared_query<T>&, std::auto_ptr<P> params);
83 
84 #ifdef ODB_CXX11
85  template <typename T, typename P>
86  void
87  cache_query (const prepared_query<T>&, std::unique_ptr<P> params);
88 #endif
89 
90  template <typename T>
92  lookup_query (const char* name) const;
93 
94  template <typename T, typename P>
96  lookup_query (const char* name, P*& params) const;
97 
98  // SQL statement tracing.
99  //
100  public:
102 
103  void
104  tracer (tracer_type&);
105 
106  void
107  tracer (tracer_type*);
108 
109  tracer_type*
110  tracer () const;
111 
112  public:
113  // Store the transaction-spacific tracer in the connection. If we
114  // were to store it in the transaction, then in order to check if
115  // it was set, we would need to get the transaction instance using
116  // the current() API. But that requires a TLS lookup, which can be
117  // slow.
118  //
119  tracer_type*
120  transaction_tracer () const;
121 
122  public:
123  virtual
124  ~connection ();
125 
126  // Recycle the connection to be used by another thread. This call
127  // invalidates uncached prepared queries.
128  //
129  void
130  recycle ();
131 
132  protected:
134 
135  template <typename T,
136  database_id DB,
138  struct query_;
139 
140  virtual void
141  cache_query_ (prepared_query_impl* pq,
142  const std::type_info& ti,
143  void* params,
144  const std::type_info* params_info,
145  void (*params_deleter) (void*));
146 
148  lookup_query_ (const char* name,
149  const std::type_info& ti,
150  void** params, // out
151  const std::type_info* params_info) const;
152 
153  template <typename P>
154  static void
155  params_deleter (void*);
156 
157  private:
158  connection (const connection&);
159  connection& operator= (const connection&);
160 
161  // Prepared query cache.
162  //
163  protected:
165  {
166  details::shared_ptr<prepared_query_impl> prep_query;
167  const std::type_info* type_info;
168  void* params;
169  const std::type_info* params_info;
170  void (*params_deleter) (void*);
171  };
172 
173  typedef
174  std::map<const char*, prepared_entry_type, details::c_string_comparator>
176 
178 
179  void
180  clear_prepared_map ();
181 
182  protected:
185 
186  // Active query result list.
187  //
188  protected:
189  friend class result_impl;
191 
192  void
193  invalidate_results ();
194 
195  // Prepared but uncached query list (cached ones are stored in
196  // prepared_map_).
197  //
198  protected:
199  friend class prepared_query_impl;
201 
202  protected:
203  friend class transaction;
205  };
206 }
207 
208 #include <odb/connection.ixx>
209 #include <odb/connection.txx>
210 
211 #include <odb/post.hxx>
212 
213 #endif // ODB_CONNECTION_HXX
std::map< const char *, prepared_entry_type, details::c_string_comparator > prepared_map_type
Definition: connection.hxx:175
Definition: statement.hxx:20
Definition: prepared-query.hxx:20
Definition: transaction.hxx:22
odb::tracer tracer_type
Definition: connection.hxx:101
details::shared_ptr< connection > connection_ptr
Definition: connection.hxx:30
Definition: connection.hxx:164
void * params
Definition: connection.hxx:168
database_type & database_
Definition: connection.hxx:183
class_kind
Definition: traits.hxx:77
Definition: query.hxx:110
prepared_map_type prepared_map_
Definition: connection.hxx:177
Definition: result.hxx:20
Definition: prepared-query.hxx:55
details::shared_ptr< prepared_query_impl > prep_query
Definition: connection.hxx:166
Definition: transaction.hxx:216
const std::type_info * type_info
Definition: connection.hxx:167
const std::type_info * params_info
Definition: connection.hxx:169
Definition: database.hxx:38
Definition: connection.hxx:33
Definition: connection.hxx:138
Definition: tracer.hxx:15
prepared_query_impl * prepared_queries_
Definition: connection.hxx:200
odb::database database_type
Definition: connection.hxx:36
database_id
Definition: forward.hxx:74
tracer_type * tracer_
Definition: connection.hxx:184
result_impl * results_
Definition: connection.hxx:190
tracer_type * transaction_tracer_
Definition: connection.hxx:204
Definition: traits.hxx:85