HHH-11145 - Fix limit handler to ignore regex search keywords that are quoted.
(cherry picked from commit 0e2b2f11e7
)
This commit is contained in:
parent
f7eb28bd20
commit
a85bd30aaa
|
@ -38,7 +38,7 @@ public class SQLServer2005LimitHandler extends AbstractLimitHandler {
|
||||||
private static final Pattern ORDER_BY_PATTERN = buildShallowIndexPattern( ORDER_BY, true );
|
private static final Pattern ORDER_BY_PATTERN = buildShallowIndexPattern( ORDER_BY, true );
|
||||||
private static final Pattern COMMA_PATTERN = buildShallowIndexPattern( ",", false );
|
private static final Pattern COMMA_PATTERN = buildShallowIndexPattern( ",", false );
|
||||||
private static final Pattern ALIAS_PATTERN =
|
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.
|
// Flag indicating whether TOP(?) expression has been added to the original query.
|
||||||
private boolean topAdded;
|
private boolean topAdded;
|
||||||
|
@ -262,10 +262,18 @@ public class SQLServer2005LimitHandler extends AbstractLimitHandler {
|
||||||
// This will match any text provided with:
|
// This will match any text provided with:
|
||||||
// columnName [[as] alias]
|
// columnName [[as] alias]
|
||||||
final Matcher matcher = ALIAS_PATTERN.matcher( expression );
|
final Matcher matcher = ALIAS_PATTERN.matcher( expression );
|
||||||
|
|
||||||
|
String alias = null;
|
||||||
if ( matcher.find() && matcher.groupCount() > 1 ) {
|
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 );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -356,7 +364,7 @@ public class SQLServer2005LimitHandler extends AbstractLimitHandler {
|
||||||
"(" +
|
"(" +
|
||||||
( wordBoundardy ? "\\b" : "" ) +
|
( wordBoundardy ? "\\b" : "" ) +
|
||||||
pattern +
|
pattern +
|
||||||
")(?![^\\(]*\\))",
|
")(?![^\\(|\\[]*(\\)|\\]))",
|
||||||
Pattern.CASE_INSENSITIVE
|
Pattern.CASE_INSENSITIVE
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -296,6 +296,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 ) )
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@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
|
@Test
|
||||||
@TestForIssue(jiraKey = "HHH-11324")
|
@TestForIssue(jiraKey = "HHH-11324")
|
||||||
public void testGetLimitStringWithSelectClauseNestedQueryUsingParenthesis() {
|
public void testGetLimitStringWithSelectClauseNestedQueryUsingParenthesis() {
|
||||||
|
|
Loading…
Reference in New Issue