Fill in implementation of HQL hexadecimal literals

And add tests for hex and BigDecimal/BigInteger literals
This commit is contained in:
gavinking 2020-02-02 09:43:21 +01:00
parent 0b6e071f4a
commit 398dcfffeb
2 changed files with 58 additions and 7 deletions

View File

@ -1756,13 +1756,7 @@ public class SemanticQueryBuilder extends HqlParserBaseVisitor implements SqmCre
return bigIntegerLiteral( ctx.literal().BIG_INTEGER_LITERAL().getText() );
}
else if ( ctx.literal().HEX_LITERAL() != null ) {
final String text = ctx.literal().HEX_LITERAL().getText();
if ( text.endsWith( "l" ) || text.endsWith( "L" ) ) {
return longLiteral( text );
}
else {
return integerLiteral( text );
}
return hexLiteral( ctx.literal().HEX_LITERAL().getText() );
}
else if ( ctx.literal().FLOAT_LITERAL() != null ) {
return floatLiteral( ctx.literal().FLOAT_LITERAL().getText() );
@ -2078,6 +2072,35 @@ public class SemanticQueryBuilder extends HqlParserBaseVisitor implements SqmCre
}
}
private SqmLiteral<Number> hexLiteral(String text) {
final String originalText = text;
text = text.substring( 2 );
try {
final Number value;
final BasicDomainType type;
if ( text.endsWith( "l" ) || text.endsWith( "L" ) ) {
text = text.substring( 0, text.length() - 1 );
value = Long.parseUnsignedLong( text, 16 );
type = resolveExpressableTypeBasic( Long.class );
}
else {
value = Integer.parseUnsignedInt( text, 16 );
type = resolveExpressableTypeBasic( Integer.class );
}
return new SqmLiteral<Number>(
value,
type,
creationContext.getNodeBuilder()
);
}
catch (NumberFormatException e) {
throw new LiteralNumberFormatException(
"Unable to convert sqm literal [" + originalText + "]",
e
);
}
}
private SqmLiteral<BigInteger> bigIntegerLiteral(String text) {
final String originalText = text;
try {

View File

@ -13,6 +13,12 @@ import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
/**
* @author Steve Ebersole
*/
@ -189,4 +195,26 @@ public class LiteralTests {
}
);
}
@Test
public void testHexadecimalLiteral(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
assertThat( session.createQuery( "select 0x1A2B" )
.getSingleResult(), is(6699) );
}
);
}
@Test
public void testBigLiterals(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
assertThat( session.createQuery( "select 10000000000000000bi" )
.getSingleResult(), is( BigInteger.valueOf(10000000000000000L) ) );
assertThat( session.createQuery( "select 9999999999999.9999bd" )
.getSingleResult(), is( BigDecimal.valueOf(99999999999999999L, 4) ) );;
}
);
}
}