backends/postgresql/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_POSTGRESQL_SOURCE
9 #include "soci-postgresql.h"
10 #include "session.h"
11 #include <connection-parameters.h>
12 #include <libpq/libpq-fs.h> // libpq
13 #include <cctype>
14 #include <cstdio>
15 #include <cstring>
16 #include <ctime>
17 #include <sstream>
18 
19 #ifdef SOCI_POSTGRESQL_NOPARAMS
20 #ifndef SOCI_POSTGRESQL_NOBINDBYNAME
21 #define SOCI_POSTGRESQL_NOBINDBYNAME
22 #endif // SOCI_POSTGRESQL_NOBINDBYNAME
23 #endif // SOCI_POSTGRESQL_NOPARAMS
24 
25 #ifdef _MSC_VER
26 #pragma warning(disable:4355 4996)
27 #endif
28 
29 using namespace soci;
30 using namespace soci::details;
31 
34  : statementCount_(0)
35 {
36  PGconn* conn = PQconnectdb(parameters.get_connect_string().c_str());
37  if (0 == conn || CONNECTION_OK != PQstatus(conn))
38  {
39  std::string msg = "Cannot establish connection to the database.";
40  if (0 != conn)
41  {
42  msg += '\n';
43  msg += PQerrorMessage(conn);
44  PQfinish(conn);
45  }
46 
47  throw soci_error(msg);
48  }
49 
50  conn_ = conn;
51 }
52 
54 {
55  clean_up();
56 }
57 
58 namespace // unnamed
59 {
60 
61 // helper function for hardcoded queries
62 void hard_exec(PGconn * conn, char const * query, char const * errMsg)
63 {
64  postgresql_result(PQexec(conn, query)).check_for_errors(errMsg);
65 }
66 
67 } // namespace unnamed
68 
70 {
71  hard_exec(conn_, "BEGIN", "Cannot begin transaction.");
72 }
73 
75 {
76  hard_exec(conn_, "COMMIT", "Cannot commit transaction.");
77 }
78 
80 {
81  hard_exec(conn_, "ROLLBACK", "Cannot rollback transaction.");
82 }
83 
85  const std::string & statementName)
86 {
87  const std::string & query = "DEALLOCATE " + statementName;
88 
89  hard_exec(conn_, query.c_str(),
90  "Cannot deallocate prepared statement.");
91 }
92 
94  session & s, std::string const & sequence, long & value)
95 {
96  s << "select nextval('" + sequence + "')", into(value);
97 
98  return true;
99 }
100 
102 {
103  if (0 != conn_)
104  {
105  PQfinish(conn_);
106  conn_ = 0;
107  }
108 }
109 
111 {
112  char nameBuf[20] = { 0 }; // arbitrary length
113  sprintf(nameBuf, "st_%d", ++statementCount_);
114  return nameBuf;
115 }
116 
118 {
119  return new postgresql_statement_backend(*this);
120 }
121 
123 {
124  return new postgresql_rowid_backend(*this);
125 }
126 
128 {
129  return new postgresql_blob_backend(*this);
130 }
details::into_container< T, details::no_indicator > into(T &t)
Definition: into.h:51
virtual postgresql_statement_backend * make_statement_backend()
void deallocate_prepared_statement(const std::string &statementName)
virtual postgresql_blob_backend * make_blob_backend()
postgresql_session_backend(connection_parameters const &parameters)
std::string const & get_connect_string() const
virtual postgresql_rowid_backend * make_rowid_backend()
void check_for_errors(char const *errMsg) const
std::vector< ISM::CombinatorialTrainerParameters > parameters
virtual bool get_next_sequence_value(session &s, std::string const &sequence, long &value)


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