Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::PreparedStatement Class Reference

A prepared statement. More...

#include <duckdb.hpp>

Collaboration diagram for duckdb::PreparedStatement:

Public Member Functions

DUCKDB_API PreparedStatement (shared_ptr< ClientContext > context, shared_ptr< PreparedStatementData > data, string query, case_insensitive_map_t< idx_t > named_param_map)
 Create a successfully prepared prepared statement object with the given name.
DUCKDB_API PreparedStatement (ErrorData error)
 Create a prepared statement that was not successfully prepared.
DUCKDB_API const string & GetError ()
 Returns the stored error message.
DUCKDB_API ErrorData & GetErrorObject ()
 Returns the stored error object.
DUCKDB_API bool HasError () const
 Returns whether or not an error occurred.
DUCKDB_API idx_t ColumnCount ()
 Returns the number of columns in the result.
DUCKDB_API StatementType GetStatementType ()
 Returns the statement type of the underlying prepared statement object.
DUCKDB_API StatementProperties GetStatementProperties ()
 Returns the underlying statement properties.
DUCKDB_API const vector< LogicalType > & GetTypes ()
 Returns the result SQL types of the prepared statement.
DUCKDB_API const vector< string > & GetNames ()
 Returns the result names of the prepared statement.
DUCKDB_API case_insensitive_map_t< LogicalType > GetExpectedParameterTypes () const
 Returns the map of parameter index to the expected type of parameter.
template<typename... ARGS>
unique_ptr< PendingQueryResult > PendingQuery (ARGS... args)
 Create a pending query result of the prepared statement with the given set of arguments.
DUCKDB_API unique_ptr< PendingQueryResult > PendingQuery (vector< Value > &values, bool allow_stream_result=true)
 Create a pending query result of the prepared statement with the given set of arguments.
DUCKDB_API unique_ptr< PendingQueryResult > PendingQuery (case_insensitive_map_t< BoundParameterData > &named_values, bool allow_stream_result=true)
 Create a pending query result of the prepared statement with the given set named arguments.
DUCKDB_API unique_ptr< QueryResult > Execute (vector< Value > &values, bool allow_stream_result=true)
 Execute the prepared statement with the given set of values.
DUCKDB_API unique_ptr< QueryResult > Execute (case_insensitive_map_t< BoundParameterData > &named_values, bool allow_stream_result=true)
 Execute the prepared statement with the given set of named+unnamed values.
template<typename... ARGS>
unique_ptr< QueryResult > Execute (ARGS... args)
 Execute the prepared statement with the given set of arguments.

Static Public Member Functions

template<class PAYLOAD>
static string ExcessValuesException (const case_insensitive_map_t< idx_t > &parameters, const case_insensitive_map_t< PAYLOAD > &values)
template<class PAYLOAD>
static string MissingValuesException (const case_insensitive_map_t< idx_t > &parameters, const case_insensitive_map_t< PAYLOAD > &values)
template<class PAYLOAD>
static void VerifyParameters (const case_insensitive_map_t< PAYLOAD > &provided, const case_insensitive_map_t< idx_t > &expected)
static bool CanCachePlan (const LogicalOperator &op)
 Returns whether or not we can / want to cache a logical plan.

Public Attributes

shared_ptr< ClientContext > context
 The client context this prepared statement belongs to.
shared_ptr< PreparedStatementData > data
 The prepared statement data.
string query
 The query that is being prepared.
bool success
 Whether or not the statement was successfully prepared.
ErrorData error
 The error message (if success = false).
case_insensitive_map_t< idx_t > named_param_map
 The parameter mapping.

Private Member Functions

unique_ptr< PendingQueryResult > PendingQueryRecursive (vector< Value > &values)
template<typename T, typename... ARGS>
unique_ptr< PendingQueryResult > PendingQueryRecursive (vector< Value > &values, T value, ARGS... args)
unique_ptr< QueryResult > ExecuteRecursive (vector< Value > &values)
template<typename T, typename... ARGS>
unique_ptr< QueryResult > ExecuteRecursive (vector< Value > &values, T value, ARGS... args)

Detailed Description

A prepared statement.

Constructor & Destructor Documentation

◆ PreparedStatement() [1/2]

duckdb::PreparedStatement::PreparedStatement ( shared_ptr< ClientContext > context,
shared_ptr< PreparedStatementData > data,
string query,
case_insensitive_map_t< idx_t > named_param_map )

