HHH-6849 Append _ROW_NUMBER()_ instead of prepending it

This commit is contained in:
Hardy Ferentschik 2011-11-25 15:18:06 +01:00 committed by Strong Liu
parent 20141cae87
commit 06952b533e
1 changed files with 11 additions and 3 deletions

View File

@ -24,6 +24,8 @@
package org.hibernate.dialect;
import java.sql.Types;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.hibernate.dialect.function.NoArgSQLFunction;
import org.hibernate.type.StandardBasicTypes;
@ -39,6 +41,11 @@ public class SQLServer2005Dialect extends SQLServerDialect {
private static final String DISTINCT = "distinct";
private static final int MAX_LENGTH = 8000;
/**
* Regular expression for stripping alias
*/
private static final Pattern ALIAS_PATTERN = Pattern.compile( "\\sas[^,]+(,?)" );
public SQLServer2005Dialect() {
// HHH-3965 fix
// As per http://www.sql-server-helper.com/faq/sql-server-2005-varchar-max-p01.aspx
@ -172,7 +179,8 @@ public class SQLServer2005Dialect extends SQLServerDialect {
* @return a string without the as statements
*/
protected static String stripAliases(String str) {
return str.replaceAll( "\\sas[^,]+(,?)", "$1" );
Matcher matcher = ALIAS_PATTERN.matcher( str );
return matcher.replaceAll( "$1" );
}
/**
@ -183,9 +191,9 @@ public class SQLServer2005Dialect extends SQLServerDialect {
*/
protected static void insertRowNumberFunction(StringBuilder sql, CharSequence orderby) {
// Find the end of the select statement
int selectEndIndex = sql.indexOf( SELECT ) + SELECT.length();
int selectEndIndex = sql.indexOf( FROM );
// Insert after the select statement the row_number() function:
sql.insert( selectEndIndex, " ROW_NUMBER() OVER (" + orderby + ") as __hibernate_row_nr__," );
sql.insert( selectEndIndex, ", ROW_NUMBER() OVER (" + orderby + ") as __hibernate_row_nr__ " );
}
}