1 28 29 package com.caucho.amber.query; 30 31 import com.caucho.util.CharBuffer; 32 33 import com.caucho.amber.table.LinkColumns; 34 import com.caucho.amber.table.ForeignColumn; 35 36 45 public class ManyToOneJoinExpr extends JoinExpr { 46 private LinkColumns _linkColumns; 47 48 private FromItem _sourceFromItem; 49 private FromItem _targetFromItem; 50 51 54 ManyToOneJoinExpr(LinkColumns link, FromItem source, FromItem target) 55 { 56 _linkColumns = link; 57 58 _sourceFromItem = source; 59 _targetFromItem = target; 60 61 if (source == null || target == null) 62 throw new NullPointerException(); 63 } 64 65 68 public boolean isBoolean() 69 { 70 return true; 71 } 72 73 76 public AmberExpr bindSelect(QueryParser parser) 77 { 78 return this; 79 } 80 81 84 public boolean bindToFromItem() 85 { 86 if (_targetFromItem.getJoinExpr() == null || 87 _targetFromItem.getJoinExpr().equals(this)) { 88 _targetFromItem.setJoinExpr(this); 89 return true; 90 } 91 else if (_sourceFromItem.getJoinExpr() == null) { 92 _sourceFromItem.setJoinExpr(new OneToManyJoinExpr(_linkColumns, 93 _sourceFromItem, 94 _targetFromItem)); 95 96 return true; 97 } 98 else 99 return false; 100 } 101 102 105 FromItem getJoinTarget() 106 { 107 return _targetFromItem; 108 } 109 110 113 FromItem getJoinParent() 114 { 115 return _sourceFromItem; 116 } 117 118 121 public boolean usesFrom(FromItem from, int type, boolean isNot) 122 { 123 return from == _targetFromItem || from == _sourceFromItem; 124 } 125 126 129 public AmberExpr replace(KeyColumnExpr id) 130 { 131 PathExpr parent = (PathExpr) id.getParent(); 132 133 if (parent.getChildFromItem() != _targetFromItem) 134 return id; 135 136 ForeignColumn sourceColumn = _linkColumns.getSourceColumn(id.getColumn()); 137 138 if (sourceColumn == null) 139 throw new IllegalStateException(id.getColumn().getName()); 140 141 return new ColumnExpr(_sourceFromItem.getIdExpr(), sourceColumn); 142 } 143 144 147 public AmberExpr replace(IdExpr id) 148 { 149 return id; 150 } 151 152 155 public void generateWhere(CharBuffer cb) 156 { 157 cb.append(_linkColumns.generateWhere(_sourceFromItem.getName(), 158 _targetFromItem.getName())); 159 } 160 161 164 public void generateJoin(CharBuffer cb) 165 { 166 cb.append(_linkColumns.generateJoin(_sourceFromItem.getName(), 167 _targetFromItem.getName())); 168 } 169 170 173 public boolean equals(Object o) 174 { 175 if (! (o instanceof ManyToOneJoinExpr)) 176 return false; 177 178 ManyToOneJoinExpr joinExpr = (ManyToOneJoinExpr) o; 179 180 return (_linkColumns.equals(joinExpr._linkColumns) && 181 _sourceFromItem.equals(joinExpr._sourceFromItem) && 182 _targetFromItem.equals(joinExpr._targetFromItem)); 183 } 184 185 186 public String toString() 187 { 188 return ("ManyToOneJoinExpr[" + _linkColumns + "," + 189 _sourceFromItem + "," + _targetFromItem + "]"); 190 } 191 } 192 | Popular Tags |