org.hibernate.type.LiteralType Java Examples

The following examples show how to use org.hibernate.type.LiteralType. 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: BooleanLiteralNode.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
@SuppressWarnings( {"unchecked"})
public String getRenderText(SessionFactoryImplementor sessionFactory) {
	final boolean literalValue = getValue();

	if ( expectedType instanceof AttributeConverterTypeAdapter ) {
		return determineConvertedValue( (AttributeConverterTypeAdapter) expectedType, literalValue );
	}
	else if ( expectedType instanceof LiteralType ) {
		try {
			return ( (LiteralType) expectedType ).objectToSQLString( getValue(), sessionFactory.getDialect() );
		}
		catch( Exception t ) {
			throw new QueryException( "Unable to render boolean literal value using expected LiteralType", t );
		}
	}

	return sessionFactory.getDialect().toBooleanValueString( literalValue );
}
 
Example #2
Source File: IdsClauseBuilder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private String quoteIdentifier(Object value, Type type) {
	Type resolvedType = ( !type.getReturnedClass().equals( value.getClass() ) ) ?
		typeResolver.heuristicType( value.getClass().getName() ) : type;

	if ( resolvedType instanceof LiteralType ) {
		LiteralType literalType = (LiteralType) resolvedType;
		try {
			return literalType.objectToSQLString( value, dialect );
		}
		catch ( Exception e ) {
			throw new IllegalArgumentException( e );
		}
	}
	return String.valueOf( value );
}
 
Example #3
Source File: JavaConstantNode.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private String resolveToLiteralString(Type type) {
	try {
		LiteralType literalType = ( LiteralType ) type;
		Dialect dialect = factory.getDialect();
		return literalType.objectToSQLString( constantValue, dialect );
	}
	catch ( Throwable t ) {
		throw new QueryException( QueryTranslator.ERROR_CANNOT_FORMAT_LITERAL + constantExpression, t );
	}
}
 
Example #4
Source File: Insert.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public Insert addColumn(String columnName, Object value, LiteralType type) throws Exception {
	return addColumn( columnName, type.objectToSQLString(value, dialect) );
}
 
Example #5
Source File: Update.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public Update addColumn(String columnName, Object value, LiteralType type) throws Exception {
	return addColumn( columnName, type.objectToSQLString(value, dialect) );
}
 
Example #6
Source File: JavaConstantNode.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public String getRenderText(SessionFactoryImplementor sessionFactory) {
	final Type type = expectedType == null
			? heuristicType
			: Number.class.isAssignableFrom( heuristicType.getReturnedClass() )
			? heuristicType
			: expectedType;
	try {
		if ( LiteralType.class.isInstance( type ) ) {
			final LiteralType literalType = (LiteralType) type;
			final Dialect dialect = factory.getDialect();
			return literalType.objectToSQLString( constantValue, dialect );
		}
		else if ( AttributeConverterTypeAdapter.class.isInstance( type ) ) {
			final AttributeConverterTypeAdapter converterType = (AttributeConverterTypeAdapter) type;
			if ( !converterType.getModelType().isInstance( constantValue ) ) {
				throw new QueryException(
						String.format(
								Locale.ENGLISH,
								"Recognized query constant expression [%s] was not resolved to type [%s] expected by defined AttributeConverter [%s]",
								constantExpression,
								constantValue.getClass().getName(),
								converterType.getModelType().getName()
						)
				);
			}
			final Object value = converterType.getAttributeConverter().toRelationalValue( constantValue );
			if ( String.class.equals( converterType.getJdbcType() ) ) {
				return "'" + value + "'";
			}
			else {
				return value.toString();
			}
		}
		else {
			throw new QueryException(
					String.format(
							Locale.ENGLISH,
							"Unrecognized Hibernate Type for handling query constant (%s); expecting LiteralType implementation or AttributeConverter",
							constantExpression
					)
			);
		}
	}
	catch (QueryException e) {
		throw e;
	}
	catch (Exception t) {
		throw new QueryException( QueryTranslator.ERROR_CANNOT_FORMAT_LITERAL + constantExpression, t );
	}
}
 
Example #7
Source File: LiteralProcessor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void setConstantValue(DotNode node, String text, Object value) {
	if ( LOG.isDebugEnabled() ) {
		LOG.debugf( "setConstantValue() %s -> %s %s", text, value, value.getClass().getName() );
	}
	// Chop off the rest of the tree.
	node.setFirstChild( null );
	if ( value instanceof String ) {
		node.setType( SqlTokenTypes.QUOTED_STRING );
	}
	else if ( value instanceof Character ) {
		node.setType( SqlTokenTypes.QUOTED_STRING );
	}
	else if ( value instanceof Byte ) {
		node.setType( SqlTokenTypes.NUM_INT );
	}
	else if ( value instanceof Short ) {
		node.setType( SqlTokenTypes.NUM_INT );
	}
	else if ( value instanceof Integer ) {
		node.setType( SqlTokenTypes.NUM_INT );
	}
	else if ( value instanceof Long ) {
		node.setType( SqlTokenTypes.NUM_LONG );
	}
	else if ( value instanceof Double ) {
		node.setType( SqlTokenTypes.NUM_DOUBLE );
	}
	else if ( value instanceof Float ) {
		node.setType( SqlTokenTypes.NUM_FLOAT );
	}
	else {
		node.setType( SqlTokenTypes.CONSTANT );
	}
	Type type;
	try {
		type = walker.getSessionFactoryHelper().getFactory().getTypeResolver().heuristicType(
				value.getClass().getName()
		);
	}
	catch (MappingException me) {
		throw new QueryException( me );
	}
	if ( type == null ) {
		throw new QueryException( QueryTranslator.ERROR_CANNOT_DETERMINE_TYPE + node.getText() );
	}
	try {
		LiteralType literalType = (LiteralType) type;
		Dialect dialect = walker.getSessionFactoryHelper().getFactory().getDialect();
		//noinspection unchecked
		node.setText( literalType.objectToSQLString( value, dialect ) );
	}
	catch (Exception e) {
		throw new QueryException( QueryTranslator.ERROR_CANNOT_FORMAT_LITERAL + node.getText(), e );
	}
	node.setDataType( type );
	node.setResolvedConstant( text );
}
 
