HHH-9707 - Nulls first behavior on SQLServer database

This commit is contained in:
amaeda 2015-04-04 13:36:04 -03:00
parent 36bf5d0ef9
commit 6f37a2ee6b
3 changed files with 48 additions and 12 deletions

View File

@ -23,6 +23,8 @@
*/ */
package org.hibernate.dialect; package org.hibernate.dialect;
import org.hibernate.NullPrecedence;
/** /**
* Microsoft SQL Server 2012 Dialect * Microsoft SQL Server 2012 Dialect
@ -65,4 +67,9 @@ public class SQLServer2012Dialect extends SQLServer2008Dialect {
public String getQuerySequencesString() { public String getQuerySequencesString() {
return "select name from sys.sequences"; 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,6 +27,7 @@ import java.sql.Types;
import org.hibernate.LockMode; import org.hibernate.LockMode;
import org.hibernate.LockOptions; import org.hibernate.LockOptions;
import org.hibernate.NullPrecedence;
import org.hibernate.dialect.function.AnsiTrimEmulationFunction; import org.hibernate.dialect.function.AnsiTrimEmulationFunction;
import org.hibernate.dialect.function.SQLFunctionTemplate; import org.hibernate.dialect.function.SQLFunctionTemplate;
import org.hibernate.dialect.function.StandardSQLFunction; import org.hibernate.dialect.function.StandardSQLFunction;
@ -208,5 +209,30 @@ public class SQLServerDialect extends AbstractTransactSQLDialect {
public int getInExpressionCountLimit() { public int getInExpressionCountLimit() {
return PARAM_LIST_SIZE_LIMIT; 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 );
}
} }

View File

@ -43,6 +43,9 @@ import org.hibernate.Session;
import org.hibernate.dialect.H2Dialect; import org.hibernate.dialect.H2Dialect;
import org.hibernate.dialect.MySQLDialect; import org.hibernate.dialect.MySQLDialect;
import org.hibernate.dialect.Oracle8iDialect; import org.hibernate.dialect.Oracle8iDialect;
import org.hibernate.dialect.SQLServer2008Dialect;
import org.hibernate.dialect.SQLServer2012Dialect;
import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.persister.collection.CollectionPersister; import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.persister.collection.QueryableCollection; import org.hibernate.persister.collection.QueryableCollection;
@ -97,10 +100,10 @@ public class OrderByTest extends BaseCoreFunctionalTestCase {
@Test @Test
@TestForIssue(jiraKey = "HHH-465") @TestForIssue(jiraKey = "HHH-465")
@RequiresDialect(value = { H2Dialect.class, MySQLDialect.class }, @RequiresDialect(value = { H2Dialect.class, MySQLDialect.class, SQLServer2008Dialect.class },
comment = "By default H2 places NULL values first, so testing 'NULLS LAST' expression. " + comment = "By default H2 places NULL values first, so testing 'NULLS LAST' expression. " +
"For MySQL testing overridden Dialect#renderOrderByElement(String, String, String, NullPrecedence) method. " + "For MySQL and SQL Server 2000/2005/2008 testing overridden Dialect#renderOrderByElement(String, String, String, NullPrecedence) method. " +
"MySQL does not support NULLS FIRST / LAST syntax at the moment, so transforming the expression to 'CASE WHEN ...'.") "MySQL and SQLServer 2000/2005/2008 does not support NULLS FIRST / LAST syntax at the moment, so transforming the expression to 'CASE WHEN ...'.")
public void testAnnotationNullsFirstLast() { public void testAnnotationNullsFirstLast() {
Session session = openSession(); Session session = openSession();
@ -156,10 +159,10 @@ public class OrderByTest extends BaseCoreFunctionalTestCase {
@Test @Test
@TestForIssue(jiraKey = "HHH-465") @TestForIssue(jiraKey = "HHH-465")
@RequiresDialect(value = { H2Dialect.class, MySQLDialect.class }, @RequiresDialect(value = { H2Dialect.class, MySQLDialect.class, SQLServer2008Dialect.class },
comment = "By default H2 places NULL values first, so testing 'NULLS LAST' expression. " + comment = "By default H2 places NULL values first, so testing 'NULLS LAST' expression. " +
"For MySQL testing overridden Dialect#renderOrderByElement(String, String, String, NullPrecedence) method. " + "For MySQL and SQL Server 2000/2005/2008 testing overridden Dialect#renderOrderByElement(String, String, String, NullPrecedence) method. " +
"MySQL does not support NULLS FIRST / LAST syntax at the moment, so transforming the expression to 'CASE WHEN ...'.") "MySQL and SQL Server 2000/2005/2008 does not support NULLS FIRST / LAST syntax at the moment, so transforming the expression to 'CASE WHEN ...'.")
public void testCriteriaNullsFirstLast() { public void testCriteriaNullsFirstLast() {
Session session = openSession(); Session session = openSession();
@ -194,10 +197,10 @@ public class OrderByTest extends BaseCoreFunctionalTestCase {
@Test @Test
@TestForIssue(jiraKey = "HHH-465") @TestForIssue(jiraKey = "HHH-465")
@RequiresDialect(value = { H2Dialect.class, MySQLDialect.class }, @RequiresDialect(value = { H2Dialect.class, MySQLDialect.class, SQLServer2008Dialect.class },
comment = "By default H2 places NULL values first, so testing 'NULLS LAST' expression. " + comment = "By default H2 places NULL values first, so testing 'NULLS LAST' expression. " +
"For MySQL testing overridden Dialect#renderOrderByElement(String, String, String, NullPrecedence) method. " + "For MySQL and SQL Server 2000/2005/2008 testing overridden Dialect#renderOrderByElement(String, String, String, NullPrecedence) method. " +
"MySQL does not support NULLS FIRST / LAST syntax at the moment, so transforming the expression to 'CASE WHEN ...'.") "MySQL and SQL Server 2000/2005/2008 does not support NULLS FIRST / LAST syntax at the moment, so transforming the expression to 'CASE WHEN ...'.")
public void testNullsFirstLastSpawnMultipleColumns() { public void testNullsFirstLastSpawnMultipleColumns() {
Session session = openSession(); Session session = openSession();
@ -243,10 +246,10 @@ public class OrderByTest extends BaseCoreFunctionalTestCase {
@Test @Test
@TestForIssue(jiraKey = "HHH-465") @TestForIssue(jiraKey = "HHH-465")
@RequiresDialect(value = { H2Dialect.class, MySQLDialect.class }, @RequiresDialect(value = { H2Dialect.class, MySQLDialect.class, SQLServer2008Dialect.class },
comment = "By default H2 places NULL values first, so testing 'NULLS LAST' expression. " + comment = "By default H2 places NULL values first, so testing 'NULLS LAST' expression. " +
"For MySQL testing overridden Dialect#renderOrderByElement(String, String, String, NullPrecedence) method. " + "For MySQL and SQL Server 2000/2005/2008 testing overridden Dialect#renderOrderByElement(String, String, String, NullPrecedence) method. " +
"MySQL does not support NULLS FIRST / LAST syntax at the moment, so transforming the expression to 'CASE WHEN ...'.") "MySQL and SQL Server 2000/2005/2008 does not support NULLS FIRST / LAST syntax at the moment, so transforming the expression to 'CASE WHEN ...'.")
public void testHqlNullsFirstLast() { public void testHqlNullsFirstLast() {
Session session = openSession(); Session session = openSession();