Cursor.h
1/* Copyright (C) 2006 by Tommi Maekitalo
2 Copyright (C) 2006-2026 by Marc Boris Duerner
3 SPDX-License-Identifier: LGPL-2.1-or-later WITH mif-exception
4*/
5
6#ifndef PT_DB_CURSOR_H
7#define PT_DB_CURSOR_H
8
9#include <Pt/Db/Api.h>
10#include <Pt/Db/ICursor.h>
11#include <Pt/Db/Result.h>
12#include <Pt/SmartPtr.h>
13#include <Pt/Signal.h>
14#include <cstddef>
15#include <iterator>
16
17namespace Pt {
18
19namespace Db {
20
21class Statement;
22class CursorIterator;
23
57class PT_DB_API Cursor
58{
59 public:
60 typedef std::size_t size_type;
61 typedef CursorIterator Iterator;
62
66 : _batchSize(0)
67 {}
68
71 Cursor(ICursor* cursor, size_type batchSize);
72
77 bool fetch();
78
82 { return _batch; }
83
86 const Result& result() const
87 { return _batch; }
88
92
95 bool isOpen() const
96 { return _cursor && _cursor->isOpen(); }
97
101
108
112 { return _cursor.get(); }
113
116 const ICursor* impl() const
117 { return _cursor.get(); }
118
121 void close();
122
125 Iterator begin();
126
129 Iterator end();
130
131 private:
133 CursorImplPtr _cursor;
134 Result _batch;
135 size_type _batchSize;
136};
137
138
141class PT_DB_API CursorIterator
142{
143 public:
144 using iterator_category = std::forward_iterator_tag;
145 using value_type = Row;
146 using difference_type = std::ptrdiff_t;
147 using pointer = const Row*;
148 using reference = const Row&;
149
150 private:
151 Cursor _cursor;
152 std::size_t _index;
153 Row _current;
154
155 public:
159 : _index(0)
160 {}
161
164 explicit CursorIterator(const Cursor& cursor);
165
166 bool operator==(const CursorIterator& other) const;
167
168 bool operator!=(const CursorIterator& other) const
169 { return ! operator==(other); }
170
171 CursorIterator& operator++();
172
173 const Row& operator*() const
174 { return _current; }
175
176 const Row* operator->() const
177 { return &_current; }
178};
179
180} // namespace Db
181
182} // namespace Pt
183
184#endif // PT_DB_CURSOR_H
Forward iterator over the rows of a Cursor.
Definition Cursor.h:142
CursorIterator()
Creates the end sentinel.
Definition Cursor.h:158
CursorIterator(const Cursor &cursor)
Creates an iterator on a cursor that already has a batch.
Batch cursor for large query results.
Definition Cursor.h:58
bool isOpen() const
Returns true if the cursor is open.
Definition Cursor.h:95
Iterator begin()
Returns an iterator to the first row, fetching the first batch.
Signal & fetchFinished()
Signal emitted when an asynchronous batch is ready.
const Result & result() const
Returns the current batch buffer.
Definition Cursor.h:86
void close()
Closes the cursor and releases its resources.
void beginFetch()
Starts asynchronous retrieval of the next batch.
Cursor(ICursor *cursor, size_type batchSize)
Takes ownership of cursor with a fixed batchSize.
Cursor()
Creates a closed cursor.
Definition Cursor.h:65
Iterator end()
Returns the end sentinel iterator.
Result & endFetch()
Completes an asynchronous fetch.
Result & result()
Returns the current batch buffer.
Definition Cursor.h:81
ICursor * impl()
Returns the backend cursor.
Definition Cursor.h:111
bool fetch()
Fetches the next batch synchronously.
const ICursor * impl() const
Returns the backend cursor.
Definition Cursor.h:116
Batch-cursor backend.
Definition ICursor.h:26
Buffered query result with random-access rows.
Definition Result.h:42
One row of a buffered query result.
Definition Row.h:34
Prepared SQL statement with named host variables.
Definition Statement.h:65
Multicast Signal to call multiple slots.
Definition Signal.h:190
Policy based smart pointer.
Definition SmartPtr.h:462
Portable SQL database access.
Core module.
Definition Allocator.h:33