1 28 29 package com.caucho.amber.expr; 30 31 import com.caucho.amber.query.FromItem; 32 import com.caucho.amber.query.QueryParser; 33 import com.caucho.amber.table.LinkColumns; 34 import com.caucho.amber.type.RelatedType; 35 import com.caucho.util.CharBuffer; 36 37 40 public class DependentEntityOneToOneExpr extends AbstractPathExpr { 41 private PathExpr _parent; 42 43 private LinkColumns _linkColumns; 44 45 private FromItem _fromItem; 46 private FromItem _childFromItem; 47 48 51 public DependentEntityOneToOneExpr(PathExpr parent, 52 LinkColumns linkColumns) 53 { 54 _parent = parent; 55 _linkColumns = linkColumns; 56 } 57 58 61 public RelatedType getTargetType() 62 { 63 return _linkColumns.getSourceTable().getType(); 64 } 65 66 69 public AmberExpr bindSelect(QueryParser parser) 70 { 71 75 _fromItem = bindSubPath(parser); 76 77 return this; 78 } 79 80 83 public FromItem getChildFromItem() 84 { 85 return _childFromItem; 86 } 87 88 91 public FromItem bindSubPath(QueryParser parser) 92 { 93 if (_childFromItem != null) 94 return _childFromItem; 95 96 DependentEntityOneToOneExpr pathExpr; 97 pathExpr = (DependentEntityOneToOneExpr) parser.addPath(this); 98 99 if (pathExpr != this) { 100 _fromItem = pathExpr._fromItem; 101 _childFromItem = pathExpr._childFromItem; 102 103 return _childFromItem; 104 } 105 106 _fromItem = _parent.bindSubPath(parser); 107 108 _childFromItem = parser.getSelectQuery().createFromItem(_linkColumns.getSourceTable(), 110 parser.createTableName()); 111 112 121 JoinExpr join = new ManyToOneJoinExpr(_linkColumns, 122 _childFromItem, 123 _fromItem); 124 125 _childFromItem.setJoinExpr(join); 126 127 return _childFromItem; 128 } 129 130 133 public boolean usesFrom(FromItem from, int type, boolean isNot) 134 { 135 return (_childFromItem == from && type == IS_INNER_JOIN || 136 _fromItem == from || 137 _parent.usesFrom(from, type)); 138 } 139 140 143 154 155 158 public void generateMatchArgWhere(CharBuffer cb) 159 { 160 if (_fromItem != null) { 161 cb.append(_linkColumns.generateMatchArgSQL(_fromItem.getName())); 162 } 163 else { 164 cb.append(_linkColumns.generateMatchArgSQL(_parent.getChildFromItem().getName())); 165 } 166 } 167 168 171 public void generateWhere(CharBuffer cb) 172 { 173 generateInternalWhere(cb, true); 174 } 175 176 179 public void generateUpdateWhere(CharBuffer cb) 180 { 181 generateInternalWhere(cb, false); 182 } 183 184 public String toString() 185 { 186 return "OneToOneExpr[" + _parent + "," + _linkColumns + "]"; 187 } 188 189 public int hashCode() 190 { 191 return 65521 * _parent.hashCode() + _linkColumns.hashCode(); 192 } 193 194 public boolean equals(Object o) 195 { 196 if (o == null || ! getClass().equals(o.getClass())) 197 return false; 198 199 DependentEntityOneToOneExpr oneToOne = (DependentEntityOneToOneExpr) o; 200 201 return (_parent.equals(oneToOne._parent) && 202 _linkColumns.equals(oneToOne._linkColumns)); 203 } 204 205 208 private void generateInternalWhere(CharBuffer cb, 209 boolean select) 210 { 211 if (_fromItem != null) { 212 213 String s; 214 215 if (select) { 216 217 s = _fromItem.getName(); 218 219 cb.append(s); 220 cb.append('.'); 221 } 222 223 226 s = _childFromItem.getTable().getIdColumns().get(0).getName(); 227 228 cb.append(s); 229 } 230 else { 231 232 if (select) 233 super.generateWhere(cb); 234 else 235 super.generateUpdateWhere(cb); 236 } 237 } 238 } 239 | Popular Tags |