use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.
the class EJBQLIdentifierColumnsTranslator method visitIdentifier.
@Override
public boolean visitIdentifier(EJBQLExpression expression) {
Map<String, String> xfields = null;
if (context.isAppendingResultColumns()) {
xfields = context.nextEntityResult().getFields();
}
// assign whatever we have to a final ivar so that it can be accessed
// within
// the inner class
final Map<String, String> fields = xfields;
final String idVar = expression.getText();
// append all table columns ... the trick is to follow the algorithm for
// describing the fields in the expression compiler, so that we could
// assign
// columns labels from FieldResults in the order we encounter them
// here...
// TODO: andrus 2008/02/17 - this is a bit of a hack, think of a better
// solution
ClassDescriptor descriptor = context.getEntityDescriptor(idVar);
PropertyVisitor visitor = new PropertyVisitor() {
public boolean visitAttribute(AttributeProperty property) {
ObjAttribute oa = property.getAttribute();
Iterator<?> dbPathIterator = oa.getDbPathIterator();
EJBQLJoinAppender joinAppender = null;
String marker = null;
EJBQLTableId lhsId = new EJBQLTableId(idVar);
while (dbPathIterator.hasNext()) {
Object pathPart = dbPathIterator.next();
if (pathPart == null) {
throw new CayenneRuntimeException("ObjAttribute has no component: %s", oa.getName());
} else if (pathPart instanceof DbRelationship) {
if (marker == null) {
marker = EJBQLJoinAppender.makeJoinTailMarker(idVar);
joinAppender = context.getTranslatorFactory().getJoinAppender(context);
}
DbRelationship dr = (DbRelationship) pathPart;
EJBQLTableId rhsId = new EJBQLTableId(lhsId, dr.getName());
joinAppender.appendOuterJoin(marker, lhsId, rhsId);
lhsId = rhsId;
} else if (pathPart instanceof DbAttribute) {
appendColumn(idVar, oa, (DbAttribute) pathPart, fields, oa.getType());
}
}
return true;
}
public boolean visitToMany(ToManyProperty property) {
visitRelationship(property);
return true;
}
public boolean visitToOne(ToOneProperty property) {
visitRelationship(property);
return true;
}
private void visitRelationship(ArcProperty property) {
ObjRelationship rel = property.getRelationship();
DbRelationship dbRel = rel.getDbRelationships().get(0);
for (DbJoin join : dbRel.getJoins()) {
DbAttribute src = join.getSource();
appendColumn(idVar, null, src, fields);
}
}
};
// EJBQL queries are polymorphic by definition - there is no distinction
// between
// inheritance/no-inheritance fetch
descriptor.visitAllProperties(visitor);
// append id columns ... (some may have been appended already via
// relationships)
DbEntity table = descriptor.getEntity().getDbEntity();
for (DbAttribute pk : table.getPrimaryKeys()) {
appendColumn(idVar, null, pk, fields);
}
// append inheritance discriminator columns...
for (ObjAttribute column : descriptor.getDiscriminatorColumns()) {
appendColumn(idVar, column, column.getDbAttribute(), fields);
}
addPrefetchedColumnsIfAny(idVar);
return false;
}
use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.
the class EJBQLJoinAppender method generateJoinsForFlattenedAttributes.
/**
* Generates Joins statements for those flattened attributes that appear after the
* FROM clause, e.g. in WHERE, ORDER BY, etc clauses. Flattened attributes of the
* entity from the SELECT clause are processed earlier and therefore are omitted.
*
* @param id table to JOIN id
*/
private void generateJoinsForFlattenedAttributes(EJBQLTableId id) {
String entityName = context.getEntityDescriptor(id.getEntityId()).getEntity().getName();
// if the dbPath is not null, all attributes of the entity are processed earlier
boolean isProcessingOmitted = id.getDbPath() != null;
String sourceExpression = context.getCompiledExpression().getSource();
List<Object> resultSetMapping = context.getMetadata().getResultSetMapping();
for (Object mapping : resultSetMapping) {
if (mapping instanceof EntityResultSegment) {
if (entityName.equals(((EntityResultSegment) mapping).getClassDescriptor().getEntity().getName())) {
// if entity is included into SELECT clause, all its attributes are processed earlier
isProcessingOmitted = true;
break;
}
}
}
if (!isProcessingOmitted) {
QuotingStrategy quoter = context.getQuotingStrategy();
Collection<ObjAttribute> attributes = context.getEntityDescriptor(id.getEntityId()).getEntity().getAttributes();
for (ObjAttribute objAttribute : attributes) {
if (objAttribute.isFlattened() && sourceExpression.contains(id.getEntityId() + "." + objAttribute.getName())) {
// joins for attribute are generated if it is flattened and appears in original statement
Iterator<CayenneMapEntry> dbPathIterator = objAttribute.getDbPathIterator();
while (dbPathIterator.hasNext()) {
CayenneMapEntry next = dbPathIterator.next();
if (next instanceof DbRelationship) {
DbRelationship rel = (DbRelationship) next;
context.append(" LEFT OUTER JOIN ");
String targetEntityName = quoter.quotedFullyQualifiedName(rel.getTargetEntity());
String subqueryTargetAlias = context.getTableAlias(id.getEntityId(), targetEntityName);
context.append(targetEntityName).append(' ').append(subqueryTargetAlias);
generateJoiningExpression(rel, context.getTableAlias(id.getEntityId(), quoter.quotedFullyQualifiedName(rel.getSourceEntity())), subqueryTargetAlias);
}
}
}
}
}
}
use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.
the class EJBQLPathTranslator method processLastPathComponent.
protected void processLastPathComponent() {
ObjAttribute attribute = currentEntity.getAttribute(lastPathComponent);
if (attribute != null) {
processTerminatingAttribute(attribute);
return;
}
ObjRelationship relationship = currentEntity.getRelationship(lastPathComponent);
if (relationship != null) {
processTerminatingRelationship(relationship);
return;
}
throw new IllegalStateException("Invalid path component: " + lastPathComponent);
}
use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.
the class DataMapUtils method getParameterNames.
private Map<String, String> getParameterNames(Expression expression, Object root) {
if (expression != null) {
Map<String, String> types = new HashMap<>();
String typeName = "";
List<String> names = new LinkedList<>();
for (int i = 0; i < expression.getOperandCount(); i++) {
Object operand = expression.getOperand(i);
if (operand instanceof Expression) {
types.putAll(getParameterNames((Expression) operand, root));
}
if (operand instanceof ASTObjPath) {
PathComponent<ObjAttribute, ObjRelationship> component = ((Entity) root).lastPathComponent((ASTObjPath) operand, null);
ObjAttribute attribute = component.getAttribute();
if (attribute != null) {
typeName = attribute.getType();
} else {
ObjRelationship relationship = component.getRelationship();
if (relationship != null) {
typeName = relationship.getTargetEntity().getClassName();
} else {
typeName = "Object";
}
}
}
if (operand instanceof ASTList) {
Object[] values = (Object[]) ((ASTList) operand).getOperand(0);
for (Object value : values) {
if (value instanceof ExpressionParameter) {
names.add(((ExpressionParameter) value).getName());
}
}
}
if (operand instanceof ExpressionParameter) {
names.add(((ExpressionParameter) operand).getName());
}
}
for (String name : names) {
types.put(Util.underscoredToJava(name, false), typeName);
}
return types;
}
return Collections.emptyMap();
}
use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.
the class ClassGenerationActionTest method testExecuteArtifactPairsAttribute.
@Test
public void testExecuteArtifactPairsAttribute() throws Exception {
ObjEntity testEntity1 = new ObjEntity("TE1");
testEntity1.setClassName("org.example.TestClass1");
ObjAttribute attr = new ObjAttribute();
attr.setName("ID");
attr.setType("int");
ObjAttribute attr1 = new ObjAttribute();
attr1.setName("name");
attr1.setType("char");
testEntity1.addAttribute(attr);
testEntity1.addAttribute(attr1);
action.setMakePairs(true);
List<String> generated = execute(new EntityArtifact(testEntity1));
assertNotNull(generated);
assertEquals(2, generated.size());
String superclass = generated.get(0);
assertTrue(superclass, superclass.contains("public void setID(int ID)"));
assertTrue(superclass, superclass.contains("this.ID = ID;"));
assertTrue(superclass, superclass.contains("public int getID()"));
assertTrue(superclass, superclass.contains("return this.ID;"));
assertTrue(superclass, superclass.contains("public void setName(char name)"));
assertTrue(superclass, superclass.contains("this.name = name;"));
assertTrue(superclass, superclass.contains("public char getName()"));
assertTrue(superclass, superclass.contains("return this.name;"));
}
Aggregations