Continued tests of Hibernate mapping model and SQL AST

This commit is contained in:
Steve Ebersole 2019-09-04 10:27:57 -05:00 committed by Andrea Boriero
parent f4441e3664
commit 1ec133a989
4 changed files with 66 additions and 24 deletions

View File

@ -271,8 +271,7 @@ public abstract class BaseSqmToSqlAstConverter
processingStateStack.getCurrent(),
this,
currentClauseStack::getCurrent,
() -> (expression) -> {},
() -> sqlQuerySpec.getSelectClause()::addSqlSelection
() -> (expression) -> {}
)
);

View File

@ -33,18 +33,14 @@ public class SqlAstQuerySpecProcessingStateImpl
private final QuerySpec querySpec;
private final Supplier<Consumer<SqlSelection>> sqlSelectionConsumerSupplier;
public SqlAstQuerySpecProcessingStateImpl(
QuerySpec querySpec,
SqlAstProcessingState parent,
SqlAstCreationState creationState,
Supplier<Clause> currentClauseAccess,
Supplier<Consumer<Expression>> resolvedExpressionConsumerAccess,
Supplier<Consumer<SqlSelection>> sqlSelectionConsumerSupplier) {
Supplier<Consumer<Expression>> resolvedExpressionConsumerAccess) {
super( parent, creationState, currentClauseAccess, resolvedExpressionConsumerAccess );
this.querySpec = querySpec;
this.sqlSelectionConsumerSupplier = sqlSelectionConsumerSupplier;
}
@Override
@ -97,8 +93,6 @@ public class SqlAstQuerySpecProcessingStateImpl
querySpec.getSelectClause().addSqlSelection( sqlSelection );
sqlSelectionConsumerSupplier.get().accept( sqlSelection );
return sqlSelection;
}
@ -106,9 +100,6 @@ public class SqlAstQuerySpecProcessingStateImpl
public SqlSelection emptySqlSelection() {
final EmptySqlSelection sqlSelection = new EmptySqlSelection( sqlSelectionMap.size() );
sqlSelectionMap.put( EmptyExpression.EMPTY_EXPRESSION, sqlSelection );
sqlSelectionConsumerSupplier.get().accept( sqlSelection );
return sqlSelection;
}

View File

