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.table.LinkColumns; 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 EntityType getTargetType() 62 { 63 return _linkColumns.getSourceTable().getType(); 64 } 65 66 69 public AmberExpr bindSelect(QueryParser parser) 70 { 71 if (_fromItem == null) 72 _fromItem = _parent.bindSubPath(parser); 73 74 return this; 75 } 76 77 80 public FromItem getChildFromItem() 81 { 82 return _childFromItem; 83 } 84 85 88 public FromItem bindSubPath(QueryParser parser) 89 { 90 if (_childFromItem != null) 91 return _childFromItem; 92 93 DependentEntityOneToOneExpr pathExpr; 94 pathExpr = (DependentEntityOneToOneExpr) parser.addPath(this); 95 96 if (pathExpr != this) { 97 _fromItem = pathExpr._fromItem; 98 _childFromItem = pathExpr._childFromItem; 99 100 return _childFromItem; 101 } 102 103 _fromItem = _parent.bindSubPath(parser); 104 105 if (_fromItem != null) 106 _childFromItem = _fromItem.getQuery().createFromItem(_linkColumns.getSourceTable(), 107 parser.createTableName()); 108 else 109 _childFromItem = parser.getSelectQuery().createFromItem(_linkColumns.getSourceTable(), 110 parser.createTableName()); 111 112 JoinExpr join = new ManyToOneJoinExpr(_linkColumns, 113 _childFromItem, 114 _fromItem); 115 116 _childFromItem.setJoinExpr(join); 117 118 return _childFromItem; 119 } 120 121 124 public boolean usesFrom(FromItem from, int type, boolean isNot) 125 { 126 return (_childFromItem == from && type == IS_INNER_JOIN || 127 _fromItem == from || 128 _parent.usesFrom(from, type)); 129 } 130 131 134 145 146 149 public void generateMatchArgWhere(CharBuffer cb) 150 { 151 if (_fromItem != null) { 152 cb.append(_linkColumns.generateMatchArgSQL(_fromItem.getName())); 153 } 154 else { 155 cb.append(_linkColumns.generateMatchArgSQL(_parent.getChildFromItem().getName())); 156 } 157 } 158 159 public String toString() 160 { 161 return "OneToOneExpr[" + _parent + "," + _linkColumns + "]"; 162 } 163 164 public int hashCode() 165 { 166 return 65521 * _parent.hashCode() + _linkColumns.hashCode(); 167 } 168 169 public boolean equals(Object o) 170 { 171 if (o == null || ! getClass().equals(o.getClass())) 172 return false; 173 174 DependentEntityOneToOneExpr oneToOne = (DependentEntityOneToOneExpr) o; 175 176 return (_parent.equals(oneToOne._parent) && 177 _linkColumns.equals(oneToOne._linkColumns)); 178 } 179 } 180 | Popular Tags |