Create a successfully prepared prepared statement object with the given name.

294362 : context(std::move(context)), data(std::move(data_p)), query(std::move(query)), success(true),
294363 named_param_map(std::move(named_param_map_p)) {
294364 D_ASSERT(data || !success);
294365}
shared_ptr< ClientContext > context
The client context this prepared statement belongs to.
Definition duckdb.hpp:27286
case_insensitive_map_t< idx_t > named_param_map
The parameter mapping.
Definition duckdb.hpp:27296
string query
The query that is being prepared.
Definition duckdb.hpp:27290
bool success
Whether or not the statement was successfully prepared.
Definition duckdb.hpp:27292
shared_ptr< PreparedStatementData > data
The prepared statement data.
Definition duckdb.hpp:27288

◆ PreparedStatement() [2/2]

duckdb::PreparedStatement::PreparedStatement ( ErrorData error)
explicit

Create a prepared statement that was not successfully prepared.

294367 : context(nullptr), success(false), error(std::move(error)) {
294368}
ErrorData error
The error message (if success = false).
Definition duckdb.hpp:27294

◆ ~PreparedStatement()

duckdb::PreparedStatement::~PreparedStatement ( )
294370 {
294371}

Member Function Documentation

◆ GetError()

const string & duckdb::PreparedStatement::GetError ( )

Returns the stored error message.

294373 {
294374 D_ASSERT(HasError());
294375 return error.Message();
294376}
DUCKDB_API bool HasError() const
Returns whether or not an error occurred.
Definition duckdb.cpp:294382
Here is the call graph for this function:

◆ GetErrorObject()

ErrorData & duckdb::PreparedStatement::GetErrorObject ( )

Returns the stored error object.

294378 {
294379 return error;
294380}

◆ HasError()

bool duckdb::PreparedStatement::HasError ( ) const

Returns whether or not an error occurred.

294382 {
294383 return !success;
294384}
Here is the caller graph for this function:

◆ ColumnCount()

idx_t duckdb::PreparedStatement::ColumnCount ( )

Returns the number of columns in the result.

294386 {
294387 D_ASSERT(data);
294388 return data->types.size();
294389}

◆ GetStatementType()

StatementType duckdb::PreparedStatement::GetStatementType ( )

Returns the statement type of the underlying prepared statement object.

294391 {
294392 D_ASSERT(data);
294393 return data->statement_type;
294394}

◆ GetStatementProperties()

StatementProperties duckdb::PreparedStatement::GetStatementProperties ( )

Returns the underlying statement properties.

294396 {
294397 D_ASSERT(data);
294398 return data->properties;
294399}

◆ GetTypes()

const vector< LogicalType > & duckdb::PreparedStatement::GetTypes ( )

Returns the result SQL types of the prepared statement.

294401 {
294402 D_ASSERT(data);
294403 return data->types;
294404}

◆ GetNames()

const vector< string > & duckdb::PreparedStatement::GetNames ( )

Returns the result names of the prepared statement.

294406 {
294407 D_ASSERT(data);
294408 return data->names;
294409}

◆ GetExpectedParameterTypes()

case_insensitive_map_t< LogicalType > duckdb::PreparedStatement::GetExpectedParameterTypes ( ) const

Returns the map of parameter index to the expected type of parameter.

294411 {
294412 D_ASSERT(data);
294413 case_insensitive_map_t<LogicalType> expected_types(data->value_map.size());
294414 for (auto &it : data->value_map) {
294415 auto &identifier = it.first;
294416 D_ASSERT(data->value_map.count(identifier));
294417 D_ASSERT(it.second);
294418 expected_types[identifier] = it.second->GetValue().type();
294419 }
294420 return expected_types;
294421}

◆ PendingQuery() [1/3]

template<typename... ARGS>
unique_ptr< PendingQueryResult > duckdb::PreparedStatement::PendingQuery ( ARGS... args)
inline

Create a pending query result of the prepared statement with the given set of arguments.

27320 {
27321 vector<Value> values;
27322 return PendingQueryRecursive(values, args...);
27323 }
Here is the caller graph for this function:

◆ PendingQuery() [2/3]

unique_ptr< PendingQueryResult > duckdb::PreparedStatement::PendingQuery ( vector< Value > & values,
bool allow_stream_result = true )

Create a pending query result of the prepared statement with the given set of arguments.

