changeset 609:591ce1e2d367

added ordering with cast to int/float
author Alain Mazy <am@orthanc.team>
date Mon, 16 Dec 2024 18:09:40 +0100
parents 3d853c8f5a9e
children c2497f8bf5b6
files Framework/Plugins/ISqlLookupFormatter.cpp Framework/Plugins/ISqlLookupFormatter.h Framework/Plugins/IndexBackend.cpp
diffstat 3 files changed, 51 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Plugins/ISqlLookupFormatter.cpp	Mon Dec 16 16:36:12 2024 +0100
+++ b/Framework/Plugins/ISqlLookupFormatter.cpp	Mon Dec 16 18:09:40 2024 +0100
@@ -924,8 +924,21 @@
         {
           orderByField = "CASE WHEN order" + boost::lexical_cast<std::string>(i) + ".value IS NULL THEN 1 ELSE 0 END, ";
         }
-        orderByField += "order" + boost::lexical_cast<std::string>(i) + ".value";
-         
+
+        switch (ordering.cast())
+        {
+          case Orthanc::DatabasePluginMessages::OrderingCast::ORDERING_CAST_INT:
+            orderByField += "CAST(order" + boost::lexical_cast<std::string>(i) + ".value AS " + formatter.FormatIntegerCast() + ")";
+            break;
+          case Orthanc::DatabasePluginMessages::OrderingCast::ORDERING_CAST_FLOAT:
+            orderByField += "CAST(order" + boost::lexical_cast<std::string>(i) + ".value AS " + formatter.FormatFloatCast() + ")";
+            break;
+          case Orthanc::DatabasePluginMessages::OrderingCast::ORDERING_CAST_STRING:
+          default:
+            orderByField += "order" + boost::lexical_cast<std::string>(i) + ".value";
+            break;
+        }
+
         if (ordering.direction() == Orthanc::DatabasePluginMessages::OrderingDirection::ORDERING_DIRECTION_ASC)
         {
           orderByField += " ASC";
--- a/Framework/Plugins/ISqlLookupFormatter.h	Mon Dec 16 16:36:12 2024 +0100
+++ b/Framework/Plugins/ISqlLookupFormatter.h	Mon Dec 16 18:09:40 2024 +0100
@@ -73,6 +73,10 @@
 
     virtual bool SupportsNullsLast() const = 0;
 
+    virtual std::string FormatIntegerCast() const = 0;
+
+    virtual std::string FormatFloatCast() const = 0;
+
     static void GetLookupLevels(Orthanc::ResourceType& lowerLevel,
                                 Orthanc::ResourceType& upperLevel,
                                 const Orthanc::ResourceType& queryLevel,
--- a/Framework/Plugins/IndexBackend.cpp	Mon Dec 16 16:36:12 2024 +0100
+++ b/Framework/Plugins/IndexBackend.cpp	Mon Dec 16 18:09:40 2024 +0100
@@ -2282,6 +2282,38 @@
       return (dialect_ == Dialect_PostgreSQL);
     }
 
+    virtual std::string FormatIntegerCast() const
+    {
+      switch (dialect_)
+      {
+        case Dialect_MSSQL:
+          return "INT";
+        case Dialect_SQLite:
+        case Dialect_PostgreSQL:
+          return "INTEGER";
+        case Dialect_MySQL:
+          return "SIGNED";
+        default:
+          throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
+      }
+    }
+
+    virtual std::string FormatFloatCast() const
+    {
+      switch (dialect_)
+      {
+        case Dialect_SQLite:
+          return "REAL";
+        case Dialect_MSSQL:
+        case Dialect_PostgreSQL:
+          return "FLOAT";
+        case Dialect_MySQL:
+          return "DECIMAL(10,10)";
+        default:
+          throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
+      }
+    }
+
     void PrepareStatement(DatabaseManager::StandaloneStatement& statement) const
     {
       statement.SetReadOnly(true);