MySQL: Keep the precision of the field when formatting the value

Change-Id: I4ab08be5112167a617d6d1d109754f2404a9605f
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@theqtcompany.com>
This commit is contained in:
Andy Shaw 2015-03-13 15:50:28 +01:00
parent a2ebd502d4
commit 392d861790
2 changed files with 29 additions and 10 deletions

View File

@ -1500,6 +1500,9 @@ QString QMYSQLDriver::formatValue(const QSqlField &field, bool trimStrings) cons
r = QLatin1String("NULL"); r = QLatin1String("NULL");
} else { } else {
switch(field.type()) { switch(field.type()) {
case QVariant::Double:
r = QString::number(field.value().toDouble(), 'g', field.precision());
break;
case QVariant::String: case QVariant::String:
// Escape '\' characters // Escape '\' characters
r = QSqlDriver::formatValue(field, trimStrings); r = QSqlDriver::formatValue(field, trimStrings);

View File

@ -56,6 +56,7 @@ public slots:
private slots: private slots:
void record(); void record();
void primaryIndex(); void primaryIndex();
void formatValue();
}; };
@ -76,13 +77,13 @@ void tst_QSqlDriver::recreateTestTables(QSqlDatabase db)
QVERIFY_SQL( q, exec("set client_min_messages='warning'")); QVERIFY_SQL( q, exec("set client_min_messages='warning'"));
tst_Databases::safeDropTable( db, relTEST1 ); tst_Databases::safeDropTable( db, relTEST1 );
QString doubleField = (dbType == QSqlDriver::SQLite) ? "more_data double" : "more_data double(8,7)";
QVERIFY_SQL( q, exec("create table " + relTEST1 + QVERIFY_SQL( q, exec("create table " + relTEST1 +
" (id int not null primary key, name varchar(20), title_key int, another_title_key int)")); " (id int not null primary key, name varchar(20), title_key int, another_title_key int, " + doubleField + ")"));
QVERIFY_SQL( q, exec("insert into " + relTEST1 + " values(1, 'harry', 1, 2)")); QVERIFY_SQL( q, exec("insert into " + relTEST1 + " values(1, 'harry', 1, 2, 1.234567)"));
QVERIFY_SQL( q, exec("insert into " + relTEST1 + " values(2, 'trond', 2, 1)")); QVERIFY_SQL( q, exec("insert into " + relTEST1 + " values(2, 'trond', 2, 1, 8.901234)"));
QVERIFY_SQL( q, exec("insert into " + relTEST1 + " values(3, 'vohi', 1, 2)")); QVERIFY_SQL( q, exec("insert into " + relTEST1 + " values(3, 'vohi', 1, 2, 5.678901)"));
QVERIFY_SQL( q, exec("insert into " + relTEST1 + " values(4, 'boris', 2, 2)")); QVERIFY_SQL( q, exec("insert into " + relTEST1 + " values(4, 'boris', 2, 2, 2.345678)"));
} }
void tst_QSqlDriver::initTestCase() void tst_QSqlDriver::initTestCase()
@ -116,11 +117,11 @@ void tst_QSqlDriver::record()
QString tablename(qTableName("relTEST1", __FILE__, db)); QString tablename(qTableName("relTEST1", __FILE__, db));
QStringList fields; QStringList fields;
fields << "id" << "name" << "title_key" << "another_title_key"; fields << "id" << "name" << "title_key" << "another_title_key" << "more_data";
//check we can get records using an unquoted mixed case table name //check we can get records using an unquoted mixed case table name
QSqlRecord rec = db.driver()->record(tablename); QSqlRecord rec = db.driver()->record(tablename);
QCOMPARE(rec.count(), 4); QCOMPARE(rec.count(), 5);
QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db); QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
// QTBUG-1363: QSqlField::length() always return -1 when using QODBC3 driver and QSqlDatabase::record() // QTBUG-1363: QSqlField::length() always return -1 when using QODBC3 driver and QSqlDatabase::record()
@ -142,7 +143,7 @@ void tst_QSqlDriver::record()
if (dbType != QSqlDriver::PostgreSQL && !db.driverName().startsWith("QODBC")) { if (dbType != QSqlDriver::PostgreSQL && !db.driverName().startsWith("QODBC")) {
//check we can get records using a properly quoted table name //check we can get records using a properly quoted table name
rec = db.driver()->record(db.driver()->escapeIdentifier(tablename,QSqlDriver::TableName)); rec = db.driver()->record(db.driver()->escapeIdentifier(tablename,QSqlDriver::TableName));
QCOMPARE(rec.count(), 4); QCOMPARE(rec.count(), 5);
} }
for (int i = 0; i < fields.count(); ++i) for (int i = 0; i < fields.count(); ++i)
@ -157,7 +158,7 @@ void tst_QSqlDriver::record()
rec = db.driver()->record(db.driver()->escapeIdentifier(tablename,QSqlDriver::TableName)); rec = db.driver()->record(db.driver()->escapeIdentifier(tablename,QSqlDriver::TableName));
if (dbType == QSqlDriver::MySqlServer || dbType == QSqlDriver::SQLite || dbType == QSqlDriver::Sybase if (dbType == QSqlDriver::MySqlServer || dbType == QSqlDriver::SQLite || dbType == QSqlDriver::Sybase
|| dbType == QSqlDriver::MSSqlServer || tst_Databases::isMSAccess(db)) || dbType == QSqlDriver::MSSqlServer || tst_Databases::isMSAccess(db))
QCOMPARE(rec.count(), 4); //mysql, sqlite and tds will match QCOMPARE(rec.count(), 5); //mysql, sqlite and tds will match
else else
QCOMPARE(rec.count(), 0); QCOMPARE(rec.count(), 0);
@ -212,5 +213,20 @@ void tst_QSqlDriver::primaryIndex()
QCOMPARE(index.count(), 0); QCOMPARE(index.count(), 0);
} }
void tst_QSqlDriver::formatValue()
{
QFETCH_GLOBAL(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db);
QString tablename(qTableName("relTEST1", __FILE__, db));
QSqlQuery qry(db);
QVERIFY_SQL(qry, exec("SELECT * FROM " + tablename));
qry.next();
QSqlRecord rec = qry.record();
QCOMPARE(db.driver()->formatValue(rec.field("id")), QString("1"));
QCOMPARE(db.driver()->formatValue(rec.field("name")), QString("'harry'"));
QCOMPARE(db.driver()->formatValue(rec.field("more_data")), QString("1.234567"));
}
QTEST_MAIN(tst_QSqlDriver) QTEST_MAIN(tst_QSqlDriver)
#include "tst_qsqldriver.moc" #include "tst_qsqldriver.moc"