diff options
Diffstat (limited to 'src/sql/kernel/qsqlresult.cpp')
-rw-r--r-- | src/sql/kernel/qsqlresult.cpp | 123 |
1 files changed, 85 insertions, 38 deletions
diff --git a/src/sql/kernel/qsqlresult.cpp b/src/sql/kernel/qsqlresult.cpp index b45f7b6790..59e9879cf0 100644 --- a/src/sql/kernel/qsqlresult.cpp +++ b/src/sql/kernel/qsqlresult.cpp @@ -3,9 +3,7 @@ #include "qsqlresult.h" -#include "qhash.h" #include "qlist.h" -#include "qpointer.h" #include "qsqldriver.h" #include "qsqlerror.h" #include "qsqlfield.h" @@ -15,7 +13,6 @@ #include "qvariant.h" #include "qdatetime.h" #include "private/qsqldriver_p.h" -#include <QDebug> QT_BEGIN_NAMESPACE @@ -26,7 +23,7 @@ QString QSqlResultPrivate::holderAt(int index) const return holders.size() > index ? holders.at(index).holderName : fieldSerial(index); } -QString QSqlResultPrivate::fieldSerial(int i) const +QString QSqlResultPrivate::fieldSerial(qsizetype i) const { return QString(":%1"_L1).arg(i); } @@ -40,15 +37,18 @@ static bool qIsAlnum(QChar ch) QString QSqlResultPrivate::positionalToNamedBinding(const QString &query) const { - int n = query.size(); + if (!positionalBindingEnabled) + return query; + + const qsizetype n = query.size(); QString result; result.reserve(n * 5 / 4); QChar closingQuote; - int count = 0; + qsizetype count = 0; bool ignoreBraces = (sqldriver->dbmsType() == QSqlDriver::PostgreSQL); - for (int i = 0; i < n; ++i) { + for (qsizetype i = 0; i < n; ++i) { QChar ch = query.at(i); if (!closingQuote.isNull()) { if (ch == closingQuote) { @@ -87,14 +87,15 @@ QString QSqlResultPrivate::namedToPositionalBinding(const QString &query) query.trimmed().startsWith("EXECUTE BLOCK"_L1, Qt::CaseInsensitive)) return query; - int n = query.size(); + const qsizetype n = query.size(); QString result; result.reserve(n); QChar closingQuote; int count = 0; - int i = 0; + qsizetype i = 0; bool ignoreBraces = (sqldriver->dbmsType() == QSqlDriver::PostgreSQL); + const bool qmarkNotationSupported = (sqldriver->dbmsType() != QSqlDriver::PostgreSQL); while (i < n) { QChar ch = query.at(i); @@ -118,10 +119,16 @@ QString QSqlResultPrivate::namedToPositionalBinding(const QString &query) int pos = i + 2; while (pos < n && qIsAlnum(query.at(pos))) ++pos; + // if question mark notation is not supported we have to use + // the native binding. fieldSerial() should be renamed + // to toNativeBinding() and used unconditionally here + if (qmarkNotationSupported) + result += u'?'; + else + result += fieldSerial(count); QString holder(query.mid(i, pos - i)); indexes[holder].append(count++); holders.append(QHolder(holder, i)); - result += u'?'; i = pos; } else { if (ch == u'\'' || ch == u'"' || ch == u'`') @@ -627,37 +634,31 @@ bool QSqlResult::exec() // fake preparation - just replace the placeholders.. QString query = lastQuery(); if (d->binds == NamedBinding) { - int i; - QVariant val; - QString holder; - for (i = d->holders.count() - 1; i >= 0; --i) { - holder = d->holders.at(i).holderName; - val = d->values.value(d->indexes.value(holder).value(0,-1)); + for (qsizetype i = d->holders.size() - 1; i >= 0; --i) { + const QString &holder = d->holders.at(i).holderName; + const QVariant val = d->values.value(d->indexes.value(holder).value(0,-1)); QSqlField f(""_L1, val.metaType()); if (QSqlResultPrivate::isVariantNull(val)) f.setValue(QVariant()); else f.setValue(val); query = query.replace(d->holders.at(i).holderPos, - holder.length(), driver()->formatValue(f)); + holder.size(), driver()->formatValue(f)); } } else { - QString val; qsizetype i = 0; - int idx = 0; - for (idx = 0; idx < d->values.count(); ++idx) { + for (const QVariant &var : std::as_const(d->values)) { i = query.indexOf(u'?', i); if (i == -1) continue; - QVariant var = d->values.value(idx); QSqlField f(""_L1, var.metaType()); if (QSqlResultPrivate::isVariantNull(var)) f.clear(); else f.setValue(var); - val = driver()->formatValue(f); - query = query.replace(i, 1, driver()->formatValue(f)); - i += val.length(); + const QString val = driver()->formatValue(f); + query = query.replace(i, 1, val); + i += val.size(); } } @@ -683,7 +684,7 @@ void QSqlResult::bindValue(int index, const QVariant& val, QSql::ParamType param QList<int> &indexes = d->indexes[d->fieldSerial(index)]; if (!indexes.contains(index)) indexes.append(index); - if (d->values.count() <= index) + if (d->values.size() <= index) d->values.resize(index + 1); d->values[index] = val; if (paramType != QSql::In || !d->types.isEmpty()) @@ -709,7 +710,7 @@ void QSqlResult::bindValue(const QString& placeholder, const QVariant& val, // bindings - don't reset it const QList<int> indexes = d->indexes.value(placeholder); for (int idx : indexes) { - if (d->values.count() <= idx) + if (d->values.size() <= idx) d->values.resize(idx + 1); d->values[idx] = val; if (paramType != QSql::In || !d->types.isEmpty()) @@ -789,22 +790,37 @@ QSql::ParamType QSqlResult::bindValueType(const QString& placeholder) const int QSqlResult::boundValueCount() const { Q_D(const QSqlResult); - return d->values.count(); + return d->values.size(); } /*! - Returns a vector of the result's bound values for the current + Returns a list of the result's bound values for the current record (row). \sa boundValueCount() */ -QList<QVariant> &QSqlResult::boundValues() const +QVariantList QSqlResult::boundValues(QT6_IMPL_NEW_OVERLOAD) const { Q_D(const QSqlResult); - return const_cast<QSqlResultPrivate *>(d)->values; + return d->values; } /*! + \overload + + Returns a mutable reference to the list of the result's bound values + for the current record (row). + + \sa boundValueCount() +*/ +QVariantList &QSqlResult::boundValues(QT6_IMPL_NEW_OVERLOAD) +{ + Q_D(QSqlResult); + return d->values; +} + + +/*! Returns the binding syntax used by prepared queries. */ QSqlResult::BindingSyntax QSqlResult::bindingSyntax() const @@ -847,10 +863,24 @@ void QSqlResult::resetBindCount() } /*! + Returns the names of all bound values. + + \sa boundValue(), boundValueName() + */ +QStringList QSqlResult::boundValueNames() const +{ + Q_D(const QSqlResult); + QList<QString> ret; + for (const QHolder &holder : std::as_const(d->holders)) + ret.push_back(holder.holderName); + return ret; +} + +/*! Returns the name of the bound value at position \a index in the current record (row). - \sa boundValue() + \sa boundValue(), boundValueNames() */ QString QSqlResult::boundValueName(int index) const { @@ -916,8 +946,6 @@ void QSqlResult::virtual_hook(int, void *) } /*! \internal - \since 4.2 - Executes a prepared query in batch mode if the driver supports it, otherwise emulates a batch execution using bindValue() and exec(). QSqlDriver::hasFeature() can be used to find out whether a driver @@ -933,7 +961,7 @@ void QSqlResult::virtual_hook(int, void *) contain equal amount of values (rows). NULL values are passed in as typed QVariants, for example - \c {QVariant(QMetaType::Int)} for an integer NULL value. + \c {QVariant(QMetaType::fromType<int>())} for an integer NULL value. Example: @@ -948,11 +976,13 @@ bool QSqlResult::execBatch(bool arrayBind) Q_UNUSED(arrayBind); Q_D(QSqlResult); - QList<QVariant> values = d->values; - if (values.count() == 0) + const QList<QVariant> values = d->values; + if (values.size() == 0) return false; - for (int i = 0; i < values.at(0).toList().count(); ++i) { - for (int j = 0; j < values.count(); ++j) + const qsizetype batchCount = values.at(0).toList().size(); + const qsizetype valueCount = values.size(); + for (qsizetype i = 0; i < batchCount; ++i) { + for (qsizetype j = 0; j < valueCount; ++j) bindValue(j, values.at(j).toList().at(i), QSql::In); if (!exec()) return false; @@ -983,6 +1013,23 @@ QSql::NumericalPrecisionPolicy QSqlResult::numericalPrecisionPolicy() const } /*! \internal + */ +void QSqlResult::setPositionalBindingEnabled(bool enable) +{ + Q_D(QSqlResult); + d->positionalBindingEnabled = enable; +} + +/*! \internal + */ +bool QSqlResult::isPositionalBindingEnabled() const +{ + Q_D(const QSqlResult); + return d->positionalBindingEnabled; +} + + +/*! \internal */ bool QSqlResult::nextResult() { |