cursor.cxx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /** Implementation of libpqxx STL-style cursor classes.
  2. *
  3. * These classes wrap SQL cursors in STL-like interfaces.
  4. *
  5. * Copyright (c) 2000-2019, Jeroen T. Vermeulen.
  6. *
  7. * See COPYING for copyright license. If you did not receive a file called
  8. * COPYING with this source code, please notify the distributor of this mistake,
  9. * or contact the author.
  10. */
  11. #include "pqxx/compiler-internal.hxx"
  12. #include <iterator>
  13. #include "pqxx/cursor"
  14. #include "pqxx/result"
  15. #include "pqxx/strconv"
  16. #include "pqxx/transaction"
  17. #include "pqxx/internal/gates/icursor_iterator-icursorstream.hxx"
  18. #include "pqxx/internal/gates/icursorstream-icursor_iterator.hxx"
  19. using namespace pqxx;
  20. using namespace pqxx::internal;
  21. pqxx::cursor_base::difference_type pqxx::cursor_base::all() noexcept
  22. {
  23. // Implemented out-of-line so we don't fall afoul of Visual Studio defining
  24. // min() and max() macros, which turn this expression into malformed code:
  25. return std::numeric_limits<int>::max() - 1;
  26. }
  27. pqxx::cursor_base::difference_type cursor_base::backward_all() noexcept
  28. {
  29. // Implemented out-of-line so we don't fall afoul of Visual Studio defining
  30. // min() and max() macros, which turn this expression into malformed code:
  31. return std::numeric_limits<int>::min() + 1;
  32. }
  33. pqxx::cursor_base::cursor_base(
  34. connection_base &context,
  35. const std::string &Name,
  36. bool embellish_name) :
  37. m_name{embellish_name ? context.adorn_name(Name) : Name}
  38. {
  39. }
  40. result::size_type pqxx::internal::obtain_stateless_cursor_size(sql_cursor &cur)
  41. {
  42. if (cur.endpos() == -1) cur.move(cursor_base::all());
  43. return result::size_type(cur.endpos() - 1);
  44. }
  45. result pqxx::internal::stateless_cursor_retrieve(
  46. sql_cursor &cur,
  47. result::difference_type size,
  48. result::difference_type begin_pos,
  49. result::difference_type end_pos)
  50. {
  51. if (begin_pos < 0 or begin_pos > size)
  52. throw range_error{"Starting position out of range"};
  53. if (end_pos < -1) end_pos = -1;
  54. else if (end_pos > size) end_pos = size;
  55. if (begin_pos == end_pos) return cur.empty_result();
  56. const int direction = ((begin_pos < end_pos) ? 1 : -1);
  57. cur.move((begin_pos-direction) - (cur.pos()-1));
  58. return cur.fetch(end_pos - begin_pos);
  59. }
  60. pqxx::icursorstream::icursorstream(
  61. transaction_base &context,
  62. const std::string &query,
  63. const std::string &basename,
  64. difference_type sstride) :
  65. m_cur{context,
  66. query,
  67. basename,
  68. cursor_base::forward_only,
  69. cursor_base::read_only,
  70. cursor_base::owned,
  71. false},
  72. m_stride{sstride},
  73. m_realpos{0},
  74. m_reqpos{0},
  75. m_iterators{nullptr},
  76. m_done{false}
  77. {
  78. set_stride(sstride);
  79. }
  80. pqxx::icursorstream::icursorstream(
  81. transaction_base &context,
  82. const field &cname,
  83. difference_type sstride,
  84. cursor_base::ownershippolicy op) :
  85. m_cur{context, cname.c_str(), op},
  86. m_stride{sstride},
  87. m_realpos{0},
  88. m_reqpos{0},
  89. m_iterators{nullptr},
  90. m_done{false}
  91. {
  92. set_stride(sstride);
  93. }
  94. void pqxx::icursorstream::set_stride(difference_type n)
  95. {
  96. if (n < 1)
  97. throw argument_error{"Attempt to set cursor stride to " + to_string(n)};
  98. m_stride = n;
  99. }
  100. result pqxx::icursorstream::fetchblock()
  101. {
  102. const result r{m_cur.fetch(m_stride)};
  103. m_realpos += r.size();
  104. if (r.empty()) m_done = true;
  105. return r;
  106. }
  107. icursorstream &pqxx::icursorstream::ignore(std::streamsize n)
  108. {
  109. auto offset = m_cur.move(difference_type(n));
  110. m_realpos += offset;
  111. if (offset < n) m_done = true;
  112. return *this;
  113. }
  114. icursorstream::size_type pqxx::icursorstream::forward(size_type n)
  115. {
  116. m_reqpos += difference_type(n) * m_stride;
  117. return icursorstream::size_type(m_reqpos);
  118. }
  119. void pqxx::icursorstream::insert_iterator(icursor_iterator *i) noexcept
  120. {
  121. gate::icursor_iterator_icursorstream{*i}.set_next(m_iterators);
  122. if (m_iterators)
  123. gate::icursor_iterator_icursorstream{*m_iterators}.set_prev(i);
  124. m_iterators = i;
  125. }
  126. void pqxx::icursorstream::remove_iterator(icursor_iterator *i) const noexcept
  127. {
  128. gate::icursor_iterator_icursorstream igate{*i};
  129. if (i == m_iterators)
  130. {
  131. m_iterators = igate.get_next();
  132. if (m_iterators)
  133. gate::icursor_iterator_icursorstream{*m_iterators}.set_prev(nullptr);
  134. }
  135. else
  136. {
  137. auto prev = igate.get_prev(), next = igate.get_next();
  138. gate::icursor_iterator_icursorstream{*prev}.set_next(next);
  139. if (next) gate::icursor_iterator_icursorstream{*next}.set_prev(prev);
  140. }
  141. igate.set_prev(nullptr);
  142. igate.set_next(nullptr);
  143. }
  144. void pqxx::icursorstream::service_iterators(difference_type topos)
  145. {
  146. if (topos < m_realpos) return;
  147. using todolist = std::multimap<difference_type,icursor_iterator*>;
  148. todolist todo;
  149. for (icursor_iterator *i = m_iterators, *next; i; i = next)
  150. {
  151. gate::icursor_iterator_icursorstream gate{*i};
  152. const auto ipos = gate.pos();
  153. if (ipos >= m_realpos and ipos <= topos)
  154. todo.insert(todolist::value_type(ipos, i));
  155. next = gate.get_next();
  156. }
  157. const auto todo_end = std::end(todo);
  158. for (auto i = std::begin(todo); i != todo_end; )
  159. {
  160. const auto readpos = i->first;
  161. if (readpos > m_realpos) ignore(readpos - m_realpos);
  162. const result r = fetchblock();
  163. for ( ; i != todo_end and i->first == readpos; ++i)
  164. gate::icursor_iterator_icursorstream{*i->second}.fill(r);
  165. }
  166. }
  167. pqxx::icursor_iterator::icursor_iterator() noexcept :
  168. m_pos{0}
  169. {
  170. }
  171. pqxx::icursor_iterator::icursor_iterator(istream_type &s) noexcept :
  172. m_stream{&s},
  173. m_pos{difference_type(gate::icursorstream_icursor_iterator(s).forward(0))}
  174. {
  175. gate::icursorstream_icursor_iterator{*m_stream}.insert_iterator(this);
  176. }
  177. pqxx::icursor_iterator::icursor_iterator(const icursor_iterator &rhs)
  178. noexcept :
  179. m_stream{rhs.m_stream},
  180. m_here{rhs.m_here},
  181. m_pos{rhs.m_pos}
  182. {
  183. if (m_stream)
  184. gate::icursorstream_icursor_iterator{*m_stream}.insert_iterator(this);
  185. }
  186. pqxx::icursor_iterator::~icursor_iterator() noexcept
  187. {
  188. if (m_stream)
  189. gate::icursorstream_icursor_iterator{*m_stream}.remove_iterator(this);
  190. }
  191. icursor_iterator pqxx::icursor_iterator::operator++(int)
  192. {
  193. icursor_iterator old{*this};
  194. m_pos = difference_type(
  195. gate::icursorstream_icursor_iterator{*m_stream}.forward());
  196. m_here.clear();
  197. return old;
  198. }
  199. icursor_iterator &pqxx::icursor_iterator::operator++()
  200. {
  201. m_pos = difference_type(
  202. gate::icursorstream_icursor_iterator{*m_stream}.forward());
  203. m_here.clear();
  204. return *this;
  205. }
  206. icursor_iterator &pqxx::icursor_iterator::operator+=(difference_type n)
  207. {
  208. if (n <= 0)
  209. {
  210. if (n == 0) return *this;
  211. throw argument_error{"Advancing icursor_iterator by negative offset."};
  212. }
  213. m_pos = difference_type(
  214. gate::icursorstream_icursor_iterator{*m_stream}.forward(
  215. icursorstream::size_type(n)));
  216. m_here.clear();
  217. return *this;
  218. }
  219. icursor_iterator &
  220. pqxx::icursor_iterator::operator=(const icursor_iterator &rhs) noexcept
  221. {
  222. if (rhs.m_stream == m_stream)
  223. {
  224. m_here = rhs.m_here;
  225. m_pos = rhs.m_pos;
  226. }
  227. else
  228. {
  229. if (m_stream)
  230. gate::icursorstream_icursor_iterator{*m_stream}.remove_iterator(this);
  231. m_here = rhs.m_here;
  232. m_pos = rhs.m_pos;
  233. m_stream = rhs.m_stream;
  234. if (m_stream)
  235. gate::icursorstream_icursor_iterator{*m_stream}.insert_iterator(this);
  236. }
  237. return *this;
  238. }
  239. bool pqxx::icursor_iterator::operator==(const icursor_iterator &rhs) const
  240. {
  241. if (m_stream == rhs.m_stream) return pos() == rhs.pos();
  242. if (m_stream and rhs.m_stream) return false;
  243. refresh();
  244. rhs.refresh();
  245. return m_here.empty() and rhs.m_here.empty();
  246. }
  247. bool pqxx::icursor_iterator::operator<(const icursor_iterator &rhs) const
  248. {
  249. if (m_stream == rhs.m_stream) return pos() < rhs.pos();
  250. refresh();
  251. rhs.refresh();
  252. return not m_here.empty();
  253. }
  254. void pqxx::icursor_iterator::refresh() const
  255. {
  256. if (m_stream)
  257. gate::icursorstream_icursor_iterator{*m_stream}.service_iterators(pos());
  258. }
  259. void pqxx::icursor_iterator::fill(const result &r)
  260. {
  261. m_here = r;
  262. }