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.Column; 34 import com.caucho.util.CharBuffer; 35 36 import java.util.ArrayList ; 37 38 41 public class EqualJoinExpr extends JoinExpr { 42 private ArrayList <Column> _keyColumns; 43 44 private FromItem _fromItemA; 45 private FromItem _fromItemB; 46 47 50 EqualJoinExpr(ArrayList <Column> keyColumns, 51 FromItem fromItemA, 52 FromItem fromItemB) 53 { 54 _keyColumns = keyColumns; 55 56 _fromItemA = fromItemA; 57 _fromItemB = fromItemB; 58 59 if (fromItemA == null || fromItemB == null) 60 throw new NullPointerException (); 61 } 62 63 66 public boolean isBoolean() 67 { 68 return true; 69 } 70 71 74 public AmberExpr bindSelect(QueryParser parser) 75 { 76 return this; 77 } 78 79 82 public boolean bindToFromItem() 83 { 84 if (_fromItemA.getJoinExpr() == null || 85 _fromItemA.getJoinExpr().equals(this)) { 86 _fromItemA.setJoinExpr(this); 87 return true; 88 } 89 else if (_fromItemB.getJoinExpr() == null) { 90 _fromItemB.setJoinExpr(this); 91 92 return true; 93 } 94 else 95 return false; 96 } 97 98 101 public FromItem getJoinTarget() 102 { 103 return _fromItemA; 104 } 105 106 109 public AmberExpr replace(IdFieldExpr id) 110 { 111 IdExpr parent = (IdExpr) id.getParent(); 112 113 if (parent.getFromItem() == _fromItemA) 114 return new ColumnExpr(new IdExpr(_fromItemA), id.getColumn()); 115 else if (parent.getFromItem() == _fromItemB) 116 return new ColumnExpr(new IdExpr(_fromItemB), id.getColumn()); 117 else 118 return id; 119 } 120 121 124 public AmberExpr replace(IdExpr id) 125 { 126 return id; 127 } 128 129 132 public void generateWhere(CharBuffer cb) 133 { 134 generateInternalWhere(cb, true); 135 } 136 137 140 public void generateUpdateWhere(CharBuffer cb) 141 { 142 generateInternalWhere(cb, false); 143 } 144 145 148 public void generateHaving(CharBuffer cb) 149 { 150 generateWhere(cb); 151 } 152 153 156 public boolean equals(Object o) 157 { 158 if (! (o instanceof EqualJoinExpr)) 159 return false; 160 161 EqualJoinExpr equalExpr = (EqualJoinExpr) o; 162 163 return (_keyColumns.equals(equalExpr._keyColumns) && 164 _fromItemA.equals(equalExpr._fromItemA) && 165 _fromItemB.equals(equalExpr._fromItemB)); 166 } 167 168 169 public String toString() 170 { 171 return ("EqualJoinExpr[" + _keyColumns + "," + 172 _fromItemA + "," + _fromItemB + "]"); 173 } 174 175 178 private void generateInternalWhere(CharBuffer cb, 179 boolean select) 180 { 181 cb.append('('); 182 183 for (int i = 0; i < _keyColumns.size(); i++) { 184 Column column = _keyColumns.get(i); 185 186 if (i != 0) 187 cb.append(" AND "); 188 189 cb.append(_fromItemA.getName()); 190 cb.append('.'); 191 cb.append(column.getName()); 192 193 cb.append('='); 194 195 cb.append(_fromItemB.getName()); 196 cb.append('.'); 197 cb.append(column.getName()); 198 } 199 200 cb.append('('); 201 } 202 } 203 | Popular Tags |