kexi
KexiDB::PreparedStatement Class Reference
#include <preparedstatement.h>
Inheritance diagram for KexiDB::PreparedStatement:

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 QVariant& 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( KexiDB::PreparedStatement::InsertStatement, tableSchema); for (i=0; i<10000; i++) { prepared << rand() << getText(i); if (!prepared.execute()) return false; prepared.clearArguments(); } return true; }
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.
Public Types | |
enum | StatementType { SelectStatement, InsertStatement } |
typedef KSharedPtr< PreparedStatement > | Ptr |
Public Member Functions | |
PreparedStatement (StatementType type, ConnectionInternal &conn, FieldList &fields, const QStringList &where=QStringList()) | |
virtual | ~PreparedStatement () |
PreparedStatement & | operator<< (const QVariant &value) |
void | clearArguments () |
virtual bool | execute ()=0 |
Protected Member Functions | |
QCString | generateStatementString () |
Protected Attributes | |
StatementType | m_type |
FieldList * | m_fields |
QValueList< QVariant > | m_args |
QStringList * | m_where |
Field::List * | m_whereFields |
Member Enumeration Documentation
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 | ( | StatementType | type, | |
ConnectionInternal & | conn, | |||
FieldList & | fields, | |||
const QStringList & | where = QStringList() | |||
) |
Creates Prepared statement. In your code use KexiDB::Connection:prepareStatement() instead.
Definition at line 28 of file preparedstatement.cpp.
Member Function Documentation
PreparedStatement & PreparedStatement::operator<< | ( | const QVariant & | value | ) |
void PreparedStatement::clearArguments | ( | ) |
Clears arguments of the prepared statement. Usually used after execute().
Definition at line 132 of file preparedstatement.cpp.
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.
QCString 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.
The documentation for this class was generated from the following files: