HHH-9732 - Audit code for spots that would benefit from a case-insensitive keyed Map

This commit is contained in:
Steve Ebersole 2015-04-22 13:17:21 -05:00
parent 3c85127f82
commit 52589379e1
2 changed files with 12 additions and 26 deletions

View File

@ -28,7 +28,6 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import javax.persistence.AttributeConverter; import javax.persistence.AttributeConverter;
import javax.persistence.SharedCacheMode; import javax.persistence.SharedCacheMode;
@ -333,15 +332,9 @@ public class MetadataBuilderImpl implements MetadataBuilder, TypeContributions {
@Override @Override
public MetadataBuilder applySqlFunction(String functionName, SQLFunction function) { public MetadataBuilder applySqlFunction(String functionName, SQLFunction function) {
if ( this.options.sqlFunctionMap == null ) { if ( this.options.sqlFunctionMap == null ) {
// need to use this form as we want to specify the "concurrency level" as 1
// since only one thread will ever (should) be updating this
this.options.sqlFunctionMap = new HashMap<String, SQLFunction>(); this.options.sqlFunctionMap = new HashMap<String, SQLFunction>();
} }
this.options.sqlFunctionMap.put( functionName, function );
// HHH-7721: SQLFunctionRegistry expects all lowercase. Enforce,
// just in case a user's customer dialect uses mixed cases.
this.options.sqlFunctionMap.put( functionName.toLowerCase(Locale.ROOT), function );
return this; return this;
} }
@ -351,7 +344,6 @@ public class MetadataBuilderImpl implements MetadataBuilder, TypeContributions {
this.options.auxiliaryDatabaseObjectList = new ArrayList<AuxiliaryDatabaseObject>(); this.options.auxiliaryDatabaseObjectList = new ArrayList<AuxiliaryDatabaseObject>();
} }
this.options.auxiliaryDatabaseObjectList.add( auxiliaryDatabaseObject ); this.options.auxiliaryDatabaseObjectList.add( auxiliaryDatabaseObject );
return this; return this;
} }

View File

@ -23,9 +23,8 @@
*/ */
package org.hibernate.dialect.function; package org.hibernate.dialect.function;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.TreeMap;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
@ -35,20 +34,20 @@ import org.hibernate.dialect.Dialect;
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class SQLFunctionRegistry { public class SQLFunctionRegistry {
private final Dialect dialect; private final Map<String,SQLFunction> functionMap = new TreeMap<String, SQLFunction>(String.CASE_INSENSITIVE_ORDER);
private final Map<String, SQLFunction> userFunctions;
/** /**
* Constructs a SQLFunctionRegistry * Constructs a SQLFunctionRegistry
* *
* @param dialect The dialect * @param dialect The dialect
* @param userFunctions Any application-supplied function definitions * @param userFunctionMap Any application-supplied function definitions
*/ */
public SQLFunctionRegistry(Dialect dialect, Map<String, SQLFunction> userFunctions) { public SQLFunctionRegistry(Dialect dialect, Map<String, SQLFunction> userFunctionMap) {
this.dialect = dialect; // Apply the Dialect functions first
this.userFunctions = new HashMap<String, SQLFunction>(); functionMap.putAll( dialect.getFunctions() );
if ( userFunctions != null ) { // so that user supplied functions "override" them
this.userFunctions.putAll( userFunctions ); if ( userFunctionMap != null ) {
functionMap.putAll( userFunctionMap );
} }
} }
@ -60,11 +59,7 @@ public class SQLFunctionRegistry {
* @return The located function, maye return {@code null} * @return The located function, maye return {@code null}
*/ */
public SQLFunction findSQLFunction(String functionName) { public SQLFunction findSQLFunction(String functionName) {
final String name = functionName.toLowerCase(Locale.ROOT); return functionMap.get( functionName );
final SQLFunction userFunction = userFunctions.get( name );
return userFunction != null
? userFunction
: dialect.getFunctions().get( name );
} }
/** /**
@ -76,8 +71,7 @@ public class SQLFunctionRegistry {
*/ */
@SuppressWarnings("UnusedDeclaration") @SuppressWarnings("UnusedDeclaration")
public boolean hasFunction(String functionName) { public boolean hasFunction(String functionName) {
final String name = functionName.toLowerCase(Locale.ROOT); return functionMap.containsKey( functionName );
return userFunctions.containsKey( name ) || dialect.getFunctions().containsKey( name );
} }
} }