diff OrthancServer/Sources/Search/ISqlLookupFormatter.cpp @ 5830:40f236ad829c find-refactoring

SQLite 3.46 + push NULL values at the end of the order by clauses
author Alain Mazy <am@orthanc.team>
date Tue, 08 Oct 2024 17:05:18 +0200
parents 7030fa489669
children
line wrap: on
line diff
--- a/OrthancServer/Sources/Search/ISqlLookupFormatter.cpp	Mon Oct 07 18:10:08 2024 +0200
+++ b/OrthancServer/Sources/Search/ISqlLookupFormatter.cpp	Tue Oct 08 17:05:18 2024 +0200
@@ -759,7 +759,15 @@
         }
         orderingJoins += orderingJoin;
         
-        std::string orderByField = "order" + boost::lexical_cast<std::string>(counter) + ".value";
+        std::string orderByField;
+
+#if ORTHANC_SQLITE_VERSION < 3030001
+        // this is a way to push NULL values at the end before "NULLS LAST" was introduced:
+        // first filter by 0/1 and then by the column value itself
+        orderByField += "order" + boost::lexical_cast<std::string>(counter) + ".value IS NULL, ";
+#endif
+        orderByField += "order" + boost::lexical_cast<std::string>(counter) + ".value";
+
         if ((*it)->GetDirection() == FindRequest::OrderingDirection_Ascending)
         {
           orderByField += " ASC";
@@ -774,7 +782,12 @@
 
       std::string orderByFieldsString;
       Toolbox::JoinStrings(orderByFieldsString, orderByFields, ", ");
-      ordering = "ROW_NUMBER() OVER (ORDER BY " + orderByFieldsString + ") AS rowNumber";
+
+      ordering = "ROW_NUMBER() OVER (ORDER BY " + orderByFieldsString;
+#if ORTHANC_SQLITE_VERSION >= 3030001
+      ordering += " NULLS LAST";
+#endif
+      ordering += ") AS rowNumber";
     }
     else
     {