HHH-7781 - Fix and test

This commit is contained in:
Lukasz Antoniak 2012-12-31 12:24:39 +01:00
parent 42bdb1b26d
commit 03520b928f
3 changed files with 40 additions and 1 deletions

View File

@ -179,7 +179,9 @@ public class SQLServer2005LimitHandler extends AbstractLimitHandler {
private String getAlias(String expression) {
Matcher matcher = ALIAS_PATTERN.matcher( expression );
if ( matcher.find() ) {
return matcher.group( 0 ).replaceFirst( "(?i)\\sas\\s", "" ).trim();
// Taking advantage of Java regular expressions greedy behavior while extracting the last AS keyword.
// Note that AS keyword can appear in CAST operator, e.g. 'cast(tab1.col1 as varchar(255)) as col1'.
return matcher.group( 0 ).replaceFirst( "(?i)(.)*\\sas\\s", "" ).trim();
}
return null;
}

View File

@ -153,6 +153,21 @@ public class SQLServer2005DialectTestCase extends BaseUnitTestCase {
);
}
@Test
@TestForIssue(jiraKey = "HHH-7781")
public void testGetLimitStringWithCastOperator() {
final String query = "select cast(lc302_doku6_.redniBrojStavke as varchar(255)) as col_0_0_, lc302_doku6_.dokumentiID as col_1_0_ " +
"from LC302_Dokumenti lc302_doku6_ order by lc302_doku6_.dokumentiID DESC";
assertEquals(
"WITH query AS (SELECT inner_query.*, ROW_NUMBER() OVER (ORDER BY CURRENT_TIMESTAMP) as __hibernate_row_nr__ FROM ( " +
"select TOP(?) cast(lc302_doku6_.redniBrojStavke as varchar(255)) as col_0_0_, lc302_doku6_.dokumentiID as col_1_0_ " +
"from LC302_Dokumenti lc302_doku6_ order by lc302_doku6_.dokumentiID DESC ) inner_query ) " +
"SELECT col_0_0_, col_1_0_ FROM query WHERE __hibernate_row_nr__ >= ? AND __hibernate_row_nr__ < ?",
dialect.buildLimitHandler( query, toRowSelection( 1, 3 ) ).getProcessedSql()
);
}
private RowSelection toRowSelection(int firstRow, int maxRows) {
RowSelection selection = new RowSelection();
selection.setFirstRow( firstRow );

View File

@ -296,6 +296,28 @@ public class SQLServerDialectTest extends BaseCoreFunctionalTestCase {
session.close();
}
@Test
@TestForIssue(jiraKey = "HHH-7781")
public void testPaginationWithCastOperator() {
Session session = openSession();
Transaction tx = session.beginTransaction();
for ( int i = 40; i < 50; i++ ) {
session.persist( new Product2( i, "Kit" + i ) );
}
session.flush();
session.clear();
List<Object[]> list = session.createQuery( "select p.id, cast(p.id as string) as string_id from Product2 p order by p.id" )
.setFirstResult( 1 ).setMaxResults( 2 ).list();
assertEquals( 2, list.size() );
assertArrayEquals( new Object[] { 41, "41" }, list.get( 0 ) );
assertArrayEquals( new Object[] { 42, "42" }, list.get( 1 ) );
tx.rollback();
session.close();
}
@Test
@TestForIssue(jiraKey = "HHH-3961")
public void testLockNowaitSqlServer() throws Exception {