From 3cddb27d60c2c7f16ad8ee920202a3b8b1b927d3 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Fri, 4 May 2012 10:49:08 -0500 Subject: [PATCH] HHH-7198 - SQLServer2005Dialect.getLimitString turns tablenames to lowercase -> SQLGrammarException: when mapping tables with capital letters --- .../java/org/hibernate/dialect/SQLServer2005Dialect.java | 9 +++++++-- .../hibernate/dialect/SQLServer2005DialectTestCase.java | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/SQLServer2005Dialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/SQLServer2005Dialect.java index 7db09c1da5..7f22fa41ae 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/SQLServer2005Dialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/SQLServer2005Dialect.java @@ -38,7 +38,7 @@ import org.hibernate.type.StandardBasicTypes; */ public class SQLServer2005Dialect extends SQLServerDialect { private static final String SELECT = "select"; - private static final String FROM = "from "; + private static final String FROM = "from"; private static final String DISTINCT = "distinct "; private static final int MAX_LENGTH = 8000; @@ -157,6 +157,9 @@ public class SQLServer2005Dialect extends SQLServerDialect { } } + public static final String SELECT_WITH_SPACE = SELECT + ' '; + public static final String FROM_WITH_SPACE = FROM + ' '; + /** * This utility method searches the given sql query for the fields of the select statement and returns them without * the aliases. @@ -167,7 +170,9 @@ public class SQLServer2005Dialect extends SQLServerDialect { */ protected static CharSequence getSelectFieldsWithoutAliases(StringBuilder sql) { final String lower = sql.toString().toLowerCase(); - String select = sql.substring( lower.indexOf( SELECT ) + SELECT.length(), lower.indexOf( FROM ) ); + final int selectStartPos = lower.indexOf( SELECT_WITH_SPACE ); + final int fromStartPos = lower.indexOf( FROM_WITH_SPACE, selectStartPos ); + String select = sql.substring( selectStartPos + SELECT.length(), fromStartPos ); // Strip the as clauses return stripAliases( select ); 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 9f9dbe98c5..8c0af1017c 100644 --- a/hibernate-core/src/test/java/org/hibernate/dialect/SQLServer2005DialectTestCase.java +++ b/hibernate-core/src/test/java/org/hibernate/dialect/SQLServer2005DialectTestCase.java @@ -45,7 +45,7 @@ public class SQLServer2005DialectTestCase extends BaseUnitTestCase { @Test public void testGetSelectFieldsWithoutAliases() { - StringBuilder input = new StringBuilder( "select some_field1 as f12, some_fild2 as f879, _field3 as _f24674_3 from...." ); + StringBuilder input = new StringBuilder( "select some_field1 as f12, some_fild2 as f879, _field3 as _f24674_3 from ...." ); String output = SQLServer2005Dialect.getSelectFieldsWithoutAliases( input ).toString(); assertEquals( " some_field1, some_fild2, _field3", output );