use of org.apache.cayenne.map.ObjRelationship in project cayenne by apache.
the class ManyToManyCandidateEntity method addFlattenedRelationship.
private void addFlattenedRelationship(ObjectNameGenerator nameGenerator, ObjEntity srcEntity, ObjEntity dstEntity, DbRelationship rel1, DbRelationship rel2) {
if (rel1.getSourceAttributes().isEmpty() && rel2.getTargetAttributes().isEmpty()) {
LOG.warn("Wrong call ManyToManyCandidateEntity.addFlattenedRelationship(... , " + srcEntity.getName() + ", " + dstEntity.getName() + ", ...)");
return;
}
ObjRelationship newRelationship = new ObjRelationship();
newRelationship.setName(NameBuilder.builder(newRelationship, srcEntity).baseName(nameGenerator.relationshipName(rel1, rel2)).name());
newRelationship.setSourceEntity(srcEntity);
newRelationship.setTargetEntityName(dstEntity);
newRelationship.addDbRelationship(rel1);
newRelationship.addDbRelationship(rel2);
srcEntity.addRelationship(newRelationship);
}
use of org.apache.cayenne.map.ObjRelationship in project cayenne by apache.
the class DiffProcessor method arcCreated.
@Override
public void arcCreated(Object nodeId, Object targetNodeId, Object arcId) {
ObjectId id = (ObjectId) nodeId;
String relationshipName = arcId.toString();
ObjEntity entity = entityResolver.getObjEntity(id.getEntityName());
ObjRelationship relationship = entity.getRelationship(relationshipName);
MutableObjectChange c = changeSet.getOrCreate(id, ObjectChangeType.UPDATE);
ObjectId tid = (ObjectId) targetNodeId;
if (relationship.isToMany()) {
c.toManyRelationshipConnected(relationshipName, tid);
} else {
c.toOneRelationshipConnected(relationshipName, tid);
}
}
use of org.apache.cayenne.map.ObjRelationship in project cayenne by apache.
the class EJBQLPathAnaliserTranslator method visitMemberOf.
@Override
public boolean visitMemberOf(EJBQLExpression expression) {
if (expression.getChildrenCount() != 2) {
throw new EJBQLException("MEMBER OF must have exactly two children, got: " + expression.getChildrenCount());
}
if (!(expression.getChild(1) instanceof EJBQLPath)) {
throw new EJBQLException("Second child of the MEMBER OF must be a collection path, got: " + expression.getChild(1));
}
QuotingStrategy quoter = context.getQuotingStrategy();
EJBQLPath path = (EJBQLPath) expression.getChild(1);
// make sure the ID for the path does not overlap with other condition
// joins...
String id = path.getAbsolutePath();
String correlatedEntityId = path.getId();
ClassDescriptor correlatedEntityDescriptor = context.getEntityDescriptor(correlatedEntityId);
String correlatedTableName = quoter.quotedFullyQualifiedName(correlatedEntityDescriptor.getEntity().getDbEntity());
String correlatedTableAlias = context.getTableAlias(correlatedEntityId, correlatedTableName);
String subqueryId = context.createIdAlias(id);
ClassDescriptor targetDescriptor = context.getEntityDescriptor(subqueryId);
if (expression.isNegated()) {
context.append(" NOT");
}
context.append(" EXISTS (SELECT 1 FROM ");
String subqueryTableName = quoter.quotedFullyQualifiedName(targetDescriptor.getEntity().getDbEntity());
String subqueryRootAlias = context.getTableAlias(subqueryId, subqueryTableName);
ObjRelationship relationship = correlatedEntityDescriptor.getEntity().getRelationship(path.getRelativePath());
if (relationship.getDbRelationshipPath().contains(".")) {
// if the DbRelationshipPath contains '.', the relationship is
// flattened
subqueryRootAlias = processFlattenedRelationShip(subqueryRootAlias, relationship);
} else {
// not using "AS" to separate table name and alias name - OpenBase
// doesn't
// support "AS", and the rest of the databases do not care
context.append(subqueryTableName).append(' ').append(subqueryRootAlias);
}
context.append(" WHERE");
DbRelationship correlatedJoinRelationship = context.getIncomingRelationships(new EJBQLTableId(id)).get(0);
for (DbJoin join : correlatedJoinRelationship.getJoins()) {
context.append(' ').append(subqueryRootAlias).append('.').append(join.getTargetName()).append(" = ");
context.append(correlatedTableAlias).append('.').append(quoter.quotedSourceName(join));
context.append(" AND");
}
// translate subquery_root_id = LHS_of_memberof
EJBQLEquals equals = new EJBQLEquals(-1);
EJBQLIdentificationVariable identifier = new EJBQLIdentificationVariable(-1);
identifier.setText(subqueryId);
equals.jjtAddChild(identifier, 0);
equals.jjtAddChild((Node) expression.getChild(0), 1);
equals.visit(this);
context.append(")");
return false;
}
use of org.apache.cayenne.map.ObjRelationship in project cayenne by apache.
the class EJBQLPathTranslator method processIntermediatePathComponent.
protected void processIntermediatePathComponent() {
ObjRelationship relationship = currentEntity.getRelationship(lastPathComponent);
if (relationship == null) {
throw new EJBQLException("Unknown relationship '" + lastPathComponent + "' for entity '" + currentEntity.getName() + "'");
}
this.currentEntity = (ObjEntity) relationship.getTargetEntity();
}
use of org.apache.cayenne.map.ObjRelationship in project cayenne by apache.
the class EJBQLSelectColumnsTranslator method visitPath.
@Override
public boolean visitPath(EJBQLExpression expression, int finishedChildIndex) {
EJBQLPathTranslator pathTranslator = new EJBQLPathTranslator(context) {
@Override
protected void appendMultiColumnPath(EJBQLMultiColumnOperand operand) {
throw new EJBQLException("Can't use multi-column paths in column clause");
}
@Override
protected void processTerminatingRelationship(ObjRelationship relationship) {
Map<String, String> xfields = null;
if (context.isAppendingResultColumns()) {
xfields = context.nextEntityResult().getFields();
}
final Map<String, String> fields = xfields;
Collection<DbAttribute> dbAttr = ((ObjEntity) relationship.getTargetEntity()).getDbEntity().getAttributes();
DbRelationship dbRelationship = relationship.getDbRelationships().get(0);
DbEntity table = (DbEntity) dbRelationship.getTargetEntity();
Iterator<DbAttribute> it = dbAttr.iterator();
if (dbAttr.size() > 0) {
resolveJoin();
}
String alias = this.lastAlias != null ? lastAlias : context.getTableAlias(idPath, context.getQuotingStrategy().quotedFullyQualifiedName(table));
boolean first = true;
while (it.hasNext()) {
context.append(!first ? ", " : " ");
DbAttribute dbAttribute = it.next();
appendColumn(TypesMapping.getJavaBySqlType(dbAttribute.getType()), alias, dbAttribute, fields != null ? fields.get(dbAttribute.getName()) : "");
first = false;
}
}
@Override
protected void processTerminatingAttribute(ObjAttribute attribute) {
DbEntity table = currentEntity.getDbEntity();
String alias = this.lastAlias != null ? lastAlias : context.getTableAlias(idPath, context.getQuotingStrategy().quotedFullyQualifiedName(table));
if (attribute.isFlattened()) {
Iterator<?> dbPathIterator = attribute.getDbPathIterator();
EJBQLTableId lhsId = new EJBQLTableId(idPath);
while (dbPathIterator.hasNext()) {
Object pathPart = dbPathIterator.next();
// later when appending table
if (pathPart == null) {
throw new CayenneRuntimeException("ObjAttribute has no component: %s", attribute.getName());
} else if (pathPart instanceof DbAttribute) {
DbAttribute dbAttribute = (DbAttribute) pathPart;
appendColumn(attribute.getType(), context.getTableAlias(lhsId.getEntityId(), context.getQuotingStrategy().quotedFullyQualifiedName((DbEntity) dbAttribute.getEntity())), dbAttribute, context.isAppendingResultColumns() ? context.nextColumnAlias() : "");
}
}
} else {
DbAttribute dbAttribute = attribute.getDbAttribute();
appendColumn(attribute.getType(), alias, dbAttribute, context.isAppendingResultColumns() ? context.nextColumnAlias() : "");
}
}
};
expression.visit(pathTranslator);
return false;
}
Aggregations