20#include "KDbPreparedStatement.h"
21#include "KDbPreparedStatementInterface.h"
22#include "KDbSqlResult.h"
23#include "KDbTableSchema.h"
26KDbPreparedStatement::Data::Data()
27 : Data(InvalidStatement, nullptr, nullptr,
QStringList())
34 :
type(_type), fields(_fields), whereFieldNames(_whereFieldNames)
35 , fieldsForParameters(nullptr), whereFields(nullptr), dirty(true), iface(_iface)
36 , lastInsertRecordId(std::numeric_limits<quint64>::max())
40KDbPreparedStatement::Data::~Data()
54 : d( new
Data(type, iface, fields, whereFieldNames) )
58KDbPreparedStatement::~KDbPreparedStatement()
66 if (!generateStatementString(&s)) {
67 m_result.setCode(ERR_OTHER);
72 m_result.setCode(ERR_OTHER);
75 if (!d->iface->prepare(s)) {
76 m_result.setCode(ERR_OTHER);
82 = d->iface->execute(d->type, *d->fieldsForParameters, d->fields, parameters);
86 d->lastInsertRecordId = result->lastInsertRecordId();
95 return generateSelectStatementString(s);
97 return generateInsertStatementString(s);
100 kdbCritical() <<
"Unsupported type" << d->type;
104bool KDbPreparedStatement::generateSelectStatementString(
KDbEscapedString * s)
109 foreach(
KDbField *f, *d->fields->fields()) {
114 s->append(f->
name());
118 delete d->whereFields;
120 foreach(
const QString& whereItem, d->whereFieldNames) {
122 s->append(
" WHERE ");
127 KDbField *f = d->fields->field(whereItem);
129 kdbWarning() <<
"field" << whereItem <<
"not found, aborting";
133 d->whereFields->append(f);
134 s->append(whereItem.
toUtf8() +
"=?");
136 d->fieldsForParameters = d->whereFields;
140bool KDbPreparedStatement::generateInsertStatementString(
KDbEscapedString * s)
143 KDbTableSchema *table = d->fields->isEmpty() ? nullptr : d->fields->field(0)->table();
150 const bool allTableFieldsUsed =
dynamic_cast<KDbTableSchema*
>(d->fields);
151 foreach(
const KDbField* f, *d->fields->fields()) {
154 if (!allTableFieldsUsed)
159 if (!allTableFieldsUsed) {
160 namesList.append(
", ");
161 namesList.append(f->
name());
169 d->fieldsForParameters = d->fields->fields();
173bool KDbPreparedStatement::isValid()
const
202QStringList KDbPreparedStatement::whereFieldNames()
const
204 return d->whereFieldNames;
207void KDbPreparedStatement::setWhereFieldNames(
const QStringList& whereFieldNames)
209 d->whereFieldNames = whereFieldNames;
215 return d->lastInsertRecordId;
Specialized string for escaping.
KDbUtils::AutodeletedList< KDbField * > List
list of fields
Prepared statement interface for backend-dependent implementations.
KDbField::List * whereFields
temporary, used for select statements, based on whereFieldNames
void setFields(KDbFieldList *fields)
Sets fields for the statement. Does nothing if fields is nullptr.
KDbPreparedStatement()
Creates an invalid prepared statement.
bool execute(const KDbPreparedStatementParameters ¶meters)
quint64 lastInsertRecordId() const
Type
Defines type of the prepared statement.
@ InsertStatement
INSERT statement will be prepared end executed.
@ InvalidStatement
Used only in invalid statements.
@ SelectStatement
SELECT statement will be prepared end executed.
Type type(const QSqlDatabase &db)
QByteArray toUtf8() const const