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  void attach();
153  void detach();
154 
156  static constexpr query_id qid_limit() noexcept
157  {
158  // Parenthesise this to work around an eternal Visual C++ problem:
159  // Without the extra parentheses, unless NOMINMAX is defined, the
160  // preprocessor will mistake this "max" for its annoying built-in macro
161  // of the same name.
162  return (std::numeric_limits<query_id>::max)();
163  }
164 
166  PQXX_PRIVATE query_id generate_id();
167 
168  bool have_pending() const noexcept
169  { return m_issuedrange.second != m_issuedrange.first; }
170 
171  PQXX_PRIVATE void issue();
172 
174  void set_error_at(query_id qid) noexcept
175  { if (qid < m_error) m_error = qid; }
176 
178  [[noreturn]] PQXX_PRIVATE void internal_error(const std::string &err);
179 
180  PQXX_PRIVATE bool obtain_result(bool expect_none=false);
181 
182  PQXX_PRIVATE void obtain_dummy();
183  PQXX_PRIVATE void get_further_available_results();
184  PQXX_PRIVATE void check_end_results();
185 
187  PQXX_PRIVATE void receive_if_available();
188 
190  PQXX_PRIVATE void receive(pipeline::QueryMap::const_iterator stop);
191  std::pair<pipeline::query_id, result>
192  retrieve(pipeline::QueryMap::iterator);
193 
194  QueryMap m_queries;
195  std::pair<QueryMap::iterator,QueryMap::iterator> m_issuedrange;
196  int m_retain = 0;
197  int m_num_waiting = 0;
198  query_id m_q_id = 0;
199 
201  bool m_dummy_pending = false;
202 
204  query_id m_error = qid_limit();
205 };
206 
207 } // namespace
208 
209 #include "pqxx/compiler-internal-post.hxx"
210 #endif
Definition: transaction_base.hxx:43
Result set containing data returned by a query or command.
Definition: result.hxx:69
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:136
Internal error in libpqxx library.
Definition: except.hxx:207
result retrieve(query_id qid)
Retrieve result for given query.
Definition: pipeline.hxx:108
long query_id
Definition: pipeline.hxx:51
Processes several queries in FIFO manner, optimized for high throughput.
Definition: pipeline.hxx:48
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
bool empty() const noexcept
Definition: pipeline.hxx:115