solomax commented on code in PR #144:
URL: https://github.com/apache/openjpa/pull/144#discussion_r3801341490


##########
openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/MySQLDictionary.java:
##########
@@ -536,5 +559,55 @@ public void indexOf(SQLBuffer buf, FilterValue str, 
FilterValue find,
         }
         buf.append(")");
     }
+
+    /**
+     * MySQL / MariaDB do not support ANSI SQL {@code NULLS FIRST} / {@code 
NULLS LAST}.
+     * Emulate via an auxiliary {@code <expr> IS NULL} sort key.
+     * <p>
+     * MySQL's default NULL ordering places NULLs before non-NULLs when sorting
+     * ASC and after non-NULLs when sorting DESC. When the requested precedence
+     * already matches that default, nothing extra is emitted. Otherwise the
+     * last order term {@code &lt;expr&gt; ASC|DESC} is rewritten to
+     * {@code &lt;expr&gt; IS NULL &lt;sort&gt;, &lt;expr&gt; ASC|DESC}.
+     */
+    @Override
+    public void appendNullsPrecedence(SQLBuffer ordering, int nullPrecedence) {
+        if (nullPrecedence != QueryExpressions.NULLS_FIRST
+                && nullPrecedence != QueryExpressions.NULLS_LAST) {
+            return;
+        }
+        String sql = ordering.getSQL();
+        int lastAsc = sql.lastIndexOf(" ASC");
+        int lastDesc = sql.lastIndexOf(" DESC");
+        boolean asc;
+        int termDirStart;
+        int termDirEnd;
+        if (lastAsc > lastDesc) {
+            asc = true;
+            termDirStart = lastAsc;
+            termDirEnd = lastAsc + " ASC".length();
+        } else if (lastDesc >= 0) {
+            asc = false;
+            termDirStart = lastDesc;
+            termDirEnd = lastDesc + " DESC".length();
+        } else {
+            return;
+        }
+        boolean defaultMatches =
+                (nullPrecedence == QueryExpressions.NULLS_FIRST && asc)
+             || (nullPrecedence == QueryExpressions.NULLS_LAST && !asc);
+        if (defaultMatches) {
+            return;
+        }
+        int termStart = Math.max(sql.lastIndexOf(", ", termDirStart), -1);

Review Comment:
   please see 52f4c065d for generalization and basic test



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to