Example usage for org.hibernate.hql.internal.ast.tree DotNode getPropertyPath

List of usage examples for org.hibernate.hql.internal.ast.tree DotNode getPropertyPath

Introduction

In this page you can find the example usage for org.hibernate.hql.internal.ast.tree DotNode getPropertyPath.

Prototype

public String getPropertyPath() 

Source Link

Usage

From source file:com.blazebit.persistence.integration.hibernate.base.HibernateExtendedQuerySupport.java

License:Apache License

@Override
public int getSqlSelectAttributePosition(EntityManager em, Query query, String expression) {
    if (expression.contains(".")) {
        // TODO: implement
        throw new UnsupportedOperationException("Embeddables are not yet supported!");
    }//from ww  w .  ja  va2  s  .  c  o m

    SessionImplementor session = em.unwrap(SessionImplementor.class);
    HQLQueryPlan plan = getOriginalQueryPlan(session, query);
    if (plan.getTranslators().length > 1) {
        throw new IllegalArgumentException("No support for multiple translators yet!");
    }
    QueryTranslator translator = plan.getTranslators()[0];

    try {
        QueryNode queryNode = getField(translator, "sqlAst");
        SelectClause selectClause = queryNode.getSelectClause();
        Type[] queryReturnTypes = selectClause.getQueryReturnTypes();

        boolean found = false;
        // The ordinal is 1 based
        int position = 1;
        for (int i = 0; i < queryReturnTypes.length; i++) {
            Type t = queryReturnTypes[i];
            if (t instanceof ManyToOneType) {
                ManyToOneType manyToOneType = (ManyToOneType) t;
                AbstractEntityPersister persister = (AbstractEntityPersister) session.getFactory()
                        .getEntityPersister(manyToOneType.getAssociatedEntityName());

                int propertyIndex = persister.getPropertyIndex(expression);
                found = true;
                for (int j = 0; j < propertyIndex; j++) {
                    position += persister.getPropertyColumnNames(j).length;
                }
                // Increment to the actual property position
                position++;
            } else {
                position++;
            }
        }

        if (found) {
            return position;
        }

        AST selectItem = selectClause.getFirstChild();

        while (selectItem != null && (selectItem.getType() == SqlTokenTypes.DISTINCT
                || selectItem.getType() == SqlTokenTypes.ALL)) {
            selectItem = selectItem.getNextSibling();
        }

        position = 1;
        for (AST n = selectItem; n != null; n = n.getNextSibling()) {
            if (n instanceof DotNode) {
                DotNode dot = (DotNode) n;
                if (expression.equals(dot.getPropertyPath())) {
                    // Check if the property is an embeddable
                    if (dot.getText().contains(",")) {
                        throw new IllegalStateException("Can't order by the embeddable: " + expression);
                    }
                    found = true;
                    break;
                }
            }
            position++;
        }

        if (found) {
            return position;
        }

        return -1;
    } catch (Exception e1) {
        throw new RuntimeException(e1);
    }
}