Search in sources :

Example 1 with LiteralNumberFormatException

use of org.hibernate.query.sqm.LiteralNumberFormatException in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method visitBinaryLiteral.

@Override
public Object visitBinaryLiteral(HqlParser.BinaryLiteralContext ctx) {
    final TerminalNode firstNode = (TerminalNode) ctx.getChild(0);
    if (firstNode.getSymbol().getType() == HqlParser.BINARY_LITERAL) {
        return binaryLiteral(firstNode.getText());
    } else {
        final StringBuilder text = new StringBuilder("x'");
        final int size = ctx.getChildCount();
        for (int i = 0; i < size; i++) {
            final TerminalNode hex = (TerminalNode) ctx.getChild(i);
            if (hex.getSymbol().getType() == HqlParser.HEX_LITERAL) {
                final String hexText = hex.getText();
                if (hexText.length() != 4) {
                    throw new LiteralNumberFormatException("not a byte: " + hexText);
                }
                text.append(hexText, 2, hexText.length());
            }
        }
        return binaryLiteral(text.append("'").toString());
    }
}
Also used : LiteralNumberFormatException(org.hibernate.query.sqm.LiteralNumberFormatException) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode)

Example 2 with LiteralNumberFormatException

use of org.hibernate.query.sqm.LiteralNumberFormatException in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method hexLiteral.

private SqmLiteral<? extends Number> hexLiteral(String text) {
    final String originalText = text;
    text = text.substring(2);
    try {
        final Number value;
        final BasicDomainType<? extends Number> type;
        if (text.endsWith("l") || text.endsWith("L")) {
            text = text.substring(0, text.length() - 1);
            value = Long.parseUnsignedLong(text, 16);
            type = resolveExpressibleTypeBasic(Long.class);
        } else {
            value = Integer.parseUnsignedInt(text, 16);
            type = resolveExpressibleTypeBasic(Integer.class);
        }
        // noinspection unchecked
        return new SqmLiteral<>(value, (SqmExpressible<Number>) type, creationContext.getNodeBuilder());
    } catch (NumberFormatException e) {
        throw new LiteralNumberFormatException("Unable to convert sqm literal [" + originalText + "]", e);
    }
}
Also used : BigInteger(java.math.BigInteger) LiteralNumberFormatException(org.hibernate.query.sqm.LiteralNumberFormatException) LiteralNumberFormatException(org.hibernate.query.sqm.LiteralNumberFormatException) SqmLiteral(org.hibernate.query.sqm.tree.expression.SqmLiteral)

Aggregations

LiteralNumberFormatException (org.hibernate.query.sqm.LiteralNumberFormatException)2 BigInteger (java.math.BigInteger)1 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)1 SqmLiteral (org.hibernate.query.sqm.tree.expression.SqmLiteral)1