SQLite update event handling

This commit is contained in:
aleks-f
2013-02-10 22:27:16 -06:00
parent 4bcddad43e
commit 93c9e83e8d
4 changed files with 218 additions and 12 deletions

View File

@@ -43,6 +43,7 @@
#include "Poco/Data/SQLite/SQLite.h"
#include "Poco/Data/MetaColumn.h"
#include "Poco/Mutex.h"
#include "Poco/Types.h"
#include <map>
@@ -67,13 +68,9 @@ public:
static const int THREAD_MODE_MULTI;
static const int THREAD_MODE_SERIAL;
Utility();
/// Maps SQLite column declared types to Poco::Data types through
/// static TypeMap member.
/// Note: SQLite is type-agnostic and it is the end-user responsibility
/// to ensure that column declared data type corresponds to the type of
/// data actually held in the database.
/// Column types are case-insensitive.
static const int OPERATION_INSERT;
static const int OPERATION_DELETE;
static const int OPERATION_UPDATE;
static std::string lastError(sqlite3* pDb);
/// Retreives the last error code from sqlite and converts it to a string
@@ -111,10 +108,63 @@ public:
static int getThreadMode();
/// Returns the thread mode.
typedef void(*UpdateCallbackType)(void*, int, const char*, const char*, Poco::Int64);
/// Update callback function type.
typedef int(*CommitCallbackType)(void*);
/// Commit callback function type.
typedef void(*RollbackCallbackType)(void*);
/// Rollback callback function type.
template <typename T, typename CBT>
static bool registerUpdateHandler(sqlite3* pDB, CBT callbackFn, T* pParam)
/// Registers the callback for (1)(insert, delete, update), (2)(commit) or
/// or (3)(rollback) events. Only one function per group can be registered
/// at a time. Registration is thread-safe. Storage pointed to by pParam
/// must remain valid as long as registration is active. Registering with
/// callbackFn set to zero disables notifications.
///
/// See http://www.sqlite.org/c3ref/update_hook.html and
/// http://www.sqlite.org/c3ref/commit_hook.html for details.
{
typedef std::map<sqlite3*, T*> RetMap;
Poco::Mutex::ScopedLock l(_mutex);
static RetMap retMap;
T* pRet = reinterpret_cast<T*>(eventHook(pDB, callbackFn, pParam));
bool success = false;
if (retMap.find(pDB) == retMap.end())
success = (pRet == 0);
else if (pRet != 0)
success = (*pRet == *retMap[pDB]);
if (success) retMap[pDB] = pParam;
return success;
}
private:
static TypeMap _types;
Poco::FastMutex _mutex;
static int _threadMode;
Utility();
/// Maps SQLite column declared types to Poco::Data types through
/// static TypeMap member.
///
/// Note: SQLite is type-agnostic and it is the end-user responsibility
/// to ensure that column declared data type corresponds to the type of
/// data actually held in the database.
///
/// Column types are case-insensitive.
Utility(const Utility&);
Utility& operator = (const Utility&);
static void* eventHook(sqlite3* pDB, UpdateCallbackType callbackFn, void* pParam);
static void* eventHook(sqlite3* pDB, CommitCallbackType callbackFn, void* pParam);
static void* eventHook(sqlite3* pDB, RollbackCallbackType callbackFn, void* pParam);
static TypeMap _types;
static Poco::Mutex _mutex;
static int _threadMode;
};