?? qsqldriver.cpp
字號:
return QSqlIndex();}/*! Returns a QSqlRecord populated with the names of the fields in table \a tableName. If no such table exists, an empty record is returned. The default implementation returns an empty record.*/QSqlRecord QSqlDriver::record(const QString & /* tableName */) const{ return QSqlRecord();}/*! Returns the \a identifier escaped according to the database rules. \a identifier can either be a table name or field name, dependent on \a type. The default implementation does nothing. */QString QSqlDriver::escapeIdentifier(const QString &identifier, IdentifierType) const{ return identifier;}/*! Returns a SQL statement of type \a type for the table \a tableName with the values from \a rec. If \a preparedStatement is true, the string will contain placeholders instead of values. This method can be used to manipulate tables without having to worry about database-dependent SQL dialects. For non-prepared statements, the values will be properly escaped.*/QString QSqlDriver::sqlStatement(StatementType type, const QString &tableName, const QSqlRecord &rec, bool preparedStatement) const{ int i; QString s; s.reserve(128); switch (type) { case SelectStatement: for (i = 0; i < rec.count(); ++i) { if (rec.isGenerated(i)) s.append(escapeIdentifier(rec.fieldName(i), FieldName)).append(QLatin1String(", ")); } if (s.isEmpty()) return s; s.chop(2); s.prepend(QLatin1String("SELECT ")).append(QLatin1String(" FROM ")).append( escapeIdentifier(tableName, TableName)); break; case WhereStatement: if (preparedStatement) { for (int i = 0; i < rec.count(); ++i) s.append(escapeIdentifier(rec.fieldName(i), FieldName)).append( QLatin1String(" = ? AND ")); } else { for (i = 0; i < rec.count(); ++i) { s.append(escapeIdentifier(rec.fieldName(i), FieldName)); QString val = formatValue(rec.field(i)); if (val == QLatin1String("NULL")) s.append(QLatin1String(" IS NULL")); else s.append(QLatin1String(" = ")).append(val); s.append(QLatin1String(" AND ")); } } if (!s.isEmpty()) { s.prepend(QLatin1String("WHERE ")); s.chop(5); // remove tailing AND } break; case UpdateStatement: s.append(QLatin1String("UPDATE ")).append(escapeIdentifier(tableName, TableName)).append( QLatin1String(" SET ")); for (i = 0; i < rec.count(); ++i) { if (!rec.isGenerated(i) || !rec.value(i).isValid()) continue; s.append(escapeIdentifier(rec.fieldName(i), FieldName)).append(QLatin1Char('=')); if (preparedStatement) s.append(QLatin1Char('?')); else s.append(formatValue(rec.field(i))); s.append(QLatin1String(", ")); } if (s.endsWith(QLatin1String(", "))) s.chop(2); else s = QString(); break; case DeleteStatement: s.append(QLatin1String("DELETE FROM ")).append(escapeIdentifier(tableName, TableName)); break; case InsertStatement: { s.append(QLatin1String("INSERT INTO ")).append(escapeIdentifier( tableName, TableName)).append(QLatin1String(" (")); QString vals; for (i = 0; i < rec.count(); ++i) { if (!rec.isGenerated(i) || !rec.value(i).isValid()) continue; s.append(escapeIdentifier(rec.fieldName(i), FieldName)).append(QLatin1String(", ")); if (preparedStatement) vals.append(QLatin1String("?")); else vals.append(formatValue(rec.field(i))); vals.append(QLatin1String(", ")); } if (vals.isEmpty()) { s = QString(); } else { vals.chop(2); // remove trailing comma s[s.length() - 2] = QLatin1Char(')'); s.append(QLatin1String("VALUES (")).append(vals).append(QLatin1String(")")); } break; } } return s;}/*! Returns a string representation of the \a field value for the database. This is used, for example, when constructing INSERT and UPDATE statements. The default implementation returns the value formatted as a string according to the following rules: \list \i If \a field is character data, the value is returned enclosed in single quotation marks, which is appropriate for many SQL databases. Any embedded single-quote characters are escaped (replaced with two single-quote characters). If \a trimStrings is true (the default is false), all trailing whitespace is trimmed from the field. \i If \a field is date/time data, the value is formatted in ISO format and enclosed in single quotation marks. If the date/time data is invalid, "NULL" is returned. \i If \a field is \link QByteArray bytearray\endlink data, and the driver can edit binary fields, the value is formatted as a hexadecimal string. \i For any other field type, toString() is called on its value and the result of this is returned. \endlist \sa QVariant::toString()*/QString QSqlDriver::formatValue(const QSqlField &field, bool trimStrings) const{ const QLatin1String nullTxt("NULL"); QString r; if (field.isNull()) r = nullTxt; else { switch (field.type()) { case QVariant::Int: case QVariant::UInt: if (field.value().type() == QVariant::Bool) r = field.value().toBool() ? QLatin1String("1") : QLatin1String("0"); else r = field.value().toString(); break;#ifndef QT_NO_DATESTRING case QVariant::Date: if (field.value().toDate().isValid()) r = QLatin1Char('\'') + field.value().toDate().toString(Qt::ISODate) + QLatin1Char('\''); else r = nullTxt; break; case QVariant::Time: if (field.value().toTime().isValid()) r = QLatin1Char('\'') + field.value().toTime().toString(Qt::ISODate) + QLatin1Char('\''); else r = nullTxt; break; case QVariant::DateTime: if (field.value().toDateTime().isValid()) r = QLatin1Char('\'') + field.value().toDateTime().toString(Qt::ISODate) + QLatin1Char('\''); else r = nullTxt; break;#endif case QVariant::String: case QVariant::Char: { QString result = field.value().toString(); if (trimStrings) { int end = result.length() - 1; while (end && result.at(end).isSpace()) /* skip white space from end */ end--; result.truncate(end); } /* escape the "'" character */ result.replace(QLatin1Char('\''), QLatin1String("''")); r = QLatin1Char('\'') + result + QLatin1Char('\''); break; } case QVariant::Bool: if (field.value().toBool()) r = QLatin1String("1"); else r = QLatin1String("0"); break; case QVariant::ByteArray : { if (hasFeature(BLOB)) { QByteArray ba = field.value().toByteArray(); QString res; static const char hexchars[] = "0123456789abcdef"; for (int i = 0; i < ba.size(); ++i) { uchar s = (uchar) ba[i]; res += QLatin1Char(hexchars[s >> 4]); res += QLatin1Char(hexchars[s & 0x0f]); } r = QLatin1Char('\'') + res + QLatin1Char('\''); break; } } default: r = field.value().toString(); break; } } return r;}/*! Returns the low-level database handle wrapped in a QVariant or an invalid variant if there is no handle. \warning Use this with uttermost care and only if you know what you're doing. \warning The handle returned here can become a stale pointer if the connection is modified (for example, if you close the connection). \warning The handle can be NULL if the connection is not open yet. The handle returned here is database-dependent, you should query the type name of the variant before accessing it. This example retrieves the handle for a connection to sqlite: \code QSqlDatabase db = ...; QVariant v = db.driver()->handle(); if (v.isValid() && v.typeName() == "sqlite3*") { // v.data() returns a pointer to the handle sqlite3 *handle = *static_cast<sqlite3 **>(v.data()); if (handle != 0) { // check that it is not NULL ... } } \endcode This snippet returns the handle for PostgreSQL or MySQL: \code if (v.typeName() == "PGconn*") { PGconn *handle = *static_cast<PGconn **>(v.data()); if (handle != 0) ... } if (v.typeName() == "MYSQL*") { MYSQL *handle = *static_cast<MYSQL **>(v.data()); if (handle != 0) ... } \endcode \sa QSqlResult::handle()*/QVariant QSqlDriver::handle() const{ return QVariant();}/*! \fn QSqlRecord QSqlDriver::record(const QSqlQuery& query) const Use query.record() instead.*//*! \fn QSqlRecord QSqlDriver::recordInfo(const QString& tablename) const Use record() instead.*//*! \fn QSqlRecord QSqlDriver::recordInfo(const QSqlQuery& query) const Use query.record() instead.*//*! \fn QString QSqlDriver::nullText() const sqlStatement() is now used to generate SQL. Use tr("NULL") for example, instead.*//*! \fn QString QSqlDriver::formatValue(const QSqlField *field, bool trimStrings) const Use the other formatValue() overload instead.*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -