make QSqlResultPrivate::fieldSerial() virtual

The Qt psql driver has its own implementation of fieldSerial() it uses
when it invokes positionalToNamedBinding() to generate a query using
its native naming style. Now that QPSQLResultPrivate is derived from
QSqlResultPrivate this can be implemented more conventionally using
a virtual function instead of pointers to static functions.

Note that this change preserves the current behavior of
executedQuery() which will continue to return the query with
positional syntax that is presented to virtual prepare() by
QSqlResult::savePrepare(). Since the driver does not have the
NamedPlaceholders feature, QSqlResult::savePrepare() will not use
positionaltoNamedBinding() to set executedQuery. Although
QPSQLResult::prepare() calls positionaltoNamedBinding(),
it does not put the result into executedQuery.

Change-Id: I7740f386cbfec9eadd9e4d6a7df3e590294655a5
Reviewed-by: Israel Lins Albuquerque <israelins85@yahoo.com.br>
Reviewed-by: Mark Brand <mabrand@mabrand.nl>
bb10
Mark Brand 2013-03-21 22:32:40 +01:00 committed by The Qt Project
parent db6190d6fe
commit 4131bfb2c2
3 changed files with 9 additions and 14 deletions

View File

@ -202,7 +202,7 @@ public:
preparedQueriesEnabled(false)
{ }
static QString fieldSerial(int i) { return QLatin1Char('$') + QString::number(i + 1); }
QString fieldSerial(int i) const { return QLatin1Char('$') + QString::number(i + 1); }
void deallocatePreparedStmt();
const QPSQLDriverPrivate * privDriver() const {Q_Q(const QPSQLResult); return reinterpret_cast<const QPSQLDriver *>(q->driver())->d; }
@ -594,7 +594,7 @@ bool QPSQLResult::prepare(const QString &query)
d->deallocatePreparedStmt();
const QString stmtId = qMakePreparedStmtId();
const QString stmt = QString::fromLatin1("PREPARE %1 AS ").arg(stmtId).append(QSqlResultPrivate::positionalToNamedBinding(query, QPSQLResultPrivate::fieldSerial));
const QString stmt = QString::fromLatin1("PREPARE %1 AS ").arg(stmtId).append(d->positionalToNamedBinding(query));
PGresult *result = d->privDriver()->exec(stmt);

View File

@ -61,7 +61,7 @@ QString QSqlResultPrivate::holderAt(int index) const
}
// return a unique id for bound names
QString QSqlResultPrivate::fieldSerial(int i)
QString QSqlResultPrivate::fieldSerial(int i) const
{
ushort arr[] = { ':', 'f', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
ushort *ptr = &arr[1];
@ -81,7 +81,7 @@ static bool qIsAlnum(QChar ch)
return u - 'a' < 26 || u - 'A' < 26 || u - '0' < 10 || u == '_';
}
QString QSqlResultPrivate::positionalToNamedBinding(const QString &query, QString (fieldSerialFunc)(int idx))
QString QSqlResultPrivate::positionalToNamedBinding(const QString &query) const
{
int n = query.size();
@ -106,7 +106,7 @@ QString QSqlResultPrivate::positionalToNamedBinding(const QString &query, QStrin
result += ch;
} else {
if (ch == QLatin1Char('?')) {
result += fieldSerialFunc(count++);
result += fieldSerial(count++);
} else {
if (ch == QLatin1Char('\'') || ch == QLatin1Char('"') || ch == QLatin1Char('`'))
closingQuote = ch;
@ -594,7 +594,7 @@ bool QSqlResult::savePrepare(const QString& query)
d->executedQuery = d->namedToPositionalBinding(query);
if (driver()->hasFeature(QSqlDriver::NamedPlaceholders))
d->executedQuery = QSqlResultPrivate::positionalToNamedBinding(query);
d->executedQuery = d->positionalToNamedBinding(query);
return prepare(d->executedQuery);
}
@ -680,7 +680,7 @@ void QSqlResult::bindValue(int index, const QVariant& val, QSql::ParamType param
{
Q_D(QSqlResult);
d->binds = PositionalBinding;
d->indexes[QSqlResultPrivate::fieldSerial(index)].append(index);
d->indexes[d->fieldSerial(index)].append(index);
if (d->values.count() <= index)
d->values.resize(index + 1);
d->values[index] = val;

View File

@ -108,13 +108,8 @@ public:
clearIndex();;
}
// positionalToNamedBinding uses fieldSerial() by default, which converts to Oracle-style names,
// because this style is used in the API. A driver can reuse positionalToNamedBinding()
// internally for its own naming style by supplying its own fieldSerialFunc. We cannot make
// fieldSerial() virtual because it would allow a driver to impose its naming style on
// executedQuery when set by QSqlResult::savePrepare().
static QString fieldSerial(int);
static QString positionalToNamedBinding(const QString &query, QString (fieldSerialFunc)(int idx) = fieldSerial);
virtual QString fieldSerial(int) const;
QString positionalToNamedBinding(const QString &query) const;
QString namedToPositionalBinding(const QString &query);
QString holderAt(int index) const;