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.ForeignColumn; 34 import com.caucho.amber.table.LinkColumns; 35 import com.caucho.util.CharBuffer; 36 37 46 public class ManyToOneJoinExpr extends JoinExpr { 47 private LinkColumns _linkColumns; 48 49 private FromItem _sourceFromItem; 50 private FromItem _targetFromItem; 51 52 55 public ManyToOneJoinExpr(LinkColumns link, 56 FromItem source, 57 FromItem target) 58 { 59 _linkColumns = link; 60 61 _sourceFromItem = source; 62 _targetFromItem = target; 63 64 67 if (target == null) 68 throw new NullPointerException (); 69 } 70 71 74 public boolean isBoolean() 75 { 76 return true; 77 } 78 79 82 public boolean isManyToMany() 83 { 84 if (_sourceFromItem == null) 85 return false; 86 87 return _sourceFromItem.getJoinExpr() instanceof OneToManyJoinExpr; 88 } 89 90 93 public AmberExpr bindSelect(QueryParser parser) 94 { 95 return this; 96 } 97 98 101 public boolean bindToFromItem() 102 { 103 if (_targetFromItem.getJoinExpr() == null || 104 _targetFromItem.getJoinExpr().equals(this)) { 105 _targetFromItem.setJoinExpr(this); 106 return true; 107 } 108 else if (_sourceFromItem.getJoinExpr() == null) { 109 _sourceFromItem.setJoinExpr(new OneToManyJoinExpr(_linkColumns, 110 _sourceFromItem, 111 _targetFromItem)); 112 113 return true; 114 } 115 else 116 return false; 117 } 118 119 122 public FromItem getJoinTarget() 123 { 124 return _targetFromItem; 125 } 126 127 130 public FromItem getJoinParent() 131 { 132 return _sourceFromItem; 133 } 134 135 138 public boolean usesFrom(FromItem from, int type, boolean isNot) 139 { 140 return from == _targetFromItem || from == _sourceFromItem; 141 } 142 143 146 @Override 147 public boolean exists(FromItem from) 148 { 149 return false; 150 } 151 152 155 public AmberExpr replace(KeyColumnExpr id) 156 { 157 PathExpr parent = (PathExpr) id.getParent(); 158 159 if (parent.getChildFromItem() != _targetFromItem) 160 return id; 161 162 ForeignColumn sourceColumn = _linkColumns.getSourceColumn(id.getColumn()); 163 164 if (sourceColumn == null) 165 throw new IllegalStateException (id.getColumn().getName()); 166 167 return new ColumnExpr(_sourceFromItem.getIdExpr(), sourceColumn); 168 } 169 170 173 public AmberExpr replace(IdExpr id) 174 { 175 return id; 176 } 177 178 181 public void generateWhere(CharBuffer cb) 182 { 183 String sourceName = null; 184 185 if (_sourceFromItem != null) 187 sourceName = _sourceFromItem.getName(); 188 189 String targetName = _targetFromItem.getName(); 190 191 cb.append(_linkColumns.generateWhere(sourceName, 192 targetName)); 193 } 194 195 198 public void generateUpdateWhere(CharBuffer cb) 199 { 200 generateWhere(cb); 201 } 202 203 206 public void generateHaving(CharBuffer cb) 207 { 208 generateWhere(cb); 209 } 210 211 214 public void generateJoin(CharBuffer cb) 215 { 216 cb.append(_linkColumns.generateJoin(_sourceFromItem.getName(), 217 _targetFromItem.getName())); 218 } 219 220 223 public boolean equals(Object o) 224 { 225 if (! (o instanceof ManyToOneJoinExpr)) 226 return false; 227 228 ManyToOneJoinExpr joinExpr = (ManyToOneJoinExpr) o; 229 230 return (_linkColumns.equals(joinExpr._linkColumns) && 231 _sourceFromItem.equals(joinExpr._sourceFromItem) && 232 _targetFromItem.equals(joinExpr._targetFromItem)); 233 } 234 235 236 public String toString() 237 { 238 return ("ManyToOneJoinExpr[" + _linkColumns + "," + 239 _sourceFromItem + "," + _targetFromItem + "]"); 240 } 241 } 242 | Popular Tags |