SQL/MySQL: fix handling of json column
Add handling for MYSQL_TYPE_JSON by treating it the same as MYSQL_TYPE_BLOB (which is used by current MariaDB Server for a json column) Fixes: QTBUG-101680 Change-Id: I4d4b0cdad73cd12e0db4df4021fddbd6a649c8ed Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io> (cherry picked from commit 9d27c07e284457fb86258ab518f39c5cab1dac80) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
f8b151891e
commit
5ed99a42a6
@ -228,6 +228,7 @@ static QMetaType qDecodeMYSQLType(int mysqltype, uint flags)
|
|||||||
case FIELD_TYPE_MEDIUM_BLOB :
|
case FIELD_TYPE_MEDIUM_BLOB :
|
||||||
case FIELD_TYPE_LONG_BLOB :
|
case FIELD_TYPE_LONG_BLOB :
|
||||||
case FIELD_TYPE_GEOMETRY :
|
case FIELD_TYPE_GEOMETRY :
|
||||||
|
case MYSQL_TYPE_JSON :
|
||||||
type = (flags & BINARY_FLAG) ? QMetaType::QByteArray : QMetaType::QString;
|
type = (flags & BINARY_FLAG) ? QMetaType::QByteArray : QMetaType::QString;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@ -266,7 +267,8 @@ static bool qIsBlob(int t)
|
|||||||
return t == MYSQL_TYPE_TINY_BLOB
|
return t == MYSQL_TYPE_TINY_BLOB
|
||||||
|| t == MYSQL_TYPE_BLOB
|
|| t == MYSQL_TYPE_BLOB
|
||||||
|| t == MYSQL_TYPE_MEDIUM_BLOB
|
|| t == MYSQL_TYPE_MEDIUM_BLOB
|
||||||
|| t == MYSQL_TYPE_LONG_BLOB;
|
|| t == MYSQL_TYPE_LONG_BLOB
|
||||||
|
|| t == MYSQL_TYPE_JSON;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool qIsTimeOrDate(int t)
|
static bool qIsTimeOrDate(int t)
|
||||||
|
@ -226,6 +226,9 @@ private slots:
|
|||||||
void sqlite_real_data() { generic_data("QSQLITE"); }
|
void sqlite_real_data() { generic_data("QSQLITE"); }
|
||||||
void sqlite_real();
|
void sqlite_real();
|
||||||
|
|
||||||
|
void prepared_query_json_row_data() { generic_data(); }
|
||||||
|
void prepared_query_json_row();
|
||||||
|
|
||||||
void aggregateFunctionTypes_data() { generic_data(); }
|
void aggregateFunctionTypes_data() { generic_data(); }
|
||||||
void aggregateFunctionTypes();
|
void aggregateFunctionTypes();
|
||||||
|
|
||||||
@ -4443,6 +4446,43 @@ void tst_QSqlQuery::sqlite_real()
|
|||||||
QCOMPARE(q.value(0).toDouble(), 5.6);
|
QCOMPARE(q.value(0).toDouble(), 5.6);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void tst_QSqlQuery::prepared_query_json_row()
|
||||||
|
{
|
||||||
|
QFETCH(QString, dbName);
|
||||||
|
QSqlDatabase db = QSqlDatabase::database(dbName);
|
||||||
|
CHECK_DATABASE(db);
|
||||||
|
if (tst_Databases::getDatabaseType(db) != QSqlDriver::MySqlServer &&
|
||||||
|
tst_Databases::getDatabaseType(db) != QSqlDriver::PostgreSQL) {
|
||||||
|
QSKIP("PostgreSQL / MySQL specific test");
|
||||||
|
}
|
||||||
|
|
||||||
|
const QString tableName(qTableName("tableWithJsonRow", __FILE__, db));
|
||||||
|
tst_Databases::safeDropTable(db, tableName);
|
||||||
|
|
||||||
|
QSqlQuery q(db);
|
||||||
|
const QLatin1String vals[] = {QLatin1String("{\"certificateNumber\": \"CERT-001\"}"),
|
||||||
|
QLatin1String("{\"certificateNumber\": \"CERT-002\"}")};
|
||||||
|
QVERIFY_SQL(q, exec(QLatin1String("CREATE TABLE %1 (id INTEGER, value JSON)").arg(tableName)));
|
||||||
|
for (const QLatin1String &json : vals) {
|
||||||
|
QVERIFY_SQL(q, exec(QLatin1String("INSERT INTO %1 (id, value) VALUES (1, '%2')")
|
||||||
|
.arg(tableName, json)));
|
||||||
|
}
|
||||||
|
|
||||||
|
QVERIFY_SQL(q, prepare(QLatin1String("SELECT id, value FROM %1 WHERE id = ?").arg(tableName)));
|
||||||
|
q.addBindValue(1);
|
||||||
|
QVERIFY_SQL(q, exec());
|
||||||
|
|
||||||
|
size_t iCount = 0;
|
||||||
|
while (q.next()) {
|
||||||
|
QVERIFY(iCount < sizeof(vals));
|
||||||
|
const int id = q.value(0).toInt();
|
||||||
|
const QByteArray json = q.value(1).toByteArray();
|
||||||
|
QCOMPARE(id, 1);
|
||||||
|
QCOMPARE(json, vals[iCount].data());
|
||||||
|
++iCount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void tst_QSqlQuery::aggregateFunctionTypes()
|
void tst_QSqlQuery::aggregateFunctionTypes()
|
||||||
{
|
{
|
||||||
QFETCH(QString, dbName);
|
QFETCH(QString, dbName);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user