From 67a2ed17cafa3f0808e96813ac72f5eb7651328c Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Tue, 27 Oct 2020 22:03:39 +0000 Subject: [PATCH] HHH-14292 Avoid defensive copy for keywords set in NormalizingIdentifierHelperImpl --- .../internal/NormalizingIdentifierHelperImpl.java | 13 +++++++------ .../jdbc/env/spi/IdentifierHelperBuilder.java | 6 ++++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/NormalizingIdentifierHelperImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/NormalizingIdentifierHelperImpl.java index 46ba1c39ef..e7f8072f5b 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/NormalizingIdentifierHelperImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/NormalizingIdentifierHelperImpl.java @@ -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 reservedWords = new TreeSet( String.CASE_INSENSITIVE_ORDER ); + private final TreeSet 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 reservedWords, + TreeSet 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 ); } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/spi/IdentifierHelperBuilder.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/spi/IdentifierHelperBuilder.java index ff1a6f739d..0f74afd8f3 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/spi/IdentifierHelperBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/spi/IdentifierHelperBuilder.java @@ -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 reservedWords = new TreeSet( 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 reservedWords = new TreeSet<>( String.CASE_INSENSITIVE_ORDER ); + private boolean globallyQuoteIdentifiers = false; private boolean skipGlobalQuotingForColumnDefinitions = false; private boolean autoQuoteKeywords = true;