Emulate null ordering for CockroachDB

This commit is contained in:
Christian Beikov 2021-04-29 13:43:31 +02:00
parent 127ee26f82
commit c096b463ee
1 changed files with 20 additions and 0 deletions

View File

@ -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;