| 1 | //===----------------------------------------------------------------------===// |
|---|---|
| 2 | // DuckDB |
| 3 | // |
| 4 | // duckdb/main/materialized_query_result.hpp |
| 5 | // |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #pragma once |
| 10 | |
| 11 | #include "duckdb/common/types/column/column_data_collection.hpp" |
| 12 | #include "duckdb/common/winapi.hpp" |
| 13 | #include "duckdb/main/query_result.hpp" |
| 14 | |
| 15 | namespace duckdb { |
| 16 | |
| 17 | class ClientContext; |
| 18 | |
| 19 | class MaterializedQueryResult : public QueryResult { |
| 20 | public: |
| 21 | static constexpr const QueryResultType TYPE = QueryResultType::MATERIALIZED_RESULT; |
| 22 | |
| 23 | public: |
| 24 | friend class ClientContext; |
| 25 | //! Creates a successful query result with the specified names and types |
| 26 | DUCKDB_API MaterializedQueryResult(StatementType statement_type, StatementProperties properties, |
| 27 | vector<string> names, unique_ptr<ColumnDataCollection> collection, |
| 28 | ClientProperties client_properties); |
| 29 | //! Creates an unsuccessful query result with error condition |
| 30 | DUCKDB_API explicit MaterializedQueryResult(PreservedError error); |
| 31 | |
| 32 | public: |
| 33 | //! Fetches a DataChunk from the query result. |
| 34 | //! This will consume the result (i.e. the result can only be scanned once with this function) |
| 35 | DUCKDB_API unique_ptr<DataChunk> Fetch() override; |
| 36 | DUCKDB_API unique_ptr<DataChunk> FetchRaw() override; |
| 37 | //! Converts the QueryResult to a string |
| 38 | DUCKDB_API string ToString() override; |
| 39 | DUCKDB_API string ToBox(ClientContext &context, const BoxRendererConfig &config) override; |
| 40 | |
| 41 | //! Gets the (index) value of the (column index) column. |
| 42 | //! Note: this is very slow. Scanning over the underlying collection is much faster. |
| 43 | DUCKDB_API Value GetValue(idx_t column, idx_t index); |
| 44 | |
| 45 | template <class T> |
| 46 | T GetValue(idx_t column, idx_t index) { |
| 47 | auto value = GetValue(column, index); |
| 48 | return (T)value.GetValue<int64_t>(); |
| 49 | } |
| 50 | |
| 51 | DUCKDB_API idx_t RowCount() const; |
| 52 | |
| 53 | //! Returns a reference to the underlying column data collection |
| 54 | ColumnDataCollection &Collection(); |
| 55 | |
| 56 | private: |
| 57 | unique_ptr<ColumnDataCollection> collection; |
| 58 | //! Row collection, only created if GetValue is called |
| 59 | unique_ptr<ColumnDataRowCollection> row_collection; |
| 60 | //! Scan state for Fetch calls |
| 61 | ColumnDataScanState scan_state; |
| 62 | bool scan_initialized; |
| 63 | }; |
| 64 | |
| 65 | } // namespace duckdb |
| 66 |