From 8e889378115c69508b050a511621ac8e30ec4158 Mon Sep 17 00:00:00 2001 From: Jesus Fernandez Date: Mon, 13 Jun 2016 19:09:15 +0200 Subject: [PATCH] Fix UNSIGNED values in QMYSQL The unsigned flag in columns was ignored when creating the list of bound values in a mysql table. So the result iteration with QSqlQuery::next stops after the first wrong truncated value. [ChangeLog][QtSql] Fixed QSqlQuery::prepare value truncation error when using UNSIGNED values in a MySQL database. Task-number: QTBUG-53969 Task-number: QTBUG-53237 Change-Id: I10d977993445f2794f1dd8c88b2e83517ef524f3 Reviewed-by: Milian Wolff --- src/sql/drivers/mysql/qsql_mysql.cpp | 1 + .../sql/kernel/qsqlquery/tst_qsqlquery.cpp | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/sql/drivers/mysql/qsql_mysql.cpp b/src/sql/drivers/mysql/qsql_mysql.cpp index 96bdcc42fa..55bf499e42 100644 --- a/src/sql/drivers/mysql/qsql_mysql.cpp +++ b/src/sql/drivers/mysql/qsql_mysql.cpp @@ -387,6 +387,7 @@ bool QMYSQLResultPrivate::bindInValues() bind->buffer_length = f.bufLength = fieldInfo->length + 1; bind->is_null = &f.nullIndicator; bind->length = &f.bufLength; + bind->is_unsigned = fieldInfo->flags & UNSIGNED_FLAG ? 1 : 0; f.outField=field; ++i; diff --git a/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp b/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp index bd553d5ffd..f1c4333ccd 100644 --- a/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp +++ b/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp @@ -233,6 +233,9 @@ private slots: void QTBUG_36211_data() { generic_data("QPSQL"); } void QTBUG_36211(); + void QTBUG_53969_data() { generic_data("QMYSQL"); } + void QTBUG_53969(); + void sqlite_constraint_data() { generic_data("QSQLITE"); } void sqlite_constraint(); @@ -3652,6 +3655,42 @@ void tst_QSqlQuery::QTBUG_36211() } } +void tst_QSqlQuery::QTBUG_53969() +{ + QFETCH( QString, dbName ); + QVector values = QVector() << 10 << 20 << 127 << 128 << 1, tableValues; + QSqlDatabase db = QSqlDatabase::database( dbName ); + CHECK_DATABASE( db ); + tableValues.reserve(values.size()); + if (tst_Databases::getDatabaseType(db) == QSqlDriver::MySqlServer) { + const QString tableName(qTableName("bug53969", __FILE__, db)); + tst_Databases::safeDropTable( db, tableName ); + + QSqlQuery q(db); + QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (id INT AUTO_INCREMENT PRIMARY KEY, " + "test_number TINYINT(3) UNSIGNED)") + .arg(tableName))); + + QVERIFY_SQL(q, prepare("INSERT INTO " + tableName + " (test_number) VALUES (:value)")); + + QVector::iterator begin = values.begin(), end = values.end(), it; + for (it = begin; it != end; ++it) { + q.bindValue(":value", *it); + QVERIFY_SQL(q, exec()); + } + + QVERIFY_SQL(q, prepare("SELECT test_number FROM " + tableName)); + QVERIFY_SQL(q, exec()); + + while (q.next()) { + bool ok; + tableValues.push_back(q.value(0).toUInt(&ok)); + QVERIFY(ok); + } + QCOMPARE(values, tableValues); + } +} + void tst_QSqlQuery::oraOCINumber() { QFETCH( QString, dbName );