use of org.apache.cayenne.map.DbRelationship in project cayenne by apache.
the class EJBQLDbPathTranslator method processLastPathComponent.
private void processLastPathComponent() {
DbAttribute attribute = currentEntity.getAttribute(lastPathComponent);
if (attribute != null) {
processTerminatingAttribute(attribute);
return;
}
DbRelationship relationship = currentEntity.getRelationship(lastPathComponent);
if (relationship != null) {
processTerminatingRelationship(relationship);
return;
}
throw new IllegalStateException("Invalid path component: " + lastPathComponent);
}
use of org.apache.cayenne.map.DbRelationship in project cayenne by apache.
the class EJBQLJoinAppender method appendJoin.
protected void appendJoin(String marker, EJBQLTableId lhsId, EJBQLTableId rhsId, String semantics) {
List<DbRelationship> joinRelationships = context.getIncomingRelationships(rhsId);
if (joinRelationships.isEmpty()) {
throw new EJBQLException("No join configured for id " + rhsId);
}
QuotingStrategy quoter = context.getQuotingStrategy();
DbRelationship incomingDB = joinRelationships.get(0);
// TODO: andrus, 1/6/2008 - move reusable join check here...
DbEntity sourceEntity = incomingDB.getSourceEntity();
String tableName = quoter.quotedFullyQualifiedName(sourceEntity);
String sourceAlias = context.getTableAlias(lhsId.getEntityId(), tableName);
if (marker != null) {
context.pushMarker(marker, false);
}
try {
context.append(" ").append(semantics);
if (joinRelationships.size() > 1) {
// if size of relationship list greater than 1,
// it's a flattened relationship
context.append(" ");
for (int i = 1; i < joinRelationships.size(); i++) {
DbRelationship dbRelationship = joinRelationships.get(i);
String subquerySourceTableName = quoter.quotedFullyQualifiedName(dbRelationship.getSourceEntity());
String subquerySourceAlias = context.getTableAlias(subquerySourceTableName, subquerySourceTableName);
String subqueryTargetTableName = quoter.quotedFullyQualifiedName(dbRelationship.getTargetEntity());
String subqueryTargetAlias;
if (i == joinRelationships.size() - 1) {
// it's the last table alias
subqueryTargetAlias = context.getTableAlias(rhsId.getEntityId(), subqueryTargetTableName);
} else {
subqueryTargetAlias = context.getTableAlias(subqueryTargetTableName, subqueryTargetTableName);
}
if (i == 1) {
// first apply the joins defined in query
context.append(subquerySourceTableName).append(' ').append(subquerySourceAlias);
generateJoiningExpression(incomingDB, sourceAlias, subquerySourceAlias);
}
context.append(" JOIN ");
context.append(subqueryTargetTableName).append(' ').append(subqueryTargetAlias);
generateJoiningExpression(dbRelationship, subquerySourceAlias, subqueryTargetAlias);
}
} else {
// non-flattened relationship
String targetAlias = appendTable(rhsId);
// apply the joins defined in query
generateJoiningExpression(incomingDB, sourceAlias, targetAlias);
}
} finally {
if (marker != null) {
context.popMarker();
}
}
}
use of org.apache.cayenne.map.DbRelationship in project cayenne by apache.
the class EJBQLPathTranslator method chooseDbRelationship.
/**
* Checks if the object relationship is flattened and then chooses the
* corresponding db relationship. The last in idPath if isFlattened and the
* first in list otherwise.
*
* @param relationship
* the object relationship
*
* @return {@link DbRelationship}
*/
protected DbRelationship chooseDbRelationship(ObjRelationship relationship) {
List<DbRelationship> dbRelationships = relationship.getDbRelationships();
String dbRelationshipPath = relationship.getDbRelationshipPath();
if (dbRelationshipPath.contains(".")) {
String dbRelName = dbRelationshipPath.substring(dbRelationshipPath.lastIndexOf(".") + 1);
for (DbRelationship dbR : dbRelationships) {
if (dbR.getName().equals(dbRelName)) {
return dbR;
}
}
}
return relationship.getDbRelationships().get(0);
}
use of org.apache.cayenne.map.DbRelationship in project cayenne by apache.
the class EJBQLSelectColumnsTranslator method visitDbPath.
@Override
public boolean visitDbPath(EJBQLExpression expression, int finishedChildIndex) {
EJBQLDbPathTranslator pathTranslator = new EJBQLDbPathTranslator(context) {
@Override
protected void appendMultiColumnPath(EJBQLMultiColumnOperand operand) {
throw new EJBQLException("Can't use multi-column paths in column clause");
}
@Override
protected void processTerminatingRelationship(DbRelationship relationship) {
Map<String, String> xfields = null;
if (context.isAppendingResultColumns()) {
xfields = context.nextEntityResult().getFields();
}
final Map<String, String> fields = xfields;
DbEntity table = (DbEntity) relationship.getTargetEntity();
Collection<DbAttribute> dbAttr = table.getAttributes();
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(DbAttribute attribute) {
String alias = this.lastAlias != null ? lastAlias : context.getTableAlias(idPath, context.getQuotingStrategy().quotedFullyQualifiedName(currentEntity));
appendColumn(TypesMapping.getJavaBySqlType(attribute.getType()), alias, attribute, context.isAppendingResultColumns() ? context.nextColumnAlias() : "");
}
};
expression.visit(pathTranslator);
return false;
}
use of org.apache.cayenne.map.DbRelationship 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