Java Code Examples for org.hibernate.type.Type.sqlTypes()

The following are Jave code examples for showing how to use sqlTypes() of the org.hibernate.type.Type class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: lams   File: ClassicAvgFunction.java   View Source Code Vote up 6 votes
@Override
public Type getReturnType(Type columnType, Mapping mapping) throws QueryException {
	int[] sqlTypes;
	try {
		sqlTypes = columnType.sqlTypes( mapping );
	}
	catch ( MappingException me ) {
		throw new QueryException( me );
	}

	if ( sqlTypes.length != 1 ) {
		throw new QueryException( "multi-column type in avg()" );
	}

	final int sqlType = sqlTypes[0];
	if ( sqlType == Types.INTEGER || sqlType == Types.BIGINT || sqlType == Types.TINYINT ) {
		return StandardBasicTypes.FLOAT;
	}
	else {
		return columnType;
	}
}
 
Example 2
Project: lams   File: SimpleExpression.java   View Source Code Vote up 5 votes
@Override
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
	final String[] columns = criteriaQuery.findColumns( propertyName, criteria );
	final Type type = criteriaQuery.getTypeUsingProjection( criteria, propertyName );
	final StringBuilder fragment = new StringBuilder();

	if ( columns.length > 1 ) {
		fragment.append( '(' );
	}
	final SessionFactoryImplementor factory = criteriaQuery.getFactory();
	final int[] sqlTypes = type.sqlTypes( factory );
	for ( int i = 0; i < columns.length; i++ ) {
		final boolean lower = ignoreCase && (sqlTypes[i] == Types.VARCHAR || sqlTypes[i] == Types.CHAR);
		if ( lower ) {
			fragment.append( factory.getDialect().getLowercaseFunction() ).append( '(' );
		}
		fragment.append( columns[i] );
		if ( lower ) {
			fragment.append( ')' );
		}

		fragment.append( getOp() ).append( "?" );
		if ( i < columns.length - 1 ) {
			fragment.append( " and " );
		}
	}
	if ( columns.length > 1 ) {
		fragment.append( ')' );
	}
	return fragment.toString();
}
 
Example 3
Project: lams   File: StandardAnsiSqlAggregationFunctions.java   View Source Code Vote up 5 votes
protected final int determineJdbcTypeCode(Type firstArgumentType, SessionFactoryImplementor factory) throws QueryException {
	try {
		final int[] jdbcTypeCodes = firstArgumentType.sqlTypes( factory );
		if ( jdbcTypeCodes.length != 1 ) {
			throw new QueryException( "multiple-column type in avg()" );
		}
		return jdbcTypeCodes[0];
	}
	catch ( MappingException me ) {
		throw new QueryException( me );
	}
}
 
Example 4
Project: lams   File: StandardAnsiSqlAggregationFunctions.java   View Source Code Vote up 5 votes
protected final int determineJdbcTypeCode(Type type, Mapping mapping) throws QueryException {
	try {
		final int[] jdbcTypeCodes = type.sqlTypes( mapping );
		if ( jdbcTypeCodes.length != 1 ) {
			throw new QueryException( "multiple-column type in sum()" );
		}
		return jdbcTypeCodes[0];
	}
	catch ( MappingException me ) {
		throw new QueryException( me );
	}
}
 
Example 5
Project: lams   File: IntoClause.java   View Source Code Vote up 5 votes
/**
 * Determine whether the two types are "assignment compatible".
 *
 * @param target The type defined in the into-clause.
 * @param source The type defined in the select clause.
 * @return True if they are assignment compatible.
 */
private boolean areCompatible(Type target, Type source) {
	if ( target.equals( source ) ) {
		// if the types report logical equivalence, return true...
		return true;
	}

	// otherwise, doAfterTransactionCompletion a "deep equivalence" check...

	if ( !target.getReturnedClass().isAssignableFrom( source.getReturnedClass() ) ) {
		return false;
	}

	int[] targetDatatypes = target.sqlTypes( getSessionFactoryHelper().getFactory() );
	int[] sourceDatatypes = source.sqlTypes( getSessionFactoryHelper().getFactory() );

	if ( targetDatatypes.length != sourceDatatypes.length ) {
		return false;
	}

	for ( int i = 0; i < targetDatatypes.length; i++ ) {
		if ( !areSqlTypesCompatible( targetDatatypes[i], sourceDatatypes[i] ) ) {
			return false;
		}
	}

	return true;
}
 
Example 6
Project: lams   File: Order.java   View Source Code Vote up 4 votes
/**
 * Render the SQL fragment
 *
 * @param criteria The criteria
 * @param criteriaQuery The overall query
 *
 * @return The ORDER BY fragment for this ordering
 */
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) {
	final String[] columns = criteriaQuery.getColumnsUsingProjection( criteria, propertyName );
	final Type type = criteriaQuery.getTypeUsingProjection( criteria, propertyName );
	final SessionFactoryImplementor factory = criteriaQuery.getFactory();
	final int[] sqlTypes = type.sqlTypes( factory );

	final StringBuilder fragment = new StringBuilder();
	for ( int i=0; i<columns.length; i++ ) {
		final StringBuilder expression = new StringBuilder();
		boolean lower = false;
		if ( ignoreCase ) {
			final int sqlType = sqlTypes[i];
			lower = sqlType == Types.VARCHAR
					|| sqlType == Types.CHAR
					|| sqlType == Types.LONGVARCHAR;
		}
		
		if ( lower ) {
			expression.append( factory.getDialect().getLowercaseFunction() )
					.append( '(' );
		}
		expression.append( columns[i] );
		if ( lower ) {
			expression.append( ')' );
		}

		fragment.append(
				factory.getDialect().renderOrderByElement(
						expression.toString(),
						null,
						ascending ? "asc" : "desc",
						nullPrecedence != null ? nullPrecedence : factory.getSettings().getDefaultNullPrecedence()
				)
		);
		if ( i < columns.length-1 ) {
			fragment.append( ", " );
		}
	}

	return fragment.toString();
}