KDbPreparedStatement

Search for usage in LXR

#include <KDbPreparedStatement.h>

Inheritance diagram for KDbPreparedStatement:

Classes

class  Data
 

Public Types

enum  Type { InvalidStatement, SelectStatement, InsertStatement }
 

Public Member Functions

 KDbPreparedStatement ()
 
bool execute (const KDbPreparedStatementParameters &parameters)
 
const KDbFieldListfields () const
 
bool isValid () const
 
quint64 lastInsertRecordId () const
 
void setFields (KDbFieldList *fields)
 
void setType (KDbPreparedStatement::Type type)
 
void setWhereFieldNames (const QStringList &whereFieldNames)
 
KDbPreparedStatement::Type type () const
 
QStringList whereFieldNames () const
 
- Public Member Functions inherited from KDbResultable
 KDbResultable (const KDbResultable &other)
 
void clearResult ()
 
KDbMessageHandlermessageHandler () const
 
KDbResultableoperator= (const KDbResultable &other)
 
KDbResult result () const
 
virtual QString serverResultName () const
 
void setMessageHandler (KDbMessageHandler *handler)
 
void showMessage ()
 

Protected Member Functions

 KDbPreparedStatement (KDbPreparedStatementInterface *iface, Type type, KDbFieldList *fields, const QStringList &whereFieldNames=QStringList())
 

Additional Inherited Members

- Protected Attributes inherited from KDbResultable
Private *const d
 
KDbResult m_result
 

Detailed Description

Prepared database command for optimizing sequences of multiple database actions.

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

To use KDbPreparedStatement, create is using KDbConnection:prepareStatement(), providing table schema; set up parameters using operator << ( const QVariant& value ); and call execute() when ready. KDbPreparedStatement objects are accessed using KDE shared pointers, i.e KDbPreparedStatement, so you do not need to remember about destroying them. However, when underlying KDbConnection object is destroyed, KDbPreparedStatement 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(KDbConnection* conn, KDbTableSchema* tableSchema)
{
KDbPreparedStatement::Insert, tableSchema);
for (i=0; i<10000; i++) {
parameters << qrand() << getText(i);
if (!statement.execute(parameters))
return false;
}
return true;
}

If you do not call clearParameters() 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 75 of file KDbPreparedStatement.h.

Member Enumeration Documentation

◆ Type

Defines type of the prepared statement.

Enumerator
InvalidStatement 

Used only in invalid statements.

SelectStatement 

SELECT statement will be prepared end executed.

InsertStatement 

INSERT statement will be prepared end executed.

Definition at line 80 of file KDbPreparedStatement.h.

Constructor & Destructor Documentation

◆ KDbPreparedStatement() [1/2]

KDbPreparedStatement::KDbPreparedStatement ( )

Creates an invalid prepared statement.

Definition at line 46 of file KDbPreparedStatement.cpp.

◆ KDbPreparedStatement() [2/2]

KDbPreparedStatement::KDbPreparedStatement ( KDbPreparedStatementInterface iface,
Type  type,
KDbFieldList fields,
const QStringList whereFieldNames = QStringList() 
)
protected

Creates a new prepared statement.

In your code use Users call KDbConnection:prepareStatement() instead.

Definition at line 51 of file KDbPreparedStatement.cpp.

Member Function Documentation

◆ execute()

bool KDbPreparedStatement::execute ( const KDbPreparedStatementParameters parameters)

Executes the prepared statement using parameters parameters. A number parameters 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 KDbConnection object that was used to create this statement object.
Todo:
error message?

Definition at line 62 of file KDbPreparedStatement.cpp.

◆ lastInsertRecordId()

quint64 KDbPreparedStatement::lastInsertRecordId ( ) const
Returns
unique identifier of the most recently inserted record. Typically this is just primary key value. This identifier could be reused when we want to reference just inserted record. If there was no insertion recently performed, std::numeric_limits<quint64>::max() is returned.

Definition at line 213 of file KDbPreparedStatement.cpp.

◆ setFields()

void KDbPreparedStatement::setFields ( KDbFieldList fields)

Sets fields for the statement. Does nothing if fields is nullptr.

Definition at line 194 of file KDbPreparedStatement.cpp.


The documentation for this class was generated from the following files:
KDbPreparedStatement prepareStatement(KDbPreparedStatement::Type type, KDbFieldList *fields, const QStringList &whereFieldNames=QStringList())
Prepared database command for optimizing sequences of multiple database actions.
Provides database connection, allowing queries and data modification.
Definition: KDbConnection.h:51
QString getText(const QString &caption, const QString &label, const QString &value=QString(), bool *ok=nullptr, QWidget *parent=nullptr, QValidator *validator=nullptr, const QString &mask=QString(), const QString &whatsThis=QString(), const QStringList &completionList=QStringList())
bool execute(const KDbPreparedStatementParameters &parameters)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Wed Aug 17 2022 04:05:00 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.