HHH-7841 : Add LoadQuery for generating SQL

This commit is contained in:
Gail Badner 2013-03-26 23:18:46 -07:00 committed by Steve Ebersole
parent 8b7091e1c7
commit 6ea20fa308
6 changed files with 136 additions and 4 deletions

View File

@ -0,0 +1,66 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2013, 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.loader.plan.internal;
import org.hibernate.LockMode;
import org.hibernate.engine.spi.LoadQueryInfluencers;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.loader.entity.EntityJoinWalker;
import org.hibernate.loader.plan.spi.LoadQuery;
import org.hibernate.persister.entity.OuterJoinLoadable;
/**
* @author Gail Badner
*/
public class EntityLoadQueryImpl implements LoadQuery {
final SessionFactoryImplementor sessionFactory;
final LoadQueryInfluencers loadQueryInfluencers;
final LockMode lockMode;
final OuterJoinLoadable entityPersister;
public EntityLoadQueryImpl(
SessionFactoryImplementor sessionFactory,
LoadQueryInfluencers loadQueryInfluencers,
LockMode lockMode,
OuterJoinLoadable entityPersister) {
this.sessionFactory = sessionFactory;
this.loadQueryInfluencers = loadQueryInfluencers;
this.lockMode = lockMode;
this.entityPersister = entityPersister;
}
@Override
public String generateSql(int batchSize) {
final EntityJoinWalker entityJoinWalker = new EntityJoinWalker(
entityPersister,
entityPersister.getIdentifierColumnNames(),
batchSize,
lockMode,
sessionFactory,
loadQueryInfluencers
);
return entityJoinWalker.getSQLString();
}
}

View File

@ -26,7 +26,9 @@ package org.hibernate.loader.plan.internal;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import org.hibernate.engine.spi.LoadQueryInfluencers;
import org.hibernate.loader.plan.spi.LoadPlan; import org.hibernate.loader.plan.spi.LoadPlan;
import org.hibernate.loader.plan.spi.LoadQuery;
import org.hibernate.loader.plan.spi.Return; import org.hibernate.loader.plan.spi.Return;
/** /**
@ -37,14 +39,16 @@ import org.hibernate.loader.plan.spi.Return;
public class LoadPlanImpl implements LoadPlan { public class LoadPlanImpl implements LoadPlan {
private final boolean hasScalars; private final boolean hasScalars;
private final List<Return> returns; private final List<Return> returns;
private final LoadQuery loadQuery;
public LoadPlanImpl(boolean hasScalars, List<Return> returns) { public LoadPlanImpl(LoadQuery loadQuery, boolean hasScalars, List<Return> returns) {
this.loadQuery = loadQuery;
this.hasScalars = hasScalars; this.hasScalars = hasScalars;
this.returns = returns; this.returns = returns;
} }
public LoadPlanImpl(boolean hasScalars, Return rootReturn) { public LoadPlanImpl(LoadQuery loadQuery, boolean hasScalars, Return rootReturn) {
this( hasScalars, Collections.singletonList( rootReturn ) ); this( loadQuery, hasScalars, Collections.singletonList( rootReturn ) );
} }
@Override @Override
@ -56,4 +60,9 @@ public class LoadPlanImpl implements LoadPlan {
public List<Return> getReturns() { public List<Return> getReturns() {
return returns; return returns;
} }
@Override
public LoadQuery getLoadQuery() {
return loadQuery;
}
} }

View File

@ -39,9 +39,11 @@ import org.hibernate.loader.plan.spi.CollectionReturn;
import org.hibernate.loader.plan.spi.EntityReturn; import org.hibernate.loader.plan.spi.EntityReturn;
import org.hibernate.loader.plan.spi.LoadPlan; import org.hibernate.loader.plan.spi.LoadPlan;
import org.hibernate.loader.plan.spi.LoadPlanBuilderStrategy; import org.hibernate.loader.plan.spi.LoadPlanBuilderStrategy;
import org.hibernate.loader.plan.spi.LoadQuery;
import org.hibernate.loader.plan.spi.Return; import org.hibernate.loader.plan.spi.Return;
import org.hibernate.persister.collection.CollectionPersister; import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.persister.entity.OuterJoinLoadable;
import org.hibernate.persister.walking.spi.AssociationAttributeDefinition; import org.hibernate.persister.walking.spi.AssociationAttributeDefinition;
import org.hibernate.persister.walking.spi.CollectionDefinition; import org.hibernate.persister.walking.spi.CollectionDefinition;
import org.hibernate.persister.walking.spi.EntityDefinition; import org.hibernate.persister.walking.spi.EntityDefinition;
@ -101,7 +103,23 @@ public class SingleRootReturnLoadPlanBuilderStrategy
@Override @Override
public LoadPlan buildLoadPlan() { public LoadPlan buildLoadPlan() {
return new LoadPlanImpl( false, rootReturn ); return new LoadPlanImpl( createLoadQuery(), false, rootReturn );
}
private LoadQuery createLoadQuery() {
if ( EntityReturn.class.isInstance( rootReturn ) ) {
final EntityReturn entityReturn = (EntityReturn) rootReturn;
return new EntityLoadQueryImpl(
sessionFactory(),
loadQueryInfluencers,
entityReturn.getLockMode(),
(OuterJoinLoadable) entityReturn.getEntityPersister()
);
}
else {
// TODO: create a LoadQuery for other types of returns.
return null;
}
} }
@Override @Override

View File

@ -25,6 +25,8 @@ package org.hibernate.loader.plan.spi;
import java.util.List; import java.util.List;
import org.hibernate.engine.spi.LoadQueryInfluencers;
/** /**
* Describes a plan for performing a load of results. * Describes a plan for performing a load of results.
* *
@ -55,6 +57,7 @@ public interface LoadPlan {
public List<Return> getReturns(); public List<Return> getReturns();
public LoadQuery getLoadQuery();
// todo : would also like to see "call back" style access for handling "subsequent actions" such as: // todo : would also like to see "call back" style access for handling "subsequent actions" such as:
// 1) follow-on locking // 1) follow-on locking

View File

@ -0,0 +1,32 @@
/*
* jDocBook, processing of DocBook sources
*
* Copyright (c) 2013, 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.loader.plan.spi;
/**
* @author Gail Badner
*/
public interface LoadQuery {
String generateSql(int batchSize);
}

View File

@ -77,6 +77,10 @@ public class LoadPlanBuilderTest extends BaseCoreFunctionalTestCase {
EntityFetch entityFetch = ExtraAssertions.assertTyping( EntityFetch.class, fetch ); EntityFetch entityFetch = ExtraAssertions.assertTyping( EntityFetch.class, fetch );
assertNotNull( entityFetch.getFetches() ); assertNotNull( entityFetch.getFetches() );
assertEquals( 0, entityFetch.getFetches().length ); assertEquals( 0, entityFetch.getFetches().length );
String loadSql = plan.getLoadQuery().generateSql( 1 );
// TODO: assert that aliases used in loadSql match up with those in entityReturn and entityFetch
// (they currently do not match up)
} }
@Test @Test