HHH-12221: incorrect formatting of SQL Server statements when escaping

This commit is contained in:
Dmitrii Bocharov 2018-03-01 15:32:23 +01:00 committed by Vlad Mihalcea
parent 86da00d66f
commit 7358944b97
2 changed files with 12 additions and 3 deletions

View File

@ -127,7 +127,17 @@ public class BasicFormatterImpl implements Formatter {
t = tokens.nextToken(); t = tokens.nextToken();
token += t; 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 ) ) { if ( afterByOrSetOrFromOrSelect && ",".equals( token ) ) {

View File

@ -13,8 +13,6 @@ import org.junit.Test;
import org.hibernate.engine.jdbc.internal.FormatStyle; import org.hibernate.engine.jdbc.internal.FormatStyle;
import org.hibernate.testing.junit4.BaseUnitTestCase; import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.jboss.logging.Logger;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
@ -49,6 +47,7 @@ public class BasicFormatterTest extends BaseUnitTestCase {
assertNoLoss( assertNoLoss(
"(select p.pid from Address where city = 'Boston') union (select p.pid from Address where city = 'Taipei')" "(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) { private void assertNoLoss(String query) {