libpqxx  5.0
pipeline.hxx
1 /*-------------------------------------------------------------------------
2  *
3  * FILE
4  * pqxx/pipeline.hxx
5  *
6  * DESCRIPTION
7  * definition of the pqxx::pipeline class.
8  * Throughput-optimized query manager
9  * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/pipeline instead.
10  *
11  * Copyright (c) 2003-2015, Jeroen T. Vermeulen <jtv@xs4all.nl>
12  *
13  * See COPYING for copyright license. If you did not receive a file called
14  * COPYING with this source code, please notify the distributor of this mistake,
15  * or contact the author.
16  *
17  *-------------------------------------------------------------------------
18  */
19 #ifndef PQXX_H_PIPELINE
20 #define PQXX_H_PIPELINE
21 
22 #include "pqxx/compiler-public.hxx"
23 #include "pqxx/compiler-internal-pre.hxx"
24 
25 #include <limits>
26 #include <map>
27 #include <string>
28 
29 #include "pqxx/transaction_base"
30 
31 
32 /* Methods tested in eg. self-test program test001 are marked with "//[t1]"
33  */
34 
35 namespace pqxx
36 {
37 
39 
55 class PQXX_LIBEXPORT pipeline : public internal::transactionfocus
56 {
57 public:
58  typedef long query_id;
59 
60  explicit pipeline(transaction_base &,
61  const std::string &Name=std::string()); //[t69]
62 
63  ~pipeline() PQXX_NOEXCEPT;
64 
66 
72  query_id insert(const std::string &); //[t69]
73 
75 
76  void complete(); //[t71]
77 
79 
88  void flush(); //[t70]
89 
91 
99  void cancel();
100 
102  bool is_finished(query_id) const; //[t71]
103 
105 
111  result retrieve(query_id qid) //[t71]
112  { return retrieve(m_queries.find(qid)).second; }
113 
115 
116  std::pair<query_id, result> retrieve(); //[t69]
117 
118  bool empty() const PQXX_NOEXCEPT { return m_queries.empty(); } //[t69]
119 
121 
132  int retain(int retain_max=2); //[t70]
133 
134 
136  void resume(); //[t70]
137 
138 private:
139  class PQXX_PRIVATE Query
140  {
141  public:
142  explicit Query(const std::string &q) : m_query(q), m_res() {}
143 
144  const result &get_result() const PQXX_NOEXCEPT { return m_res; }
145  void set_result(const result &r) PQXX_NOEXCEPT { m_res = r; }
146  const std::string &get_query() const PQXX_NOEXCEPT { return m_query; }
147 
148  private:
149  std::string m_query;
150  result m_res;
151  };
152 
153  typedef std::map<query_id,Query> QueryMap;
154 
155  struct getquery:std::unary_function<QueryMap::const_iterator,std::string>
156  {
157  getquery(){} // Silences bogus warning in some gcc versions
158  std::string operator()(QueryMap::const_iterator i) const
159  { return i->second.get_query(); }
160  };
161 
162  void attach();
163  void detach();
164 
166  static query_id qid_limit() PQXX_NOEXCEPT
167  {
168  return std::numeric_limits<query_id>::max();
169  }
170 
172  PQXX_PRIVATE query_id generate_id();
173 
174  bool have_pending() const PQXX_NOEXCEPT
175  { return m_issuedrange.second != m_issuedrange.first; }
176 
177  PQXX_PRIVATE void issue();
178 
180  void set_error_at(query_id qid) PQXX_NOEXCEPT
181  { if (qid < m_error) m_error = qid; }
182 
184  PQXX_NORETURN PQXX_PRIVATE void internal_error(const std::string &err);
185 
186  PQXX_PRIVATE bool obtain_result(bool expect_none=false);
187 
188  PQXX_PRIVATE void obtain_dummy();
189  PQXX_PRIVATE void get_further_available_results();
190  PQXX_PRIVATE void check_end_results();
191 
193  PQXX_PRIVATE void receive_if_available();
194 
196  PQXX_PRIVATE void receive(pipeline::QueryMap::const_iterator stop);
197  std::pair<pipeline::query_id, result>
198  retrieve(pipeline::QueryMap::iterator);
199 
200  QueryMap m_queries;
201  std::pair<QueryMap::iterator,QueryMap::iterator> m_issuedrange;
202  int m_retain;
203  int m_num_waiting;
204  query_id m_q_id;
205 
207  bool m_dummy_pending;
208 
210  query_id m_error;
211 
213  pipeline(const pipeline &);
215  pipeline &operator=(const pipeline &);
216 };
217 
218 
219 } // namespace
220 
221 
222 #include "pqxx/compiler-internal-post.hxx"
223 
224 #endif
225 
Definition: transaction_base.hxx:52
bool empty() const PQXX_NOEXCEPT
Definition: pipeline.hxx:118
Processes several queries in FIFO manner, optimized for high throughput.
Definition: pipeline.hxx:55
Result set containing data returned by a query or command.
Definition: result.hxx:78
long query_id
Definition: pipeline.hxx:58
Definition: transaction_base.hxx:133