QString QSqlResultPrivate::namedToPositionalBinding() { int n = sql.size(); QString result; result.reserve(n); bool inQuote = false; int count = 0; int i = 0; while (i < n) { QChar ch = sql.at(i); if (ch == QLatin1Char(':') && !inQuote && (i == 0 || sql.at(i - 1) != QLatin1Char(':')) && (i + 1 < n && qIsAlnum(sql.at(i + 1)))) { int pos = i + 2; while (pos < n && qIsAlnum(sql.at(pos))) ++pos; QString holder(sql.mid(i, pos - i)); indexes[holder].append(count++); holders.append(QHolder(holder, i)); result += QLatin1Char('?'); i = pos; } else { if (ch == QLatin1Char('\'')) inQuote = !inQuote; result += ch; ++i; } } result.squeeze(); values.resize(holders.size()); return result; }
/*! Prepares the given \a query for execution; the query will normally use placeholders so that it can be executed repeatedly. Returns true if the query is prepared successfully; otherwise returns false. \sa exec() */ bool QSqlResult::prepare(const QString& query) { if (d->holders.isEmpty()) { int n = query.size(); bool inQuote = false; int i = 0; while (i < n) { QChar ch = query.at(i); if (ch == QLatin1Char(':') && !inQuote && (i == 0 || query.at(i - 1) != QLatin1Char(':')) && (i + 1 < n && qIsAlnum(query.at(i + 1)))) { int pos = i + 2; while (pos < n && qIsAlnum(query.at(pos))) ++pos; QString holder(query.mid(i, pos - i)); d->indexes[holder].append(d->holders.size()); d->holders.append(QHolder(holder, i)); i = pos; } else { if (ch == QLatin1Char('\'')) inQuote = !inQuote; ++i; } } d->values.resize(d->holders.size()); } d->sql = query; return true; // fake prepares should always succeed }
QString QSqlResultPrivate::namedToPositionalBinding(const QString &query) { int n = query.size(); QString result; result.reserve(n); QChar closingQuote; int count = 0; int i = 0; bool ignoreBraces = (sqldriver->d_func()->dbmsType == QSqlDriver::PostgreSQL); while (i < n) { QChar ch = query.at(i); if (!closingQuote.isNull()) { if (ch == closingQuote) { if (closingQuote == QLatin1Char(']') && i + 1 < n && query.at(i + 1) == closingQuote) { // consume the extra character. don't close. ++i; result += ch; } else { closingQuote = QChar(); } } result += ch; ++i; } else { if (ch == QLatin1Char(':') && (i == 0 || query.at(i - 1) != QLatin1Char(':')) && (i + 1 < n && qIsAlnum(query.at(i + 1)))) { int pos = i + 2; while (pos < n && qIsAlnum(query.at(pos))) ++pos; QString holder(query.mid(i, pos - i)); indexes[holder].append(count++); holders.append(QHolder(holder, i)); result += QLatin1Char('?'); i = pos; } else { if (ch == QLatin1Char('\'') || ch == QLatin1Char('"') || ch == QLatin1Char('`')) closingQuote = ch; else if (!ignoreBraces && ch == QLatin1Char('[')) closingQuote = QLatin1Char(']'); result += ch; ++i; } } } result.squeeze(); values.resize(holders.size()); return result; }