1#include "duckdb/main/prepared_statement.hpp"
2#include "duckdb/common/exception.hpp"
3#include "duckdb/main/client_context.hpp"
4#include "duckdb/main/prepared_statement_data.hpp"
5
6using namespace duckdb;
7using namespace std;
8
9PreparedStatement::PreparedStatement(ClientContext *context, string name, string query, PreparedStatementData &data,
10 idx_t n_param)
11 : context(context), name(name), query(query), success(true), is_invalidated(false), n_param(n_param) {
12 this->type = data.statement_type;
13 this->types = data.sql_types;
14 this->names = data.names;
15}
16
17PreparedStatement::PreparedStatement(string error)
18 : context(nullptr), success(false), error(error), is_invalidated(false) {
19}
20
21PreparedStatement::~PreparedStatement() {
22 if (!is_invalidated && success) {
23 assert(context);
24 context->RemovePreparedStatement(this);
25 }
26}
27
28unique_ptr<QueryResult> PreparedStatement::Execute(vector<Value> &values, bool allow_stream_result) {
29 if (!success) {
30 return make_unique<MaterializedQueryResult>("Attempting to execute an unsuccessfully prepared statement!");
31 }
32 if (is_invalidated) {
33 return make_unique<MaterializedQueryResult>(
34 "Cannot execute prepared statement: underlying database or connection has been destroyed");
35 }
36 assert(context);
37 return context->Execute(name, values, allow_stream_result, query);
38}
39