libpqxx
pipeline.hxx
1 
13 #ifndef PQXX_H_PIPELINE
14 #define PQXX_H_PIPELINE
15 
16 #include "pqxx/compiler-public.hxx"
17 #include "pqxx/compiler-internal-pre.hxx"
18 
19 #include <limits>
20 #include <map>
21 #include <string>
22 
23 #include "pqxx/transaction_base.hxx"
24 
25 
26 // Methods tested in eg. test module test01 are marked with "//[t01]".
27 
28 namespace pqxx
29 {
30 
32 
48 class PQXX_LIBEXPORT pipeline : public internal::transactionfocus
49 {
50 public:
51  using query_id = long;
52 
53  pipeline(const pipeline &) =delete;
54  pipeline &operator=(const pipeline &) =delete;
55 
56  explicit pipeline( //[t69]
58  const std::string &Name=std::string());
59 
60  ~pipeline() noexcept;
61 
63 
69  query_id insert(const std::string &); //[t69]
70 
72 
73  void complete(); //[t71]
74 
76 
85  void flush(); //[t70]
86 
88 
96  void cancel();
97 
99  bool is_finished(query_id) const; //[t71]
100 
102 
108  result retrieve(query_id qid) //[t71]
109  { return retrieve(m_queries.find(qid)).second; }
110 
112 
113  std::pair<query_id, result> retrieve(); //[t69]
114 
115  bool empty() const noexcept { return m_queries.empty(); } //[t69]
116 
118 
129  int retain(int retain_max=2); //[t70]
130 
131 
133  void resume(); //[t70]
134 
135 private:
136  class PQXX_PRIVATE Query
137  {
138  public:
139  explicit Query(const std::string &q) : m_query(q), m_res() {}
140 
141  const result &get_result() const noexcept { return m_res; }
142  void set_result(const result &r) noexcept { m_res = r; }
143  const std::string &get_query() const noexcept { return m_query; }
144 
145  private:
146  std::string m_query;
147  result m_res;
148  };
149 
150  using QueryMap = std::map<query_id,Query>;
151 
152  struct getquery
153  {
154  getquery(){} // Silences bogus warning in some gcc versions
155  std::string operator()(QueryMap::const_iterator i) const
156  { return i->second.get_query(); }
157  };
158 
159  void attach();
160  void detach();
161 
163  static constexpr query_id qid_limit() noexcept
164  {
165  return std::numeric_limits<query_id>::max();
166  }
167 
169  PQXX_PRIVATE query_id generate_id();
170 
171  bool have_pending() const noexcept
172  { return m_issuedrange.second != m_issuedrange.first; }
173 
174  PQXX_PRIVATE void issue();
175 
177  void set_error_at(query_id qid) noexcept
178  { if (qid < m_error) m_error = qid; }
179 
181  [[noreturn]] PQXX_PRIVATE void internal_error(const std::string &err);
182 
183  PQXX_PRIVATE bool obtain_result(bool expect_none=false);
184 
185  PQXX_PRIVATE void obtain_dummy();
186  PQXX_PRIVATE void get_further_available_results();
187  PQXX_PRIVATE void check_end_results();
188 
190  PQXX_PRIVATE void receive_if_available();
191 
193  PQXX_PRIVATE void receive(pipeline::QueryMap::const_iterator stop);
194  std::pair<pipeline::query_id, result>
195  retrieve(pipeline::QueryMap::iterator);
196 
197  QueryMap m_queries;
198  std::pair<QueryMap::iterator,QueryMap::iterator> m_issuedrange;
199  int m_retain = 0;
200  int m_num_waiting = 0;
201  query_id m_q_id = 0;
202 
204  bool m_dummy_pending = false;
205 
207  query_id m_error = qid_limit();
208 };
209 
210 } // namespace
211 
212 #include "pqxx/compiler-internal-post.hxx"
213 
214 #endif
long query_id
Definition: pipeline.hxx:51
bool empty() const noexcept
Definition: pipeline.hxx:115
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:22
Definition: transaction_base.hxx:42
Processes several queries in FIFO manner, optimized for high throughput.
Definition: pipeline.hxx:48
Result set containing data returned by a query or command.
Definition: result.hxx:67
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:130
STL namespace.
Internal error in libpqxx library.
Definition: except.hxx:205