lintel 0.2.0
Loading...
Searching...
No Matches
Classes | Public Member Functions | Static Public Member Functions | List of all members
lintel::postgresql::PreparedStatement Class Reference

#include <PreparedStatement.h>

Classes

struct  Definition
 

Public Member Functions

 PreparedStatement (const Connection &connection, const Definition &definition)
 
 ~PreparedStatement () noexcept
 
std::shared_ptr< Result > execute (const std::vector< std::string > &params)
 
void close ()
 

Static Public Member Functions

static constexpr int32_t initNParams (std::string_view tempStatement)
 
static std::string initStatement (const std::string &tempStatement)
 

Detailed Description

format of prepared statement example: insert into table (..,..) values(?,?);

=> ? will be replace automatically with $1 and $2 => algorithm will automatically count the number of parameters

Constructor & Destructor Documentation

◆ PreparedStatement()

lintel::postgresql::PreparedStatement::PreparedStatement ( const Connection &  connection,
const Definition &  definition 
)

Prepares a statement on the given connection.

Parameters
connectionthe PostgreSQL connection
definitionthe statement name and SQL template

◆ ~PreparedStatement()

lintel::postgresql::PreparedStatement::~PreparedStatement ( )
noexcept

Destroys the object and deallocates the server-side prepared statement.

A pooled connection is reused, so a prepared statement that survived the release would still be visible to the next lease (and a name collision would make the next prepare fail). The destructor deallocates it, and because destructors must not throw it swallows the failure of a connection that is already gone - the pool's health check drops such a connection anyway.

The statement stays named rather than unnamed (PQprepare with ""): the parameterized query path (Statement::execute(query, builder)) uses PQexecParams, which also targets the unnamed statement, so an unnamed prepared statement would be clobbered between its prepare and execute. Deallocating in the destructor is the deterministic equivalent for reuse.

Member Function Documentation

◆ close()

void lintel::postgresql::PreparedStatement::close ( )

Closes the prepared statement.

◆ execute()

std::shared_ptr< Result > lintel::postgresql::PreparedStatement::execute ( const std::vector< std::string > &  params)

Executes the prepared statement with the given parameters.

Parameters
paramsthe parameter values as strings
Returns
shared pointer to the result

◆ initNParams()

static constexpr int32_t lintel::postgresql::PreparedStatement::initNParams ( std::string_view  tempStatement)
inlinestaticconstexpr

Counts the number of '?' placeholders in a statement template.

Parameters
tempStatementthe statement template
Returns
the number of parameters

◆ initStatement()

static std::string lintel::postgresql::PreparedStatement::initStatement ( const std::string &  tempStatement)
static

Converts '?' placeholders to PostgreSQL's $1, $2, ... format.

Parameters
tempStatementthe statement template with '?' placeholders
Returns
the converted statement

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