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 | |
| typedef KSharedPtr< PreparedStatement > | Ptr |
| enum | StatementType { SelectStatement, InsertStatement } |
| Defines type of the prepared statement. More... | |
Public Member Functions | |
| PreparedStatement (StatementType type, ConnectionInternal &conn, FieldList &fields, const QStringList &where=QStringList()) | |
| Creates Prepared statement. In your code use KexiDB::Connection:prepareStatement() instead. | |
| virtual | ~PreparedStatement () |
| PreparedStatement & | operator<< (const QVariant &value) |
| Appends argument value to the statement. | |
| void | clearArguments () |
| Clears arguments of the prepared statement. Usually used after execute(). | |
| 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 Typedef Documentation
|
|
Definition at line 76 of file preparedstatement.h. |
Member Enumeration Documentation
|
|
Defines type of the prepared statement.
Definition at line 79 of file preparedstatement.h. |
Constructor & Destructor Documentation
|
||||||||||||||||||||
|
Creates Prepared statement. In your code use KexiDB::Connection:prepareStatement() instead.
Definition at line 28 of file preparedstatement.cpp. |
|
|
Definition at line 39 of file preparedstatement.cpp. References m_where, and m_whereFields. |
Member Function Documentation
|
|
Clears arguments of the prepared statement. Usually used after execute().
Definition at line 132 of file preparedstatement.cpp. References m_args. |
|
|
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.
Implemented in KexiDB::MySqlPreparedStatement, KexiDB::pqxxPreparedStatement, and KexiDB::SQLitePreparedStatement. |
|
|
Definition at line 45 of file preparedstatement.cpp. References KexiDB::FieldList::field(), KexiDB::fieldCount(), KexiDB::FieldList::fieldCount(), KexiDB::FieldList::fieldsIterator(), InsertStatement, KexiDBWarn, m_fields, m_type, m_where, m_whereFields, KexiDB::SchemaData::name(), and SelectStatement. Referenced by KexiDB::MySqlPreparedStatement::MySqlPreparedStatement(), and KexiDB::SQLitePreparedStatement::SQLitePreparedStatement(). |
|
|
Appends argument value to the statement.
Definition at line 113 of file preparedstatement.cpp. References m_args. |
Member Data Documentation
|
|
Definition at line 110 of file preparedstatement.h. Referenced by clearArguments(), KexiDB::SQLitePreparedStatement::execute(), KexiDB::pqxxPreparedStatement::execute(), KexiDB::MySqlPreparedStatement::execute(), and operator<<(). |
|
|
Definition at line 109 of file preparedstatement.h. Referenced by KexiDB::SQLitePreparedStatement::execute(), KexiDB::pqxxPreparedStatement::execute(), KexiDB::MySqlPreparedStatement::execute(), and generateStatementString(). |
|
|
Definition at line 108 of file preparedstatement.h. Referenced by KexiDB::SQLitePreparedStatement::execute(), KexiDB::MySqlPreparedStatement::execute(), and generateStatementString(). |
|
|
Definition at line 111 of file preparedstatement.h. Referenced by generateStatementString(), and ~PreparedStatement(). |
|
|
Definition at line 112 of file preparedstatement.h. Referenced by KexiDB::SQLitePreparedStatement::execute(), KexiDB::MySqlPreparedStatement::execute(), generateStatementString(), and ~PreparedStatement(). |
The documentation for this class was generated from the following files:
