#include <preparedstatement.h>

Inheritance diagram for KexiDB::PreparedStatement:
KexiDB::MySqlPreparedStatement KexiDB::SQLitePreparedStatement KexiDB::pqxxPreparedStatement

Public Types

enum  StatementType { SelectStatement , InsertStatement }
 
typedef TDESharedPtr< PreparedStatement > Ptr
 

Public Member Functions

 PreparedStatement (StatementType type, ConnectionInternal &conn, FieldList &fields, const TQStringList &where=TQStringList())
 
PreparedStatement & operator<< (const TQVariant &value)
 
void clearArguments ()
 
virtual bool execute ()=0
 

Protected Member Functions

TQCString generateStatementString ()
 

Protected Attributes

StatementType m_type
 
FieldList * m_fields
 
TQValueList< TQVariant > m_args
 
TQStringList * m_where
 
Field::List * m_whereFields
 

Detailed Description

Prepared database command for optimizing sequences of multiple database actions.

Currently INSERT and SELECT statements are supported. For example, wher using PreparedStatement for INSERTs, you can gain about 30% speedup compared to using multiple connection.insertRecord(*tabelSchema, dbRowBuffer).

To use PreparedStatement, create is using KexiDB::Connection:prepareStatement(), providing table schema; set up arguments using operator << ( const TQVariant& value ); and call execute() when ready. PreparedStatement objects are accessed using KDE shared pointers, i.e KexiDB::PreparedStatement::Ptr, so you do not need to remember about destroying them. However, when underlying Connection object is destroyed, PreparedStatement should not be used.

Let's assume tableSchema contains two columns NUMBER integer and TEXT text. Following code inserts 10000 records with random numbers and text strings obtained elsewhere using getText(i).

bool insertMultiple(KexiDB::Connection &conn, KexiDB::TableSchema& tableSchema)
{
KexiDB::PreparedStatement::Ptr prepared = conn.prepareStatement(
for (i=0; i<10000; i++) {
prepared << rand() << getText(i);
if (!prepared.execute())
return false;
prepared.clearArguments();
}
return true;
}
Provides database connection, allowing queries and data modification.
Definition connection.h:62
virtual PreparedStatement::Ptr prepareStatement(PreparedStatement::StatementType type, FieldList &fields)=0
@ InsertStatement
INSERT statement will be prepared end executed.

If you do not call clearArguments() after every insert, you can insert the same value multiple times using execute() what increases efficiency even more.

Another use case is inserting large objects (BLOBs or CLOBs). Depending on database backend, you can avoid escaping BLOBs. See KexiFormView::storeData() for example use.

Definition at line 73 of file preparedstatement.h.

Member Typedef Documentation

◆ Ptr

typedef TDESharedPtr<PreparedStatement> KexiDB::PreparedStatement::Ptr

Definition at line 76 of file preparedstatement.h.

Member Enumeration Documentation

◆ StatementType

Defines type of the prepared statement.

Enumerator
SelectStatement 

SELECT statement will be prepared end executed.

InsertStatement 

INSERT statement will be prepared end executed.

Definition at line 79 of file preparedstatement.h.

Constructor & Destructor Documentation

◆ PreparedStatement()

PreparedStatement::PreparedStatement ( StatementType  type,
ConnectionInternal &  conn,
FieldList &  fields,
const TQStringList &  where = TQStringList() 
)

Creates Prepared statement. In your code use KexiDB::Connection:prepareStatement() instead.

Definition at line 28 of file preparedstatement.cpp.

◆ ~PreparedStatement()

PreparedStatement::~PreparedStatement ( )
virtual

Definition at line 39 of file preparedstatement.cpp.

Member Function Documentation

◆ clearArguments()

void PreparedStatement::clearArguments ( )

Clears arguments of the prepared statement. Usually used after execute()

Definition at line 132 of file preparedstatement.cpp.

◆ execute()

virtual bool KexiDB::PreparedStatement::execute ( )
pure virtual

Executes the prepared statement. In most cases you will need to clear arguments after executing, using clearArguments(). A number arguments set up for the statement must be the same as a number of fields defined in the underlying database table.

Returns
false on failure. Detailed error status can be obtained from KexiDB::Connection object used to create this statement.

Implemented in KexiDB::MySqlPreparedStatement, KexiDB::pqxxPreparedStatement, and KexiDB::SQLitePreparedStatement.

◆ generateStatementString()

TQCString PreparedStatement::generateStatementString ( )
protected
Todo:
is this portable across backends?
Todo:
only tables and trivial queries supported for select...
Todo:
only tables supported for insert; what about views?

Definition at line 45 of file preparedstatement.cpp.

◆ operator<<()

PreparedStatement & PreparedStatement::operator<< ( const TQVariant &  value)

Appends argument value to the statement.

Definition at line 113 of file preparedstatement.cpp.

Member Data Documentation

◆ m_args

TQValueList<TQVariant> KexiDB::PreparedStatement::m_args
protected

Definition at line 110 of file preparedstatement.h.

◆ m_fields

FieldList* KexiDB::PreparedStatement::m_fields
protected

Definition at line 109 of file preparedstatement.h.

◆ m_type

StatementType KexiDB::PreparedStatement::m_type
protected

Definition at line 108 of file preparedstatement.h.

◆ m_where

TQStringList* KexiDB::PreparedStatement::m_where
protected

Definition at line 111 of file preparedstatement.h.

◆ m_whereFields

Field::List* KexiDB::PreparedStatement::m_whereFields
protected

Definition at line 112 of file preparedstatement.h.


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