sptk2 logo
SPTK Home Page
Public Member Functions | Static Public Member Functions | Protected Member Functions | Friends | List of all members
sptk::Query Class Reference

#include <Query.h>

Inheritance diagram for sptk::Query:
sptk::QueryStatementManagement sptk::DataSource sptk::InsertQuery

Public Member Functions

 Query () noexcept
 
 Query (const DatabaseConnection &db, const String &sql="", bool autoPrepare=true)
 
 Query (PoolDatabaseConnection *db, const String &sql="", bool autoPrepare=true)
 
 Query (const Query &)=delete
 
Queryoperator= (const Query &)=delete
 
 ~Query () override
 
Fieldoperator[] (size_t fieldIndex) override
 
Fieldoperator[] (const String &fieldName) override
 
size_t fieldCount () const override
 
size_t recordCount () const override
 
virtual String sql () const
 
virtual void sql (const String &_sql)
 
FieldListfields ()
 
QueryParameterListparams ()
 
bool readField (const char *fname, Variant &value) override
 
bool writeField (const char *fname, const Variant &fvalue) override
 
bool open () override
 
bool close () override
 
bool next () override
 
virtual void exec ()
 
virtual void exec (const String &newSQL)
 
void fetch ()
 
size_t paramCount () const
 
QueryParameterparam (const char *paramName) const
 
QueryParameterparam (const String &paramName) const
 
QueryParameterparam (size_t paramIndex) const
 
- Public Member Functions inherited from sptk::QueryStatementManagement
 QueryStatementManagement (bool autoPrepare)
 
 QueryStatementManagement (const QueryStatementManagement &other)=delete
 
StmtHandle statement () const
 
bool autoPrepare () const
 
bool active () const
 
bool prepared () const
 
bool eof () const override
 
bool bulkMode () const
 
void connect (PoolDatabaseConnection *db)
 
void disconnect ()
 
PoolDatabaseConnectiondatabase () const
 
void database (PoolDatabaseConnection *db)
 
- Public Member Functions inherited from sptk::DataSource
 DataSource ()=default
 
virtual ~DataSource ()=default
 
virtual Fieldoperator[] (size_t fieldIndex)=0
 
virtual Fieldoperator[] (const String &fieldName)=0
 
virtual size_t fieldCount () const =0
 
virtual size_t recordCount () const =0
 
virtual bool readField (const char *fieldName, Variant &fieldValue)=0
 
virtual bool writeField (const char *fieldName, const Variant &fieldValue)=0
 
virtual bool open ()
 
virtual bool close ()
 
virtual bool first ()
 
virtual bool next ()
 
virtual bool prior ()
 
virtual bool last ()
 
virtual bool find (const String &, const Variant &)
 
virtual bool eof () const
 
bool load ()
 
bool save ()
 
void exportRowTo (const xdoc::SNode &node, bool compactXmlMode, bool nullLargeData=false)
 
virtual void exportTo (xdoc::Node &parentNode, const String &nodeName, bool compactXmlMode)
 

Static Public Member Functions

static void throwError (const String &method, const String &error)
 

Protected Member Functions

void execute ()
 
bool loadData () override
 
bool saveData () override
 
- Protected Member Functions inherited from sptk::QueryStatementManagement
void setDatabase (PoolDatabaseConnection *db)
 
void setStatement (StmtHandle statement)
 
void setPrepared (bool prepared)
 
void setActive (bool active)
 
void setEof (bool eof)
 
void setBulkMode (bool _bulkMode)
 
void closeStmt (bool freeStatement=false)
 
void closeQuery (bool releaseStatement=false)
 
Stringsmessages ()
 
StringgetSQL ()
 
const StringgetSQL () const
 
void setSQL (const String &sql)
 
void notImplemented (const String &functionName) const
 
virtual bool loadData ()
 
virtual bool saveData ()
 

Friends

class PoolDatabaseConnection
 
class PoolDatabaseConnectionQueryMethods
 

Detailed Description

Database query

A CDataset connected to the database to execute a database queries. The type of the database depends on the DatabaseConnection object query is connected to.

Constructor & Destructor Documentation

◆ Query() [1/4]

sptk::Query::Query ( )
noexcept

Default constructor

◆ Query() [2/4]

sptk::Query::Query ( const DatabaseConnection &  db,
const String sql = "",
bool  autoPrepare = true 
)
explicit

Constructor

You can optionally provide the name of the file and line number where this query is created. This is used to collect statistical information for the query calls. If file and line information is provided, then calls statistics is stored to the database object during the query dtor.

Parameters
dbThe database to connect to, optional
sqlThe SQL query text to use, optional
autoPrepareIf true then statement is auto-prepared before execution (if not yet prepared), otherwise it's called directly. Parameter binding is not available in not prepared statements.

◆ Query() [3/4]

sptk::Query::Query ( PoolDatabaseConnection db,
const String sql = "",
bool  autoPrepare = true 
)
explicit

Constructor

You can optionally provide the name of the file and line number where this query is created. This is used to collect statistical information for the query calls. If file and line information is provided, then calls statistics is stored to the database object during the query dtor.

