1 | //===----------------------------------------------------------------------===// |
2 | // DuckDB |
3 | // |
4 | // duckdb/main/prepared_statement.hpp |
5 | // |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #pragma once |
10 | |
11 | #include "duckdb/main/materialized_query_result.hpp" |
12 | |
13 | namespace duckdb { |
14 | class ClientContext; |
15 | class PreparedStatementData; |
16 | |
17 | //! A prepared statement |
18 | class PreparedStatement { |
19 | public: |
20 | //! Create a successfully prepared prepared statement object with the given name |
21 | PreparedStatement(ClientContext *context, string name, string query, PreparedStatementData &data, |
22 | idx_t n_param = 0); |
23 | //! Create a prepared statement that was not successfully prepared |
24 | PreparedStatement(string error); |
25 | |
26 | ~PreparedStatement(); |
27 | |
28 | public: |
29 | StatementType type; |
30 | //! The client context this prepared statement belongs to |
31 | ClientContext *context; |
32 | //! The internal name of the prepared statement |
33 | string name; |
34 | //! The query that is being prepared |
35 | string query; |
36 | //! Whether or not the statement was successfully prepared |
37 | bool success; |
38 | //! The error message (if success = false) |
39 | string error; |
40 | //! Whether or not the prepared statement has been invalidated because the underlying connection has been destroyed |
41 | bool is_invalidated; |
42 | //! The amount of bound parameters |
43 | idx_t n_param; |
44 | //! The result SQL types of the prepared statement |
45 | vector<SQLType> types; |
46 | //! The result names of the prepared statement |
47 | vector<string> names; |
48 | |
49 | public: |
50 | //! Execute the prepared statement with the given set of arguments |
51 | template <typename... Args> unique_ptr<QueryResult> Execute(Args... args) { |
52 | vector<Value> values; |
53 | return ExecuteRecursive(values, args...); |
54 | } |
55 | |
56 | //! Execute the prepared statement with the given set of values |
57 | unique_ptr<QueryResult> Execute(vector<Value> &values, bool allow_stream_result = true); |
58 | |
59 | private: |
60 | unique_ptr<QueryResult> ExecuteRecursive(vector<Value> &values) { |
61 | return Execute(values); |
62 | } |
63 | |
64 | template <typename T, typename... Args> |
65 | unique_ptr<QueryResult> ExecuteRecursive(vector<Value> &values, T value, Args... args) { |
66 | values.push_back(Value::CreateValue<T>(value)); |
67 | return ExecuteRecursive(values, args...); |
68 | } |
69 | }; |
70 | |
71 | } // namespace duckdb |
72 | |