diff SQLite/Plugins/SQLiteIndex.cpp @ 85:1012fe77241c db-changes

new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 10 Jan 2019 18:04:12 +0100
parents 714c5d2bee76
children 4cd7e45b671e
line wrap: on
line diff
--- a/SQLite/Plugins/SQLiteIndex.cpp	Thu Jan 10 13:33:33 2019 +0100
+++ b/SQLite/Plugins/SQLiteIndex.cpp	Thu Jan 10 18:04:12 2019 +0100
@@ -21,6 +21,7 @@
 
 #include "SQLiteIndex.h"
 
+#include "../../Framework/Common/Integer64Value.h"
 #include "../../Framework/Plugins/GlobalProperties.h"
 #include "../../Framework/SQLite/SQLiteDatabase.h"
 #include "../../Framework/SQLite/SQLiteTransaction.h"
@@ -173,4 +174,35 @@
 
     return dynamic_cast<SQLiteDatabase&>(statement.GetDatabase()).GetLastInsertRowId();
   }
+
+
+  int64_t SQLiteIndex::GetLastChangeIndex()
+  {
+    DatabaseManager::CachedStatement statement(
+      STATEMENT_FROM_HERE, GetManager(),
+      "SELECT seq FROM sqlite_sequence WHERE name='Changes'");
+
+    statement.SetReadOnly(true);
+    statement.Execute();
+    
+    if (statement.IsDone())
+    {
+      // No change has been recorded so far in the database
+      return 0;
+    }
+    else
+    {
+      const IValue& value = statement.GetResultField(0);
+      
+      switch (value.GetType())
+      {
+        case ValueType_Integer64:
+          return dynamic_cast<const Integer64Value&>(value).GetValue();
+          
+        default:
+          //LOG(ERROR) << value.Format();
+          throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
+      }
+    }
+  }
 }