Kexi API Documentation (2.0 alpha)

KexiDB::PreparedStatement Class Reference

#include <preparedstatement.h>

Inheritance diagram for KexiDB::PreparedStatement:

KShared KexiDB::MySqlPreparedStatement KexiDB::pqxxPreparedStatement KexiDB::SQLitePreparedStatement List of all members.

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< PreparedStatementPtr
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 ()
PreparedStatementoperator<< (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
FieldListm_fields
QValueList< QVariant > m_args
QStringList * m_where
Field::Listm_whereFields


Member Typedef Documentation

typedef KSharedPtr<PreparedStatement> KexiDB::PreparedStatement::Ptr
 

Definition at line 76 of file preparedstatement.h.


Member Enumeration Documentation

enum KexiDB::PreparedStatement::StatementType
 

Defines type of the prepared statement.

Enumeration values:
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.

PreparedStatement::~PreparedStatement  )  [virtual]
 

Definition at line 39 of file preparedstatement.cpp.

References m_where, and m_whereFields.


Member Function Documentation

void PreparedStatement::clearArguments  ) 
 

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

Definition at line 132 of file preparedstatement.cpp.

References m_args.

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?

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().

PreparedStatement & PreparedStatement::operator<< const QVariant &  value  ) 
 

Appends argument value to the statement.

Definition at line 113 of file preparedstatement.cpp.

References m_args.


Member Data Documentation

QValueList<QVariant> KexiDB::PreparedStatement::m_args [protected]
 

Definition at line 110 of file preparedstatement.h.

Referenced by clearArguments(), KexiDB::SQLitePreparedStatement::execute(), KexiDB::pqxxPreparedStatement::execute(), KexiDB::MySqlPreparedStatement::execute(), and operator<<().

FieldList* KexiDB::PreparedStatement::m_fields [protected]
 

Definition at line 109 of file preparedstatement.h.

Referenced by KexiDB::SQLitePreparedStatement::execute(), KexiDB::pqxxPreparedStatement::execute(), KexiDB::MySqlPreparedStatement::execute(), and generateStatementString().

StatementType KexiDB::PreparedStatement::m_type [protected]
 

Definition at line 108 of file preparedstatement.h.

Referenced by KexiDB::SQLitePreparedStatement::execute(), KexiDB::MySqlPreparedStatement::execute(), and generateStatementString().

QStringList* KexiDB::PreparedStatement::m_where [protected]
 

Definition at line 111 of file preparedstatement.h.

Referenced by generateStatementString(), and ~PreparedStatement().

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

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:
KDE Logo
This file is part of the documentation for Kexi 2.0 alpha.
Documentation copyright © 2002-2007 the Kexi Team.
Generated on Tue Apr 1 20:50:47 2008 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003