Search in sources :

Example 6 with QualifiedName

use of org.qi4j.api.common.QualifiedName 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)

Example 7 with QualifiedName

use of org.qi4j.api.common.QualifiedName in project qi4j-sdk by Qi4j.

the class AbstractSQLQuerying method traversePropertyPath.

protected Integer traversePropertyPath(PropertyFunction<?> reference, Integer lastTableIndex, Integer nextAvailableIndex, SQLVendor vendor, TableReferenceBuilder builder, JoinType joinStyle) {
    Stack<QualifiedName> qNameStack = new Stack<>();
    Stack<PropertyFunction<?>> refStack = new Stack<>();
    while (reference != null) {
        qNameStack.add(QualifiedName.fromAccessor(reference.accessor()));
        refStack.add(reference);
        if (reference.traversedProperty() == null && (reference.traversedAssociation() != null || reference.traversedManyAssociation() != null)) {
            Integer lastAssoTableIndex = this.traverseAssociationPath(new TraversedAssoOrManyAssoRef(reference), lastTableIndex, nextAvailableIndex, vendor, builder, joinStyle, true);
            if (lastAssoTableIndex > lastTableIndex) {
                lastTableIndex = lastAssoTableIndex;
                nextAvailableIndex = lastTableIndex + 1;
            }
        }
        reference = reference.traversedProperty();
    }
    PropertyFunction<?> prevRef = null;
    String schemaName = this._state.schemaName().get();
    TableReferenceFactory t = vendor.getTableReferenceFactory();
    BooleanFactory b = vendor.getBooleanFactory();
    ColumnsFactory c = vendor.getColumnsFactory();
    while (!qNameStack.isEmpty()) {
        QualifiedName qName = qNameStack.pop();
        PropertyFunction<?> ref = refStack.pop();
        if (!qName.type().equals(Identity.class.getName())) {
            QNameInfo info = this._state.qNameInfos().get().get(qName);
            if (info == null) {
                throw new InternalError("No qName info found for qName [" + qName + "].");
            }
            String prevTableAlias = TABLE_NAME_PREFIX + lastTableIndex;
            String nextTableAlias = TABLE_NAME_PREFIX + nextAvailableIndex;
            TableReferenceByName nextTable = t.table(t.tableName(schemaName, info.getTableName()), t.tableAlias(nextTableAlias));
            // @formatter:off
            if (prevRef == null) {
                builder.addQualifiedJoin(joinStyle, nextTable, t.jc(b.booleanBuilder(b.eq(c.colName(prevTableAlias, DBNames.ENTITY_TABLE_PK_COLUMN_NAME), c.colName(nextTableAlias, DBNames.ENTITY_TABLE_PK_COLUMN_NAME))).and(b.isNull(c.colName(nextTableAlias, DBNames.QNAME_TABLE_PARENT_QNAME_COLUMN_NAME))).createExpression()));
            } else {
                builder.addQualifiedJoin(joinStyle, nextTable, t.jc(b.booleanBuilder(b.eq(c.colName(prevTableAlias, DBNames.ALL_QNAMES_TABLE_PK_COLUMN_NAME), c.colName(nextTableAlias, DBNames.QNAME_TABLE_PARENT_QNAME_COLUMN_NAME))).and(b.eq(c.colName(prevTableAlias, DBNames.ENTITY_TABLE_PK_COLUMN_NAME), c.colName(nextTableAlias, DBNames.ENTITY_TABLE_PK_COLUMN_NAME))).createExpression()));
            }
            // @formatter:on
            lastTableIndex = nextAvailableIndex;
            ++nextAvailableIndex;
            prevRef = ref;
        }
    }
    return lastTableIndex;
}
Also used : TableReferenceByName(org.sql.generation.api.grammar.query.TableReferenceByName) QualifiedName(org.qi4j.api.common.QualifiedName) ColumnsFactory(org.sql.generation.api.grammar.factories.ColumnsFactory) BooleanFactory(org.sql.generation.api.grammar.factories.BooleanFactory) Stack(java.util.Stack) TableReferenceFactory(org.sql.generation.api.grammar.factories.TableReferenceFactory) PropertyFunction(org.qi4j.api.query.grammar.PropertyFunction) QNameInfo(org.qi4j.index.sql.support.common.QNameInfo)

