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.LinkColumns; 34 import com.caucho.amber.table.Table; 35 import com.caucho.amber.type.EntityType; 36 import com.caucho.amber.type.Type; 37 import com.caucho.util.CharBuffer; 38 39 45 public class OneToManyExpr extends AbstractPathExpr { 46 private PathExpr _parent; 47 48 private LinkColumns _linkColumns; 50 51 private FromItem _fromItem; 52 private FromItem _childFromItem; 53 54 57 public OneToManyExpr(QueryParser parser, 58 PathExpr parent, 59 LinkColumns linkColumns) 60 { 61 _parent = parent; 62 63 _linkColumns = linkColumns; 64 } 65 66 69 public LinkColumns getLinkColumns() 70 { 71 return _linkColumns; 72 } 73 74 77 public Type getType() 78 { 79 return _linkColumns.getSourceTable().getType(); 80 } 81 82 85 public EntityType getTargetType() 86 { 87 return (EntityType) getType(); 88 } 89 90 93 public boolean usesFrom(FromItem from, int type, boolean isNot) 94 { 95 return (from == _childFromItem || 96 type == IS_INNER_JOIN && _parent.usesFrom(from, type)); 97 } 98 99 102 public PathExpr getParent() 103 { 104 return _parent; 105 } 106 107 110 public AmberExpr bindSelect(QueryParser parser) 111 { 112 return bindSelect(parser, null); 113 } 114 115 118 public PathExpr bindSelect(QueryParser parser, String id) 119 { 120 if (_fromItem != null) 121 return this; 122 123 _fromItem = _parent.bindSubPath(parser); 124 125 Table sourceTable = _linkColumns.getSourceTable(); 126 _childFromItem = parser.addFromItem(sourceTable, id); 127 128 JoinExpr joinExpr; 129 joinExpr = new OneToManyJoinExpr(_linkColumns, 130 _childFromItem, 131 _fromItem); 132 133 _childFromItem.setJoinExpr(joinExpr); 134 _childFromItem.setCollectionExpr(this); 135 136 return this; 137 } 138 139 142 public FromItem getChildFromItem() 143 { 144 return _childFromItem; 145 } 146 147 150 public FromItem bindSubPath(QueryParser parser) 151 { 152 if (_childFromItem == null) 153 bindSelect(parser, null); 154 155 return _childFromItem; 156 } 157 158 161 public Table getTable() 162 { 163 return _fromItem.getTable(); 165 } 166 167 170 public void generateWhere(CharBuffer cb) 171 { 172 throw new IllegalStateException (getClass().getName()); 173 } 174 175 178 public void generateUpdateWhere(CharBuffer cb) 179 { 180 generateWhere(cb); 181 } 182 183 186 public void generateHaving(CharBuffer cb) 187 { 188 generateWhere(cb); 189 } 190 191 194 public void generateSelect(CharBuffer cb) 195 { 196 String id = _childFromItem.getName(); 197 198 cb.append(_linkColumns.generateSelectSQL(id)); 199 } 200 201 public String toString() 202 { 203 return "OneToManyExpr[" + _parent + "," + _linkColumns + "]"; 204 } 205 } 206 | Popular Tags |