Example usage for org.hibernate.hql.internal.ast.tree QueryNode getSelectClause

List of usage examples for org.hibernate.hql.internal.ast.tree QueryNode getSelectClause

Introduction

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

Prototype

public final SelectClause getSelectClause() 

Source Link

Document

Locate the select clause that is part of this select statement.

Usage

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

License:Apache License

@Override
public int getSqlSelectAliasPosition(EntityManager em, Query query, String alias) {
    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!");
    }/*w w w. ja v a  2  s .c om*/
    QueryTranslator translator = plan.getTranslators()[0];

    try {
        QueryNode queryNode = getField(translator, "sqlAst");

        String[] aliases = queryNode.getSelectClause().getQueryReturnAliases();

        for (int i = 0; i < aliases.length; i++) {
            if (alias.equals(aliases[i])) {
                // the ordinal is 1 based
                return i + 1;
            }
        }

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

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  w ww  .  ja  v a 2  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);
    }
}