From 0f63862059ed2ca47f1b2ed802f99bd2b320c1e0 Mon Sep 17 00:00:00 2001 From: Hernan Martinez Date: Wed, 27 Jul 2022 14:23:34 -0600 Subject: [PATCH] Only support extracting JSON fields when the SDK supports it (#3717) * Added preprocessor defined to detect support for JSON * Only support extracting JSON fields when the SDK supports it * Fix version comparison --- Data/MySQL/include/Poco/Data/MySQL/Extractor.h | 4 ++-- Data/MySQL/include/Poco/Data/MySQL/MySQL.h | 12 ++++++++++++ Data/MySQL/src/Extractor.cpp | 10 ++++++++-- Data/MySQL/src/ResultMetadata.cpp | 2 ++ Data/MySQL/testsuite/src/MySQLTest.cpp | 7 ++++--- Data/MySQL/testsuite/src/MySQLTest.h | 4 ++++ Data/MySQL/testsuite/src/SQLExecutor.cpp | 3 ++- Data/MySQL/testsuite/src/SQLExecutor.h | 2 ++ 8 files changed, 36 insertions(+), 8 deletions(-) diff --git a/Data/MySQL/include/Poco/Data/MySQL/Extractor.h b/Data/MySQL/include/Poco/Data/MySQL/Extractor.h index 6952f8cd4..606916b7c 100644 --- a/Data/MySQL/include/Poco/Data/MySQL/Extractor.h +++ b/Data/MySQL/include/Poco/Data/MySQL/Extractor.h @@ -324,9 +324,9 @@ private: bool realExtractFixed(std::size_t pos, enum_field_types type, void* buffer, bool isUnsigned = false); bool extractLongLOB(std::size_t pos); - +#ifdef POCO_MYSQL_JSON bool extractJSON(std::size_t pos); - +#endif // Prevent VC8 warning "operator= could not be generated" Extractor& operator=(const Extractor&); diff --git a/Data/MySQL/include/Poco/Data/MySQL/MySQL.h b/Data/MySQL/include/Poco/Data/MySQL/MySQL.h index 6ddcd3988..238659094 100644 --- a/Data/MySQL/include/Poco/Data/MySQL/MySQL.h +++ b/Data/MySQL/include/Poco/Data/MySQL/MySQL.h @@ -62,5 +62,17 @@ #endif #endif +// +// Detect support for JSON data type +// +#if defined(MARIADB_VERSION_ID) + #if MARIADB_VERSION_ID >= 100207 + #define POCO_MYSQL_JSON + #endif +#else + #if MYSQL_VERSION_ID >= 50708 + #define POCO_MYSQL_JSON + #endif +#endif #endif // MySQL_MySQL_INCLUDED diff --git a/Data/MySQL/src/Extractor.cpp b/Data/MySQL/src/Extractor.cpp index cb06a2d5d..9af6ba46e 100644 --- a/Data/MySQL/src/Extractor.cpp +++ b/Data/MySQL/src/Extractor.cpp @@ -128,12 +128,16 @@ bool Extractor::extract(std::size_t pos, std::string& val) //mysql reports TEXT types as FDT_BLOB when being extracted MetaColumn::ColumnDataType columnType = _metadata.metaColumn(static_cast(pos)).type(); +#ifdef POCO_MYSQL_JSON if (columnType != Poco::Data::MetaColumn::FDT_STRING && columnType != Poco::Data::MetaColumn::FDT_BLOB && columnType != Poco::Data::MetaColumn::FDT_JSON) +#else + if (columnType != Poco::Data::MetaColumn::FDT_STRING && columnType != Poco::Data::MetaColumn::FDT_BLOB) +#endif throw MySQLException("Extractor: not a string"); - +#ifdef POCO_MYSQL_JSON if (columnType == Poco::Data::MetaColumn::FDT_JSON && !extractJSON(pos)) return false; - +#endif if (columnType == Poco::Data::MetaColumn::FDT_BLOB && !extractLongLOB(pos)) return false; @@ -291,6 +295,7 @@ bool Extractor::extractLongLOB(std::size_t pos) return true; } +#ifdef POCO_MYSQL_JSON bool Extractor::extractJSON(std::size_t pos) { // JSON columns are fetched with a zero-length @@ -306,6 +311,7 @@ bool Extractor::extractJSON(std::size_t pos) return true; } +#endif ////////////// // Not implemented diff --git a/Data/MySQL/src/ResultMetadata.cpp b/Data/MySQL/src/ResultMetadata.cpp index 33c3785ed..61b49dd54 100644 --- a/Data/MySQL/src/ResultMetadata.cpp +++ b/Data/MySQL/src/ResultMetadata.cpp @@ -72,7 +72,9 @@ namespace case MYSQL_TYPE_MEDIUM_BLOB: case MYSQL_TYPE_LONG_BLOB: case MYSQL_TYPE_BLOB: +#ifdef POCO_MYSQL_JSON case MYSQL_TYPE_JSON: +#endif return field.length; default: diff --git a/Data/MySQL/testsuite/src/MySQLTest.cpp b/Data/MySQL/testsuite/src/MySQLTest.cpp index cbe6a5dc8..80a3683b1 100644 --- a/Data/MySQL/testsuite/src/MySQLTest.cpp +++ b/Data/MySQL/testsuite/src/MySQLTest.cpp @@ -487,6 +487,7 @@ void MySQLTest::testLongTEXT() _pExecutor->longText(); } +#ifdef POCO_MYSQL_JSON void MySQLTest::testJSON() { if (!_pSession) fail("Test not available."); @@ -494,7 +495,7 @@ void MySQLTest::testJSON() recreatePersonJSONTable(); _pExecutor->json(); } - +#endif void MySQLTest::testUnsignedInts() { @@ -796,7 +797,7 @@ void MySQLTest::recreatePersonLongBLOBTable() catch(StatementException& se){ std::cout << se.displayText() << std::endl; fail ("recreatePersonLongBLOBTable()"); } } - +#ifdef POCO_MYSQL_JSON void MySQLTest::recreatePersonJSONTable() { dropTable("Person"); @@ -804,7 +805,7 @@ void MySQLTest::recreatePersonJSONTable() catch (ConnectionException& ce) { std::cout << ce.displayText() << std::endl; fail("recreatePersonJSONTable()"); } catch (StatementException& se) { std::cout << se.displayText() << std::endl; fail("recreatePersonJSONTable()"); } } - +#endif void MySQLTest::recreateIntsTable() { diff --git a/Data/MySQL/testsuite/src/MySQLTest.h b/Data/MySQL/testsuite/src/MySQLTest.h index 8afdfb2d2..9a2568b3d 100644 --- a/Data/MySQL/testsuite/src/MySQLTest.h +++ b/Data/MySQL/testsuite/src/MySQLTest.h @@ -81,7 +81,9 @@ public: void testBLOBStmt(); void testLongBLOB(); void testLongTEXT(); +#ifdef POCO_MYSQL_JSON void testJSON(); +#endif void testUnsignedInts(); void testFloat(); @@ -122,7 +124,9 @@ private: void recreatePersonTimeTable(); void recreatePersonTimestampTable(); void recreatePersonLongBLOBTable(); +#ifdef POCO_MYSQL_JSON void recreatePersonJSONTable(); +#endif void recreateStringsTable(); void recreateIntsTable(); void recreateUnsignedIntsTable(); diff --git a/Data/MySQL/testsuite/src/SQLExecutor.cpp b/Data/MySQL/testsuite/src/SQLExecutor.cpp index 6c036b9b4..cd43f7797 100644 --- a/Data/MySQL/testsuite/src/SQLExecutor.cpp +++ b/Data/MySQL/testsuite/src/SQLExecutor.cpp @@ -1513,6 +1513,7 @@ void SQLExecutor::longText() poco_assert (longTextRes == biography); } +#ifdef POCO_MYSQL_JSON void SQLExecutor::json() { std::string funct = "json()"; @@ -1537,7 +1538,7 @@ void SQLExecutor::json() catch (StatementException& se) { std::cout << se.displayText() << std::endl; fail(funct); } poco_assert(res == biography); } - +#endif void SQLExecutor::tuples() { diff --git a/Data/MySQL/testsuite/src/SQLExecutor.h b/Data/MySQL/testsuite/src/SQLExecutor.h index 0bec61457..ba300212e 100644 --- a/Data/MySQL/testsuite/src/SQLExecutor.h +++ b/Data/MySQL/testsuite/src/SQLExecutor.h @@ -86,7 +86,9 @@ public: void timestamp(); void longBlob(); void longText(); +#ifdef POCO_MYSQL_JSON void json(); +#endif void unsignedInts(); void floats(); void doubles();