1 23 24 30 31 package com.sun.jdo.spi.persistence.support.sqlstore.sql.constraint; 32 33 import com.sun.jdo.spi.persistence.support.sqlstore.ActionDesc; 34 import com.sun.jdo.spi.persistence.support.sqlstore.model.LocalFieldDesc; 35 import com.sun.jdo.spi.persistence.support.sqlstore.sql.generator.QueryPlan; 36 37 import java.util.ArrayList ; 38 import java.util.List ; 39 40 41 43 public class Constraint extends Object { 44 48 public List stack; 49 50 55 private List outerJoinStack; 56 57 66 68 83 public void addField(String name, ActionDesc desc) { 84 stack.add(new ConstraintFieldName(name, desc)); 85 } 86 87 97 public void addField(LocalFieldDesc desc) { 98 stack.add(new ConstraintFieldDesc(desc)); 99 } 100 101 114 public void addField(LocalFieldDesc desc, QueryPlan plan) { 115 stack.add(new ConstraintFieldDesc(desc, plan)); 116 } 117 118 public void addField(ConstraintFieldDesc constraintDesc) { 119 stack.add(constraintDesc); 120 } 121 122 public void addForeignField(String name, ActionDesc desc) { 123 stack.add(new ConstraintForeignFieldName(name, desc)); 124 } 125 126 136 public void addOperation(int operation) { 137 stack.add(new ConstraintOperation(operation)); 138 } 139 140 148 public void addValue(Object value, LocalFieldDesc localField) { 149 stack.add(new ConstraintValue(value, localField)); 150 } 151 152 157 public void addConstraintFieldSubQuery(String field, ActionDesc rd) { 158 stack.add(new ConstraintFieldNameSubQuery(field, rd)); 159 } 160 161 167 public void addParamIndex(int index, int enumType, 168 LocalFieldDesc localField) { 169 stack.add(new ConstraintParamIndex(index,enumType, localField)); 170 } 171 172 179 public void addJoinConstraint(ConstraintJoin join) { 180 if (join.operation == ActionDesc.OP_EQUIJOIN) { 181 stack.add(join); 182 } else { 183 assert join.operation == ActionDesc.OP_LEFTJOIN; 186 outerJoinStack.add(join); 187 } 188 } 189 190 196 public boolean mergeConstraint(Constraint foreignConstraint, int joinOp) { 197 198 stack.addAll(foreignConstraint.stack); 199 outerJoinStack.addAll(foreignConstraint.outerJoinStack); 200 201 return addAnd(foreignConstraint, joinOp); 202 } 203 204 212 private boolean addAnd(Constraint foreignConstraint, int joinOp) { 213 return (joinOp == ActionDesc.OP_EQUIJOIN) 221 222 && !foreignConstraint.isEmptyOrOrderBy(); 226 } 227 228 245 private boolean isEmptyOrOrderBy() { 246 boolean rc = true; 247 for (int i = stack.size() - 1; i >= 0 && rc; ) { 249 ConstraintNode node = (ConstraintNode) stack.get(i); 250 251 if ((node instanceof ConstraintOperation) 252 && ((((ConstraintOperation) node).operation == ActionDesc.OP_ORDERBY) || 253 (((ConstraintOperation) node).operation == ActionDesc.OP_ORDERBY_DESC))) { 254 if ((i > 0) && ( 255 stack.get(i - 1) instanceof ConstraintFieldName || 256 stack.get(i - 1) instanceof ConstraintFieldDesc)) { 257 i--; 259 if ((i > 0) && (stack.get(i - 1) instanceof ConstraintValue)) { 260 i--; 262 } 263 } else { 264 rc = false; 265 } 266 } else { 267 rc = false; 268 } 269 270 i--; 272 } 273 return rc; 274 } 275 276 280 public List getConstraints() { 281 return stack; 282 } 283 284 288 public List getOuterJoinConstraints() { 289 return outerJoinStack; 290 } 291 292 public Constraint() { 293 stack = new ArrayList (); 294 outerJoinStack = new ArrayList (); 295 } 296 297 } 298 | Popular Tags |