1 28 29 package com.caucho.amber.query; 30 31 import com.caucho.util.CharBuffer; 32 33 import com.caucho.amber.type.Type; 34 import com.caucho.amber.type.EntityType; 35 36 import com.caucho.amber.table.Table; 37 import com.caucho.amber.table.LinkColumns; 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 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 generateSelect(CharBuffer cb) 179 { 180 String id = _childFromItem.getName(); 181 182 cb.append(_linkColumns.generateSelectSQL(id)); 183 } 184 185 public String toString() 186 { 187 return "OneToManyExpr[" + _parent + "," + _linkColumns + "]"; 188 } 189 } 190 | Popular Tags |