Example #8
Source File: Insert.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Insert addColumn(String columnName, Object value, LiteralType type) throws Exception {
	return addColumn( columnName, type.objectToSQLString(value, dialect) );
}
 
Example #9
Source File: Update.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Update addColumn(String columnName, Object value, LiteralType type) throws Exception {
	return addColumn( columnName, type.objectToSQLString(value, dialect) );
}
 
Example #10
Source File: LiteralProcessor.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void setConstantValue(DotNode node, String text, Object value) {
	if ( log.isDebugEnabled() ) {
		log.debug( "setConstantValue() " + text + " -> " + value + " " + value.getClass().getName() );
	}
	node.setFirstChild( null );	// Chop off the rest of the tree.
	if ( value instanceof String ) {
		node.setType( SqlTokenTypes.QUOTED_STRING );
	}
	else if ( value instanceof Character ) {
		node.setType( SqlTokenTypes.QUOTED_STRING );
	}
	else if ( value instanceof Byte ) {
		node.setType( SqlTokenTypes.NUM_INT );
	}
	else if ( value instanceof Short ) {
		node.setType( SqlTokenTypes.NUM_INT );
	}
	else if ( value instanceof Integer ) {
		node.setType( SqlTokenTypes.NUM_INT );
	}
	else if ( value instanceof Long ) {
		node.setType( SqlTokenTypes.NUM_LONG );
	}
	else if ( value instanceof Double ) {
		node.setType( SqlTokenTypes.NUM_DOUBLE );
	}
	else if ( value instanceof Float ) {
		node.setType( SqlTokenTypes.NUM_FLOAT );
	}
	else {
		node.setType( SqlTokenTypes.CONSTANT );
	}
	Type type;
	try {
		type = TypeFactory.heuristicType( value.getClass().getName() );
	}
	catch ( MappingException me ) {
		throw new QueryException( me );
	}
	if ( type == null ) {
		throw new QueryException( QueryTranslator.ERROR_CANNOT_DETERMINE_TYPE + node.getText() );
	}
	try {
		LiteralType literalType = ( LiteralType ) type;
		Dialect dialect = walker.getSessionFactoryHelper().getFactory().getDialect();
		node.setText( literalType.objectToSQLString( value, dialect ) );
	}
	catch ( Exception e ) {
		throw new QueryException( QueryTranslator.ERROR_CANNOT_FORMAT_LITERAL + node.getText(), e );
	}
	node.setDataType( type );
	node.setResolvedConstant( text );
}
 
Example #11
Source File: WhereParser.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void doToken(String token, QueryTranslatorImpl q) throws QueryException {
	if ( q.isName( StringHelper.root( token ) ) ) { //path expression
		doPathExpression( q.unalias( token ), q );
	}
	else if ( token.startsWith( ParserHelper.HQL_VARIABLE_PREFIX ) ) { //named query parameter
		q.addNamedParameter( token.substring( 1 ) );
		appendToken( q, "?" );
	}
	else {
		Queryable persister = q.getEntityPersisterUsingImports( token );
		if ( persister != null ) { // the name of a class
			final String discrim = persister.getDiscriminatorSQLValue();
			if ( InFragment.NULL.equals(discrim) || InFragment.NOT_NULL.equals(discrim) ) {
				throw new QueryException( "subclass test not allowed for null or not null discriminator" );
			}
			else {
				appendToken( q, discrim );
			}
		}
		else {
			Object constant;
			if (
					token.indexOf( '.' ) > -1 &&
					( constant = ReflectHelper.getConstantValue( token ) ) != null
			) {
				Type type;
				try {
					type = TypeFactory.heuristicType( constant.getClass().getName() );
				}
				catch ( MappingException me ) {
					throw new QueryException( me );
				}
				if ( type == null ) throw new QueryException( QueryTranslator.ERROR_CANNOT_DETERMINE_TYPE + token );
				try {
					appendToken( q, ( ( LiteralType ) type ).objectToSQLString( constant, q.getFactory().getDialect() ) );
				}
				catch ( Exception e ) {
					throw new QueryException( QueryTranslator.ERROR_CANNOT_FORMAT_LITERAL + token, e );
				}
			}
			else { //anything else

				String negatedToken = negated ? ( String ) NEGATIONS.get( token.toLowerCase() ) : null;
				if ( negatedToken != null && ( !betweenSpecialCase || !"or".equals( negatedToken ) ) ) {
					appendToken( q, negatedToken );
				}
				else {
					appendToken( q, token );
				}
			}
		}
	}
}