From 7358944b974e4051a4b727ba24a91aba5fa60c39 Mon Sep 17 00:00:00 2001 From: Dmitrii Bocharov Date: Thu, 1 Mar 2018 15:32:23 +0100 Subject: [PATCH] HHH-12221: incorrect formatting of SQL Server statements when escaping --- .../engine/jdbc/internal/BasicFormatterImpl.java | 12 +++++++++++- .../org/hibernate/jdbc/util/BasicFormatterTest.java | 3 +-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/internal/BasicFormatterImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/internal/BasicFormatterImpl.java index 8dc3a659b9..a7aed17f92 100755 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/internal/BasicFormatterImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/internal/BasicFormatterImpl.java @@ -127,7 +127,17 @@ public class BasicFormatterImpl implements Formatter { t = tokens.nextToken(); token += t; } - while ( !"\"".equals( t ) ); + while ( !"\"".equals( t ) && tokens.hasMoreTokens() ); + } + // SQL Server uses "[" and "]" to escape reserved words + // see SQLServerDialect.openQuote and SQLServerDialect.closeQuote + else if ( "[".equals( token ) ) { + String t; + do { + t = tokens.nextToken(); + token += t; + } + while ( !"]".equals( t ) && tokens.hasMoreTokens()); } if ( afterByOrSetOrFromOrSelect && ",".equals( token ) ) { diff --git a/hibernate-core/src/test/java/org/hibernate/jdbc/util/BasicFormatterTest.java b/hibernate-core/src/test/java/org/hibernate/jdbc/util/BasicFormatterTest.java index 925888a529..e5f244ae06 100644 --- a/hibernate-core/src/test/java/org/hibernate/jdbc/util/BasicFormatterTest.java +++ b/hibernate-core/src/test/java/org/hibernate/jdbc/util/BasicFormatterTest.java @@ -13,8 +13,6 @@ import org.junit.Test; import org.hibernate.engine.jdbc.internal.FormatStyle; import org.hibernate.testing.junit4.BaseUnitTestCase; -import org.jboss.logging.Logger; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -49,6 +47,7 @@ public class BasicFormatterTest extends BaseUnitTestCase { assertNoLoss( "(select p.pid from Address where city = 'Boston') union (select p.pid from Address where city = 'Taipei')" ); + assertNoLoss( "select group0.[order] as order0 from [Group] group0 where group0.[order]=?1" ); } private void assertNoLoss(String query) {