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);
}
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);
}
}
Aggregations