HHH-13512 Optimise StringHelper#isQuoted(String, Dialect)

This commit is contained in:
Sanne Grinovero 2019-08-01 15:39:11 +01:00 committed by gbadner
parent c9f41dc9b7
commit ccae2388ed
2 changed files with 26 additions and 6 deletions

View File

@ -708,12 +708,15 @@ public final class StringHelper {
*
* @return True if quoted, false otherwise
*/
public static boolean isQuoted(String name, Dialect dialect) {
return name != null && name.length() != 0
&& ( ( name.charAt( 0 ) == '`' && name.charAt( name.length() - 1 ) == '`' )
|| ( name.charAt( 0 ) == '"' && name.charAt( name.length() - 1 ) == '"' )
|| ( name.charAt( 0 ) == dialect.openQuote()
&& name.charAt( name.length() - 1 ) == dialect.closeQuote() ) );
public static boolean isQuoted(final String name, final Dialect dialect) {
if ( name == null || name.isEmpty() ) {
return false;
}
final char first = name.charAt( 0 );
final char last = name.charAt( name.length() - 1 );
return ( ( first == last ) && ( first == '`' || first == '"' ) )
|| ( first == dialect.openQuote() && last == dialect.closeQuote() );
}
/**

View File

@ -11,7 +11,9 @@ import java.util.Arrays;
import org.junit.Assert;
import org.junit.Test;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.testing.junit4.BaseUnitTestCase;
@ -83,4 +85,19 @@ public class StringHelperTest extends BaseUnitTestCase {
assertTrue( Arrays.equals( expectation, output ) );
}
@Test
public void testIsQuotedWithDialect() {
Assert.assertFalse( StringHelper.isQuoted( "a", DIALECT ) );
Assert.assertTrue( StringHelper.isQuoted( "`a`", DIALECT ) );
//This dialect has a different "open" than "close" quoting symbol:
final SQLServerDialect sqlServerDialect = new SQLServerDialect();
Assert.assertTrue( StringHelper.isQuoted( "[a]", sqlServerDialect ) );
Assert.assertFalse( StringHelper.isQuoted( "`a]", sqlServerDialect ) );
Assert.assertFalse( StringHelper.isQuoted( "[a`", sqlServerDialect ) );
Assert.assertFalse( StringHelper.isQuoted( "\"a`", sqlServerDialect ) );
Assert.assertFalse( StringHelper.isQuoted( "`a\"", sqlServerDialect ) );
Assert.assertFalse( StringHelper.isQuoted( "a", sqlServerDialect ) );
}
}