HHH-9190 : consolidate contracts

This commit is contained in:
Steve Ebersole 2014-05-22 09:38:16 -05:00
parent d1959cd121
commit fbe72e7b64
8 changed files with 78 additions and 204 deletions

View File

@ -21,19 +21,30 @@
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.engine.query.spi;
package org.hibernate.engine.query.internal;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.engine.query.spi.NamedParameterDescriptor;
import org.hibernate.engine.query.spi.NativeQueryInterpreter;
import org.hibernate.engine.query.spi.NativeSQLQueryPlan;
import org.hibernate.engine.query.spi.OrdinalParameterDescriptor;
import org.hibernate.engine.query.spi.ParamLocationRecognizer;
import org.hibernate.engine.query.spi.ParameterMetadata;
import org.hibernate.engine.query.spi.sql.NativeSQLQuerySpecification;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.loader.custom.CustomQuery;
import org.hibernate.loader.custom.sql.SQLCustomQuery;
/**
* A {@link ParameterMetadataRecognizer} which retrieves parameter meta-data
* from native SQL queries.
*
* @author Gunnar Morling
*
* @author Steve Ebersole
*/
public class SQLParameterMetadataRecognizer implements ParameterMetadataRecognizer {
public class NativeQueryInterpreterStandardImpl implements NativeQueryInterpreter {
/**
* Singleton access
*/
public static final NativeQueryInterpreterStandardImpl INSTANCE = new NativeQueryInterpreterStandardImpl();
@Override
public ParameterMetadata getParameterMetadata(String nativeQuery) {
@ -64,4 +75,19 @@ public class SQLParameterMetadataRecognizer implements ParameterMetadataRecogniz
return new ParameterMetadata( ordinalDescriptors, namedParamDescriptorMap );
}
@Override
public NativeSQLQueryPlan createQueryPlan(
NativeSQLQuerySpecification specification,
SessionFactoryImplementor sessionFactory) {
CustomQuery customQuery = new SQLCustomQuery(
specification.getQueryString(),
specification.getQueryReturns(),
specification.getQuerySpaces(),
sessionFactory
);
return new NativeSQLQueryPlan( specification.getQueryString(), customQuery );
}
}

View File

@ -1,51 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.engine.query.spi;
import org.hibernate.engine.query.spi.sql.NativeSQLQuerySpecification;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.loader.custom.CustomQuery;
import org.hibernate.loader.custom.sql.SQLCustomQuery;
/**
* Default query plan factory used by Hibernate ORM. Creates query plans for SQL
* queries.
*
* @author Gunnar Morling
*
*/
public class DefaultQueryPlanFactory implements QueryPlanFactory {
@Override
public NativeSQLQueryPlan createNativeQueryPlan(NativeSQLQuerySpecification nativeQuerySpecification, SessionFactoryImplementor sessionFactory) {
CustomQuery customQuery = new SQLCustomQuery(
nativeQuerySpecification.getQueryString(),
nativeQuerySpecification.getQueryReturns(),
nativeQuerySpecification.getQuerySpaces(),
sessionFactory
);
return new NativeSQLQueryPlan( nativeQuerySpecification.getQueryString(), customQuery );
}
}

View File

@ -23,30 +23,35 @@
*/
package org.hibernate.engine.query.spi;
import org.hibernate.engine.query.spi.sql.NativeSQLQuerySpecification;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.service.Service;
import org.hibernate.service.spi.SessionFactoryServiceInitiator;
/**
* Service contract for extracting {@link ParameterMetadata} from given native
* queries.
* <p>
* The default implementation extracts parameter meta-data from native SQL
* queries, but integrators can plug-in alternative recognizer implementations
* by contributing a custom {@link SessionFactoryServiceInitiator} for this
* service contract.
* Service contract for dealing with native queries.
*
* @author Steve Ebersole
* @author Gunnar Morling
*/
public interface ParameterMetadataRecognizer extends Service {
public interface NativeQueryInterpreter extends Service {
/**
* Returns a meta-data object with information about the named and ordinal
* parameters contained in the given native query.
*
* @param nativeQuery
* the native query to analyze.
* @param nativeQuery the native query to analyze.
*
* @return a meta-data object describing the parameters of the given query.
* Must not be {@code null}.
*/
ParameterMetadata getParameterMetadata(String nativeQuery);
/**
* Creates a new query plan for the specified native query.
*
* @param specification Describes the query to create a plan for
* @param sessionFactory The current session factory
*
* @return A query plan for the specified native query.
*/
NativeSQLQueryPlan createQueryPlan(NativeSQLQuerySpecification specification, SessionFactoryImplementor sessionFactory);
}

View File

@ -24,32 +24,39 @@
package org.hibernate.engine.query.spi;
import org.hibernate.cfg.Configuration;
import org.hibernate.engine.query.internal.NativeQueryInterpreterStandardImpl;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.metamodel.spi.MetadataImplementor;
import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.hibernate.service.spi.SessionFactoryServiceInitiator;
/**
* Initiates the default {@link QueryPlanFactory}.
*
* @author Gunnar Morling
* @author Steve Ebersole
*/
public class QueryPlanFactoryInitiator implements SessionFactoryServiceInitiator<QueryPlanFactory> {
public static final QueryPlanFactoryInitiator INSTANCE = new QueryPlanFactoryInitiator();
public class NativeQueryInterpreterInitiator implements SessionFactoryServiceInitiator<NativeQueryInterpreter> {
/**
* Singleton access
*/
public static final NativeQueryInterpreterInitiator INSTANCE = new NativeQueryInterpreterInitiator();
@Override
public QueryPlanFactory initiateService(SessionFactoryImplementor sessionFactory, Configuration configuration, ServiceRegistryImplementor registry) {
return new DefaultQueryPlanFactory();
public NativeQueryInterpreter initiateService(
SessionFactoryImplementor sessionFactory,
Configuration configuration,
ServiceRegistryImplementor registry) {
return NativeQueryInterpreterStandardImpl.INSTANCE;
}
@Override
public QueryPlanFactory initiateService(SessionFactoryImplementor sessionFactory, MetadataImplementor metadata, ServiceRegistryImplementor registry) {
return new DefaultQueryPlanFactory();
public NativeQueryInterpreter initiateService(
SessionFactoryImplementor sessionFactory,
MetadataImplementor metadata,
ServiceRegistryImplementor registry) {
return NativeQueryInterpreterStandardImpl.INSTANCE;
}
@Override
public Class<QueryPlanFactory> getServiceInitiated() {
return QueryPlanFactory.class;
public Class<NativeQueryInterpreter> getServiceInitiated() {
return NativeQueryInterpreter.class;
}
}

View File

@ -1,55 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.engine.query.spi;
import org.hibernate.cfg.Configuration;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.metamodel.spi.MetadataImplementor;
import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.hibernate.service.spi.SessionFactoryServiceInitiator;
/**
* Initiates the default {@link ParameterMetadataRecognizer}.
*
* @author Gunnar Morling
*/
public class ParameterMetadataRecognizerInitiator implements SessionFactoryServiceInitiator<ParameterMetadataRecognizer> {
public static final ParameterMetadataRecognizerInitiator INSTANCE = new ParameterMetadataRecognizerInitiator();
@Override
public ParameterMetadataRecognizer initiateService(SessionFactoryImplementor sessionFactory, Configuration configuration, ServiceRegistryImplementor registry) {
return new SQLParameterMetadataRecognizer();
}
@Override
public ParameterMetadataRecognizer initiateService(SessionFactoryImplementor sessionFactory, MetadataImplementor metadata, ServiceRegistryImplementor registry) {
return new SQLParameterMetadataRecognizer();
}
@Override
public Class<ParameterMetadataRecognizer> getServiceInitiated() {
return ParameterMetadataRecognizer.class;
}
}

View File

@ -81,6 +81,9 @@ public class QueryPlanCache implements Serializable {
*/
private final BoundedConcurrentHashMap<String,ParameterMetadata> parameterMetadataCache;
private NativeQueryInterpreter nativeQueryInterpreterService;
/**
* Constructs the QueryPlanCache to be used by the given SessionFactory
*
@ -120,6 +123,7 @@ public class QueryPlanCache implements Serializable {
BoundedConcurrentHashMap.Eviction.LIRS
);
nativeQueryInterpreterService = factory.getServiceRegistry().getService( NativeQueryInterpreter.class );
}
/**
@ -135,10 +139,7 @@ public class QueryPlanCache implements Serializable {
public ParameterMetadata getSQLParameterMetadata(final String query) {
ParameterMetadata value = parameterMetadataCache.get( query );
if ( value == null ) {
value = factory.getServiceRegistry()
.getService( ParameterMetadataRecognizer.class )
.getParameterMetadata( query );
value = nativeQueryInterpreterService.getParameterMetadata( query );
parameterMetadataCache.putIfAbsent( query, value );
}
return value;
@ -222,10 +223,7 @@ public class QueryPlanCache implements Serializable {
NativeSQLQueryPlan value = (NativeSQLQueryPlan) queryPlanCache.get( spec );
if ( value == null ) {
LOG.tracev( "Unable to locate native-sql query plan in cache; generating ({0})", spec.getQueryString() );
value = factory.getServiceRegistry()
.getService(QueryPlanFactory.class )
.createNativeQueryPlan( spec, factory );
value = nativeQueryInterpreterService.createQueryPlan( spec, factory );
queryPlanCache.putIfAbsent( spec, value );
}
else {

View File

@ -1,55 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.engine.query.spi;
import org.hibernate.engine.query.spi.sql.NativeSQLQuerySpecification;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.service.Service;
import org.hibernate.service.spi.SessionFactoryServiceInitiator;
/**
* Contract for factories of query plans.
* <p>
* The default implementation creates query plans for SQL queries, but
* integrators can plug in another factory by registering a custom
* {@link SessionFactoryServiceInitiator} for this service contract.
*
* @author Gunnar Morling
*
*/
public interface QueryPlanFactory extends Service {
/**
* Creates a new query plan for the specified native query.
*
* @param nativeQuerySpecification
* Describes the query to create a plan for
* @param sessionFactory
* The current session factory
* @return A query plan for the specified native query.
*/
NativeSQLQueryPlan createNativeQueryPlan(
NativeSQLQuerySpecification nativeQuerySpecification,
SessionFactoryImplementor sessionFactory);
}

View File

@ -27,8 +27,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.hibernate.engine.query.spi.ParameterMetadataRecognizerInitiator;
import org.hibernate.engine.query.spi.QueryPlanFactoryInitiator;
import org.hibernate.engine.query.spi.NativeQueryInterpreterInitiator;
import org.hibernate.engine.spi.CacheInitiator;
import org.hibernate.event.service.internal.EventListenerServiceInitiator;
import org.hibernate.service.spi.SessionFactoryServiceInitiator;
@ -49,8 +48,8 @@ public class StandardSessionFactoryServiceInitiators {
serviceInitiators.add( EventListenerServiceInitiator.INSTANCE );
serviceInitiators.add( StatisticsInitiator.INSTANCE );
serviceInitiators.add( CacheInitiator.INSTANCE );
serviceInitiators.add( ParameterMetadataRecognizerInitiator.INSTANCE );
serviceInitiators.add( QueryPlanFactoryInitiator.INSTANCE );
serviceInitiators.add( NativeQueryInterpreterInitiator.INSTANCE );
return Collections.unmodifiableList( serviceInitiators );
}