Statement Class Reference

#include <Pt/Db/Statement.h>

Prepared SQL statement with named host variables. More...

Public Types

typedef IStatement::size_type size_type
 Size type of this statement.

Public Member Functions

 Statement (IStatement *stmt=0)
 Takes ownership of the backend stmt.
Statementclear ()
 Sets all host variables to NULL.
StatementsetNull (const std::string &col)
 Sets the host variable col to NULL.
Statementset (const std::string &col, bool data)
 Sets the host variable col to data.
Statementset (const std::string &col, int data)
 Sets the host variable col to data.
Statementset (const std::string &col, unsigned data)
 Sets the host variable col to data.
Statementset (const std::string &col, float data)
 Sets the host variable col to data.
Statementset (const std::string &col, double data)
 Sets the host variable col to data.
Statementset (const std::string &col, char data)
 Sets the host variable col to data.
Statementset (const std::string &col, const std::string &data)
 Sets the host variable col to data.
Statementset (const std::string &col, const Blob &data)
 Sets the host variable col to data.
Statementset (const std::string &col, const char *data)
 Sets the host variable col to data, or to NULL if data is 0.
Statementset (const std::string &col, const Date &data)
 Sets the host variable col to data.
Statementset (const std::string &col, const Time &data)
 Sets the host variable col to data.
Statementset (const std::string &col, const DateTime &data)
 Sets the host variable col to data.
Cursor getCursor (size_type batchSize)
 Opens a batch cursor that fetches batchSize rows at a time.
void cancel ()
 Cancels any pending asynchronous operation.
size_type execute ()
 Executes the statement and returns the number of rows changed.
void beginExecute ()
 Starts asynchronous execution.
size_type endExecute ()
 Completes asynchronous execution.
SignalexecuteFinished ()
 Signal emitted when asynchronous execution completes.
long long lastInsertId () const
 Returns the generated row id of the last insert.
Result select ()
 Executes the statement and returns a buffered result.
Row selectRow ()
 Executes the statement and returns the first row.
Value selectValue ()
 Executes the statement and returns the first column of the first row.
void beginSelect ()
 Starts an asynchronous select.
Result endSelect ()
 Completes an asynchronous select.
SignalselectFinished ()
 Signal emitted when an asynchronous select completes.
bool operator! () const
 Returns true if this object is not bound to a statement.
const IStatementgetImpl () const
 Returns the backend implementation.
IStatementimpl ()
 Returns the backend implementation.

Detailed Description

Statement is the compiled query the group described. Obtain it from Connection::prepare() or Connection::prepareCached(). It is a shared value. An unbound statement is empty; operator!() is true then. The constructor that takes an IStatement takes ownership of that backend.

Host variables are :name. Bind them with set() before execute or select. setNull() binds SQL NULL. clear() sets every host variable to NULL. A const char pointer that is 0 is bound as NULL.

execute() runs a statement that does not return rows and returns how many rows changed. lastInsertId() is the generated id after that execute. select() returns a buffered Result. selectRow() returns the first row and discards the rest. selectValue() returns the first column of the first row. Both throw InvalidQuery when the query returns no row.

getCursor() opens a batch cursor on this statement. beginExecute() / endExecute() / executeFinished() and beginSelect() / endSelect() / selectFinished() are the async forms. cancel() aborts a pending operation.

Pt::Db::Statement stmt = conn.prepare(
"SELECT name FROM users WHERE id = :id");
stmt.set("id", 1);
Pt::Db::Value name = stmt.selectValue();
Prepared SQL statement with named host variables.
Definition Statement.h:65
Statement & set(const std::string &col, bool data)
Sets the host variable col to data.
Definition Statement.h:102
Value selectValue()
Executes the statement and returns the first column of the first row.
One column value of a query result.
Definition Value.h:35

Member Function Documentation

◆ clear()

Statement & clear ( )
Returns
This statement.

◆ setNull()

Statement & setNull ( const std::string & col)
Returns
This statement.

◆ set() [1/12]

Statement & set ( const std::string & col,
bool data )
Returns
This statement.

◆ set() [2/12]

Statement & set ( const std::string & col,
int data )
Returns
This statement.

◆ set() [3/12]

Statement & set ( const std::string & col,
unsigned data )
Returns
This statement.

◆ set() [4/12]

Statement & set ( const std::string & col,
float data )
Returns
This statement.

◆ set() [5/12]

Statement & set ( const std::string & col,
double data )
Returns
This statement.

◆ set() [6/12]

Statement & set ( const std::string & col,
char data )
Returns
This statement.

◆ set() [7/12]

Statement & set ( const std::string & col,
const std::string & data )
Returns
This statement.

◆ set() [8/12]

Statement & set ( const std::string & col,
const Blob & data )
Returns
This statement.

◆ set() [9/12]

Statement & set ( const std::string & col,
const char * data )
Returns
This statement.

◆ set() [10/12]

Statement & set ( const std::string & col,
const Date & data )
Returns
This statement.

◆ set() [11/12]

Statement & set ( const std::string & col,
const Time & data )
Returns
This statement.

◆ set() [12/12]

Statement & set ( const std::string & col,
const DateTime & data )
Returns
This statement.

◆ endExecute()

size_type endExecute ( )
Returns
Number of rows changed.

◆ selectRow()

Row selectRow ( )

Additional rows are discarded.

Exceptions
%InvalidQueryif the query returns no row.

◆ selectValue()

Value selectValue ( )
Exceptions
%InvalidQueryif the query returns no row.

◆ endSelect()

Result endSelect ( )
Returns
Buffered result of the query.