Search in sources :

Example 6 with SemanticException

use of antlr.SemanticException in project hibernate-orm by hibernate.

the class BinaryArithmeticOperatorNode method initialize.

@Override
public void initialize() throws SemanticException {
    final Node lhs = getLeftHandOperand();
    if (lhs == null) {
        throw new SemanticException("left-hand operand of a binary operator was null");
    }
    final Node rhs = getRightHandOperand();
    if (rhs == null) {
        throw new SemanticException("right-hand operand of a binary operator was null");
    }
    final Type lhType = (lhs instanceof SqlNode) ? ((SqlNode) lhs).getDataType() : null;
    final Type rhType = (rhs instanceof SqlNode) ? ((SqlNode) rhs).getDataType() : null;
    if (ExpectedTypeAwareNode.class.isAssignableFrom(lhs.getClass()) && rhType != null) {
        Type expectedType = null;
        // we have something like : "? [op] rhs"
        if (isDateTimeType(rhType)) {
            // more specifically : "? [op] datetime"
            //      1) if the operator is MINUS, the param needs to be of
            //          some datetime type
            //      2) if the operator is PLUS, the param needs to be of
            //          some numeric type
            expectedType = getType() == HqlSqlTokenTypes.PLUS ? StandardBasicTypes.DOUBLE : rhType;
        } else {
            expectedType = rhType;
        }
        ((ExpectedTypeAwareNode) lhs).setExpectedType(expectedType);
    } else if (ParameterNode.class.isAssignableFrom(rhs.getClass()) && lhType != null) {
        Type expectedType = null;
        // we have something like : "lhs [op] ?"
        if (isDateTimeType(lhType)) {
            //          some numeric type
            if (getType() == HqlSqlTokenTypes.PLUS) {
                expectedType = StandardBasicTypes.DOUBLE;
            }
        } else {
            expectedType = lhType;
        }
        ((ExpectedTypeAwareNode) rhs).setExpectedType(expectedType);
    }
}
Also used : Type(org.hibernate.type.Type) SemanticException(antlr.SemanticException)

Example 7 with SemanticException

use of antlr.SemanticException in project hibernate-orm by hibernate.

the class MethodNode method typeDiscriminator.

private void typeDiscriminator(AST path) throws SemanticException {
    if (path == null) {
        throw new SemanticException("type() discriminator reference has no path!");
    }
    FromReferenceNode pathAsFromReferenceNode = (FromReferenceNode) path;
    FromElement fromElement = pathAsFromReferenceNode.getFromElement();
    TypeDiscriminatorMetadata typeDiscriminatorMetadata = fromElement.getTypeDiscriminatorMetadata();
    setDataType(typeDiscriminatorMetadata.getResolutionType());
    setText(typeDiscriminatorMetadata.getSqlFragment());
    setType(SqlTokenTypes.SQL_TOKEN);
}
Also used : TypeDiscriminatorMetadata(org.hibernate.hql.internal.ast.TypeDiscriminatorMetadata) SemanticException(antlr.SemanticException)

Example 8 with SemanticException

use of antlr.SemanticException in project hibernate-orm by hibernate.

the class IndexNode method resolve.