Example 8 with QualifiedName

use of org.qi4j.api.common.QualifiedName in project qi4j-sdk by Qi4j.

the class AbstractSQLStartup method extractAssociationQNames.

private void extractAssociationQNames(EntityDescriptor entityDesc, Map<QualifiedName, QNameInfo> extractedQNames, Set<QualifiedName> newQNames, Boolean setQNameTableNameToNull) {
    for (AssociationDescriptor assoDesc : entityDesc.state().associations()) {
        if (SQLSkeletonUtil.isQueryable(assoDesc.accessor())) {
            QualifiedName qName = assoDesc.qualifiedName();
            if (!extractedQNames.containsKey(qName)) {
                //
                extractedQNames.put(//
                qName, //
                QNameInfo.fromAssociation(//
                qName, setQNameTableNameToNull ? null : (QNAME_TABLE_NAME_PREFIX + extractedQNames.size()), //
                assoDesc));
                newQNames.add(qName);
            }
        }
    }
}
Also used : QualifiedName(org.qi4j.api.common.QualifiedName) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor)

Example 9 with QualifiedName

use of org.qi4j.api.common.QualifiedName in project qi4j-sdk by Qi4j.

the class AbstractSQLStartup method constructApplicationInfo.

private ApplicationInfo constructApplicationInfo(Boolean setQNameTableNameToNull) throws SQLException {
    final ApplicationInfo appInfo = new ApplicationInfo();
    final List<CompositeDescriptorInfo> valueDescriptors = new ArrayList<>();
    final Deque<Object> currentPath = new ArrayDeque<>();
    _app.descriptor().accept(new HierarchicalVisitorAdapter<Object, Object, RuntimeException>() {

        @Override
        public boolean visitEnter(Object visited) throws RuntimeException {
            if (visited instanceof LayerDescriptor || visited instanceof ModuleDescriptor) {
                currentPath.push(visited);
            }
            if (visited instanceof EntityDescriptor || visited instanceof ValueDescriptor) {
                // TODO filter non-visible descriptors away.
                if (visited instanceof EntityDescriptor) {
                    EntityDescriptor entityDescriptor = (EntityDescriptor) visited;
                    if (entityDescriptor.queryable()) {
                        LOGGER.debug("THIS ONE WORKS: {}", entityDescriptor);
                        appInfo.entityDescriptors.put(first(entityDescriptor.types()).getName(), entityDescriptor);
                    }
                } else {
                    valueDescriptors.add(new CompositeDescriptorInfo((LayerDescriptor) Iterables.first(Iterables.skip(1, currentPath)), (ModuleDescriptor) Iterables.first(currentPath), (CompositeDescriptor) visited));
                }
                return false;
            }
            return true;
        }

        @Override
        public boolean visitLeave(Object visited) {
            if (visited instanceof LayerDescriptor || visited instanceof ModuleDescriptor) {
                currentPath.pop();
            }
            return true;
        }
    });
    for (EntityDescriptor descriptor : appInfo.entityDescriptors.values()) {
        Set<QualifiedName> newQNames = new HashSet<>();
        this.extractPropertyQNames(descriptor, this._state.qNameInfos().get(), newQNames, valueDescriptors, appInfo.enumValues, setQNameTableNameToNull);
        this.extractAssociationQNames(descriptor, this._state.qNameInfos().get(), newQNames, setQNameTableNameToNull);
        this.extractManyAssociationQNames(descriptor, this._state.qNameInfos().get(), newQNames, setQNameTableNameToNull);
        this._state.entityUsedQNames().get().put(descriptor, newQNames);
    }
    appInfo.usedValueComposites.addAll(valueDescriptors);
    return appInfo;
}
Also used : ValueDescriptor(org.qi4j.api.value.ValueDescriptor) QualifiedName(org.qi4j.api.common.QualifiedName) ArrayList(java.util.ArrayList) ArrayDeque(java.util.ArrayDeque) ModuleDescriptor(org.qi4j.api.structure.ModuleDescriptor) EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) LayerDescriptor(org.qi4j.api.structure.LayerDescriptor) HashSet(java.util.HashSet)

