HHH-7869 simple improvement, using generic

This commit is contained in:
Strong Liu 2012-12-17 14:09:14 +08:00
parent 8998b4154a
commit 681528b7cc
4 changed files with 30 additions and 34 deletions

View File

@ -309,24 +309,20 @@ public class HQLQueryPlan implements Serializable {
); );
} }
Iterator itr = recognizer.getNamedParameterDescriptionMap().entrySet().iterator();
Map<String, NamedParameterDescriptor> namedParamDescriptorMap = new HashMap<String, NamedParameterDescriptor>(); Map<String, NamedParameterDescriptor> namedParamDescriptorMap = new HashMap<String, NamedParameterDescriptor>();
while( itr.hasNext() ) { Map<String, ParamLocationRecognizer.NamedParameterDescription> map = recognizer.getNamedParameterDescriptionMap();
final Map.Entry entry = ( Map.Entry ) itr.next(); for ( final String name : map.keySet() ) {
final String name = ( String ) entry.getKey(); final ParamLocationRecognizer.NamedParameterDescription description = map.get( name );
final ParamLocationRecognizer.NamedParameterDescription description =
( ParamLocationRecognizer.NamedParameterDescription ) entry.getValue();
namedParamDescriptorMap.put( namedParamDescriptorMap.put(
name, name,
new NamedParameterDescriptor( new NamedParameterDescriptor(
name, name,
parameterTranslations.getNamedParameterExpectedType( name ), parameterTranslations.getNamedParameterExpectedType( name ),
description.buildPositionsArray(), description.buildPositionsArray(),
description.isJpaStyle() description.isJpaStyle()
) )
); );
} }
return new ParameterMetadata( ordinalParamDescriptors, namedParamDescriptorMap ); return new ParameterMetadata( ordinalParamDescriptors, namedParamDescriptorMap );
} }
public QueryTranslator[] getTranslators() { public QueryTranslator[] getTranslators() {

View File

@ -41,7 +41,7 @@ public class ParamLocationRecognizer implements ParameterParser.Recognizer {
public static class NamedParameterDescription { public static class NamedParameterDescription {
private final boolean jpaStyle; private final boolean jpaStyle;
private final List positions = new ArrayList(); private final List<Integer> positions = new ArrayList<Integer>();
public NamedParameterDescription(boolean jpaStyle) { public NamedParameterDescription(boolean jpaStyle) {
this.jpaStyle = jpaStyle; this.jpaStyle = jpaStyle;
@ -60,8 +60,8 @@ public class ParamLocationRecognizer implements ParameterParser.Recognizer {
} }
} }
private Map namedParameterDescriptions = new HashMap(); private Map<String, NamedParameterDescription> namedParameterDescriptions = new HashMap<String, NamedParameterDescription>();
private List ordinalParameterLocationList = new ArrayList(); private List<Integer> ordinalParameterLocationList = new ArrayList<Integer>();
/** /**
* Convenience method for creating a param location recognizer and * Convenience method for creating a param location recognizer and
@ -82,7 +82,7 @@ public class ParamLocationRecognizer implements ParameterParser.Recognizer {
* *
* @return The map of named parameter locations. * @return The map of named parameter locations.
*/ */
public Map getNamedParameterDescriptionMap() { public Map<String, NamedParameterDescription> getNamedParameterDescriptionMap() {
return namedParameterDescriptions; return namedParameterDescriptions;
} }
@ -94,38 +94,38 @@ public class ParamLocationRecognizer implements ParameterParser.Recognizer {
* *
* @return The list of ordinal parameter locations. * @return The list of ordinal parameter locations.
*/ */
public List getOrdinalParameterLocationList() { public List<Integer> getOrdinalParameterLocationList() {
return ordinalParameterLocationList; return ordinalParameterLocationList;
} }
// Recognition code ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Recognition code ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@Override
public void ordinalParameter(int position) { public void ordinalParameter(int position) {
ordinalParameterLocationList.add( position ); ordinalParameterLocationList.add( position );
} }
@Override
public void namedParameter(String name, int position) { public void namedParameter(String name, int position) {
getOrBuildNamedParameterDescription( name, false ).add( position ); getOrBuildNamedParameterDescription( name, false ).add( position );
} }
@Override
public void jpaPositionalParameter(String name, int position) { public void jpaPositionalParameter(String name, int position) {
getOrBuildNamedParameterDescription( name, true ).add( position ); getOrBuildNamedParameterDescription( name, true ).add( position );
} }
private NamedParameterDescription getOrBuildNamedParameterDescription(String name, boolean jpa) { private NamedParameterDescription getOrBuildNamedParameterDescription(String name, boolean jpa) {
NamedParameterDescription desc = ( NamedParameterDescription ) namedParameterDescriptions.get( name ); NamedParameterDescription desc = namedParameterDescriptions.get( name );
if ( desc == null ) { if ( desc == null ) {
desc = new NamedParameterDescription( jpa ); desc = new NamedParameterDescription( jpa );
namedParameterDescriptions.put( name, desc ); namedParameterDescriptions.put( name, desc );
} }
return desc; return desc;
} }
@Override
public void other(char character) { public void other(char character) {
// don't care... // don't care...
} }
@Override
public void outParameter(int position) { public void outParameter(int position) {
// don't care... // don't care...
} }

View File

@ -88,7 +88,7 @@ public class ParameterParser {
else { else {
if ( c == ':' ) { if ( c == ':' ) {
// named parameter // named parameter
int right = StringHelper.firstIndexOfChar( sqlString, ParserHelper.HQL_SEPARATORS, indx + 1 ); int right = StringHelper.firstIndexOfChar( sqlString, ParserHelper.HQL_SEPARATORS_BITSET, indx + 1 );
int chopLocation = right < 0 ? sqlString.length() : right; int chopLocation = right < 0 ? sqlString.length() : right;
String param = sqlString.substring( indx + 1, chopLocation ); String param = sqlString.substring( indx + 1, chopLocation );
if ( StringHelper.isEmpty( param ) ) { if ( StringHelper.isEmpty( param ) ) {

View File

@ -139,23 +139,23 @@ public class QueryPlanCache implements Serializable {
final int size = recognizer.getOrdinalParameterLocationList().size(); final int size = recognizer.getOrdinalParameterLocationList().size();
OrdinalParameterDescriptor[] ordinalDescriptors = new OrdinalParameterDescriptor[ size ]; OrdinalParameterDescriptor[] ordinalDescriptors = new OrdinalParameterDescriptor[ size ];
for ( int i = 0; i < size; i++ ) { for ( int i = 0; i < size; i++ ) {
final Integer position = ( Integer ) recognizer.getOrdinalParameterLocationList().get( i ); final Integer position = recognizer.getOrdinalParameterLocationList().get( i );
ordinalDescriptors[i] = new OrdinalParameterDescriptor( i, null, position ); ordinalDescriptors[i] = new OrdinalParameterDescriptor( i, null, position );
} }
Map<String, NamedParameterDescriptor> namedParamDescriptorMap = new HashMap<String, NamedParameterDescriptor>();
Iterator itr = recognizer.getNamedParameterDescriptionMap().entrySet().iterator(); Map<String, ParamLocationRecognizer.NamedParameterDescription> map = recognizer.getNamedParameterDescriptionMap();
Map<String,NamedParameterDescriptor> namedParamDescriptorMap = new HashMap<String,NamedParameterDescriptor>(); for ( final String name : map.keySet() ) {
while( itr.hasNext() ) { final ParamLocationRecognizer.NamedParameterDescription description = map.get( name );
final Map.Entry entry = ( Map.Entry ) itr.next();
final String name = ( String ) entry.getKey();
final ParamLocationRecognizer.NamedParameterDescription description =
( ParamLocationRecognizer.NamedParameterDescription ) entry.getValue();
namedParamDescriptorMap.put( namedParamDescriptorMap.put(
name , name,
new NamedParameterDescriptor( name, null, description.buildPositionsArray(), description.isJpaStyle() ) new NamedParameterDescriptor(
name,
null,
description.buildPositionsArray(),
description.isJpaStyle()
)
); );
} }
return new ParameterMetadata( ordinalDescriptors, namedParamDescriptorMap ); return new ParameterMetadata( ordinalDescriptors, namedParamDescriptorMap );
} }