From abd26075a7c68fe7cd3bd04c1c9f9292ad8a5555 Mon Sep 17 00:00:00 2001
From: Matthias Kuhn <matthias@opengis.ch>
Date: Mon, 1 May 2017 18:50:12 +0200
Subject: [PATCH] Allow QgsFeatureRequest::OrderBy expressions to be prepared

---
 src/core/qgsfeaturerequest.cpp | 21 +++++++++++++++++++++
 src/core/qgsfeaturerequest.h   | 28 ++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)

diff --git a/src/core/qgsfeaturerequest.cpp b/src/core/qgsfeaturerequest.cpp
index 3372548e227..9cb71ece25b 100644
--- a/src/core/qgsfeaturerequest.cpp
+++ b/src/core/qgsfeaturerequest.cpp
@@ -302,6 +302,22 @@ QgsFeatureRequest::OrderByClause::OrderByClause( const QString &expression, bool
 {
 }
 
+QgsFeatureRequest::OrderByClause::OrderByClause( const QgsExpression &expression, bool ascending )
+  : mExpression( expression )
+  , mAscending( ascending )
+{
+  // postgres behavior: default for ASC: NULLS LAST, default for DESC: NULLS FIRST
+  mNullsFirst = !ascending;
+}
+
+QgsFeatureRequest::OrderByClause::OrderByClause( const QgsExpression &expression, bool ascending, bool nullsfirst )
+  : mExpression( expression )
+  , mAscending( ascending )
+  , mNullsFirst( nullsfirst )
+{
+
+}
+
 bool QgsFeatureRequest::OrderByClause::ascending() const
 {
   return mAscending;
@@ -335,6 +351,11 @@ QgsExpression QgsFeatureRequest::OrderByClause::expression() const
   return mExpression;
 }
 
+bool QgsFeatureRequest::OrderByClause::prepare( QgsExpressionContext *context )
+{
+  return mExpression.prepare( context );
+}
+
 QgsFeatureRequest::OrderBy::OrderBy( const QList<QgsFeatureRequest::OrderByClause> &other )
 {
   Q_FOREACH ( const QgsFeatureRequest::OrderByClause &clause, other )
diff --git a/src/core/qgsfeaturerequest.h b/src/core/qgsfeaturerequest.h
index 2a8b6383e95..7210fea02c7 100644
--- a/src/core/qgsfeaturerequest.h
+++ b/src/core/qgsfeaturerequest.h
@@ -138,12 +138,40 @@ class CORE_EXPORT QgsFeatureRequest
          */
         OrderByClause( const QString &expression, bool ascending, bool nullsfirst );
 
+        /**
+         * Creates a new OrderByClause for a QgsFeatureRequest
+         *
+         * \param expression The expression to use for ordering
+         * \param ascending  If the order should be ascending (1,2,3) or descending (3,2,1)
+         *                   If the order is ascending, by default nulls are last
+         *                   If the order is descending, by default nulls are first
+         */
+        OrderByClause( const QgsExpression &expression, bool ascending = true );
+
+        /**
+         * Creates a new OrderByClause for a QgsFeatureRequest
+         *
+         * \param expression The expression to use for ordering
+         * \param ascending  If the order should be ascending (1,2,3) or descending (3,2,1)
+         * \param nullsfirst If true, NULLS are at the beginning, if false, NULLS are at the end
+         */
+        OrderByClause( const QgsExpression &expression, bool ascending, bool nullsfirst );
+
         /**
          * The expression
          * \returns the expression
          */
         QgsExpression expression() const;
 
+        /**
+         * Prepare the expression with the given context.
+         *
+         * \see QgsExpression::prepare
+         *
+         * \since QGIS 3.0
+         */
+        bool prepare( QgsExpressionContext *context );
+
         /**
          * Order ascending
          * \returns If ascending order is requested