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