diff PostgreSQL/Plugins/PostgreSQLIndex.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 cb0aac9bbada
children eb08ec14fb04
line wrap: on
line diff
--- a/PostgreSQL/Plugins/PostgreSQLIndex.cpp	Thu Jan 10 13:33:33 2019 +0100
+++ b/PostgreSQL/Plugins/PostgreSQLIndex.cpp	Thu Jan 10 18:04:12 2019 +0100
@@ -37,6 +37,7 @@
   static const GlobalProperty GlobalProperty_HasTrigramIndex = GlobalProperty_DatabaseInternal0;
   static const GlobalProperty GlobalProperty_HasCreateInstance = GlobalProperty_DatabaseInternal1;
   static const GlobalProperty GlobalProperty_HasFastCountResources = GlobalProperty_DatabaseInternal2;
+  static const GlobalProperty GlobalProperty_GetLastChangeIndex = GlobalProperty_DatabaseInternal3;
 }
 
 
@@ -236,6 +237,29 @@
       t.Commit();
     }
 
+    {
+      PostgreSQLTransaction t(*db);
+
+      // Installing this extension requires the "GlobalIntegers" table
+      // created by the "GetLastChangeIndex" extension
+      int property = 0;
+      if (!LookupGlobalIntegerProperty(property, *db, t,
+                                       Orthanc::GlobalProperty_GetLastChangeIndex) ||
+          property != 1)
+      {
+        LOG(INFO) << "Installing the GetLastChangeIndex extension";
+
+        std::string query;
+        Orthanc::EmbeddedResources::GetFileResource
+          (query, Orthanc::EmbeddedResources::POSTGRESQL_GET_LAST_CHANGE_INDEX);
+        db->Execute(query);
+
+        SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_GetLastChangeIndex, 1);
+      }
+
+      t.Commit();
+    }
+
     return db.release();
   }
 
@@ -394,4 +418,17 @@
     assert(result == IndexBackend::GetResourceCount(resourceType));
     return result;
   }
+
+
+  int64_t PostgreSQLIndex::GetLastChangeIndex()
+  {
+    DatabaseManager::CachedStatement statement(
+      STATEMENT_FROM_HERE, GetManager(),
+      "SELECT value FROM GlobalIntegers WHERE key = 6");
+
+    statement.SetReadOnly(true);
+    statement.Execute();
+
+    return ReadInteger64(statement, 0);
+  }
 }