Search in sources :

Example 1 with SqlGenerator

use of org.hibernate.hql.internal.ast.SqlGenerator in project hibernate-orm by hibernate.

the class AssignmentSpecification method getSqlAssignmentFragment.

public String getSqlAssignmentFragment() {
    if (sqlAssignmentString == null) {
        try {
            SqlGenerator sqlGenerator = new SqlGenerator(factory);
            sqlGenerator.comparisonExpr(eq, false);
            // false indicates to not generate parens around the assignment
            sqlAssignmentString = sqlGenerator.getSQL();
        } catch (Throwable t) {
            throw new QueryException("cannot interpret set-clause assignment");
        }
    }
    return sqlAssignmentString;
}
Also used : QueryException(org.hibernate.QueryException) SqlGenerator(org.hibernate.hql.internal.ast.SqlGenerator)

Example 2 with SqlGenerator

use of org.hibernate.hql.internal.ast.SqlGenerator 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 before []: " + 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 3 with SqlGenerator

use of org.hibernate.hql.internal.ast.SqlGenerator in project hibernate-orm by hibernate.

the class AbstractTableBasedBulkIdHandler method processWhereClause.

/**
 * Interprets the {@code WHERE} clause from the user-defined update/delete  query
 *
 * @param whereClause The user-defined {@code WHERE} clause
 *
 * @return The bulk-id-ready {@code WHERE} clause representation
 */
@SuppressWarnings("unchecked")
protected ProcessedWhereClause processWhereClause(AST whereClause) {
    if (whereClause.getNumberOfChildren() != 0) {
        // ids that will be returned and inserted into the id table...
        try {
            SqlGenerator sqlGenerator = new SqlGenerator(sessionFactory);
            sqlGenerator.whereClause(whereClause);
            // strip the " where "
            String userWhereClause = sqlGenerator.getSQL().substring(7);
            List<ParameterSpecification> idSelectParameterSpecifications = sqlGenerator.getCollectedParameters();
            return new ProcessedWhereClause(userWhereClause, idSelectParameterSpecifications);
        } catch (RecognitionException e) {
            throw new HibernateException("Unable to generate id select for DML operation", e);
        }
    } else {
        return ProcessedWhereClause.NO_WHERE_CLAUSE;
    }
}
Also used : ParameterSpecification(org.hibernate.param.ParameterSpecification) HibernateException(org.hibernate.HibernateException) SqlGenerator(org.hibernate.hql.internal.ast.SqlGenerator) RecognitionException(antlr.RecognitionException)

Aggregations

SqlGenerator (org.hibernate.hql.internal.ast.SqlGenerator)3 RecognitionException (antlr.RecognitionException)2 QueryException (org.hibernate.QueryException)2 ParameterSpecification (org.hibernate.param.ParameterSpecification)2 SemanticException (antlr.SemanticException)1 AST (antlr.collections.AST)1 HibernateException (org.hibernate.HibernateException)1 JoinSequence (org.hibernate.engine.internal.JoinSequence)1 SessionFactoryHelper (org.hibernate.hql.internal.ast.util.SessionFactoryHelper)1 QueryableCollection (org.hibernate.persister.collection.QueryableCollection)1 CollectionType (org.hibernate.type.CollectionType)1 Type (org.hibernate.type.Type)1