@ -7,6 +7,7 @@
package org.hibernate.orm.test.metamodel.mapping;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.Table;
@ -15,6 +16,7 @@ import org.hibernate.cfg.AvailableSettings;
import org.hibernate.metamodel.mapping.EntityIdentifierMapping;
import org.hibernate.metamodel.mapping.ModelPart;
import org.hibernate.metamodel.mapping.internal.BasicValuedSingularAttributeMapping;
import org.hibernate.metamodel.model.convert.internal.NamedEnumValueConverter;
import org.hibernate.metamodel.model.convert.internal.OrdinalEnumValueConverter;
import org.hibernate.persister.entity.EntityPersister;
@ -49,18 +51,32 @@ public class SmokeTests {
final EntityIdentifierMapping identifierMapping = entityDescriptor.getIdentifierMapping();
assert Integer.class.equals( identifierMapping.getMappedTypeDescriptor().getMappedJavaTypeDescriptor().getJavaType() );
final ModelPart namePart = entityDescriptor.findSubPart( "name" );
assert namePart instanceof BasicValuedSingularAttributeMapping;
assert "mapping_simple_entity".equals( ( (BasicValuedSingularAttributeMapping) namePart ).getContainingTableExpression() );
assert "name".equals( ( (BasicValuedSingularAttributeMapping) namePart ).getMappedColumnExpression() );
{
final ModelPart namePart = entityDescriptor.findSubPart( "name" );
assert namePart instanceof BasicValuedSingularAttributeMapping;
assert "mapping_simple_entity".equals( ( (BasicValuedSingularAttributeMapping) namePart ).getContainingTableExpression() );
assert "name".equals( ( (BasicValuedSingularAttributeMapping) namePart ).getMappedColumnExpression() );
}
final ModelPart genderPart = entityDescriptor.findSubPart( "gender" );
assert genderPart instanceof BasicValuedSingularAttributeMapping;
final BasicValuedSingularAttributeMapping genderAttrMapping = (BasicValuedSingularAttributeMapping) genderPart;
assert "mapping_simple_entity".equals( genderAttrMapping.getContainingTableExpression() );
assert "gender".equals( genderAttrMapping.getMappedColumnExpression() );
assert genderAttrMapping.getConverter() != null;
assert genderAttrMapping.getConverter() instanceof OrdinalEnumValueConverter;
{
final ModelPart genderPart = entityDescriptor.findSubPart( "gender" );
assert genderPart instanceof BasicValuedSingularAttributeMapping;
final BasicValuedSingularAttributeMapping genderAttrMapping = (BasicValuedSingularAttributeMapping) genderPart;
assert "mapping_simple_entity".equals( genderAttrMapping.getContainingTableExpression() );
assert "gender".equals( genderAttrMapping.getMappedColumnExpression() );
assert genderAttrMapping.getConverter() != null;
assert genderAttrMapping.getConverter() instanceof OrdinalEnumValueConverter;
}
{
final ModelPart part = entityDescriptor.findSubPart( "gender2" );
assert part instanceof BasicValuedSingularAttributeMapping;
final BasicValuedSingularAttributeMapping attrMapping = (BasicValuedSingularAttributeMapping) part;
assert "mapping_simple_entity".equals( attrMapping.getContainingTableExpression() );
assert "gender2".equals( attrMapping.getMappedColumnExpression() );
assert attrMapping.getConverter() != null;
assert attrMapping.getConverter() instanceof NamedEnumValueConverter;
}
}
public enum Gender {
@ -70,10 +86,12 @@ public class SmokeTests {
@Entity( name = "SimpleEntity" )
@Table( name = "mapping_simple_entity" )
@SuppressWarnings("unused")
public static class SimpleEntity {
private Integer id;
private String name;
private Gender gender;
private Gender gender2;
@Id
public Integer getId() {
@ -100,6 +118,15 @@ public class SmokeTests {
public void setGender(Gender gender) {
this.gender = gender;
}
@Enumerated( EnumType.STRING )
public Gender getGender2() {
return gender2;
}
public void setGender2(Gender gender2) {
this.gender2 = gender2;
}
}

View File

@ -13,15 +13,22 @@ import org.hibernate.query.sqm.internal.QuerySqmImpl;
import org.hibernate.query.sqm.sql.internal.SqmSelectInterpretation;
import org.hibernate.query.sqm.sql.internal.SqmSelectToSqlAstConverter;
import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
import org.hibernate.sql.ast.spi.SqlSelection;
import org.hibernate.sql.ast.tree.from.FromClause;
import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.select.SelectClause;
import org.hibernate.sql.ast.tree.select.SelectStatement;
import org.hibernate.testing.orm.domain.StandardDomainModel;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
/**
* @author Steve Ebersole
*/
@ -56,6 +63,24 @@ public class SmokeTests {
final SqmSelectInterpretation interpretation = sqmConverter.interpret( sqmStatement );
final SelectStatement sqlAst = interpretation.getSqlAst();
final FromClause fromClause = sqlAst.getQuerySpec().getFromClause();
assertThat( fromClause.getRoots().size(), is( 1 ) );
final TableGroup rootTableGroup = fromClause.getRoots().get( 0 );
assertThat( rootTableGroup.hasTableGroupJoins(), is( false ) );
assertThat( rootTableGroup.getPrimaryTableReference(), notNullValue() );
assertThat( rootTableGroup.getPrimaryTableReference().getTableExpression(), is( "mapping_simple_entity" ) );
// `s` is the "alias stem" for `SimpleEntity` and as it is the first entity with that stem in
// the query the base becomes `s1`. The primary table reference is always suffixed as `_0`
assertThat( rootTableGroup.getPrimaryTableReference().getIdentificationVariable(), is( "s1_0" ) );
final SelectClause selectClause = sqlAst.getQuerySpec().getSelectClause();
assertThat( selectClause.getSqlSelections().size(), is( 1 ) ) ;
final SqlSelection sqlSelection = selectClause.getSqlSelections().get( 0 );
assertThat( sqlSelection.getJdbcResultSetIndex(), is( 1 ) );
assertThat( sqlSelection.getValuesArrayPosition(), is( 0 ) );
assertThat( sqlSelection.getJdbcValueExtractor(), notNullValue() );
}
);
}