HHH-11145 - Fix limit handler to ignore regex search keywords that are quoted.

This commit is contained in:
Chris Cranford 2016-10-03 09:30:28 -04:00
parent dd14feac5a
commit 0e2b2f11e7
2 changed files with 47 additions and 4 deletions

View File

@ -39,7 +39,7 @@ public class SQLServer2005LimitHandler extends AbstractLimitHandler {
private static final Pattern ORDER_BY_PATTERN = buildShallowIndexPattern( ORDER_BY, true );
private static final Pattern COMMA_PATTERN = buildShallowIndexPattern( ",", false );
private static final Pattern ALIAS_PATTERN =
Pattern.compile( "\\S+\\s*(\\s(?i)as\\s)\\s*(\\S+)*\\s*$|\\s+(\\S+)$" );
Pattern.compile( "(?![^\\[]*(\\]))\\S+\\s*(\\s(?i)as\\s)\\s*(\\S+)*\\s*$|(?![^\\[]*(\\]))\\s+(\\S+)$" );
// Flag indicating whether TOP(?) expression has been added to the original query.
private boolean topAdded;
@ -265,10 +265,18 @@ public class SQLServer2005LimitHandler extends AbstractLimitHandler {
// This will match any text provided with:
// columnName [[as] alias]
final Matcher matcher = ALIAS_PATTERN.matcher( expression );
String alias = null;
if ( matcher.find() && matcher.groupCount() > 1 ) {
return matcher.group( 1 ) != null ? matcher.group( 2 ) : matcher.group( 3 );
// default to the alias after 'as' if detected
alias = matcher.group( 3 );
if ( alias == null ) {
// use the clause which has on proceeding 'as' fragment.
alias = matcher.group( 0 );
}
return null;
}
return ( alias != null ? alias.trim() : null );
}
/**
@ -343,7 +351,7 @@ public class SQLServer2005LimitHandler extends AbstractLimitHandler {
"(" +
( wordBoundardy ? "\\b" : "" ) +
pattern +
")(?![^\\(]*\\))",
")(?![^\\(|\\[]*(\\)|\\]))",
Pattern.CASE_INSENSITIVE
);
}

View File

@ -327,6 +327,41 @@ public class SQLServer2005DialectTestCase extends BaseUnitTestCase {
);
}
@Test
@TestForIssue(jiraKey = "HHH-11145")
public void testGetLimitStringWithFromInColumnName() {
final String query = "select [Created From Nonstock Item], field2 from table1";
assertEquals( "WITH query AS (SELECT inner_query.*, ROW_NUMBER() OVER (ORDER BY CURRENT_TIMESTAMP) as __hibernate_row_nr__ FROM ( " +
"select [Created From Nonstock Item] as page0_, field2 as page1_ from table1 ) inner_query ) " +
"SELECT page0_, page1_ FROM query WHERE __hibernate_row_nr__ >= ? AND __hibernate_row_nr__ < ?",
dialect.getLimitHandler().processSql( query, toRowSelection( 1, 5 ) )
);
}
@Test
@TestForIssue(jiraKey = "HHH-11145")
public void testGetLimitStringWithQuotedColumnNamesAndAlias() {
final String query = "select [Created From Item] c1, field2 from table1";
assertEquals( "WITH query AS (SELECT inner_query.*, ROW_NUMBER() OVER (ORDER BY CURRENT_TIMESTAMP) as __hibernate_row_nr__ FROM ( " +
"select [Created From Item] c1, field2 as page0_ from table1 ) inner_query ) " +
"SELECT c1, page0_ FROM query WHERE __hibernate_row_nr__ >= ? AND __hibernate_row_nr__ < ?",
dialect.getLimitHandler().processSql( query, toRowSelection( 1, 5 ) )
);
}
@Test
@TestForIssue(jiraKey = "HHH-11145")
public void testGetLimitStringWithQuotedColumnNamesAndAliasWithAs() {
final String query = "select [Created From Item] as c1, field2 from table1";
assertEquals( "WITH query AS (SELECT inner_query.*, ROW_NUMBER() OVER (ORDER BY CURRENT_TIMESTAMP) as __hibernate_row_nr__ FROM ( " +
"select [Created From Item] as c1, field2 as page0_ from table1 ) inner_query ) " +
"SELECT c1, page0_ FROM query WHERE __hibernate_row_nr__ >= ? AND __hibernate_row_nr__ < ?",
dialect.getLimitHandler().processSql( query, toRowSelection( 1, 5 ) )
);
}
@Test
@TestForIssue(jiraKey = "HHH-9635")
public void testAppendLockHintReadPastLocking() {