1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/main/prepared_statement.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/common/winapi.hpp"
12#include "duckdb/main/materialized_query_result.hpp"
13#include "duckdb/main/pending_query_result.hpp"
14#include "duckdb/common/preserved_error.hpp"
15#include "duckdb/common/case_insensitive_map.hpp"
16
17namespace duckdb {
18class ClientContext;
19class PreparedStatementData;
20
21//! A prepared statement
22class PreparedStatement {
23public:
24 //! Create a successfully prepared prepared statement object with the given name
25 DUCKDB_API PreparedStatement(shared_ptr<ClientContext> context, shared_ptr<PreparedStatementData> data,
26 string query, idx_t n_param, case_insensitive_map_t<idx_t> named_param_map);
27 //! Create a prepared statement that was not successfully prepared
28 DUCKDB_API explicit PreparedStatement(PreservedError error);
29
30 DUCKDB_API ~PreparedStatement();
31
32public:
33 //! The client context this prepared statement belongs to
34 shared_ptr<ClientContext> context;
35 //! The prepared statement data
36 shared_ptr<PreparedStatementData> data;
37 //! The query that is being prepared
38 string query;
39 //! Whether or not the statement was successfully prepared
40 bool success;
41 //! The error message (if success = false)
42 PreservedError error;
43 //! The amount of bound parameters
44 idx_t n_param;
45 //! The (optional) named parameters
46 case_insensitive_map_t<idx_t> named_param_map;
47
48public:
49 //! Returns the stored error message
50 DUCKDB_API const string &GetError();
51 //! Returns the stored error object
52 DUCKDB_API PreservedError &GetErrorObject();
53 //! Returns whether or not an error occurred
54 DUCKDB_API bool HasError() const;
55 //! Returns the number of columns in the result
56 DUCKDB_API idx_t ColumnCount();
57 //! Returns the statement type of the underlying prepared statement object
58 DUCKDB_API StatementType GetStatementType();
59 //! Returns the underlying statement properties
60 DUCKDB_API StatementProperties GetStatementProperties();
61 //! Returns the result SQL types of the prepared statement
62 DUCKDB_API const vector<LogicalType> &GetTypes();
63 //! Returns the result names of the prepared statement
64 DUCKDB_API const vector<string> &GetNames();
65 //! Returns the map of parameter index to the expected type of parameter
66 DUCKDB_API vector<LogicalType> GetExpectedParameterTypes() const;
67
68 //! Create a pending query result of the prepared statement with the given set of arguments
69 template <typename... Args>
70 unique_ptr<PendingQueryResult> PendingQuery(Args... args) {
71 vector<Value> values;
72 return PendingQueryRecursive(values, args...);
73 }
74
75 //! Execute the prepared statement with the given set of arguments
76 template <typename... Args>
77 unique_ptr<QueryResult> Execute(Args... args) {
78 vector<Value> values;
79 return ExecuteRecursive(values, args...);
80 }
81
82 //! Create a pending query result of the prepared statement with the given set of arguments
83 DUCKDB_API unique_ptr<PendingQueryResult> PendingQuery(vector<Value> &values, bool allow_stream_result = true);
84
85 //! Execute the prepared statement with the given set of values
86 DUCKDB_API unique_ptr<QueryResult> Execute(vector<Value> &values, bool allow_stream_result = true);
87
88private:
89 unique_ptr<PendingQueryResult> PendingQueryRecursive(vector<Value> &values) {
90 return PendingQuery(values);
91 }
92
93 template <typename T, typename... Args>
94 unique_ptr<PendingQueryResult> PendingQueryRecursive(vector<Value> &values, T value, Args... args) {
95 values.push_back(Value::CreateValue<T>(value));
96 return PendingQueryRecursive(values, args...);
97 }
98
99 unique_ptr<QueryResult> ExecuteRecursive(vector<Value> &values) {
100 return Execute(values);
101 }
102
103 template <typename T, typename... Args>
104 unique_ptr<QueryResult> ExecuteRecursive(vector<Value> &values, T value, Args... args) {
105 values.push_back(Value::CreateValue<T>(value));
106 return ExecuteRecursive(values, args...);
107 }
108};
109
110} // namespace duckdb
111