soci-postgresql.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2008 Maciej Sobczak, Stephen Hutton
3 // Copyright (C) 2011 Gevorg Voskanyan
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 
9 #ifndef SOCI_POSTGRESQL_H_INCLUDED
10 #define SOCI_POSTGRESQL_H_INCLUDED
11 
12 #ifdef _WIN32
13 # ifdef SOCI_DLL
14 # ifdef SOCI_POSTGRESQL_SOURCE
15 # define SOCI_POSTGRESQL_DECL __declspec(dllexport)
16 # else
17 # define SOCI_POSTGRESQL_DECL __declspec(dllimport)
18 # endif // SOCI_POSTGRESQL_SOURCE
19 # endif // SOCI_DLL
20 #endif // _WIN32
21 //
22 // If SOCI_POSTGRESQL_DECL isn't defined yet define it now
23 #ifndef SOCI_POSTGRESQL_DECL
24 # define SOCI_POSTGRESQL_DECL
25 #endif
26 
27 #include <soci-backend.h>
28 #include <libpq-fe.h>
29 #include <vector>
30 
31 #ifdef _MSC_VER
32 #pragma warning(disable:4512 4511)
33 #endif
34 
35 namespace soci
36 {
37 
39 {
40 public:
41  postgresql_soci_error(std::string const & msg, char const * sqlst);
42 
43  std::string sqlstate() const;
44 
45 private:
46  char sqlstate_[ 5 ]; // not std::string to keep copy-constructor no-throw
47 };
48 
49 namespace details
50 {
51 
52 // A class thinly encapsulating PGresult. Its main purpose is to ensure that
53 // PQclear() is always called, avoiding result memory leaks.
55 {
56 public:
57  // Creates a wrapper for the given, possibly NULL, result. The wrapper
58  // object takes ownership of the object and will call PQclear() on it.
59  explicit postgresql_result(PGresult* result = NULL)
60  {
61  init(result);
62  }
63 
64  // Frees any currently stored result pointer and takes ownership of the
65  // given one.
66  void reset(PGresult* result = NULL)
67  {
68  free();
69  init(result);
70  }
71 
72  // Check whether the status is PGRES_COMMAND_OK and throw an exception if
73  // it is different. Notice that if the query can return any results,
74  // check_for_data() below should be used instead to verify whether anything
75  // was returned or not.
76  //
77  // The provided error message is used only for the exception being thrown
78  // and should describe the operation which yielded this result.
79  void check_for_errors(char const* errMsg) const;
80 
81  // Check whether the status indicates successful query completion, either
82  // with the return results (in which case true is returned) or without them
83  // (then false is returned). If the status corresponds to an error, throws
84  // an exception, just as check_for_errors().
85  bool check_for_data(char const* errMsg) const;
86 
87  // Implicit conversion to const PGresult: this is somewhat dangerous but
88  // allows us to avoid changing the existing code that uses PGresult and
89  // avoids the really bad problem with calling PQclear() twice accidentally
90  // as this would require a conversion to non-const pointer that we do not
91  // provide.
92  operator const PGresult*() const { return result_; }
93 
94  // Get the associated result (which may be NULL). Unlike the implicit
95  // conversion above, this one returns a non-const pointer, so you should be
96  // careful to avoid really modifying it.
97  PGresult* get_result() const { return result_; }
98 
99  // Dtor frees the result.
100  ~postgresql_result() { free(); }
101 
102 private:
103  void init(PGresult* result)
104  {
105  result_ = result;
106  }
107 
108  void free()
109  {
110  // Notice that it is safe to call PQclear() with NULL pointer, it
111  // simply does nothing in this case.
112  PQclear(result_);
113  }
114 
115  PGresult* result_;
116 
117  // This class can't be copied as it owns result_ which can't be duplicated.
119  postgresql_result& operator=(postgresql_result const &);
120 };
121 
122 } // namespace details
123 
126 {
128  : statement_(st) {}
129 
130  virtual void define_by_pos(int & position,
131  void * data, details::exchange_type type);
132 
133  virtual void pre_fetch();
134  virtual void post_fetch(bool gotData, bool calledFromFetch,
135  indicator * ind);
136 
137  virtual void clean_up();
138 
140 
141  void * data_;
144 };
145 
147 {
149  : statement_(st) {}
150 
151  virtual void define_by_pos(int & position,
152  void * data, details::exchange_type type);
153 
154  virtual void pre_fetch();
155  virtual void post_fetch(bool gotData, indicator * ind);
156 
157  virtual void resize(std::size_t sz);
158  virtual std::size_t size();
159 
160  virtual void clean_up();
161 
163 
164  void * data_;
167 };
168 
170 {
172  : statement_(st), position_(0), buf_(NULL) {}
173 
174  virtual void bind_by_pos(int & position,
175  void * data, details::exchange_type type, bool readOnly);
176  virtual void bind_by_name(std::string const & name,
177  void * data, details::exchange_type type, bool readOnly);
178 
179  virtual void pre_use(indicator const * ind);
180  virtual void post_use(bool gotData, indicator * ind);
181 
182  virtual void clean_up();
183 
185 
186  void * data_;
189  std::string name_;
190  char * buf_;
191 };
192 
194 {
196  : statement_(st), position_(0) {}
197 
198  virtual void bind_by_pos(int & position,
199  void * data, details::exchange_type type);
200  virtual void bind_by_name(std::string const & name,
201  void * data, details::exchange_type type);
202 
203  virtual void pre_use(indicator const * ind);
204 
205  virtual std::size_t size();
206 
207  virtual void clean_up();
208 
210 
211  void * data_;
214  std::string name_;
215  std::vector<char *> buffers_;
216 };
217 
220 {
223 
224  virtual void alloc();
225  virtual void clean_up();
226  virtual void prepare(std::string const & query,
227  details::statement_type stType);
228 
229  virtual exec_fetch_result execute(int number);
230  virtual exec_fetch_result fetch(int number);
231 
232  virtual long long get_affected_rows();
233  virtual int get_number_of_rows();
234 
235  virtual std::string rewrite_for_procedure_call(std::string const & query);
236 
237  virtual int prepare_for_describe();
238  virtual void describe_column(int colNum, data_type & dtype,
239  std::string & columnName);
240 
241  virtual postgresql_standard_into_type_backend * make_into_type_backend();
242  virtual postgresql_standard_use_type_backend * make_use_type_backend();
243  virtual postgresql_vector_into_type_backend * make_vector_into_type_backend();
244  virtual postgresql_vector_use_type_backend * make_vector_use_type_backend();
245 
247 
249  std::string query_;
251  std::string statementName_;
252  std::vector<std::string> names_; // list of names for named binds
253 
254  long long rowsAffectedBulk_; // number of rows affected by the last bulk operation
255 
256  int numberOfRows_; // number of rows retrieved from the server
257  int currentRow_; // "current" row number to consume in postFetch
258  int rowsToConsume_; // number of rows to be consumed in postFetch
259 
260  bool justDescribed_; // to optimize row description with immediately
261  // following actual statement execution
262 
267 
268  // the following maps are used for finding data buffers according to
269  // use elements specified by the user
270 
271  typedef std::map<int, char **> UseByPosBuffersMap;
272  UseByPosBuffersMap useByPosBuffers_;
273 
274  typedef std::map<std::string, char **> UseByNameBuffersMap;
275  UseByNameBuffersMap useByNameBuffers_;
276 };
277 
279 {
281 
283 
284  unsigned long value_;
285 };
286 
288 {
290 
292 
293  virtual std::size_t get_len();
294  virtual std::size_t read(std::size_t offset, char * buf,
295  std::size_t toRead);
296  virtual std::size_t write(std::size_t offset, char const * buf,
297  std::size_t toWrite);
298  virtual std::size_t append(char const * buf, std::size_t toWrite);
299  virtual void trim(std::size_t newLen);
300 
302 
303  unsigned long oid_; // oid of the large object
304  int fd_; // descriptor of the large object
305 };
306 
308 {
310 
312 
313  virtual void begin();
314  virtual void commit();
315  virtual void rollback();
316 
317  void deallocate_prepared_statement(const std::string & statementName);
318 
319  virtual bool get_next_sequence_value(session & s,
320  std::string const & sequence, long & value);
321 
322  virtual std::string get_backend_name() const { return "postgresql"; }
323 
324  void clean_up();
325 
326  virtual postgresql_statement_backend * make_statement_backend();
327  virtual postgresql_rowid_backend * make_rowid_backend();
328  virtual postgresql_blob_backend * make_blob_backend();
329 
330  std::string get_next_statement_name();
331 
333  PGconn * conn_;
334 };
335 
336 
338 {
340  virtual postgresql_session_backend * make_session(
341  connection_parameters const & parameters) const;
342 };
343 
345 
346 extern "C"
347 {
348 
349 // for dynamic backend loading
352 
353 } // extern "C"
354 
355 } // namespace soci
356 
357 #endif // SOCI_POSTGRESQL_H_INCLUDED
details::statement_type stType_
postgresql_result(PGresult *result=NULL)
#define SOCI_POSTGRESQL_DECL
postgresql_vector_into_type_backend(postgresql_statement_backend &st)
postgresql_vector_use_type_backend(postgresql_statement_backend &st)
std::map< int, char ** > UseByPosBuffersMap
details::postgresql_result result_
postgresql_session_backend & session_
postgresql_statement_backend & statement_
SOCI_POSTGRESQL_DECL void register_factory_postgresql()
postgresql_standard_use_type_backend(postgresql_statement_backend &st)
std::vector< std::string > names_
SOCI_POSTGRESQL_DECL backend_factory const * factory_postgresql()
std::map< std::string, char ** > UseByNameBuffersMap
postgresql_session_backend & session_
postgresql_statement_backend & statement_
virtual std::string get_backend_name() const
void reset(PGresult *result=NULL)
postgresql_statement_backend & statement_
postgresql_soci_error(std::string const &msg, char const *sqlst)
postgresql_statement_backend & statement_
std::vector< ISM::CombinatorialTrainerParameters > parameters
postgresql_standard_into_type_backend(postgresql_statement_backend &st)
SOCI_POSTGRESQL_DECL postgresql_backend_factory const postgresql


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