1 29 30 package com.caucho.amber.expr; 31 32 import com.caucho.amber.query.FromItem; 33 import com.caucho.amber.query.QueryParser; 34 import com.caucho.amber.table.LinkColumns; 35 import com.caucho.amber.table.Table; 36 import com.caucho.amber.type.RelatedType; 37 import com.caucho.amber.type.Type; 38 import com.caucho.util.CharBuffer; 39 40 43 public class ManyToOneExpr extends AbstractPathExpr { 44 private PathExpr _parent; 45 46 private LinkColumns _linkColumns; 47 48 private FromItem _fromItem; 49 private FromItem _childFromItem; 50 51 54 public ManyToOneExpr(PathExpr parent, LinkColumns linkColumns) 55 { 56 _parent = parent; 57 _linkColumns = linkColumns; 58 } 59 60 63 public RelatedType getTargetType() 64 { 65 return _linkColumns.getTargetTable().getType(); 66 } 67 68 71 public Type getType() 72 { 73 return getTargetType(); 74 } 75 76 79 public AmberExpr bindSelect(QueryParser parser) 80 { 81 _fromItem = _parent.bindSubPath(parser); 82 83 return this; 84 } 85 86 89 public FromItem getFromItem() 90 { 91 return _fromItem; 92 } 93 94 97 public LinkColumns getLinkColumns() 98 { 99 return _linkColumns; 100 } 101 102 105 public FromItem getChildFromItem() 106 { 107 return _childFromItem; 108 } 109 110 113 public FromItem bindSubPath(QueryParser parser) 114 { 115 if (_childFromItem != null) 116 return _childFromItem; 117 118 ManyToOneExpr pathExpr = (ManyToOneExpr) parser.addPath(this); 119 120 if (pathExpr != this) { 121 _fromItem = pathExpr._fromItem; 122 _childFromItem = pathExpr._childFromItem; 123 124 return _childFromItem; 125 } 126 127 _parent = _parent.bindSelect(parser, null); 129 130 bindSelect(parser, parser.createTableName()); 131 132 return _childFromItem; 133 } 134 135 138 public PathExpr bindSelect(QueryParser parser, String id) 139 { 140 if (_childFromItem != null) 141 return this; 142 143 if (_fromItem == null) 144 _fromItem = _parent.bindSubPath(parser); 145 146 Table targetTable = _linkColumns.getTargetTable(); 147 _childFromItem = parser.addFromItem(targetTable, id); 148 149 JoinExpr joinExpr; 150 joinExpr = new ManyToOneJoinExpr(_linkColumns, 151 _fromItem, 152 _childFromItem); 153 154 _childFromItem.setJoinExpr(joinExpr); 155 156 return this; 157 } 158 159 162 public boolean usesFrom(FromItem from, int type, boolean isNot) 163 { 164 return (_childFromItem == from && type == IS_INNER_JOIN 165 || _fromItem == from 166 || _parent.usesFrom(from, type)); 167 } 168 169 172 @Override 173 public boolean exists(FromItem from) 174 { 175 System.out.println("P: " + _parent); 176 return (_fromItem == from 177 && _parent.exists()); 178 } 179 180 183 194 195 198 public void generateMatchArgWhere(CharBuffer cb) 199 { 200 if (_fromItem != null) { 201 cb.append(_linkColumns.generateMatchArgSQL(_fromItem.getName())); 202 } 203 else { 204 cb.append(_linkColumns.generateMatchArgSQL(_parent.getChildFromItem().getName())); 205 } 206 } 207 208 211 public void generateWhere(CharBuffer cb) 212 { 213 generateInternalWhere(cb, true); 214 } 215 216 219 public void generateUpdateWhere(CharBuffer cb) 220 { 221 generateInternalWhere(cb, false); 222 } 223 224 227 public void generateSelect(CharBuffer cb) 229 { 230 String tableName; 231 232 if (_fromItem != null) 233 tableName = _fromItem.getName(); 234 else 235 tableName = _parent.getChildFromItem().getName(); 236 237 cb.append(_linkColumns.generateSelectSQL(tableName)); 238 } 239 240 243 public PathExpr getParent() 244 { 245 return _parent; 246 } 247 248 public int hashCode() 249 { 250 return 65521 * _parent.hashCode() + _linkColumns.hashCode(); 251 } 252 253 public boolean equals(Object o) 254 { 255 if (o == null || ! getClass().equals(o.getClass())) 256 return false; 257 258 ManyToOneExpr manyToOne = (ManyToOneExpr) o; 259 260 return (_parent.equals(manyToOne._parent) && 261 _linkColumns.equals(manyToOne._linkColumns)); 262 } 263 264 public String toString() 265 { 266 return "ManyToOneExpr[" + _childFromItem + "," + _fromItem + "," + _parent + "]"; 267 } 268 269 272 private void generateInternalWhere(CharBuffer cb, 273 boolean select) 274 { 275 if (_fromItem != null) { 276 277 if (select) { 278 cb.append(_fromItem.getName()); 279 cb.append('.'); 280 } 281 282 cb.append(_linkColumns.getColumns().get(0).getName()); 283 } 284 else { 285 286 if (select) 287 super.generateWhere(cb); 288 else 289 super.generateUpdateWhere(cb); 290 } 291 } 292 } 293 | Popular Tags |