Example 10 with QualifiedName

use of org.qi4j.api.common.QualifiedName in project qi4j-sdk by Qi4j.

the class AbstractSQLStartup method processPropertyTypeForQNames.

private void processPropertyTypeForQNames(PropertyDescriptor pType, Map<QualifiedName, QNameInfo> qNameInfos, Set<QualifiedName> newQNames, List<CompositeDescriptorInfo> vDescriptors, Set<String> enumValues, Boolean setQNameTableNameToNull) {
    QualifiedName qName = pType.qualifiedName();
    if (!newQNames.contains(qName) && !qName.name().equals(Identity.class.getName())) {
        newQNames.add(qName);
        // System.out.println("QName: " + qName + ", hc: " + qName.hashCode());
        QNameInfo info = qNameInfos.get(qName);
        if (info == null) {
            info = QNameInfo.fromProperty(//
            qName, setQNameTableNameToNull ? null : (QNAME_TABLE_NAME_PREFIX + qNameInfos.size()), //
            pType);
            qNameInfos.put(qName, info);
        }
        Type vType = info.getFinalType();
        while (vType instanceof ParameterizedType) {
            vType = ((ParameterizedType) vType).getRawType();
        }
        if (//
        vType instanceof Class<?>) {
            final Class<?> vTypeClass = (Class<?>) vType;
            if (((Class<?>) vType).isInterface()) {
                for (CompositeDescriptorInfo descInfo : vDescriptors) {
                    CompositeDescriptor desc = descInfo.composite;
                    if (desc instanceof ValueDescriptor) {
                        ValueDescriptor vDesc = (ValueDescriptor) desc;
                        // other Serializable
                        if (Iterables.matchesAny(new Specification<Class<?>>() {

                            @Override
                            public boolean satisfiedBy(Class<?> item) {
                                return vTypeClass.isAssignableFrom(item);
                            }
                        }, vDesc.types())) {
                            for (PropertyDescriptor subPDesc : vDesc.state().properties()) {
                                //
                                this.processPropertyTypeForQNames(//
                                subPDesc, //
                                qNameInfos, //
                                newQNames, //
                                vDescriptors, //
                                enumValues, //
                                setQNameTableNameToNull);
                            }
                        }
                    }
                }
            } else if (Enum.class.isAssignableFrom((Class<?>) vType)) {
                for (Object value : ((Class<?>) vType).getEnumConstants()) {
                    enumValues.add(QualifiedName.fromClass((Class<?>) vType, value.toString()).toString());
                }
            }
        }
    }
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) QualifiedName(org.qi4j.api.common.QualifiedName) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) ParameterizedType(java.lang.reflect.ParameterizedType) QNameType(org.qi4j.index.sql.support.common.QNameInfo.QNameType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ObjectType(org.sql.generation.api.grammar.manipulation.ObjectType) SQLDataType(org.sql.generation.api.grammar.common.datatypes.SQLDataType) CompositeDescriptor(org.qi4j.api.composite.CompositeDescriptor) QNameInfo(org.qi4j.index.sql.support.common.QNameInfo) Identity(org.qi4j.api.entity.Identity)

Aggregations

QualifiedName (org.qi4j.api.common.QualifiedName)27 HashMap (java.util.HashMap)11 Map (java.util.Map)10 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)10 EntityReference (org.qi4j.api.entity.EntityReference)9 ArrayList (java.util.ArrayList)8 List (java.util.List)7 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)7 LinkedHashMap (java.util.LinkedHashMap)6 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)6 QNameInfo (org.qi4j.index.sql.support.common.QNameInfo)6 EntityStoreException (org.qi4j.spi.entitystore.EntityStoreException)6 Type (java.lang.reflect.Type)5 Member (java.lang.reflect.Member)4 JSONArray (org.json.JSONArray)4 JSONException (org.json.JSONException)4 JSONObject (org.json.JSONObject)4 AccessibleObject (java.lang.reflect.AccessibleObject)3 TypeVariable (java.lang.reflect.TypeVariable)3 HashSet (java.util.HashSet)3