use of org.hibernate.query.sqm.ParsingException in project hibernate-orm by hibernate.
the class SemanticQueryBuilder method visitSortSpecification.
@Override
public SqmSortSpecification visitSortSpecification(HqlParser.SortSpecificationContext ctx) {
final SqmExpression<?> sortExpression = visitSortExpression((HqlParser.SortExpressionContext) ctx.getChild(0));
if (sortExpression == null) {
throw new ParsingException("Could not resolve sort-expression : " + ctx.getChild(0).getText());
}
if (sortExpression instanceof SqmLiteral || sortExpression instanceof SqmParameter) {
HqlLogging.QUERY_LOGGER.debugf("Questionable sorting by constant value : %s", sortExpression);
}
final SortOrder sortOrder;
final NullPrecedence nullPrecedence;
int nextIndex = 1;
if (nextIndex < ctx.getChildCount()) {
ParseTree parseTree = ctx.getChild(nextIndex);
if (parseTree instanceof HqlParser.SortDirectionContext) {
switch(((TerminalNode) parseTree.getChild(0)).getSymbol().getType()) {
case HqlParser.ASC:
sortOrder = SortOrder.ASCENDING;
break;
case HqlParser.DESC:
sortOrder = SortOrder.DESCENDING;
break;
default:
throw new SemanticException("Unrecognized sort ordering: " + parseTree.getText());
}
nextIndex++;
} else {
sortOrder = null;
}
parseTree = ctx.getChild(nextIndex);
if (parseTree instanceof HqlParser.NullsPrecedenceContext) {
switch(((TerminalNode) parseTree.getChild(1)).getSymbol().getType()) {
case HqlParser.FIRST:
nullPrecedence = NullPrecedence.FIRST;
break;
case HqlParser.LAST:
nullPrecedence = NullPrecedence.LAST;
break;
default:
throw new SemanticException("Unrecognized null precedence: " + parseTree.getText());
}
} else {
nullPrecedence = null;
}
} else {
sortOrder = null;
nullPrecedence = null;
}
return new SqmSortSpecification(sortExpression, sortOrder, nullPrecedence);
}
use of org.hibernate.query.sqm.ParsingException in project hibernate-orm by hibernate.
the class SemanticQueryBuilder method createCollectionReferenceSubQuery.
private <X> SqmSubQuery<X> createCollectionReferenceSubQuery(HqlParser.SimplePathContext pathCtx, TerminalNode collectionReferenceCtx) {
final SqmPath<?> pluralAttributePath = consumeDomainPath(pathCtx);
final SqmPathSource<?> referencedPathSource = pluralAttributePath.getReferencedPathSource();
if (!(referencedPathSource instanceof PluralPersistentAttribute)) {
throw new PathException("Path is not a plural path '" + pluralAttributePath.getNavigablePath() + "'");
}
final SqmSubQuery<?> subQuery = new SqmSubQuery<>(processingStateStack.getCurrent().getProcessingQuery(), creationContext.getNodeBuilder());
final SqmSelectClause selectClause = new SqmSelectClause(false, 1, creationContext.getNodeBuilder());
final SqmFromClause fromClause = new SqmFromClause(1);
SqmPath<?> lhs = pluralAttributePath.getLhs();
final List<String> implicitJoinPaths = new ArrayList<>();
while (!(lhs instanceof AbstractSqmFrom<?, ?>)) {
implicitJoinPaths.add(lhs.getNavigablePath().getUnaliasedLocalName());
lhs = lhs.getLhs();
}
final AbstractSqmFrom<?, ?> correlationBase = (AbstractSqmFrom<?, ?>) lhs;
final SqmCorrelation<?, ?> correlation = correlationBase.createCorrelation();
SqmFrom<?, ?> joinBase = correlation;
for (int i = implicitJoinPaths.size() - 1; i >= 0; i--) {
joinBase = joinBase.join(implicitJoinPaths.get(i));
}
final SqmAttributeJoin<?, ?> collectionJoin = joinBase.join(pluralAttributePath.getNavigablePath().getUnaliasedLocalName());
fromClause.addRoot(correlation.getCorrelatedRoot());
if (collectionReferenceCtx == null) {
final SqmLiteral<Integer> literal = new SqmLiteral<>(1, creationContext.getNodeBuilder().getIntegerType(), creationContext.getNodeBuilder());
subQuery.applyInferableType(literal.getNodeType());
selectClause.setSelection(literal);
} else {
final String partName;
switch(collectionReferenceCtx.getSymbol().getType()) {
case HqlParser.ELEMENTS:
partName = CollectionPart.Nature.ELEMENT.getName();
break;
case HqlParser.INDICES:
partName = CollectionPart.Nature.INDEX.getName();
break;
default:
throw new ParsingException("Unexpected collection reference : " + collectionReferenceCtx.getText());
}
final SqmPath<?> path = collectionJoin.resolvePathPart(partName, true, this);
subQuery.applyInferableType(path.getNodeType());
selectClause.setSelection(path);
}
final SqmQuerySpec<?> querySpec = subQuery.getQuerySpec();
querySpec.setFromClause(fromClause);
querySpec.setSelectClause(selectClause);
// noinspection unchecked
return (SqmSubQuery<X>) subQuery;
}
use of org.hibernate.query.sqm.ParsingException in project hibernate-orm by hibernate.
the class SemanticQueryBuilder method visitUnaryNumericLiteralExpression.
@Override
public SqmExpression<?> visitUnaryNumericLiteralExpression(HqlParser.UnaryNumericLiteralExpressionContext ctx) {
final TerminalNode node = (TerminalNode) ctx.getChild(1).getChild(0);
final String text;
if (((TerminalNode) ctx.getChild(0).getChild(0)).getSymbol().getType() == HqlParser.MINUS) {
text = "-" + node.getText();
} else {
text = node.getText();
}
switch(node.getSymbol().getType()) {
case HqlParser.INTEGER_LITERAL:
return integerOrLongLiteral(text);
case HqlParser.LONG_LITERAL:
return longLiteral(text);
case HqlParser.BIG_INTEGER_LITERAL:
return bigIntegerLiteral(text);
case HqlParser.HEX_LITERAL:
return hexLiteral(text);
case HqlParser.FLOAT_LITERAL:
return floatLiteral(text);
case HqlParser.DOUBLE_LITERAL:
return doubleLiteral(text);
case HqlParser.BIG_DECIMAL_LITERAL:
return bigDecimalLiteral(text);
default:
throw new ParsingException("Unexpected terminal node [" + text + "]");
}
}
use of org.hibernate.query.sqm.ParsingException in project hibernate-orm by hibernate.
the class SemanticQueryBuilder method visitMultiplicationExpression.
@Override
public Object visitMultiplicationExpression(HqlParser.MultiplicationExpressionContext ctx) {
if (ctx.getChildCount() != 3) {
throw new ParsingException("Expecting 2 operands to the multiplicative operator");
}
final SqmExpression<?> left = (SqmExpression<?>) ctx.getChild(0).accept(this);
final SqmExpression<?> right = (SqmExpression<?>) ctx.getChild(2).accept(this);
final BinaryArithmeticOperator operator = (BinaryArithmeticOperator) ctx.getChild(1).accept(this);
if (operator == BinaryArithmeticOperator.MODULO) {
return getFunctionDescriptor("mod").generateSqmExpression(asList(left, right), null, creationContext.getQueryEngine(), creationContext.getJpaMetamodel().getTypeConfiguration());
} else {
return new SqmBinaryArithmetic<>(operator, left, right, creationContext.getJpaMetamodel(), creationContext.getNodeBuilder());
}
}
use of org.hibernate.query.sqm.ParsingException in project hibernate-orm by hibernate.
the class StandardHqlTranslator method parseHql.
private HqlParser.StatementContext parseHql(String hql) {
// Build the lexer
final HqlLexer hqlLexer = HqlParseTreeBuilder.INSTANCE.buildHqlLexer(hql);
// Build the parse tree
final HqlParser hqlParser = HqlParseTreeBuilder.INSTANCE.buildHqlParser(hql, hqlLexer);
// try to use SLL(k)-based parsing first - its faster
hqlLexer.addErrorListener(ERR_LISTENER);
hqlParser.getInterpreter().setPredictionMode(PredictionMode.SLL);
hqlParser.removeErrorListeners();
hqlParser.addErrorListener(ERR_LISTENER);
hqlParser.setErrorHandler(new BailErrorStrategy());
try {
return hqlParser.statement();
} catch (ParseCancellationException e) {
// reset the input token stream and parser state
hqlLexer.reset();
hqlParser.reset();
// fall back to LL(k)-based parsing
hqlParser.getInterpreter().setPredictionMode(PredictionMode.LL);
hqlParser.setErrorHandler(new DefaultErrorStrategy());
return hqlParser.statement();
} catch (ParsingException ex) {
throw new SemanticException("A query exception occurred", hql, ex);
}
}
Aggregations