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));
}
}
}
}
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;
}
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);
}
}
}
}
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;
}
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());
}
}
}
}
}
Aggregations