HHH-14292 Avoid defensive copy for keywords set in NormalizingIdentifierHelperImpl

This commit is contained in:
Sanne Grinovero 2020-10-27 22:03:39 +00:00
parent 18b3def741
commit 67a2ed17ca
2 changed files with 11 additions and 8 deletions

View File

@ -7,9 +7,9 @@
package org.hibernate.engine.jdbc.env.internal;
import java.util.Locale;
import java.util.Set;
import java.util.TreeSet;
import org.hibernate.AssertionFailure;
import org.hibernate.boot.model.naming.DatabaseIdentifier;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.engine.jdbc.env.spi.IdentifierCaseStrategy;
@ -31,7 +31,7 @@ public class NormalizingIdentifierHelperImpl implements IdentifierHelper {
private final boolean globallyQuoteIdentifiers;
private final boolean globallyQuoteIdentifiersSkipColumnDefinitions;
private final boolean autoQuoteKeywords;
private final Set<String> reservedWords = new TreeSet<String>( String.CASE_INSENSITIVE_ORDER );
private final TreeSet<String> reservedWords;
private final IdentifierCaseStrategy unquotedCaseStrategy;
private final IdentifierCaseStrategy quotedCaseStrategy;
@ -41,7 +41,7 @@ public class NormalizingIdentifierHelperImpl implements IdentifierHelper {
boolean globallyQuoteIdentifiers,
boolean globallyQuoteIdentifiersSkipColumnDefinitions,
boolean autoQuoteKeywords,
Set<String> reservedWords,
TreeSet<String> reservedWords, //careful, we intentionally omit making a defensive copy to not waste memory
IdentifierCaseStrategy unquotedCaseStrategy,
IdentifierCaseStrategy quotedCaseStrategy) {
this.jdbcEnvironment = jdbcEnvironment;
@ -49,9 +49,7 @@ public class NormalizingIdentifierHelperImpl implements IdentifierHelper {
this.globallyQuoteIdentifiers = globallyQuoteIdentifiers;
this.globallyQuoteIdentifiersSkipColumnDefinitions = globallyQuoteIdentifiersSkipColumnDefinitions;
this.autoQuoteKeywords = autoQuoteKeywords;
if ( reservedWords != null ) {
this.reservedWords.addAll( reservedWords );
}
this.reservedWords = reservedWords;
this.unquotedCaseStrategy = unquotedCaseStrategy == null ? IdentifierCaseStrategy.UPPER : unquotedCaseStrategy;
this.quotedCaseStrategy = quotedCaseStrategy == null ? IdentifierCaseStrategy.MIXED : quotedCaseStrategy;
}
@ -98,6 +96,9 @@ public class NormalizingIdentifierHelperImpl implements IdentifierHelper {
@Override
public boolean isReservedWord(String word) {
if ( autoQuoteKeywords == false ) {
throw new AssertionFailure( "The reserved keywords map is only initialized if autoQuoteKeywords is true" );
}
return reservedWords.contains( word );
}

View File

@ -8,7 +8,6 @@ package org.hibernate.engine.jdbc.env.spi;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
@ -32,7 +31,10 @@ public class IdentifierHelperBuilder {
private NameQualifierSupport nameQualifierSupport = NameQualifierSupport.BOTH;
private Set<String> reservedWords = new TreeSet<String>( String.CASE_INSENSITIVE_ORDER );
//TODO interesting computer science puzzle: find a more compact representation?
// we only need "contains" on this set, and it has to be case sensitive and efficient.
private final TreeSet<String> reservedWords = new TreeSet<>( String.CASE_INSENSITIVE_ORDER );
private boolean globallyQuoteIdentifiers = false;
private boolean skipGlobalQuotingForColumnDefinitions = false;
private boolean autoQuoteKeywords = true;