QString QMYSQLDriver::formatValue( const QSqlField* field, bool trimStrings ) const { QString r; if ( field->isNull() ) { r = nullText(); } else { switch( field->type() ) { case QVariant::ByteArray: { const QByteArray ba = field->value().toByteArray(); // buffer has to be at least length*2+1 bytes char* buffer = new char[ ba.size() * 2 + 1 ]; /*uint escapedSize =*/ mysql_escape_string( buffer, ba.data(), ba.size() ); r.append("'").append(buffer).append("'"); delete[] buffer; } break; case QVariant::String: case QVariant::CString: { // Escape '\' characters r = QSqlDriver::formatValue( field ); r.replace( "\\", "\\\\" ); break; } default: r = QSqlDriver::formatValue( field, trimStrings ); } } return r; }
QString SqliteDriver::formatValue(int t, const QVariant &v, const bool upper) { QString res; switch (FLFieldMetaData::flDecodeType(t)) { case QVariant::Bool: { QString s(v.toString().left(1).upper()); if (s == QApplication::tr("Sí").left(1).upper()) res = "0"; else if (s == QApplication::tr("No").left(1).upper()) res = "1"; else res = nullText(); } break; case QVariant::Date: res = "'" + FLUtil::dateDMAtoAMD(v.toString()) + "'"; break; case QVariant::Time: { QTime t(v.toTime()); if (t.isValid() && !t.isNull()) res = "'" + t.toString(Qt::ISODate) + "'"; else res = nullText(); } break; case QVariant::Int: case QVariant::UInt: case QVariant::Double: res += v.toString(); break; default: if (upper) res = "'" + v.toString().upper() + "'"; else res = "'" + v.toString() + "'"; } return res; }
/*! 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 NULL, nullText() is returned. \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, nullText() is returned. \i If \a field is bytearray data, and the driver can edit binary fields, the value is formatted as a hexadecimal string. \i For any other field type toString() will be called on its value and the result returned. \endlist \sa QVariant::toString(). */ QString QSqlDriver::formatValue( const QSqlField* field, bool trimStrings ) const { QString r; if ( field->isNull() ) r = nullText(); else { switch ( field->type() ) { case QVariant::Int: case QVariant::UInt: if ( field->value().type() == QVariant::Bool ) r = field->value().toBool() ? "1" : "0"; else r = field->value().toString(); break; case QVariant::Date: if ( field->value().toDate().isValid() ) r = "'" + field->value().toDate().toString( Qt::ISODate ) + "'"; else r = nullText(); break; case QVariant::Time: if ( field->value().toTime().isValid() ) r = "'" + field->value().toTime().toString( Qt::ISODate ) + "'"; else r = nullText(); break; case QVariant::DateTime: if ( field->value().toDateTime().isValid() ) r = "'" + field->value().toDateTime().toString( Qt::ISODate ) + "'"; else r = nullText(); break; case QVariant::String: case QVariant::CString: { QString result = field->value().toString(); if ( trimStrings ) { int end = result.length() - 1; while ( end && result[end].isSpace() ) /* skip white space from end */ end--; result.truncate( end ); } /* escape the "'" character */ result.replace( QChar( '\'' ), "''" ); r = "'" + result + "'"; break; } case QVariant::Bool: if ( field->value().toBool() ) r = "1"; else r = "0"; break; case QVariant::ByteArray : { if ( hasFeature( BLOB ) ) { QByteArray ba = field->value().toByteArray(); QString res; static const char hexchars[] = "0123456789abcdef"; for ( uint i = 0; i < ba.size(); ++i ) { uchar s = (uchar) ba[(int)i]; res += hexchars[s >> 4]; res += hexchars[s & 0x0f]; } r = "'" + res + "'"; break; } } default: r = field->value().toString(); break; } } return r; }