@Override
public void resolve(boolean generateJoin, boolean implicitJoin, String classAlias, AST parent, AST parentPredicate) throws SemanticException {
    if (isResolved()) {
        return;
    }
    FromReferenceNode collectionNode = (FromReferenceNode) getFirstChild();
    SessionFactoryHelper sessionFactoryHelper = getSessionFactoryHelper();
    // Fully resolve the map reference, create implicit joins.
    collectionNode.resolveIndex(this);
    Type type = collectionNode.getDataType();
    if (!type.isCollectionType()) {
        throw new SemanticException("The [] operator cannot be applied to type " + type.toString());
    }
    String collectionRole = ((CollectionType) type).getRole();
    QueryableCollection queryableCollection = sessionFactoryHelper.requireQueryableCollection(collectionRole);
    if (!queryableCollection.hasIndex()) {
        throw new QueryException("unindexed fromElement beforeQuery []: " + collectionNode.getPath());
    }
    // Generate the inner join -- The elements need to be joined to the collection they are in.
    FromElement fromElement = collectionNode.getFromElement();
    String elementTable = fromElement.getTableAlias();
    FromClause fromClause = fromElement.getFromClause();
    String path = collectionNode.getPath();
    FromElement elem = fromClause.findCollectionJoin(path);
    if (elem == null) {
        FromElementFactory factory = new FromElementFactory(fromClause, fromElement, path);
        elem = factory.createCollectionElementsJoin(queryableCollection, elementTable);
        LOG.debugf("No FROM element found for the elements of collection join path %s, created %s", path, elem);
    } else {
        LOG.debugf("FROM element found for collection join path %s", path);
    }
    // The 'from element' that represents the elements of the collection.
    setFromElement(fromElement);
    // Add the condition to the join sequence that qualifies the indexed element.
    AST selector = collectionNode.getNextSibling();
    if (selector == null) {
        throw new QueryException("No index value!");
    }
    // Sometimes use the element table alias, sometimes use the... umm... collection table alias (many to many)
    String collectionTableAlias = elementTable;
    if (elem.getCollectionTableAlias() != null) {
        collectionTableAlias = elem.getCollectionTableAlias();
    }
    // TODO: get SQL rendering out of here, create an AST for the join expressions.
    // Use the SQL generator grammar to generate the SQL text for the index expression.
    JoinSequence joinSequence = fromElement.getJoinSequence();
    String[] indexCols = queryableCollection.getIndexColumnNames();
    if (indexCols.length != 1) {
        throw new QueryException("composite-index appears in []: " + collectionNode.getPath());
    }
    SqlGenerator gen = new SqlGenerator(getSessionFactoryHelper().getFactory());
    try {
        //TODO: used to be exprNoParens! was this needed?
        gen.simpleExpr(selector);
    } catch (RecognitionException e) {
        throw new QueryException(e.getMessage(), e);
    }
    String selectorExpression = gen.getSQL();
    joinSequence.addCondition(collectionTableAlias + '.' + indexCols[0] + " = " + selectorExpression);
    List<ParameterSpecification> paramSpecs = gen.getCollectedParameters();
    if (paramSpecs != null) {
        switch(paramSpecs.size()) {
            case 0:
                // nothing to do
                break;
            case 1:
                ParameterSpecification paramSpec = paramSpecs.get(0);
                paramSpec.setExpectedType(queryableCollection.getIndexType());
                fromElement.setIndexCollectionSelectorParamSpec(paramSpec);
                break;
            default:
                fromElement.setIndexCollectionSelectorParamSpec(new AggregatedIndexCollectionSelectorParameterSpecifications(paramSpecs));
                break;
        }
    }
    // Now, set the text for this node.  It should be the element columns.
    String[] elementColumns = queryableCollection.getElementColumnNames(elementTable);
    setText(elementColumns[0]);
    setResolved();
}
Also used : AST(antlr.collections.AST) ParameterSpecification(org.hibernate.param.ParameterSpecification) SessionFactoryHelper(org.hibernate.hql.internal.ast.util.SessionFactoryHelper) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) CollectionType(org.hibernate.type.CollectionType) Type(org.hibernate.type.Type) QueryException(org.hibernate.QueryException) SqlGenerator(org.hibernate.hql.internal.ast.SqlGenerator) CollectionType(org.hibernate.type.CollectionType) JoinSequence(org.hibernate.engine.internal.JoinSequence) RecognitionException(antlr.RecognitionException) SemanticException(antlr.SemanticException)

Example 9 with SemanticException

use of antlr.SemanticException in project hibernate-orm by hibernate.

the class MethodNode method collectionProperty.

private void collectionProperty(AST path, AST name) throws SemanticException {
    if (path == null) {
        throw new SemanticException("Collection function " + name.getText() + " has no path!");
    }
    SqlNode expr = (SqlNode) path;
    Type type = expr.getDataType();
    LOG.debugf("collectionProperty() :  name=%s type=%s", name, type);
    resolveCollectionProperty(expr);
}
Also used : Type(org.hibernate.type.Type) SemanticException(antlr.SemanticException)

Example 10 with SemanticException

use of antlr.SemanticException in project hibernate-orm by hibernate.

the class HqlSqlWalker method generatePositionalParameter.

@Override
protected AST generatePositionalParameter(AST inputNode) throws SemanticException {
    if (namedParameters.size() > 0) {
        throw new SemanticException("cannot define positional parameter afterQuery any named parameters have been defined");
    }
    LOG.warnf("[DEPRECATION] Encountered positional parameter near line %s, column %s in HQL: [%s].  Positional parameter " + "are considered deprecated; use named parameters or JPA-style positional parameters instead.", inputNode.getLine(), inputNode.getColumn(), queryTranslatorImpl.getQueryString());
    ParameterNode parameter = (ParameterNode) astFactory.create(PARAM, "?");
    PositionalParameterSpecification paramSpec = new PositionalParameterSpecification(inputNode.getLine(), inputNode.getColumn(), positionalParameterCount++);
    parameter.setHqlParameterSpecification(paramSpec);
    parameters.add(paramSpec);
    return parameter;
}
Also used : ParameterNode(org.hibernate.hql.internal.ast.tree.ParameterNode) PositionalParameterSpecification(org.hibernate.param.PositionalParameterSpecification) SemanticException(antlr.SemanticException)

Aggregations

SemanticException (antlr.SemanticException)17 Type (org.hibernate.type.Type)7 AST (antlr.collections.AST)6 QueryException (org.hibernate.QueryException)4 QueryableCollection (org.hibernate.persister.collection.QueryableCollection)3 JoinType (org.hibernate.sql.JoinType)3 CollectionType (org.hibernate.type.CollectionType)3 RecognitionException (antlr.RecognitionException)2 FromReferenceNode (org.hibernate.hql.internal.ast.tree.FromReferenceNode)2 ParameterNode (org.hibernate.hql.internal.ast.tree.ParameterNode)2 ParameterSpecification (org.hibernate.param.ParameterSpecification)2 PositionalParameterSpecification (org.hibernate.param.PositionalParameterSpecification)2 EntityPersister (org.hibernate.persister.entity.EntityPersister)2 CompositeType (org.hibernate.type.CompositeType)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 PropertyNotFoundException (org.hibernate.PropertyNotFoundException)1 ClassLoaderService (org.hibernate.boot.registry.classloading.spi.ClassLoaderService)1 ClassLoadingException (org.hibernate.boot.registry.classloading.spi.ClassLoadingException)1 JoinSequence (org.hibernate.engine.internal.JoinSequence)1