From 60ecf597c4a30d6fcca4eb2562527a9902b536c5 Mon Sep 17 00:00:00 2001 From: Dan Hosseinzadeh Date: Thu, 1 Sep 2016 00:46:53 -0400 Subject: [PATCH] Add API to allow SQLite types to be added at runtime - new types can be added via Poco::Data::SQLite::Utility::addColumnType() issue-1375 --- .../SQLite/include/Poco/Data/SQLite/Utility.h | 11 ++++++++++ Data/SQLite/src/Utility.cpp | 20 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/Data/SQLite/include/Poco/Data/SQLite/Utility.h b/Data/SQLite/include/Poco/Data/SQLite/Utility.h index fb9e2e47d..9c1c867b1 100644 --- a/Data/SQLite/include/Poco/Data/SQLite/Utility.h +++ b/Data/SQLite/include/Poco/Data/SQLite/Utility.h @@ -56,6 +56,15 @@ public: static const int OPERATION_DELETE; static const int OPERATION_UPDATE; + static void addColumnType(std::string sqliteType, MetaColumn::ColumnDataType pocoType); + /// Adds or replaces the mapping for SQLite column type \p sqliteType + /// to a Poco type \p pocoType + /// + /// \p sqliteType is a case-insensitive desription of the column type with + /// any value \p pocoType value but MetaColumn::FDT_UNKNOWN. A + /// Poco::Data::NotSupportedException is thrown if \p pocoType is invalid. + + static sqlite3* dbHandle(const Session& session); /// Returns native DB handle. @@ -190,6 +199,8 @@ private: Utility(const Utility&); Utility& operator = (const Utility&); + static void initializeDefaultTypes(); + static void* eventHookRegister(sqlite3* pDB, UpdateCallbackType callbackFn, void* pParam); static void* eventHookRegister(sqlite3* pDB, CommitCallbackType callbackFn, void* pParam); static void* eventHookRegister(sqlite3* pDB, RollbackCallbackType callbackFn, void* pParam); diff --git a/Data/SQLite/src/Utility.cpp b/Data/SQLite/src/Utility.cpp index 000ac101c..04761636d 100644 --- a/Data/SQLite/src/Utility.cpp +++ b/Data/SQLite/src/Utility.cpp @@ -62,6 +62,11 @@ Poco::Mutex Utility::_mutex; Utility::Utility() +{ + initializeDefaultTypes(); +} + +void Utility::initializeDefaultTypes() { if (_types.empty()) { @@ -125,6 +130,21 @@ Utility::Utility() } +void Utility::addColumnType(std::string sqliteType, MetaColumn::ColumnDataType pocoType) +{ + // Check for errors in the mapping + if (MetaColumn::FDT_UNKNOWN == pocoType) + throw Poco::Data::NotSupportedException("Cannot map to unknown poco type."); + + // Initialize default types + initializeDefaultTypes(); + + // Add type to internal map + Poco::toUpperInPlace(sqliteType); + _types[sqliteType] = pocoType; +} + + std::string Utility::lastError(sqlite3* pDB) { return std::string(sqlite3_errmsg(pDB));