From c096b463eef9e2447e9b68fcfd7354d6debe2bf2 Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Thu, 29 Apr 2021 13:43:31 +0200 Subject: [PATCH] Emulate null ordering for CockroachDB --- .../dialect/CockroachDB192Dialect.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/CockroachDB192Dialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/CockroachDB192Dialect.java index 1b89423ded..18ad31c338 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/CockroachDB192Dialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/CockroachDB192Dialect.java @@ -14,6 +14,7 @@ import java.util.Map; import org.hibernate.JDBCException; import org.hibernate.LockMode; import org.hibernate.LockOptions; +import org.hibernate.NullPrecedence; import org.hibernate.PessimisticLockException; import org.hibernate.boot.model.TypeContributions; import org.hibernate.cfg.Environment; @@ -234,6 +235,25 @@ public class CockroachDB192Dialect extends Dialect { return true; } + @Override + public String renderOrderByElement(String expression, String collation, String order, NullPrecedence nulls) { + final StringBuilder orderByElement = new StringBuilder(); + if ( nulls != NullPrecedence.NONE ) { + // Workaround for NULLS FIRST / LAST support. + orderByElement.append( "case when " ).append( expression ).append( " is null then " ); + if ( nulls == NullPrecedence.FIRST ) { + orderByElement.append( "0 else 1" ); + } + else { + orderByElement.append( "1 else 0" ); + } + orderByElement.append( " end, " ); + } + // Nulls precedence has already been handled so passing NONE value. + orderByElement.append( super.renderOrderByElement( expression, collation, order, NullPrecedence.NONE ) ); + return orderByElement.toString(); + } + @Override public String getForUpdateString(String aliases) { return getForUpdateString() + " of " + aliases;