From a85bd30aaa046b4d441f7f5870cb6b6f6bf27651 Mon Sep 17 00:00:00 2001 From: Chris Cranford Date: Mon, 3 Oct 2016 09:30:28 -0400 Subject: [PATCH] HHH-11145 - Fix limit handler to ignore regex search keywords that are quoted. (cherry picked from commit 0e2b2f11e7ea04adf848dc7aa945cecd5dc15702) --- .../pagination/SQLServer2005LimitHandler.java | 16 ++++++--- .../dialect/SQLServer2005DialectTestCase.java | 35 +++++++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/pagination/SQLServer2005LimitHandler.java b/hibernate-core/src/main/java/org/hibernate/dialect/pagination/SQLServer2005LimitHandler.java index 0ff2062e15..63f074149f 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/pagination/SQLServer2005LimitHandler.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/pagination/SQLServer2005LimitHandler.java @@ -38,7 +38,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; @@ -262,10 +262,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 ); } /** @@ -356,7 +364,7 @@ public class SQLServer2005LimitHandler extends AbstractLimitHandler { "(" + ( wordBoundardy ? "\\b" : "" ) + pattern + - ")(?![^\\(]*\\))", + ")(?![^\\(|\\[]*(\\)|\\]))", Pattern.CASE_INSENSITIVE ); } diff --git a/hibernate-core/src/test/java/org/hibernate/dialect/SQLServer2005DialectTestCase.java b/hibernate-core/src/test/java/org/hibernate/dialect/SQLServer2005DialectTestCase.java index 7800016ec7..669fd18d63 100644 --- a/hibernate-core/src/test/java/org/hibernate/dialect/SQLServer2005DialectTestCase.java +++ b/hibernate-core/src/test/java/org/hibernate/dialect/SQLServer2005DialectTestCase.java @@ -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 @TestForIssue(jiraKey = "HHH-11324") public void testGetLimitStringWithSelectClauseNestedQueryUsingParenthesis() {