Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/duckdb/src/main/client_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,26 @@ struct ActiveQueryContext {
BaseQueryResult *open_result = nullptr;
};

//! RAII wrapper that ensures the active query is reset if an exception occurs during preparation
struct ActiveQueryGuard {
unique_ptr<ActiveQueryContext> &active_query;
bool set_active_query;

ActiveQueryGuard(unique_ptr<ActiveQueryContext> &active_query_p, const string &query)
: active_query(active_query_p), set_active_query(false) {
if (!active_query) {
active_query = make_uniq<ActiveQueryContext>();
set_active_query = true;
active_query->query = query;
}
}
~ActiveQueryGuard() {
if (set_active_query) {
active_query.reset();
}
}
};

#ifdef DEBUG
struct DebugClientContextState : public ClientContextState {
~DebugClientContextState() override {
Expand Down Expand Up @@ -711,7 +731,11 @@ unique_ptr<PreparedStatement> ClientContext::PrepareInternal(ClientContextLock &
shared_ptr<PreparedStatementData> prepared_data;
auto unbound_statement = statement->Copy();
RunFunctionInTransactionInternal(
lock, [&]() { prepared_data = CreatePreparedStatement(lock, statement_query, std::move(statement), {}); },
lock,
[&]() {
ActiveQueryGuard guard(active_query, statement_query);
prepared_data = CreatePreparedStatement(lock, statement_query, std::move(statement), {});
},
false);
prepared_data->unbound_statement = std::move(unbound_statement);
return make_uniq<PreparedStatement>(shared_from_this(), std::move(prepared_data), std::move(statement_query),
Expand Down