下面列出了org.hibernate.type.Type#getColumnSpan ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private int bindFilterParameterValues(
PreparedStatement st,
QueryParameters queryParameters,
int position,
SessionImplementor session) throws SQLException {
// todo : better to handle dynamic filters through implicit DynamicFilterParameterSpecification
// see the discussion there in DynamicFilterParameterSpecification's javadocs as to why
// it is currently not done that way.
int filteredParamCount = queryParameters.getFilteredPositionalParameterTypes() == null
? 0
: queryParameters.getFilteredPositionalParameterTypes().length;
int nonfilteredParamCount = queryParameters.getPositionalParameterTypes() == null
? 0
: queryParameters.getPositionalParameterTypes().length;
int filterParamCount = filteredParamCount - nonfilteredParamCount;
for ( int i = 0; i < filterParamCount; i++ ) {
Type type = queryParameters.getFilteredPositionalParameterTypes()[i];
Object value = queryParameters.getFilteredPositionalParameterValues()[i];
type.nullSafeSet( st, value, position, session );
position += type.getColumnSpan( getFactory() );
}
return position;
}
protected final void mutateRowValueConstructorSyntaxesIfNecessary(Type lhsType, Type rhsType) {
// TODO : this really needs to be delayed unitl after we definitively know all node types
// where this is currently a problem is parameters for which where we cannot unequivocally
// resolve an expected type
SessionFactoryImplementor sessionFactory = getSessionFactoryHelper().getFactory();
if ( lhsType != null && rhsType != null ) {
int lhsColumnSpan = lhsType.getColumnSpan( sessionFactory );
if ( lhsColumnSpan != rhsType.getColumnSpan( sessionFactory ) ) {
throw new TypeMismatchException(
"left and right hand sides of a binary logic operator were incompatibile [" +
lhsType.getName() + " : "+ rhsType.getName() + "]"
);
}
if ( lhsColumnSpan > 1 ) {
// for dialects which are known to not support ANSI-SQL row-value-constructor syntax,
// we should mutate the tree.
if ( !sessionFactory.getDialect().supportsRowValueConstructorSyntax() ) {
mutateRowValueConstructorSyntax( lhsColumnSpan );
}
}
}
}
/**
* Count the number of columns this projection uses.
*
* @param criteria The criteria
* @param criteriaQuery The query
*
* @return The number of columns
*/
public int getColumnCount(Criteria criteria, CriteriaQuery criteriaQuery) {
final Type[] types = getTypes( criteria, criteriaQuery );
int count = 0;
for ( Type type : types ) {
count += type.getColumnSpan( criteriaQuery.getFactory() );
}
return count;
}
private int getColumnSpan(Type type, SessionFactoryImplementor sfi) {
int columnSpan = type.getColumnSpan( sfi );
if ( columnSpan == 0 && type instanceof OneToOneType ) {
columnSpan = ( (OneToOneType) type ).getIdentifierOrUniqueKeyType( sfi ).getColumnSpan( sfi );
}
return columnSpan;
}
@Override
public void initialize() {
// TODO : this really needs to be delayed until after we definitively know the operand node type;
// where this is currently a problem is parameters for which where we cannot unequivocally
// resolve an expected type
Type operandType = extractDataType( getOperand() );
if ( operandType == null ) {
return;
}
SessionFactoryImplementor sessionFactory = getSessionFactoryHelper().getFactory();
int operandColumnSpan = operandType.getColumnSpan( sessionFactory );
if ( operandColumnSpan > 1 ) {
mutateRowValueConstructorSyntax( operandColumnSpan );
}
}
@Override
public Serializable loadEntityIdByNaturalId(
Object[] naturalIdValues,
LockOptions lockOptions,
SharedSessionContractImplementor session) {
if ( LOG.isTraceEnabled() ) {
LOG.tracef(
"Resolving natural-id [%s] to id : %s ",
naturalIdValues,
MessageHelper.infoString( this )
);
}
final boolean[] valueNullness = determineValueNullness( naturalIdValues );
final String sqlEntityIdByNaturalIdString = determinePkByNaturalIdQuery( valueNullness );
try {
PreparedStatement ps = session
.getJdbcCoordinator()
.getStatementPreparer()
.prepareStatement( sqlEntityIdByNaturalIdString );
try {
int positions = 1;
int loop = 0;
for ( int idPosition : getNaturalIdentifierProperties() ) {
final Object naturalIdValue = naturalIdValues[loop++];
if ( naturalIdValue != null ) {
final Type type = getPropertyTypes()[idPosition];
type.nullSafeSet( ps, naturalIdValue, positions, session );
positions += type.getColumnSpan( session.getFactory() );
}
}
ResultSet rs = session.getJdbcCoordinator().getResultSetReturn().extract( ps );
try {
// if there is no resulting row, return null
if ( !rs.next() ) {
return null;
}
final Object hydratedId = getIdentifierType().hydrate( rs, getIdentifierAliases(), session, null );
return (Serializable) getIdentifierType().resolve( hydratedId, session, null );
}
finally {
session.getJdbcCoordinator().getResourceRegistry().release( rs, ps );
}
}
finally {
session.getJdbcCoordinator().getResourceRegistry().release( ps );
session.getJdbcCoordinator().afterStatementExecution();
}
}
catch (SQLException e) {
throw getFactory().getSQLExceptionHelper().convert(
e,
String.format(
"could not resolve natural-id [%s] to id : %s",
naturalIdValues,
MessageHelper.infoString( this )
),
sqlEntityIdByNaturalIdString
);
}
}
/**
* Bind the appropriate value into the given statement at the specified position.
*
* @param statement The statement into which the value should be bound.
* @param qp The defined values for the current query execution.
* @param session The session against which the current execution is occuring.
* @param position The position from which to start binding value(s).
*
* @return The number of sql bind positions "eaten" by this bind operation.
*/
public int bind(PreparedStatement statement, QueryParameters qp, SessionImplementor session, int position) throws SQLException {
Type type = qp.getPositionalParameterTypes()[hqlPosition];
Object value = qp.getPositionalParameterValues()[hqlPosition];
type.nullSafeSet( statement, value, position, session );
return type.getColumnSpan( session.getFactory() );
}
/**
* Retrieve the number of columns represented by this type.
*
* @param type The type.
*
* @return The number of columns.
*/
public int getColumnSpan(Type type) {
return type.getColumnSpan( sfi );
}
/**
* Retreive the number of columns represented by this type.
*
* @param type The type.
* @return The number of columns.
*/
public int getColumnSpan(Type type) {
return type.getColumnSpan( sfi );
}