@@ -16,10 +16,20 @@ namespace opsqlite {
1616 std::unordered_map<std::string, sqlite3 *> dbMap = std::unordered_map<std::string, sqlite3 *>();
1717 std::unordered_map<
1818 std::string,
19- std::function<void (std::string dbName, std::string tableName, std::string operation, int rowId)>> callbackMap =
19+ std::function<void (std::string dbName, std::string tableName, std::string operation, int rowId)>> updateCallbackMap =
2020 std::unordered_map<
2121 std::string,
2222 std::function<void (std::string dbName, std::string tableName, std::string operation, int rowId)>>();
23+
24+ std::unordered_map<
25+ std::string,
26+ std::function<void (std::string dbName)>> commitCallbackMap =
27+ std::unordered_map<std::string, std::function<void (std::string dbName)>>();
28+
29+ std::unordered_map<
30+ std::string,
31+ std::function<void (std::string dbName)>> rollbackCallbackMap =
32+ std::unordered_map<std::string, std::function<void (std::string dbName)>>();
2333
2434 bool folder_exists (const std::string &foldername)
2535 {
@@ -490,32 +500,34 @@ namespace opsqlite {
490500 dbMap.clear ();
491501 }
492502
493- std::string operationToString (int operation_type) {
494- switch (operation_type) {
495- case SQLITE_INSERT:
496- return " INSERT" ;
497-
498- case SQLITE_DELETE:
499- return " DELETE" ;
503+ std::string operationToString (int operation_type) {
504+ switch (operation_type) {
505+ case SQLITE_INSERT:
506+ return " INSERT" ;
500507
501- case SQLITE_UPDATE:
502- return " UPDATE" ;
503-
504- default :
505- throw std::invalid_argument (" Uknown SQLite operation on hook" );
508+ case SQLITE_DELETE:
509+ return " DELETE" ;
510+
511+ case SQLITE_UPDATE:
512+ return " UPDATE" ;
513+
514+ default :
515+ throw std::invalid_argument (" Uknown SQLite operation on hook" );
516+ }
506517 }
507- }
508518
509- void update_callback ( void *dbName, int operation_type,
510- char const *database, char const *table,
511- sqlite3_int64 rowid)
512- {
513- std::string &strDbName = *(static_cast <std::string*>(dbName));
514- auto callback = callbackMap[strDbName];
515- callback (strDbName, std::string (table), operationToString (operation_type), static_cast <int >(rowid));
519+ void update_callback (void *dbName,
520+ int operation_type,
521+ char const *database,
522+ char const *table,
523+ sqlite3_int64 rowid) {
524+ std::string &strDbName = *(static_cast <std::string*>(dbName));
525+ auto callback = updateCallbackMap[strDbName];
526+ callback (strDbName, std::string (table), operationToString (operation_type), static_cast <int >(rowid));
516527 }
517528
518- BridgeResult registerUpdateHook (std::string const dbName, std::function<void (std::string dbName, std::string tableName, std::string operation, int rowId)> const callback) {
529+ BridgeResult registerUpdateHook (std::string const dbName,
530+ std::function<void (std::string dbName, std::string tableName, std::string operation, int rowId)> const callback) {
519531 if (dbMap.count (dbName) == 0 )
520532 {
521533 return {
@@ -525,7 +537,7 @@ void update_callback ( void *dbName, int operation_type,
525537 }
526538
527539 sqlite3 *db = dbMap[dbName];
528- callbackMap [dbName] = callback;
540+ updateCallbackMap [dbName] = callback;
529541 const std::string *key = nullptr ;
530542
531543 // TODO find a more elegant way to retrieve a reference to the key
@@ -544,4 +556,80 @@ void update_callback ( void *dbName, int operation_type,
544556 SQLiteOk
545557 };
546558 }
559+
560+ int commit_callback (void *dbName) {
561+ std::string &strDbName = *(static_cast <std::string*>(dbName));
562+ auto callback = commitCallbackMap[strDbName];
563+ callback (strDbName);
564+ // You need to return 0 to allow commits to continue
565+ return 0 ;
566+ }
567+
568+ BridgeResult registerCommitHook (std::string const dbName,
569+ std::function<void (std::string dbName)> const callback) {
570+ if (dbMap.count (dbName) == 0 )
571+ {
572+ return {
573+ SQLiteError,
574+ " [op-sqlite] Database not opened: " + dbName
575+ };
576+ }
577+
578+ sqlite3 *db = dbMap[dbName];
579+ commitCallbackMap[dbName] = callback;
580+ const std::string *key = nullptr ;
581+
582+ // TODO find a more elegant way to retrieve a reference to the key
583+ for (auto const & element : dbMap) {
584+ if (element.first == dbName) {
585+ key = &element.first ;
586+ }
587+ }
588+
589+ sqlite3_commit_hook (
590+ db,
591+ &commit_callback,
592+ (void *)key);
593+
594+ return {
595+ SQLiteOk
596+ };
597+ }
598+
599+ void rollback_callback (void *dbName) {
600+ std::string &strDbName = *(static_cast <std::string*>(dbName));
601+ auto callback = rollbackCallbackMap[strDbName];
602+ callback (strDbName);
603+ }
604+
605+ BridgeResult registerRollbackHook (std::string const dbName,
606+ std::function<void (std::string dbName)> const callback) {
607+ if (dbMap.count (dbName) == 0 )
608+ {
609+ return {
610+ SQLiteError,
611+ " [op-sqlite] Database not opened: " + dbName
612+ };
613+ }
614+
615+ sqlite3 *db = dbMap[dbName];
616+ rollbackCallbackMap[dbName] = callback;
617+ const std::string *key = nullptr ;
618+
619+ // TODO find a more elegant way to retrieve a reference to the key
620+ for (auto const & element : dbMap) {
621+ if (element.first == dbName) {
622+ key = &element.first ;
623+ }
624+ }
625+
626+ sqlite3_rollback_hook (
627+ db,
628+ &rollback_callback,
629+ (void *)key);
630+
631+ return {
632+ SQLiteOk
633+ };
634+ }
547635}
0 commit comments