Search in sources :

Example 1 with LiteralType

use of org.hibernate.type.LiteralType in project hibernate-orm by hibernate.

the class LiteralProcessor method setConstantValue.

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);
}
Also used : LiteralType(org.hibernate.type.LiteralType) SemanticException(antlr.SemanticException) InvalidPathException(org.hibernate.hql.internal.ast.InvalidPathException) MappingException(org.hibernate.MappingException) HibernateException(org.hibernate.HibernateException) QueryException(org.hibernate.QueryException) MappingException(org.hibernate.MappingException) BigInteger(java.math.BigInteger) LiteralType(org.hibernate.type.LiteralType) Type(org.hibernate.type.Type) QueryException(org.hibernate.QueryException) Dialect(org.hibernate.dialect.Dialect)

Example 2 with LiteralType

use of org.hibernate.type.LiteralType in project hibernate-orm by hibernate.

the class JavaConstantNode method getRenderText.

@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().convertToDatabaseColumn(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);
    }
}
Also used : LiteralType(org.hibernate.type.LiteralType) Type(org.hibernate.type.Type) QueryException(org.hibernate.QueryException) Dialect(org.hibernate.dialect.Dialect) LiteralType(org.hibernate.type.LiteralType) AttributeConverterTypeAdapter(org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter) QueryException(org.hibernate.QueryException)

Aggregations

QueryException (org.hibernate.QueryException)2 Dialect (org.hibernate.dialect.Dialect)2 LiteralType (org.hibernate.type.LiteralType)2 Type (org.hibernate.type.Type)2 SemanticException (antlr.SemanticException)1 BigInteger (java.math.BigInteger)1 HibernateException (org.hibernate.HibernateException)1 MappingException (org.hibernate.MappingException)1 InvalidPathException (org.hibernate.hql.internal.ast.InvalidPathException)1 AttributeConverterTypeAdapter (org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter)1