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

The following examples show how to use org.hibernate.type.Type#sqlTypes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProcedureParameterImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void setHibernateType(Type expectedType) {
	super.setHibernateType( expectedType );

	if ( mode == ParameterMode.REF_CURSOR ) {
		sqlTypes = new int[] { Types.REF_CURSOR };
	}
	else {
		if ( expectedType == null ) {
			throw new IllegalArgumentException( "Type cannot be null" );
		}
		else {
			sqlTypes = expectedType.sqlTypes( procedureCall.getSession().getFactory() );
		}
	}

}
 
Example 2
Source File: Order.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Render the SQL fragment
 *
 */
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) 
throws HibernateException {
	String[] columns = criteriaQuery.getColumnsUsingProjection(criteria, propertyName);
	Type type = criteriaQuery.getTypeUsingProjection(criteria, propertyName);
	StringBuffer fragment = new StringBuffer();
	for ( int i=0; i<columns.length; i++ ) {
		SessionFactoryImplementor factory = criteriaQuery.getFactory();
		boolean lower = ignoreCase && type.sqlTypes( factory )[i]==Types.VARCHAR;
		if (lower) {
			fragment.append( factory.getDialect().getLowercaseFunction() )
				.append('(');
		}
		fragment.append( columns[i] );
		if (lower) fragment.append(')');
		fragment.append( ascending ? " asc" : " desc" );
		if ( i<columns.length-1 ) fragment.append(", ");
	}
	return fragment.toString();
}
 
Example 3
Source File: SimpleExpression.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
throws HibernateException {

	String[] columns = criteriaQuery.getColumnsUsingProjection(criteria, propertyName);
	Type type = criteriaQuery.getTypeUsingProjection(criteria, propertyName);
	StringBuffer fragment = new StringBuffer();
	if (columns.length>1) fragment.append('(');
	SessionFactoryImplementor factory = criteriaQuery.getFactory();
	int[] sqlTypes = type.sqlTypes( factory );
	for ( int i=0; i<columns.length; i++ ) {
		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 4
Source File: ClassicAvgFunction.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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()" );
	int sqlType = sqlTypes[0];
	if ( sqlType == Types.INTEGER || sqlType == Types.BIGINT || sqlType == Types.TINYINT ) {
		return Hibernate.FLOAT;
	}
	else {
		return columnType;
	}
}
 
Example 5
Source File: SimpleExpression.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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 || 
				sqlTypes[i] == Types.NVARCHAR || sqlTypes[i] == Types.NCHAR);
		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 6
Source File: StandardAnsiSqlAggregationFunctions.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
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 7
Source File: StandardAnsiSqlAggregationFunctions.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
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 8
Source File: IntoClause.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 9
Source File: Dialect.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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()" );
	return Hibernate.DOUBLE;
}
 
Example 10
Source File: Dialect.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public Type getReturnType(Type columnType, Mapping mapping) {
	//pre H3.2 behavior: super.getReturnType(ct, m);
	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 sum()" );
	int sqlType = sqlTypes[0];

	// First allow the actual type to control the return value. (the actual underlying sqltype could actually be different)
	if ( columnType == Hibernate.BIG_INTEGER ) {
		return Hibernate.BIG_INTEGER;
	}
	else if ( columnType == Hibernate.BIG_DECIMAL ) {
		return Hibernate.BIG_DECIMAL;
	}
	else if ( columnType == Hibernate.LONG || columnType == Hibernate.SHORT || columnType == Hibernate.INTEGER) {
		return Hibernate.LONG;
	}
	else if ( columnType == Hibernate.FLOAT || columnType == Hibernate.DOUBLE) {
		return Hibernate.DOUBLE;
	}

	// finally use the sqltype if == on Hibernate types did not find a match.
	if ( sqlType == Types.NUMERIC ) {
		return columnType; //because numeric can be anything
	}
	else if ( sqlType == Types.FLOAT || sqlType == Types.DOUBLE || sqlType == Types.DECIMAL || sqlType == Types.REAL) {
		return Hibernate.DOUBLE;
	}
	else if ( sqlType == Types.BIGINT || sqlType == Types.INTEGER || sqlType == Types.SMALLINT || sqlType == Types.TINYINT ) {
		return Hibernate.LONG;
	}
	else {
		return columnType;
	}
}
 
Example 11
Source File: IntoClause.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 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, perform 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 12
Source File: Order.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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();
}
 
Example 13
Source File: ProcedureParameterImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public T extract(CallableStatement statement) {
	if ( mode == ParameterMode.IN ) {
		throw new ParameterMisuseException( "IN parameter not valid for output extraction" );
	}
	try {
		if ( mode == ParameterMode.REF_CURSOR ) {
			if ( procedureCall.getParameterStrategy() == ParameterStrategy.NAMED ) {
				return (T) statement.getObject( name );
			}
			else {
				return (T) statement.getObject( startIndex );
			}
		}
		else {
			final Type hibernateType = determineHibernateType();
			final int[] sqlTypes = hibernateType.sqlTypes( procedureCall.getSession().getFactory() );

			// TODO: sqlTypesToUse.length > 1 does not seem to have a working use case (HHH-10769).
			// For now, if sqlTypes.length > 1 with a named parameter, then extract
			// parameter values by position (since we only have one name).
			final boolean useNamed = sqlTypes.length == 1 &&
					procedureCall.getParameterStrategy() == ParameterStrategy.NAMED &&
					canDoNameParameterBinding( hibernateType );


			if ( ProcedureParameterExtractionAware.class.isInstance( hibernateType ) ) {
				if ( useNamed ) {
					return (T) ( (ProcedureParameterExtractionAware) hibernateType ).extract(
							statement,
							new String[] { getName() },
							procedureCall.getSession()
					);
				}
				else {
					return (T) ( (ProcedureParameterExtractionAware) hibernateType ).extract(
							statement,
							startIndex,
							procedureCall.getSession()
					);
				}
			}
			else {
				if ( useNamed ) {
					return (T) statement.getObject( name );
				}
				else {
					return (T) statement.getObject( startIndex );
				}
			}
		}
	}
	catch (SQLException e) {
		throw procedureCall.getSession().getFactory().getSQLExceptionHelper().convert(
				e,
				"Unable to extract OUT/INOUT parameter value"
		);
	}
}