HHH-9707 - Nulls first behavior on SQLServer database

This commit is contained in:
amaeda 2015-04-06 23:57:48 -03:00
parent 6f37a2ee6b
commit 046426b619
3 changed files with 23 additions and 33 deletions

View File

@ -25,6 +25,7 @@ package org.hibernate.dialect;
import java.sql.Types;
import org.hibernate.NullPrecedence;
import org.hibernate.dialect.function.NoArgSQLFunction;
import org.hibernate.type.StandardBasicTypes;
@ -46,4 +47,26 @@ public class SQLServer2008Dialect extends SQLServer2005Dialect {
"current_timestamp", new NoArgSQLFunction( "current_timestamp", StandardBasicTypes.TIMESTAMP, false )
);
}
@Override
public String renderOrderByElement(String expression, String collation, String order, NullPrecedence nulls) {
final StringBuilder orderByElement = new StringBuilder();
if ( nulls != null && !NullPrecedence.NONE.equals( nulls ) ) {
// Workaround for NULLS FIRST / LAST support.
orderByElement.append( "case when " ).append( expression ).append( " is null then " );
if ( NullPrecedence.FIRST.equals( nulls ) ) {
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();
}
}

View File

@ -23,9 +23,6 @@
*/
package org.hibernate.dialect;
import org.hibernate.NullPrecedence;
/**
* Microsoft SQL Server 2012 Dialect
*
@ -67,9 +64,4 @@ public class SQLServer2012Dialect extends SQLServer2008Dialect {
public String getQuerySequencesString() {
return "select name from sys.sequences";
}
@Override
public String renderOrderByElement(String expression, String collation, String order, NullPrecedence nulls) {
return renderOrderByElementDefaultBehavior( expression, collation, order, nulls );
}
}

View File

@ -27,7 +27,6 @@ import java.sql.Types;
import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.NullPrecedence;
import org.hibernate.dialect.function.AnsiTrimEmulationFunction;
import org.hibernate.dialect.function.SQLFunctionTemplate;
import org.hibernate.dialect.function.StandardSQLFunction;
@ -209,30 +208,6 @@ public class SQLServerDialect extends AbstractTransactSQLDialect {
public int getInExpressionCountLimit() {
return PARAM_LIST_SIZE_LIMIT;
}
@Override
public String renderOrderByElement(String expression, String collation, String order, NullPrecedence nulls) {
final StringBuilder orderByElement = new StringBuilder();
if ( nulls != null && !NullPrecedence.NONE.equals( nulls ) ) {
// Workaround for NULLS FIRST / LAST support.
orderByElement.append( "case when " ).append( expression ).append( " is null then " );
if ( NullPrecedence.FIRST.equals( nulls ) ) {
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();
}
String renderOrderByElementDefaultBehavior(String expression, String collation, String order, NullPrecedence nulls) {
return super.renderOrderByElement( expression, collation, order, nulls );
}
}