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  // Parenthesise this to work around an eternal Visual C++ problem:
166  // Without the extra parentheses, unless NOMINMAX is defined, the
167  // preprocessor will mistake this "max" for its annoying built-in macro
168  // of the same name.
169  return (std::numeric_limits<query_id>::max)();
170  }
171 
173  PQXX_PRIVATE query_id generate_id();
174 
175  bool have_pending() const noexcept
176  { return m_issuedrange.second != m_issuedrange.first; }
177 
178  PQXX_PRIVATE void issue();
179 
181  void set_error_at(query_id qid) noexcept
182  { if (qid < m_error) m_error = qid; }
183 
185  [[noreturn]] PQXX_PRIVATE void internal_error(const std::string &err);
186 
187  PQXX_PRIVATE bool obtain_result(bool expect_none=false);
188 
189  PQXX_PRIVATE void obtain_dummy();
190  PQXX_PRIVATE void get_further_available_results();
191  PQXX_PRIVATE void check_end_results();
192 
194  PQXX_PRIVATE void receive_if_available();
195 
197  PQXX_PRIVATE void receive(pipeline::QueryMap::const_iterator stop);
198  std::pair<pipeline::query_id, result>
199  retrieve(pipeline::QueryMap::iterator);
200 
201  QueryMap m_queries;
202  std::pair<QueryMap::iterator,QueryMap::iterator> m_issuedrange;
203  int m_retain = 0;
204  int m_num_waiting = 0;
205  query_id m_q_id = 0;
206 
208  bool m_dummy_pending = false;
209 
211  query_id m_error = qid_limit();
212 };
213 
214 } // namespace
215 
216 #include "pqxx/compiler-internal-post.hxx"
217 
218 #endif
bool empty() const noexcept
Definition: pipeline.hxx:115
Result set containing data returned by a query or command.
Definition: result.hxx:65
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:22
Processes several queries in FIFO manner, optimized for high throughput.
Definition: pipeline.hxx:48
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:130
Internal error in libpqxx library.
Definition: except.hxx:205
STL namespace.
long query_id
Definition: pipeline.hxx:51
Definition: transaction_base.hxx:42