Search in sources :

Example 1 with Ordering

use of org.sql.generation.api.grammar.query.Ordering in project qi4j-sdk by Qi4j.

the class AbstractSQLQuerying method processOrderBySegments.

protected void processOrderBySegments(OrderBy[] orderBy, SQLVendor vendor, QuerySpecificationBuilder builder) {
    if (orderBy != null) {
        QNameInfo[] qNames = new QNameInfo[orderBy.length];
        QueryFactory q = vendor.getQueryFactory();
        ColumnsFactory c = vendor.getColumnsFactory();
        Integer tableIndex = 0;
        for (Integer idx = 0; idx < orderBy.length; ++idx) {
            if (orderBy[idx] != null) {
                PropertyFunction<?> ref = orderBy[idx].property();
                QualifiedName qName = QualifiedName.fromAccessor(ref.accessor());
                QNameInfo info = this._state.qNameInfos().get().get(qName);
                qNames[idx] = info;
                if (info == null) {
                    throw new InternalError("No qName info found for qName [" + qName + "].");
                }
                tableIndex = this.traversePropertyPath(ref, 0, tableIndex + 1, vendor, builder.getFrom().getTableReferences().iterator().next(), JoinType.LEFT_OUTER);
                Class<?> declaringType = ((Member) ref.accessor()).getDeclaringClass();
                String colName;
                Integer tableIdx;
                if (Identity.class.equals(declaringType)) {
                    colName = DBNames.ENTITY_TABLE_IDENTITY_COLUMN_NAME;
                    tableIdx = tableIndex - 1;
                } else {
                    colName = DBNames.QNAME_TABLE_VALUE_COLUMN_NAME;
                    tableIdx = tableIndex;
                }
                Ordering ordering = Ordering.ASCENDING;
                if (orderBy[idx].order() == Order.DESCENDING) {
                    ordering = Ordering.DESCENDING;
                }
                builder.getOrderBy().addSortSpecs(q.sortSpec(c.colName(TABLE_NAME_PREFIX + tableIdx, colName), ordering));
            }
        }
    }
}
Also used : QueryFactory(org.sql.generation.api.grammar.factories.QueryFactory) ColumnsFactory(org.sql.generation.api.grammar.factories.ColumnsFactory) QualifiedName(org.qi4j.api.common.QualifiedName) Ordering(org.sql.generation.api.grammar.query.Ordering) QNameInfo(org.qi4j.index.sql.support.common.QNameInfo) Member(java.lang.reflect.Member)

Aggregations

Member (java.lang.reflect.Member)1 QualifiedName (org.qi4j.api.common.QualifiedName)1 QNameInfo (org.qi4j.index.sql.support.common.QNameInfo)1 ColumnsFactory (org.sql.generation.api.grammar.factories.ColumnsFactory)1 QueryFactory (org.sql.generation.api.grammar.factories.QueryFactory)1 Ordering (org.sql.generation.api.grammar.query.Ordering)1