294455 {
294456 case_insensitive_map_t<BoundParameterData> named_values;
294457 for (idx_t i = 0; i < values.size(); i++) {
294458 auto &val = values[i];
294459 named_values[std::to_string(i + 1)] = BoundParameterData(val);
294460 }
294461 return PendingQuery(named_values, allow_stream_result);
294462}
unique_ptr< PendingQueryResult > PendingQuery(ARGS... args)
Create a pending query result of the prepared statement with the given set of arguments.
Definition duckdb.hpp:27320
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
Here is the call graph for this function:

◆ PendingQuery() [3/3]

unique_ptr< PendingQueryResult > duckdb::PreparedStatement::PendingQuery ( case_insensitive_map_t< BoundParameterData > & named_values,
bool allow_stream_result = true )

Create a pending query result of the prepared statement with the given set named arguments.

294465 {
294466 if (!success) {
294467 auto exception = InvalidInputException("Attempting to execute an unsuccessfully prepared statement!");
294468 return make_uniq<PendingQueryResult>(ErrorData(exception));
294469 }
294470 PendingQueryParameters parameters;
294471 parameters.parameters = &named_values;
294472
294473 try {
294474 VerifyParameters(named_values, named_param_map);
294475 } catch (const std::exception &ex) {
294476 return make_uniq<PendingQueryResult>(ErrorData(ex));
294477 }
294478
294479 D_ASSERT(data);
294480 parameters.query_parameters.output_type =
294481 allow_stream_result && data->properties.output_type == QueryResultOutputType::ALLOW_STREAMING
294482 ? QueryResultOutputType::ALLOW_STREAMING
294483 : QueryResultOutputType::FORCE_MATERIALIZED;
294484 auto result = context->PendingQuery(query, data, parameters);
294485 // The result should not contain any reference to the 'vector<Value> parameters.parameters'
294486 return result;
294487}

◆ Execute() [1/3]

unique_ptr< QueryResult > duckdb::PreparedStatement::Execute ( vector< Value > & values,
bool allow_stream_result = true )

Execute the prepared statement with the given set of values.

294447 {
294448 case_insensitive_map_t<BoundParameterData> named_values;
294449 for (idx_t i = 0; i < values.size(); i++) {
294450 named_values[std::to_string(i + 1)] = BoundParameterData(values[i]);
294451 }
294452 return Execute(named_values, allow_stream_result);
294453}
DUCKDB_API unique_ptr< QueryResult > Execute(vector< Value > &values, bool allow_stream_result=true)
Execute the prepared statement with the given set of values.
Definition duckdb.cpp:294447
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Execute() [2/3]

unique_ptr< QueryResult > duckdb::PreparedStatement::Execute ( case_insensitive_map_t< BoundParameterData > & named_values,
bool allow_stream_result = true )

Execute the prepared statement with the given set of named+unnamed values.

294424 {
294425 if (!success) {
294426 return make_uniq<MaterializedQueryResult>(
294427 ErrorData(InvalidInputException("Attempting to execute an unsuccessfully prepared statement!")));
294428 }
294429
294430 try {
294431 VerifyParameters(named_values, named_param_map);
294432 } catch (const std::exception &ex) {
294433 return make_uniq<MaterializedQueryResult>(ErrorData(ex));
294434 }
294435
294436 PendingQueryParameters parameters;
294437 parameters.parameters = &named_values;
294438 D_ASSERT(data);
294439 parameters.query_parameters.output_type =
294440 allow_stream_result && data->properties.output_type == QueryResultOutputType::ALLOW_STREAMING
294441 ? QueryResultOutputType::ALLOW_STREAMING
294442 : QueryResultOutputType::FORCE_MATERIALIZED;
294443
294444 return context->Execute(query, data, parameters);
294445}

◆ Execute() [3/3]

template<typename... ARGS>
unique_ptr< QueryResult > duckdb::PreparedStatement::Execute ( ARGS... args)
inline

Execute the prepared statement with the given set of arguments.

27341 {
27342 vector<Value> values;
27343 return ExecuteRecursive(values, args...);
27344 }

◆ ExcessValuesException()

