PSqlQuery Class Reference

Detailed Description

The PSqlQuery class represents a SQL query and can execute and manipulate SQL statements.

This class is intended to be used with PSqlDatabase.

Executing a Query:

To execute an SQL statement, simply create a PSqlQuery object and call PSqlQuery::Exec() like this:

PSqlQuery query(myDatabase);
PResult ret = query.Exec("SELECT name, age FROM person WHERE age > 18");
if (ret.Ok())
{
...
}
else
{
P_LOG_ERROR << ret;
}

If an error occurs, Exec() returns a PResult with an error message.

Navigating the Result Set:

PSqlQuery provides access to the result set one record at a time. After the call to Exec(), PSqlQuery's internal pointer is located one position before the first record. You must call PSqlQuery::Next() once to advance to the first record, then Next() again repeatedly to access the other records, until it returns false. Here's a typical loop that iterates over all the records in order:

PString name;
PInt32 age;
while (query.Next())
{
if (query.GetByIndex(0, name).Ok() && query.GetByIndex(1, age).Ok())
{
P_LOG_INFO << "Name=" << name << " age=" << age;
}
}

Definition at line 66 of file PSqlQuery.h.

#include <PSqlQuery.h>

Constructor & Destructor Documentation

◆ PSqlQuery() [1/3]

PSqlQuery::PSqlQuery ( const PSqlDatabase db,
bool  forwardOnly = true 
)

Constructs a PSqlQuery object attached to the specified database; by default creates a "forward only" query which is faster than a general query.

◆ PSqlQuery() [2/3]

PSqlQuery::PSqlQuery ( const PSqlDatabase db,
const PString query,
bool  forwardOnly = true 
)

Constructs a PSqlQuery object attached to the specified database and executes the specified query; by default the query is "forward only".

◆ PSqlQuery() [3/3]

PSqlQuery::PSqlQuery ( const PSqlQuery other)

Constructs a shared copy of other.

◆ ~PSqlQuery()

virtual PSqlQuery::~PSqlQuery ( )
virtual

Destroys this object.

Member Function Documentation

◆ BindValue()

PResult PSqlQuery::BindValue ( const PString placeholder,
const PObject value 
)

Sets the placeholder placeholder to be bound to value val in the prepared statement.

Note that the placeholder mark (e.g :) must be included when specifying the placeholder name. Returns PResult::C_OK if success, returns another result otherwise.

◆ Exec() [1/2]

PResult PSqlQuery::Exec ( )

Executes a previously prepared SQL query.

Returns PResult::C_OK if the query executed successfully, returns another result otherwise.

◆ Exec() [2/2]

PResult PSqlQuery::Exec ( const PString query)

Executes the SQL in query.

Returns PResult::C_OK if the query executed successfully, returns another result otherwise.

◆ First()

PResult PSqlQuery::First ( )

Retrieves the first record in the result, if available, and positions the query on the retrieved record (it works only if the query is NOT "forward only", see PQuery constructor).

◆ Get() [1/2]

PResult PSqlQuery::Get ( int32  n,
PObject value 
) const

DEPRECATED, use GetByIndex() instead.

Gets the nth field of the current record. Returns PResult::C_OK if success, another value otherwise.

◆ Get() [2/2]

PResult PSqlQuery::Get ( const PString name,
PObject value 
) const

DEPRECATED, use GetByName() instead.

Gets the value of the field named 'name' in the current record. Returns PResult::C_OK if success, another value otherwise.

◆ GetByIndex()

PResult PSqlQuery::GetByIndex ( int32  n,
PObject value 
) const

Gets the nth field of the current record.

Returns PResult::C_OK if success, another value otherwise.

◆ GetByName()

PResult PSqlQuery::GetByName ( const PString name,
PObject value 
) const

Gets the value of the field named 'name' in the current record.

Returns PResult::C_OK if success, another value otherwise.

◆ GetNumRowsAffected()

papillon::int32 PSqlQuery::GetNumRowsAffected ( ) const

Returns the number of rows affected by the result's SQL statement, or -1 if it cannot be determined.

Note that for SELECT statements, the value is undefined. If the query is not active, -1 is returned.

◆ IsForwardOnly()

bool PSqlQuery::IsForwardOnly ( ) const

Returns true if the query is forward only (you cannot use Previous() or First() functions), false otherwise.

◆ IsValid()

bool PSqlQuery::IsValid ( ) const

Returns true if the query is currently positioned on a valid record; otherwise returns false.

◆ Last()

PResult PSqlQuery::Last ( )

Retrieves the last record in the result, if available, and positions the query on the retrieved record.

◆ Next()

PResult PSqlQuery::Next ( )

Retrieves the next record in the result, if available, and positions the query on the retrieved record.

◆ operator=()

PSqlQuery& PSqlQuery::operator= ( const PSqlQuery other)

Performs a shared copy of other to this object.

◆ Prepare()

PResult PSqlQuery::Prepare ( const PString query)

Prepares the SQL query query for execution.

Returns PResult::C_OK if the query is prepared successfully, returns another result otherwise.

◆ Previous()

PResult PSqlQuery::Previous ( )

Retrieves the previous record in the result, if available, and positions the query on the retrieved record (it works only if the query is NOT "forward only", see PQuery constructor).

◆ Size()

papillon::int32 PSqlQuery::Size ( ) const

Returns the size of the result (number of rows returned), or -1 if the size cannot be determined or if the database does not support reporting information about query sizes.