Parameters
dbThe database to connect to, optional
sqlThe SQL query text to use, optional
autoPrepareIf true then statement is auto-prepared before execution (if not yet prepared), otherwise it's called directly. Parameter binding is not available in not prepared statements.

◆ Query() [4/4]

sptk::Query::Query ( const Query )
delete

Deleted copy constructor

◆ ~Query()

sptk::Query::~Query ( )
override

Destructor

Member Function Documentation

◆ close()

bool sptk::Query::close ( )
inlineoverridevirtual

Closes the query

Doesn't release the db statement, so the query may be called again, and faster than for the first time.

Reimplemented from sptk::DataSource.

◆ exec() [1/2]

virtual void sptk::Query::exec ( )
inlinevirtual

Executes the query and closes the statement.

Reimplemented in sptk::InsertQuery.

◆ exec() [2/2]

virtual void sptk::Query::exec ( const String newSQL)
inlinevirtual

Executes the query and closes the statement.

Query SQL would be set to the new SQL statement

Parameters
newSQLSQL statement to execute

Reimplemented in sptk::InsertQuery.

◆ execute()

void sptk::Query::execute ( )
protected

Executes a statement

◆ fetch()

void sptk::Query::fetch ( )

Fetches the next row from the recordset, same as next()

◆ fieldCount()

size_t sptk::Query::fieldCount ( ) const
inlineoverridevirtual

Returns field count in the recordset

Returns
field count

Implements sptk::DataSource.

◆ fields()

FieldList & sptk::Query::fields ( )
inline

Returns the query fields list

◆ loadData()

bool sptk::Query::loadData ( )
inlineoverrideprotectedvirtual

In CDataset it should load data into the dataset.

Since the query loads all the data by open() or fetch(), in Query this method does exactly nothing

Reimplemented from sptk::DataSource.

◆ next()

bool sptk::Query::next ( )
inlineoverridevirtual

Fetches the next row from the recordset, same as fetch()

Reimplemented from sptk::DataSource.

◆ open()

bool sptk::Query::open ( )
overridevirtual

Opens the query and fetches the first row.

It is necessary for the select queries and some stored procedures that may return the dataset. First-time call for open() also prepares the query.

Reimplemented from sptk::DataSource.

◆ operator[]() [1/2]

Field & sptk::Query::operator[] ( const String fieldName)
inlineoverridevirtual

Field access by field name.

Implements sptk::DataSource.

◆ operator[]() [2/2]

Field & sptk::Query::operator[] ( size_t  fieldIndex)
inlineoverridevirtual

Field access by number, const version

Field index should be inside 0..fieldCount()-1

Parameters
fieldIndexField index

Implements sptk::DataSource.

◆ param() [1/3]

QueryParameter & sptk::Query::param ( const char *  paramName) const
inline

Returns the parameter by the name.

If the parameter name isn't found, throws an exception

Parameters
paramNameconst char *, parameter name
Returns
parameter
See also
CParamList

◆ param() [2/3]

QueryParameter & sptk::Query::param ( const String paramName) const
inline

Returns the parameter by the name.

If the parameter name isn't found, throws an exception

Parameters
paramNameParameter name
Returns
parameter reference

◆ param() [3/3]

QueryParameter & sptk::Query::param ( size_t  paramIndex) const
inline

Returns the parameter by the index.

Parameters
paramIndexParameter index
Returns
parameter reference

◆ paramCount()

size_t sptk::Query::paramCount ( ) const
inline

Reports the number of unique parameters in the query.

Makes sense after the SQL query text is set.

◆ params()

QueryParameterList & sptk::Query::params ( )
inline

Returns the query parameters list

◆ readField()

bool sptk::Query::readField ( const char *  fname,
Variant value 
)
overridevirtual

Field read access by the field name, for the universal data connection

Implements sptk::DataSource.

◆ recordCount()

size_t sptk::Query::recordCount ( ) const
inlineoverridevirtual

Reports the record count for the recordset, returned by the open() method.

Currently is NOT implemented.

Implements sptk::DataSource.

◆ saveData()

bool sptk::Query::saveData ( )
inlineoverrideprotectedvirtual

In CDataset it should save data into the dataset.

Since the query saves all the data by execute, in Query this method does exactly nothing

Reimplemented from sptk::DataSource.

◆ sql() [1/2]

virtual String sptk::Query::sql ( ) const
inlinevirtual

Returns the text of current SQL query as String

Reimplemented in sptk::InsertQuery.

◆ sql() [2/2]

virtual void sptk::Query::sql ( const String _sql)
virtual

Sets SQL Query text. If the Query text is not the same and the db statement was prepared earlier then the db statement is released and new one is created.

Reimplemented in sptk::InsertQuery.

◆ throwError()

static void sptk::Query::throwError ( const String method,
const String error 
)
static

Throws an exception

Before exception is thrown, it is logged into the logfile (if the logfile is defined)

Parameters
methodMethod name where error has occured
errorError text

◆ writeField()

bool sptk::Query::writeField ( const char *  fname,
const Variant fvalue 
)
overridevirtual

Field write access by the field name, for the universal data connection

Implements sptk::DataSource.


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

Fri Oct 14 2022 09:58:44: SPTK 5.4.1