template<class PAYLOAD>
string duckdb::PreparedStatement::ExcessValuesException ( const case_insensitive_map_t< idx_t > & parameters,
const case_insensitive_map_t< PAYLOAD > & values )
inlinestatic
27348 {
27349 // Too many values
27350 set<string> excess_set;
27351 for (auto &pair : values) {
27352 auto &name = pair.first;
27353 if (!parameters.count(name)) {
27354 excess_set.insert(name);
27355 }
27356 }
27357 vector<string> excess_values;
27358 for (auto &val : excess_set) {
27359 excess_values.push_back(val);
27360 }
27361 return StringUtil::Format("Parameter argument/count mismatch, identifiers of the excess parameters: %s",
27362 StringUtil::Join(excess_values, ", "));
27363 }
static string Format(const string fmt_str, ARGS... params)
Format a string using printf semantics.
Definition duckdb.hpp:4002
static DUCKDB_API string Join(const vector< string > &input, const string &separator)
Join multiple strings into one string. Components are concatenated by the given separator.
Definition duckdb.cpp:109497

◆ MissingValuesException()

template<class PAYLOAD>
string duckdb::PreparedStatement::MissingValuesException ( const case_insensitive_map_t< idx_t > & parameters,
const case_insensitive_map_t< PAYLOAD > & values )
inlinestatic
27367 {
27368 // Missing values
27369 set<string> missing_set;
27370 for (auto &pair : parameters) {
27371 auto &name = pair.first;
27372 if (!values.count(name)) {
27373 missing_set.insert(name);
27374 }
27375 }
27376 vector<string> missing_values;
27377 for (auto &val : missing_set) {
27378 missing_values.push_back(val);
27379 }
27380 return StringUtil::Format("Values were not provided for the following prepared statement parameters: %s",
27381 StringUtil::Join(missing_values, ", "));
27382 }

◆ VerifyParameters()

template<class PAYLOAD>
void duckdb::PreparedStatement::VerifyParameters ( const case_insensitive_map_t< PAYLOAD > & provided,
const case_insensitive_map_t< idx_t > & expected )
inlinestatic
27386 {
27387 if (expected.size() == provided.size()) {
27388 // Same amount of identifiers, if
27389 for (auto &pair : expected) {
27390 auto &identifier = pair.first;
27391 if (!provided.count(identifier)) {
27392 throw InvalidInputException(MissingValuesException(expected, provided));
27393 }
27394 }
27395 return;
27396 }
27397 // Mismatch in expected and provided parameters/values
27398 if (expected.size() > provided.size()) {
27399 throw InvalidInputException(MissingValuesException(expected, provided));
27400 } else {
27401 D_ASSERT(provided.size() > expected.size());
27402 throw InvalidInputException(ExcessValuesException(expected, provided));
27403 }
27404 }

◆ CanCachePlan()

bool duckdb::PreparedStatement::CanCachePlan ( const LogicalOperator & op)
static

Returns whether or not we can / want to cache a logical plan.

294489 {
294490 vector<const_reference<LogicalOperator>> operators;
294491 operators.push_back(root);
294492
294493 for (idx_t i = 0; i < operators.size(); i++) {
294494 auto &op = operators[i].get();
294495 switch (op.type) {
294496 case LogicalOperatorType::LOGICAL_GET:
294497 // this operator prevents caching
294498 return false;
294499 default:
294500 break;
294501 }
294502 // investigate the children of this operator
294503 for (auto &child : op.children) {
294504 operators.push_back(*child);
294505 }
294506 }
294507 return true;
294508}

◆ PendingQueryRecursive() [1/2]

unique_ptr< PendingQueryResult > duckdb::PreparedStatement::PendingQueryRecursive ( vector< Value > & values)
inlineprivate
27410 {
27411 return PendingQuery(values);
27412 }

◆ PendingQueryRecursive() [2/2]

template<typename T, typename... ARGS>
unique_ptr< PendingQueryResult > duckdb::PreparedStatement::PendingQueryRecursive ( vector< Value > & values,
T value,
ARGS... args )
inlineprivate
27415 {
27416 values.push_back(Value::CreateValue<T>(value));
27417 return PendingQueryRecursive(values, args...);
27418 }

◆ ExecuteRecursive() [1/2]

unique_ptr< QueryResult > duckdb::PreparedStatement::ExecuteRecursive ( vector< Value > & values)
inlineprivate
27420 {
27421 return Execute(values);
27422 }

◆ ExecuteRecursive() [2/2]

template<typename T, typename... ARGS>
unique_ptr< QueryResult > duckdb::PreparedStatement::ExecuteRecursive ( vector< Value > & values,
T value,
ARGS... args )
inlineprivate
27425 {
27426 values.push_back(Value::CreateValue<T>(value));
27427 return ExecuteRecursive(values, args...);
27428 }

The documentation for this class was generated from the following files: