1 19 package org.apache.cayenne.access.jdbc; 20 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Set ; 25 26 import org.apache.cayenne.CayenneRuntimeException; 27 import org.apache.cayenne.dba.TypesMapping; 28 import org.apache.cayenne.ejbql.EJBQLBaseVisitor; 29 import org.apache.cayenne.ejbql.EJBQLExpression; 30 import org.apache.cayenne.map.DbAttribute; 31 import org.apache.cayenne.map.DbEntity; 32 import org.apache.cayenne.map.DbJoin; 33 import org.apache.cayenne.map.DbRelationship; 34 import org.apache.cayenne.map.ObjAttribute; 35 import org.apache.cayenne.map.ObjRelationship; 36 import org.apache.cayenne.reflect.ArcProperty; 37 import org.apache.cayenne.reflect.AttributeProperty; 38 import org.apache.cayenne.reflect.ClassDescriptor; 39 import org.apache.cayenne.reflect.PropertyVisitor; 40 import org.apache.cayenne.reflect.ToManyProperty; 41 import org.apache.cayenne.reflect.ToOneProperty; 42 43 47 class EJBQLIdentifierColumnsTranslator extends EJBQLBaseVisitor { 48 49 private EJBQLTranslationContext context; 50 private Set columns; 51 private boolean resultColumns; 52 53 EJBQLIdentifierColumnsTranslator(EJBQLTranslationContext context, 54 boolean resultColumns) { 55 this.context = context; 56 this.resultColumns = resultColumns; 57 } 58 59 public boolean visitIdentifier(EJBQLExpression expression) { 60 61 final String idVar = expression.getText(); 62 63 ClassDescriptor descriptor = context.getCompiledExpression().getEntityDescriptor( 65 idVar); 66 67 PropertyVisitor visitor = new PropertyVisitor() { 68 69 public boolean visitAttribute(AttributeProperty property) { 70 ObjAttribute oa = property.getAttribute(); 71 Iterator dbPathIterator = oa.getDbPathIterator(); 72 while (dbPathIterator.hasNext()) { 73 Object pathPart = dbPathIterator.next(); 74 if (pathPart instanceof DbRelationship) { 75 } 78 else if (pathPart instanceof DbAttribute) { 79 DbAttribute dbAttr = (DbAttribute) pathPart; 80 if (dbAttr == null) { 81 throw new CayenneRuntimeException( 82 "ObjAttribute has no DbAttribute: " + oa.getName()); 83 } 84 85 appendColumn(idVar, dbAttr, oa.getType()); 86 } 87 } 88 return true; 89 } 90 91 public boolean visitToMany(ToManyProperty property) { 92 visitRelationship(property); 93 return true; 94 } 95 96 public boolean visitToOne(ToOneProperty property) { 97 visitRelationship(property); 98 return true; 99 } 100 101 private void visitRelationship(ArcProperty property) { 102 ObjRelationship rel = property.getRelationship(); 103 DbRelationship dbRel = (DbRelationship) rel.getDbRelationships().get(0); 104 105 List joins = dbRel.getJoins(); 106 int len = joins.size(); 107 for (int i = 0; i < len; i++) { 108 DbJoin join = (DbJoin) joins.get(i); 109 DbAttribute src = join.getSource(); 110 111 appendColumn(idVar, src); 112 } 113 } 114 }; 115 116 descriptor.visitAllProperties(visitor); 119 120 122 DbEntity table = descriptor.getEntity().getDbEntity(); 123 Iterator it = table.getPrimaryKey().iterator(); 124 while (it.hasNext()) { 125 DbAttribute pk = (DbAttribute) it.next(); 126 appendColumn(idVar, pk); 127 } 128 129 return false; 130 } 131 132 private void appendColumn(String identifier, DbAttribute column) { 133 appendColumn(identifier, column, null); 134 } 135 136 private void appendColumn(String identifier, DbAttribute column, String javaType) { 137 DbEntity table = (DbEntity) column.getEntity(); 138 String alias = context.getTableAlias(identifier, table.getFullyQualifiedName()); 139 String columnName = alias + "." + column.getName(); 140 141 Set columns = getColumns(); 142 143 if (columns.add(columnName)) { 144 145 context.append(columns.size() > 1 ? ", " : " "); 146 147 if (resultColumns) { 148 context.append("#result('"); 149 } 150 151 context.append(columnName); 152 153 if (resultColumns) { 154 if (javaType == null) { 155 javaType = TypesMapping.getJavaBySqlType(column.getType()); 156 } 157 158 context.append("' '").append(javaType).append("' '").append( 161 column.getName()).append("' '").append(column.getName()).append( 162 "' " + column.getType()).append(")"); 163 } 164 } 165 } 166 167 private Set getColumns() { 168 169 if (columns == null) { 170 columns = new HashSet (); 171 } 172 173 return columns; 174 } 175 } 176 | Popular Tags |