1 28 29 package com.caucho.amber.expr; 30 31 import com.caucho.amber.field.AmberField; 32 import com.caucho.amber.field.IdField; 33 import com.caucho.amber.field.KeyManyToOneField; 34 import com.caucho.amber.field.KeyPropertyField; 35 import com.caucho.amber.query.FromItem; 36 import com.caucho.amber.query.QueryParser; 37 import com.caucho.amber.table.Column; 38 import com.caucho.amber.type.EntityType; 39 import com.caucho.util.CharBuffer; 40 41 44 public class KeyManyToOneExpr extends AbstractPathExpr 45 implements IdFieldExpr { 46 private PathExpr _parent; 47 private KeyManyToOneField _manyToOne; 49 50 private FromItem _fromItem; 51 private FromItem _childFromItem; 52 53 56 public KeyManyToOneExpr(PathExpr parent, KeyManyToOneField manyToOne) 57 { 58 _parent = parent; 59 _manyToOne = manyToOne; 60 } 61 62 65 public PathExpr getParent() 66 { 67 return _parent; 68 } 69 70 73 public IdField getField() 74 { 75 return _manyToOne; 76 } 77 78 81 public EntityType getTargetType() 82 { 83 return _manyToOne.getEntityType(); 84 } 85 86 89 public Column getColumn() 90 { 91 throw new UnsupportedOperationException (); 92 } 93 94 97 public AmberExpr createField(QueryParser parser, String name) 98 { 99 AmberField field = getTargetType().getField(name); 100 101 if (field == null) 102 return null; 103 else if (field instanceof IdField) { 104 KeyPropertyField idField = _manyToOne.getIdField((IdField) field); 105 106 return idField.createExpr(parser, _parent); 107 } 108 else 109 return field.createExpr(parser, this); 110 } 111 112 115 public AmberExpr bindSelect(QueryParser parser) 116 { 117 _fromItem = _parent.bindSubPath(parser); 118 119 return this; 120 } 121 122 125 public FromItem getChildFromItem() 126 { 127 return _childFromItem; 128 } 129 130 133 public FromItem bindSubPath(QueryParser parser) 134 { 135 if (_childFromItem != null) 136 return _childFromItem; 137 138 KeyManyToOneExpr pathExpr = (KeyManyToOneExpr) parser.addPath(this); 139 140 if (pathExpr != this) { 141 _fromItem = pathExpr._fromItem; 142 _childFromItem = pathExpr._childFromItem; 143 144 return _childFromItem; 145 } 146 147 _fromItem = _parent.bindSubPath(parser); 148 149 if (_fromItem != null) 150 _childFromItem = _fromItem.getQuery().createFromItem(getTargetType().getTable(), 151 parser.createTableName()); 152 else 153 _childFromItem = parser.getSelectQuery().createFromItem(getTargetType().getTable(), 154 parser.createTableName()); 155 156 JoinExpr link = new ManyToOneJoinExpr(_manyToOne.getLinkColumns(), 157 _fromItem, 158 _childFromItem); 159 160 _childFromItem.setJoinExpr(link); 161 162 return _childFromItem; 163 } 164 165 168 public boolean usesFrom(FromItem from, int type, boolean isNot) 169 { 170 return from == _childFromItem || _parent.usesFrom(from, type); 171 } 172 173 176 186 187 190 201 202 205 public void generateMatchArgWhere(CharBuffer cb) 206 { 207 String table; 208 209 if (_fromItem != null) 210 table = _fromItem.getName(); 211 else 212 table = _parent.getChildFromItem().getName(); 213 214 cb.append(_manyToOne.getLinkColumns().generateMatchArgSQL(table)); 215 } 216 217 public String toString() 218 { 219 return _parent + "." + getField(); 220 } 221 222 public int hashCode() 223 { 224 return 65521 * _parent.hashCode() + getField().hashCode(); 225 } 226 227 public boolean equals(Object o) 228 { 229 if (o == null || ! getClass().equals(o.getClass())) 230 return false; 231 232 KeyManyToOneExpr manyToOne = (KeyManyToOneExpr) o; 233 234 return (_parent.equals(manyToOne._parent) && 235 getField().equals(manyToOne.getField())); 236 } 237 } 238 | Popular Tags |