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 40 public class OneToManyJoinExpr extends JoinExpr { 41 private LinkColumns _linkColumns; 42 43 private FromItem _sourceFromItem; 44 private FromItem _targetFromItem; 45 46 49 public OneToManyJoinExpr(LinkColumns linkColumns, 50 FromItem source, 51 FromItem target) 52 { 53 _linkColumns = linkColumns; 54 55 _sourceFromItem = source; 56 _targetFromItem = target; 57 58 if (source == null || target == null) 59 throw new NullPointerException (); 60 } 61 62 65 public boolean isBoolean() 66 { 67 return true; 68 } 69 70 73 public AmberExpr bindSelect(QueryParser parser) 74 { 75 return this; 76 } 77 78 81 public boolean bindToFromItem() 82 { 83 if (_sourceFromItem.getJoinExpr() == null || 84 _sourceFromItem.getJoinExpr().equals(this)) { 85 _sourceFromItem.setJoinExpr(this); 86 return true; 87 } 88 89 ManyToOneJoinExpr manyToOne = new ManyToOneJoinExpr(_linkColumns, 90 _sourceFromItem, 91 _targetFromItem); 92 93 if (_targetFromItem.getJoinExpr() == null || 94 _targetFromItem.getJoinExpr().equals(manyToOne)) { 95 _targetFromItem.setJoinExpr(manyToOne); 96 97 return true; 98 } 99 else 100 return false; 101 } 102 103 106 public FromItem getJoinTarget() 107 { 108 return _targetFromItem; 109 } 110 111 117 public FromItem getJoinParent() 118 { 119 return _targetFromItem; 120 } 121 122 125 public AmberExpr replace(KeyColumnExpr id) 126 { 127 PathExpr parent = id.getParent(); 128 129 if (parent.getChildFromItem() != _targetFromItem) 130 return id; 131 132 ForeignColumn sourceColumn = _linkColumns.getSourceColumn(id.getColumn()); 133 134 if (sourceColumn == null) 135 throw new IllegalStateException (id.getColumn().getName()); 136 137 return new ColumnExpr(_sourceFromItem.getIdExpr(), sourceColumn); 138 } 139 140 143 public AmberExpr replace(IdExpr id) 144 { 145 return id; 146 } 147 148 151 public AmberExpr getWhere() 152 { 153 167 168 return null; 169 } 170 171 174 public void generateWhere(CharBuffer cb) 175 { 176 cb.append(_linkColumns.generateWhere(_sourceFromItem.getName(), 177 _targetFromItem.getName())); 178 } 179 180 183 public void generateUpdateWhere(CharBuffer cb) 184 { 185 generateWhere(cb); 186 } 187 188 191 public void generateHaving(CharBuffer cb) 192 { 193 generateWhere(cb); 194 } 195 196 199 public void generateJoin(CharBuffer cb) 200 { 201 cb.append(_linkColumns.generateJoin(_sourceFromItem.getName(), 202 _targetFromItem.getName())); 203 } 204 205 208 public boolean equals(Object o) 209 { 210 if (! (o instanceof OneToManyJoinExpr)) 211 return false; 212 213 OneToManyJoinExpr joinExpr = (OneToManyJoinExpr) o; 214 215 return (_linkColumns.equals(joinExpr._linkColumns) && 216 _targetFromItem.equals(joinExpr._targetFromItem) && 217 _sourceFromItem.equals(joinExpr._sourceFromItem)); 218 } 219 220 221 public String toString() 222 { 223 return ("OneToManyJoinExpr[" + _linkColumns + "," + 224 _targetFromItem + "," + _sourceFromItem + "]"); 225 } 226 } 227 | Popular Tags |