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
This commit is contained in:
Hernan Martinez
2022-07-27 14:23:34 -06:00
committed by GitHub
parent 459f1ea19c
commit 0f63862059
8 changed files with 36 additions and 8 deletions

View File

@@ -324,9 +324,9 @@ private:
bool realExtractFixed(std::size_t pos, enum_field_types type, void* buffer, bool isUnsigned = false); bool realExtractFixed(std::size_t pos, enum_field_types type, void* buffer, bool isUnsigned = false);
bool extractLongLOB(std::size_t pos); bool extractLongLOB(std::size_t pos);
#ifdef POCO_MYSQL_JSON
bool extractJSON(std::size_t pos); bool extractJSON(std::size_t pos);
#endif
// Prevent VC8 warning "operator= could not be generated" // Prevent VC8 warning "operator= could not be generated"
Extractor& operator=(const Extractor&); Extractor& operator=(const Extractor&);

View File

@@ -62,5 +62,17 @@
#endif #endif
#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 #endif // MySQL_MySQL_INCLUDED

View File

@@ -128,12 +128,16 @@ bool Extractor::extract(std::size_t pos, std::string& val)
//mysql reports TEXT types as FDT_BLOB when being extracted //mysql reports TEXT types as FDT_BLOB when being extracted
MetaColumn::ColumnDataType columnType = _metadata.metaColumn(static_cast<Poco::UInt32>(pos)).type(); MetaColumn::ColumnDataType columnType = _metadata.metaColumn(static_cast<Poco::UInt32>(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) 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"); throw MySQLException("Extractor: not a string");
#ifdef POCO_MYSQL_JSON
if (columnType == Poco::Data::MetaColumn::FDT_JSON && !extractJSON(pos)) if (columnType == Poco::Data::MetaColumn::FDT_JSON && !extractJSON(pos))
return false; return false;
#endif
if (columnType == Poco::Data::MetaColumn::FDT_BLOB && !extractLongLOB(pos)) if (columnType == Poco::Data::MetaColumn::FDT_BLOB && !extractLongLOB(pos))
return false; return false;
@@ -291,6 +295,7 @@ bool Extractor::extractLongLOB(std::size_t pos)
return true; return true;
} }
#ifdef POCO_MYSQL_JSON
bool Extractor::extractJSON(std::size_t pos) bool Extractor::extractJSON(std::size_t pos)
{ {
// JSON columns are fetched with a zero-length // JSON columns are fetched with a zero-length
@@ -306,6 +311,7 @@ bool Extractor::extractJSON(std::size_t pos)
return true; return true;
} }
#endif
////////////// //////////////
// Not implemented // Not implemented

View File

@@ -72,7 +72,9 @@ namespace
case MYSQL_TYPE_MEDIUM_BLOB: case MYSQL_TYPE_MEDIUM_BLOB:
case MYSQL_TYPE_LONG_BLOB: case MYSQL_TYPE_LONG_BLOB:
case MYSQL_TYPE_BLOB: case MYSQL_TYPE_BLOB:
#ifdef POCO_MYSQL_JSON
case MYSQL_TYPE_JSON: case MYSQL_TYPE_JSON:
#endif
return field.length; return field.length;
default: default:

View File

@@ -487,6 +487,7 @@ void MySQLTest::testLongTEXT()
_pExecutor->longText(); _pExecutor->longText();
} }
#ifdef POCO_MYSQL_JSON
void MySQLTest::testJSON() void MySQLTest::testJSON()
{ {
if (!_pSession) fail("Test not available."); if (!_pSession) fail("Test not available.");
@@ -494,7 +495,7 @@ void MySQLTest::testJSON()
recreatePersonJSONTable(); recreatePersonJSONTable();
_pExecutor->json(); _pExecutor->json();
} }
#endif
void MySQLTest::testUnsignedInts() void MySQLTest::testUnsignedInts()
{ {
@@ -796,7 +797,7 @@ void MySQLTest::recreatePersonLongBLOBTable()
catch(StatementException& se){ std::cout << se.displayText() << std::endl; fail ("recreatePersonLongBLOBTable()"); } catch(StatementException& se){ std::cout << se.displayText() << std::endl; fail ("recreatePersonLongBLOBTable()"); }
} }
#ifdef POCO_MYSQL_JSON
void MySQLTest::recreatePersonJSONTable() void MySQLTest::recreatePersonJSONTable()
{ {
dropTable("Person"); dropTable("Person");
@@ -804,7 +805,7 @@ void MySQLTest::recreatePersonJSONTable()
catch (ConnectionException& ce) { std::cout << ce.displayText() << std::endl; fail("recreatePersonJSONTable()"); } catch (ConnectionException& ce) { std::cout << ce.displayText() << std::endl; fail("recreatePersonJSONTable()"); }
catch (StatementException& se) { std::cout << se.displayText() << std::endl; fail("recreatePersonJSONTable()"); } catch (StatementException& se) { std::cout << se.displayText() << std::endl; fail("recreatePersonJSONTable()"); }
} }
#endif
void MySQLTest::recreateIntsTable() void MySQLTest::recreateIntsTable()
{ {

View File

@@ -81,7 +81,9 @@ public:
void testBLOBStmt(); void testBLOBStmt();
void testLongBLOB(); void testLongBLOB();
void testLongTEXT(); void testLongTEXT();
#ifdef POCO_MYSQL_JSON
void testJSON(); void testJSON();
#endif
void testUnsignedInts(); void testUnsignedInts();
void testFloat(); void testFloat();
@@ -122,7 +124,9 @@ private:
void recreatePersonTimeTable(); void recreatePersonTimeTable();
void recreatePersonTimestampTable(); void recreatePersonTimestampTable();
void recreatePersonLongBLOBTable(); void recreatePersonLongBLOBTable();
#ifdef POCO_MYSQL_JSON
void recreatePersonJSONTable(); void recreatePersonJSONTable();
#endif
void recreateStringsTable(); void recreateStringsTable();
void recreateIntsTable(); void recreateIntsTable();
void recreateUnsignedIntsTable(); void recreateUnsignedIntsTable();

View File

@@ -1513,6 +1513,7 @@ void SQLExecutor::longText()
poco_assert (longTextRes == biography); poco_assert (longTextRes == biography);
} }
#ifdef POCO_MYSQL_JSON
void SQLExecutor::json() void SQLExecutor::json()
{ {
std::string funct = "json()"; std::string funct = "json()";
@@ -1537,7 +1538,7 @@ void SQLExecutor::json()
catch (StatementException& se) { std::cout << se.displayText() << std::endl; fail(funct); } catch (StatementException& se) { std::cout << se.displayText() << std::endl; fail(funct); }
poco_assert(res == biography); poco_assert(res == biography);
} }
#endif
void SQLExecutor::tuples() void SQLExecutor::tuples()
{ {

View File

@@ -86,7 +86,9 @@ public:
void timestamp(); void timestamp();
void longBlob(); void longBlob();
void longText(); void longText();
#ifdef POCO_MYSQL_JSON
void json(); void json();
#endif
void unsignedInts(); void unsignedInts();
void floats(); void floats();
void doubles(); void doubles();