core/session.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2008 Maciej Sobczak, Stephen Hutton
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7 
8 #define SOCI_SOURCE
9 #include "session.h"
10 #include "connection-parameters.h"
11 #include "connection-pool.h"
12 #include "soci-backend.h"
13 #include "query_transformation.h"
14 
15 #ifdef _MSC_VER
16 #pragma warning(disable:4355)
17 #endif
18 
19 using namespace soci;
20 using namespace soci::details;
21 
22 namespace // anonymous
23 {
24 
25 void ensureConnected(session_backend * backEnd)
26 {
27  if (backEnd == NULL)
28  {
29  throw soci_error("Session is not connected.");
30  }
31 }
32 
33 } // namespace anonymous
34 
36  : once(this), prepare(this), query_transformation_(NULL), logStream_(NULL),
37  uppercaseColumnNames_(false), backEnd_(NULL),
38  isFromPool_(false), pool_(NULL)
39 {
40 }
41 
43  : once(this), prepare(this), query_transformation_(NULL), logStream_(NULL),
44  lastConnectParameters_(parameters),
45  uppercaseColumnNames_(false), backEnd_(NULL),
46  isFromPool_(false), pool_(NULL)
47 {
49 }
50 
52  std::string const & connectString)
53  : once(this), prepare(this), query_transformation_(NULL), logStream_(NULL),
54  lastConnectParameters_(factory, connectString),
55  uppercaseColumnNames_(false), backEnd_(NULL),
56  isFromPool_(false), pool_(NULL)
57 {
59 }
60 
61 session::session(std::string const & backendName,
62  std::string const & connectString)
63  : once(this), prepare(this), query_transformation_(NULL), logStream_(NULL),
64  lastConnectParameters_(backendName, connectString),
65  uppercaseColumnNames_(false), backEnd_(NULL),
66  isFromPool_(false), pool_(NULL)
67 {
69 }
70 
71 session::session(std::string const & connectString)
72  : once(this), prepare(this), query_transformation_(NULL), logStream_(NULL),
73  lastConnectParameters_(connectString),
74  uppercaseColumnNames_(false), backEnd_(NULL),
75  isFromPool_(false), pool_(NULL)
76 {
78 }
79 
81  : query_transformation_(NULL), logStream_(NULL), isFromPool_(true), pool_(&pool)
82 {
83  poolPosition_ = pool.lease();
84  session & pooledSession = pool.at(poolPosition_);
85 
86  once.set_session(&pooledSession);
87  prepare.set_session(&pooledSession);
88  backEnd_ = pooledSession.get_backend();
89 }
90 
92 {
93  if (isFromPool_)
94  {
96  }
97  else
98  {
99  delete query_transformation_;
100  delete backEnd_;
101  }
102 }
103 
105 {
106  if (isFromPool_)
107  {
108  pool_->at(poolPosition_).open(parameters);
109  }
110  else
111  {
112  if (backEnd_ != NULL)
113  {
114  throw soci_error("Cannot open already connected session.");
115  }
116 
117  backend_factory const * const factory = parameters.get_factory();
118  if (factory == NULL)
119  {
120  throw soci_error("Cannot connect without a valid backend.");
121  }
122 
123  backEnd_ = factory->make_session(parameters);
125  }
126 }
127 
128 void session::open(backend_factory const & factory,
129  std::string const & connectString)
130 {
131  open(connection_parameters(factory, connectString));
132 }
133 
134 void session::open(std::string const & backendName,
135  std::string const & connectString)
136 {
137  open(connection_parameters(backendName, connectString));
138 }
139 
140 void session::open(std::string const & connectString)
141 {
142  open(connection_parameters(connectString));
143 }
144 
146 {
147  if (isFromPool_)
148  {
150  backEnd_ = NULL;
151  }
152  else
153  {
154  delete backEnd_;
155  backEnd_ = NULL;
156  }
157 }
158 
160 {
161  if (isFromPool_)
162  {
165  }
166  else
167  {
168  backend_factory const * const lastFactory = lastConnectParameters_.get_factory();
169  if (lastFactory == NULL)
170  {
171  throw soci_error("Cannot reconnect without previous connection.");
172  }
173 
174  if (backEnd_ != NULL)
175  {
176  close();
177  }
178 
180  }
181 }
182 
184 {
185  ensureConnected(backEnd_);
186 
187  backEnd_->begin();
188 }
189 
191 {
192  ensureConnected(backEnd_);
193 
194  backEnd_->commit();
195 }
196 
198 {
199  ensureConnected(backEnd_);
200 
201  backEnd_->rollback();
202 }
203 
204 std::ostringstream & session::get_query_stream()
205 {
206  if (isFromPool_)
207  {
209  }
210  else
211  {
212  return query_stream_;
213  }
214 }
215 
216 std::string session::get_query() const
217 {
218  if (isFromPool_)
219  {
220  return pool_->at(poolPosition_).get_query();
221  }
222  else
223  {
224  // preserve logical constness of get_query,
225  // stream used as read-only here,
226  session* pthis = const_cast<session*>(this);
227 
228  // sole place where any user-defined query transformation is applied
230  {
231  return (*query_transformation_)(pthis->get_query_stream().str());
232  }
233  return pthis->get_query_stream().str();
234  }
235 }
236 
238  std::auto_ptr<details::query_transformation_function> qtf)
239 {
240  if (isFromPool_)
241  {
243  }
244  else
245  {
246  delete query_transformation_;
247  query_transformation_= qtf.release();
248  }
249 }
250 
251 void session::set_log_stream(std::ostream * s)
252 {
253  if (isFromPool_)
254  {
256  }
257  else
258  {
259  logStream_ = s;
260  }
261 }
262 
263 std::ostream * session::get_log_stream() const
264 {
265  if (isFromPool_)
266  {
268  }
269  else
270  {
271  return logStream_;
272  }
273 }
274 
275 void session::log_query(std::string const & query)
276 {
277  if (isFromPool_)
278  {
279  pool_->at(poolPosition_).log_query(query);
280  }
281  else
282  {
283  if (logStream_ != NULL)
284  {
285  *logStream_ << query << '\n';
286  }
287 
288  lastQuery_ = query;
289  }
290 }
291 
292 std::string session::get_last_query() const
293 {
294  if (isFromPool_)
295  {
297  }
298  else
299  {
300  return lastQuery_;
301  }
302 }
303 
304 void session::set_got_data(bool gotData)
305 {
306  if (isFromPool_)
307  {
308  pool_->at(poolPosition_).set_got_data(gotData);
309  }
310  else
311  {
312  gotData_ = gotData;
313  }
314 }
315 
316 bool session::got_data() const
317 {
318  if (isFromPool_)
319  {
320  return pool_->at(poolPosition_).got_data();
321  }
322  else
323  {
324  return gotData_;
325  }
326 }
327 
328 void session::uppercase_column_names(bool forceToUpper)
329 {
330  if (isFromPool_)
331  {
333  }
334  else
335  {
336  uppercaseColumnNames_ = forceToUpper;
337  }
338 }
339 
341 {
342  if (isFromPool_)
343  {
345  }
346  else
347  {
348  return uppercaseColumnNames_;
349  }
350 }
351 
352 bool session::get_next_sequence_value(std::string const & sequence, long & value)
353 {
354  ensureConnected(backEnd_);
355 
356  return backEnd_->get_next_sequence_value(*this, sequence, value);
357 }
358 
359 bool session::get_last_insert_id(std::string const & sequence, long & value)
360 {
361  ensureConnected(backEnd_);
362 
363  return backEnd_->get_last_insert_id(*this, sequence, value);
364 }
365 
366 std::string session::get_backend_name() const
367 {
368  ensureConnected(backEnd_);
369 
370  return backEnd_->get_backend_name();
371 }
372 
374 {
375  ensureConnected(backEnd_);
376 
378 }
379 
381 {
382  ensureConnected(backEnd_);
383 
384  return backEnd_->make_rowid_backend();
385 }
386 
388 {
389  ensureConnected(backEnd_);
390 
391  return backEnd_->make_blob_backend();
392 }
bool got_data() const
std::string get_last_query() const
backend_factory const & backEnd
Definition: test-db2.cpp:22
virtual details::session_backend * make_session(connection_parameters const &parameters) const =0
virtual rowid_backend * make_rowid_backend()=0
void log_query(std::string const &query)
std::ostream * logStream_
Definition: session.h:133
std::string get_backend_name() const
details::session_backend * backEnd_
Definition: session.h:140
std::string lastQuery_
Definition: session.h:134
details::session_backend * get_backend()
Definition: session.h:118
void give_back(std::size_t pos)
virtual statement_backend * make_statement_backend()=0
void set_got_data(bool gotData)
virtual bool get_last_insert_id(session &, std::string const &, long &)
Definition: soci-backend.h:242
bool get_last_insert_id(std::string const &table, long &value)
std::ostringstream & get_query_stream()
std::ostringstream query_stream_
Definition: session.h:130
details::once_type once
Definition: session.h:68
session & at(std::size_t pos)
virtual blob_backend * make_blob_backend()=0
void set_session(session *s)
bool get_next_sequence_value(std::string const &sequence, long &value)
void uppercase_column_names(bool forceToUpper)
connection_parameters lastConnectParameters_
Definition: session.h:136
std::string get_query() const
bool uppercaseColumnNames_
Definition: session.h:138
details::blob_backend * make_blob_backend()
details::statement_backend * make_statement_backend()
void set_log_stream(std::ostream *s)
void open(connection_parameters const &parameters)
details::rowid_backend * make_rowid_backend()
details::query_transformation_function * query_transformation_
Definition: session.h:131
void set_query_transformation_(std::auto_ptr< details::query_transformation_function > qtf)
bool isFromPool_
Definition: session.h:144
connection_pool * pool_
Definition: session.h:146
std::string connectString
Definition: test-db2.cpp:21
void set_session(session *s)
std::size_t poolPosition_
Definition: session.h:145
bool get_uppercase_column_names() const
std::ostream * get_log_stream() const
bool gotData_
Definition: session.h:142
std::vector< ISM::CombinatorialTrainerParameters > parameters
details::prepare_type prepare
Definition: session.h:69
backend_factory const * get_factory() const
virtual bool get_next_sequence_value(session &, std::string const &, long &)
Definition: soci-backend.h:238
virtual std::string get_backend_name() const =0


asr_lib_ism
Author(s): Hanselmann Fabian, Heller Florian, Heizmann Heinrich, Kübler Marcel, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Reckling Reno, Stroh Daniel
autogenerated on Wed Jan 8 2020 04:02:41