Hyper API for C++ 0.0.20746
Hyper client library for C++ applications
Loading...
Searching...
No Matches
Result.hpp
Go to the documentation of this file.
1
5#ifndef TABLEAU_HYPER_RESULT_HPP
6#define TABLEAU_HYPER_RESULT_HPP
7
9#include <hyperapi/Date.hpp>
11#include <hyperapi/Interval.hpp>
12#include <hyperapi/Numeric.hpp>
15#include <hyperapi/Time.hpp>
18#include <hyperapi/hyperapi.h>
19
20#include <functional>
21#include <iterator>
22#include <string>
23
24namespace hyperapi {
25
26class ChunkIterator;
27struct Chunks;
28class ColumnIterator;
29class Connection;
30class Result;
31class Row;
32class ChunkedResultIterator;
33class ResultIterator;
34namespace internal {
35inline bool hasCopyData(Result& result);
36struct CopyData;
37inline CopyData getCopyData(Result& result);
38inline hyper_rowset_type_t getResultType(Result& result);
39}
40
44class Value final {
45 public:
47 Value(hyper_value_t value, SqlType type, string_view columnName) noexcept
48 : value_(value), type_(type), columnName_(columnName) {
49 }
50
52 Value() noexcept {}
53
58 template <typename ReturnType>
59 operator ReturnType() const { return get<ReturnType>(); }
60
88 template <typename ReturnType>
89 ReturnType get() const;
90
92 SqlType getType() const noexcept { return type_; }
93
95 bool isNull() const noexcept { return !value_.value; }
96
98 friend std::ostream& operator<<(std::ostream& os, const Value& value);
99
101 friend bool operator==(const Value& lhs, const Value& rhs) noexcept { return lhs.value_.value == rhs.value_.value; }
102
104 friend bool operator!=(const Value& lhs, const Value& rhs) noexcept { return !(lhs == rhs); }
105
106 private:
107 friend class ColumnIterator;
108 template <typename ReturnType>
109 friend struct internal::ValueAccess;
110 friend class Connection;
111
113 Value(const Value&) = default;
114 Value(Value&&) = default;
115 Value& operator=(const Value&) = default;
116 Value& operator=(Value&&) = default;
117
119 hyper_value_t value_{nullptr, 0};
121 SqlType type_{TypeTag::Unsupported, 0};
123 string_view columnName_ = "";
124};
125
129class Chunk final {
130 public:
134 Chunk() noexcept = default;
135
137 ~Chunk() noexcept;
138
140 Chunk(Chunk& other) = delete;
142 Chunk& operator=(Chunk& other) = delete;
143
145 Chunk(Chunk&& other) noexcept;
147 Chunk& operator=(Chunk&& other) noexcept;
148
155 Row getRowAt(hyper_row_index_t chunkRowIndex) const noexcept;
156
161 size_t getRowCount() const noexcept { return rowCount_; }
162
164 bool isOpen() const noexcept { return (chunk_ != nullptr); }
165
167 operator bool() const noexcept { return isOpen(); }
168
170 friend bool operator==(const Chunk& lhs, const Chunk& rhs) noexcept { return lhs.chunk_ == rhs.chunk_; }
171
173 friend bool operator!=(const Chunk& lhs, const Chunk& rhs) noexcept { return !(lhs == rhs); }
174
175 private:
181 Chunk(hyper_rowset_chunk_t* chunk, const Result& result) noexcept;
182
184 hyper_rowset_chunk_t* chunk_ = nullptr;
186 mutable const uint8_t* const* values_ = nullptr;
188 mutable const size_t* valuesSizes_ = nullptr;
190 size_t chunkSize_ = 0;
192 const Result* result_ = nullptr;
194 size_t rowCount_ = 0;
195
197 const uint8_t* const* getValues() const noexcept;
199 const size_t* getValuesSizes() const noexcept;
200
201 friend class Row;
202 friend class ColumnIterator;
203 friend class Result;
204};
205
210class Row final {
211 public:
222 template <typename ReturnType = Value>
223 ReturnType get(hyper_field_index_t columnIndex) const;
224
230 const ResultSchema& getSchema() const noexcept;
231
232 private:
238 Row(const Chunk& chunk, hyper_row_index_t chunkRowIndex) noexcept;
239
241 Row(const Row&) = default;
242 Row(Row&&) = default;
243 Row& operator=(const Row&) = default;
244 Row& operator=(Row&&) = default;
245
247 std::reference_wrapper<const Chunk> chunk_;
249 hyper_row_index_t chunkRowIndex_;
250
251 friend class Chunk;
252 friend class ResultIterator;
253 friend class ColumnIterator;
254 friend class ChunkIterator;
255};
256
259};
260static constexpr IteratorBeginTag iteratorBeginTag = {};
261
264};
265static constexpr IteratorEndTag iteratorEndTag = {};
266
268class ChunkIterator final {
269 public:
270 using iterator_category = std::input_iterator_tag;
271 using value_type = Row;
272 using difference_type = std::ptrdiff_t;
273 using pointer = const Row*;
274 using reference = const Row&;
275
277 ChunkIterator(const Chunk& chunk, IteratorBeginTag) noexcept
278 : chunk_(chunk), rowIndex_(0), currentRow_(chunk, 0) {
279 }
280
282 ChunkIterator(const Chunk& chunk, IteratorEndTag) noexcept
283 : chunk_(chunk), rowIndex_(static_cast<hyper_row_index_t>(chunk.getRowCount())), currentRow_(chunk, 0) {
284 }
285
287 reference operator*() const noexcept;
288
290 pointer operator->() const noexcept { return &**this; }
291
294
296 ChunkIterator operator++(int) noexcept;
297
299 friend bool operator==(const ChunkIterator& lhs, const ChunkIterator& rhs) noexcept { return (lhs.chunk_.get() == rhs.chunk_.get()) && (lhs.rowIndex_ == rhs.rowIndex_); }
300
302 friend bool operator!=(const ChunkIterator& lhs, const ChunkIterator& rhs) noexcept { return !(lhs == rhs); }
303
304 private:
305 std::reference_wrapper<const Chunk> chunk_;
306 hyper_row_index_t rowIndex_;
307 mutable Row currentRow_;
308
309 friend class ResultIterator;
310};
311
313inline ChunkIterator begin(const Chunk& chunk) {
314 return {chunk, iteratorBeginTag};
315}
316
318inline ChunkIterator end(const Chunk& chunk) noexcept {
319 return {chunk, iteratorEndTag};
320}
321
326class Result final {
327 public:
333 Result() : currentChunkIterator_(end(currentChunk_)) {}
334
335 ~Result() noexcept;
336
338 Result(Result&& other) noexcept;
339
341 Result& operator=(Result&& other) noexcept;
342
344 Result& operator=(const Result& other) = delete;
345 Result(const Result& other) = delete;
346
351 const ResultSchema& getSchema() const noexcept { return *schema_; }
352
361
368
373 bool isOpen() const noexcept;
374
379 void close() noexcept;
380
381 private:
392 explicit Result(hyper_rowset_t* rowset, Connection& conn);
393
395 void fetchNextChunk();
396
398 Chunk& getCurrentChunk();
399
401 ChunkIterator& getCurrentChunkIterator();
402
404 void createSchema();
405
407 hyper_rowset_t* rowset_ = nullptr;
408
410 Connection* conn_ = nullptr;
411
413 mutable optional<ResultSchema> schema_;
414
416 Chunk currentChunk_;
418 ChunkIterator currentChunkIterator_;
419
420 friend class Row;
421 friend class Connection;
422 friend class Chunk;
423 friend class ChunkedResultIterator;
424 friend class ResultIterator;
425 friend Result internal::executePreparedQuery(Connection& connection, const std::string& statement_name, hyper_rowset_result_format_t result_format);
426 friend Result internal::executeQueryParams(Connection& connection, const std::string& query, hyper_rowset_result_format_t result_format);
427 friend bool operator==(const ChunkedResultIterator& lhs, const ChunkedResultIterator& rhs) noexcept;
428 friend bool operator==(const ResultIterator& lhs, const ResultIterator& rhs) noexcept;
429 friend inline bool internal::hasCopyData(Result&);
430 friend inline internal::CopyData internal::getCopyData(Result& result);
431 friend inline hyper_rowset_type_t internal::getResultType(Result& result);
432};
433
435struct Chunks {
437 Chunks(Result& result) : result(result) {
438 }
439
442};
443
445class ColumnIterator final {
446 public:
447 using iterator_category = std::input_iterator_tag;
448 using value_type = Value;
449 using difference_type = std::ptrdiff_t;
450 using pointer = const Value*;
451 using reference = const Value&;
452
454 ColumnIterator(const Row& row, IteratorBeginTag) noexcept
455 : row_(row), columnIndex_(0) {
456 }
457
459 ColumnIterator(const Row& row, IteratorEndTag) noexcept
460 : row_(row), columnIndex_(static_cast<hyper_field_index_t>(row.getSchema().getColumnCount())) {
461 }
462
464 reference operator*() const noexcept;
465
467 pointer operator->() const noexcept { return &**this; }
468
471
474
476 friend bool operator==(const ColumnIterator& lhs, const ColumnIterator& rhs) noexcept { return (&lhs.row_.get() == &rhs.row_.get()) && (lhs.columnIndex_ == rhs.columnIndex_); }
477
479 friend bool operator!=(const ColumnIterator& lhs, const ColumnIterator& rhs) noexcept { return !(lhs == rhs); }
480
481 private:
482 std::reference_wrapper<const Row> row_;
483 hyper_field_index_t columnIndex_;
484 mutable Value currentValue_;
485};
486
488inline ColumnIterator begin(const Row& row) {
489 return {row, iteratorBeginTag};
490}
491
493inline ColumnIterator end(const Row& row) noexcept {
494 return {row, iteratorEndTag};
495}
496
499 public:
500 using iterator_category = std::input_iterator_tag;
501 using value_type = Chunk;
502 using difference_type = std::ptrdiff_t;
503 using pointer = Chunk*;
504 using reference = Chunk&;
505
510 : result_(result), isEndIterator_(true) {
511 }
512
514 reference operator*() noexcept { return result_.get().currentChunk_; }
515
517 pointer operator->() noexcept { return &**this; }
518
521
523 friend bool operator==(const ChunkedResultIterator& lhs, const ChunkedResultIterator& rhs) noexcept;
524
526 friend bool operator!=(const ChunkedResultIterator& lhs, const ChunkedResultIterator& rhs) noexcept { return !(lhs == rhs); }
527
528 private:
530 std::reference_wrapper<Result> result_;
532 bool isEndIterator_;
533};
534
536inline ChunkedResultIterator begin(const Chunks& chunks) {
537 return {chunks.result, iteratorBeginTag};
538}
539
541inline ChunkedResultIterator end(const Chunks& chunks) noexcept {
542 return {chunks.result, iteratorEndTag};
543}
544
551class ResultIterator final {
552 public:
553 using iterator_category = std::input_iterator_tag;
554 using value_type = Row;
555 using difference_type = std::ptrdiff_t;
556 using pointer = const Row*;
557 using reference = const Row&;
558
563 : result_(result), isEndIterator_(true) {
564 }
565
567 reference operator*() const noexcept { return *result_.get().getCurrentChunkIterator(); }
568
570 pointer operator->() const noexcept { return &**this; }
571
574
576 friend bool operator==(const ResultIterator& lhs, const ResultIterator& rhs) noexcept;
577
579 friend bool operator!=(const ResultIterator& lhs, const ResultIterator& rhs) noexcept { return !(lhs == rhs); }
580
581 private:
583 std::reference_wrapper<Result> result_;
585 bool isEndIterator_;
586};
587
589inline ResultIterator begin(Result& result) {
590 return {result, iteratorBeginTag};
591}
592
594inline ResultIterator end(Result& result) noexcept {
595 return {result, iteratorEndTag};
596}
597}
598
599#include <hyperapi/impl/Result.impl.hpp>
600
601#endif
Iterates over a hyperapi::Chunk in rows (hyperapi::Row).
Definition Result.hpp:268
friend bool operator!=(const ChunkIterator &lhs, const ChunkIterator &rhs) noexcept
Comparison operator.
Definition Result.hpp:302
ChunkIterator & operator++() noexcept
Advances the iterator.
ChunkIterator(const Chunk &chunk, IteratorEndTag) noexcept
Constructs the end-iterator.
Definition Result.hpp:282
reference operator*() const noexcept
Returns a reference to the current value.
ChunkIterator(const Chunk &chunk, IteratorBeginTag) noexcept
Constructs the begin-iterator.
Definition Result.hpp:277
A chunk of a result.
Definition Result.hpp:129
friend bool operator==(const Chunk &lhs, const Chunk &rhs) noexcept
Comparison operator.
Definition Result.hpp:170
bool isOpen() const noexcept
Returns whether the chunk is valid.
Definition Result.hpp:164
Row getRowAt(hyper_row_index_t chunkRowIndex) const noexcept
Retrieves the row at the given index inside the chunk (starting at 0).
size_t getRowCount() const noexcept
Definition Result.hpp:161
Chunk() noexcept=default
Constructor, constructs a closed chunk.
friend bool operator!=(const Chunk &lhs, const Chunk &rhs) noexcept
Comparison operator.
Definition Result.hpp:173
Iterates over a hyperapi::Result in hyperapi::Chunk.
Definition Result.hpp:498
reference operator*() noexcept
Returns a reference to the current value.
Definition Result.hpp:514
friend bool operator!=(const ChunkedResultIterator &lhs, const ChunkedResultIterator &rhs) noexcept
Comparison operator.
Definition Result.hpp:526
pointer operator->() noexcept
Returns a pointer to the current value.
Definition Result.hpp:517
friend bool operator==(const ChunkedResultIterator &lhs, const ChunkedResultIterator &rhs) noexcept
Comparison operator.
ChunkedResultIterator & operator++()
Advances the iterator.
ChunkedResultIterator(Result &result, IteratorEndTag) noexcept
Constructs the end-iterator.
Definition Result.hpp:509
ChunkedResultIterator(Result &result, IteratorBeginTag)
Constructs the begin-iterator.
Iterates over a hyperapi::Row in values (hyperapi::Value).
Definition Result.hpp:445
reference operator*() const noexcept
Returns a reference to the current value.
ColumnIterator & operator++()
Advances the iterator.
friend bool operator==(const ColumnIterator &lhs, const ColumnIterator &rhs) noexcept
Comparison operator.
Definition Result.hpp:476
ColumnIterator operator++(int)
Advances the iterator.
friend bool operator!=(const ColumnIterator &lhs, const ColumnIterator &rhs) noexcept
Comparison operator.
Definition Result.hpp:479
ColumnIterator(const Row &row, IteratorEndTag) noexcept
Constructs the end-iterator.
Definition Result.hpp:459
ColumnIterator(const Row &row, IteratorBeginTag) noexcept
Constructs the begin-iterator.
Definition Result.hpp:454
Defines a Hyper connection.
Iterates over a hyperapi::Result in rows (hyperapi::Row).
Definition Result.hpp:551
ResultIterator(Result &result, IteratorBeginTag)
Constructs the begin-iterator.
friend bool operator==(const ResultIterator &lhs, const ResultIterator &rhs) noexcept
Comparison operator.
friend bool operator!=(const ResultIterator &lhs, const ResultIterator &rhs) noexcept
Comparison operator.
Definition Result.hpp:579
ResultIterator(Result &result, IteratorEndTag) noexcept
Constructs the end-iterator.
Definition Result.hpp:562
reference operator*() const noexcept
Returns a reference to the current value.
Definition Result.hpp:567
pointer operator->() const noexcept
Returns a pointer to the current value.
Definition Result.hpp:570
ResultIterator & operator++()
Advances the iterator.
Base class for a result of a query.
Definition Result.hpp:326
Connection & getConnection()
Returns the connection of the SQL statement that yielded this result.
bool isOpen() const noexcept
Checks whether the result is open.
optional< size_t > getAffectedRowCount() const
Get the affected row count, if the statement had any.
Result()
Constructs an empty Result object.
Definition Result.hpp:333
A Row inside a chunk.
Definition Result.hpp:210
ReturnType get(hyper_field_index_t columnIndex) const
Returns the value of field at position columnIndex.
const ResultSchema & getSchema() const noexcept
Returns the schema of the row.
A Hyper SQL type.
Definition SqlType.hpp:44
A value inside a row.
Definition Result.hpp:44
Value(hyper_value_t value, SqlType type, string_view columnName) noexcept
Constructs a Value object.
Definition Result.hpp:47
Value() noexcept
Default constructs a Value object.
Definition Result.hpp:52
bool isNull() const noexcept
Returns whether the value is null.
Definition Result.hpp:95
SqlType getType() const noexcept
Get the type of the value.
Definition Result.hpp:92
ReturnType get() const
Get the value as one of the supported types.
friend std::ostream & operator<<(std::ostream &os, const Value &value)
Stream output operator.
friend bool operator==(const Value &lhs, const Value &rhs) noexcept
Comparison operator.
Definition Result.hpp:101
friend bool operator!=(const Value &lhs, const Value &rhs) noexcept
Comparison operator.
Definition Result.hpp:104
Surrogate for C++17 std::optional
Definition optional.hpp:40
Describes an object that can refer to a constant, contiguous sequence of char-like objects.
The primary namespace of the Hyper API for C++.
Definition ByteSpan.hpp:15
ChunkIterator begin(const Chunk &chunk)
Returns the begin-iterator for the rows of the given chunk.
Definition Result.hpp:313
ChunkIterator end(const Chunk &chunk) noexcept
Returns the end-iterator for the rows of the given chunk.
Definition Result.hpp:318
A tag for an iterator-begin constructor.
Definition Result.hpp:258
A tag for an iterator-end constructor.
Definition Result.hpp:263
A tag that makes a result iterable in chunks.
Definition Result.hpp:435
Result & result
the result
Definition Result.hpp:441
Chunks(Result &result)
Construct a Chunks object.
Definition Result.hpp:437