KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > sqlstore > sql > constraint > Constraint


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * Constraint.java
26  *
27  * Created on March 3, 2000
28  *
29  */

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 JavaDoc;
38 import java.util.List JavaDoc;
39
40
41 /**
42  */

43 public class Constraint extends Object JavaDoc {
44     /**
45      * The stack that contains all constraints beside join constraints.
46      * Join constraints are handled by a separate stack.
47      */

48     public List JavaDoc stack;
49
50     /**
51      * The stack that contains outer join constraints. All the elements of this stack
52      * are instance of {@link ConstraintJoin}. Outer join constraints can be appended
53      * to the query w/o changing the semantic.
54      */

55     private List JavaDoc outerJoinStack;
56
57     /**
58      * The stack that contains order by constraints. We would like to
59      * separate Order By constraints from the main stack, but they must
60      * be merged with all other constraints to preserve the order of
61      * constraints from different stacks. E.g.
62      * "order by firstname ascending, department.name ascending"
63      *
64      * @see #isEmptyOrOrderBy
65      */

66 // private List orderByStack;
67

68     /**
69      * Adds a field to the constraint stack.
70      *
71      * AddField creates a ConstraintField node for the indicated
72      * named field, optionally including an operation descriptor
73      * and adds it to the constraint stack.
74      *
75      * @param name
76      * The name parameter specifies the name of the field to be
77      * added to the constrant stack.
78      *
79      * @param desc
80      * The desc parameter specifies an operation descriptor describing
81      * what is to be done with the field named by the name parameter.
82      */

83     public void addField(String JavaDoc name, ActionDesc desc) {
84         stack.add(new ConstraintFieldName(name, desc));
85     }
86
87     /**
88      * Adds a field to the constraint stack.
89      *
90      * AddField creates a ConstraintFieldDesc node for the indicated
91      * field descriptor and adds it to the constraint stack.
92      *
93      * @param desc
94      * The Desc parameter is the field descriptor to be
95      * added to the constrant stack.
96      */

97     public void addField(LocalFieldDesc desc) {
98         stack.add(new ConstraintFieldDesc(desc));
99     }
100
101     /**
102      * Adds a field to the constraint stack.
103      *
104      * AddField creates a ConstraintFieldDesc node for the indicated
105      * field descriptor and adds it to the constraint stack.
106      *
107      * @param desc
108      * The Desc parameter is the field descriptor to be
109      * added to the constrant stack.
110      *
111      * @param plan
112      * The query plan to which this desc belongs
113      */

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 JavaDoc name, ActionDesc desc) {
123         stack.add(new ConstraintForeignFieldName(name, desc));
124     }
125
126     /**
127      * Adds an operation to the constraint stack.
128      *
129      * AddOperation creates a ConstraintOperation node whose operation is
130      * operation and adds it to the constraint stack.
131      *
132      * @param operation
133      * The operation parameter specifies the operation to be added to
134      * the constrant stack.
135      */

136     public void addOperation(int operation) {
137         stack.add(new ConstraintOperation(operation));
138     }
139
140     /**
141      * Adds a data value to the constraint stack. Creates a ConstraintValue
142      * node whose value is value and adds it to the constraint stack.
143      * @param value The value to be added to the constrant stack.
144      * @param localField The localField to which this value is bound.
145      * Please note that localField can be null for values involved in
146      * complex expressions in a query.
147      */

148     public void addValue(Object JavaDoc value, LocalFieldDesc localField) {
149         stack.add(new ConstraintValue(value, localField));
150     }
151
152     /**
153      * Adds a subquery constraint on the stack.
154      * @param field The field on which subquery constraint is added.
155      * @param rd Retrieve descriptor corresponding to the subquery.
156      */

157     public void addConstraintFieldSubQuery(String JavaDoc field, ActionDesc rd) {
158         stack.add(new ConstraintFieldNameSubQuery(field, rd));
159     }
160
161     /**
162      * Adds the index of a parameter to the stack.
163      * @param index the parameter index.
164      * @param enumType the type for this parameter.
165      * @param localField the localField to which this parameter is bound.
166      */

167     public void addParamIndex(int index, int enumType,
168                                     LocalFieldDesc localField) {
169         stack.add(new ConstraintParamIndex(index,enumType, localField));
170     }
171
172     /**
173      * Adds specified join constraint. Equi joins are added to the
174      * constraint stack, outer joins are added to a separate stack, the
175      * <code>outerJoinStack</code>.
176      *
177      * @param join The join constraint to be added.
178      */

179     public void addJoinConstraint(ConstraintJoin join) {
180         if (join.operation == ActionDesc.OP_EQUIJOIN) {
181             stack.add(join);
182         } else {
183             // The current logic is written with the assumption that a join
184
// that is not an equi join is always a left join
185
assert join.operation == ActionDesc.OP_LEFTJOIN;
186             outerJoinStack.add(join);
187         }
188     }
189
190     /**
191      * Merges the stack with the specified foreign constraint stack.
192      * @param foreignConstraint The constraint to be merged.
193      * @param joinOp Join operation as defined in {@link ActionDesc}.
194      * @return True, if we need to add an additional "AND" constraint.
195      */

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     /**
205      * Decides, if we need to add an additional "AND" constraint to
206      * the stack <em>after</em> merging the foreign stack.
207      *
208      * @param foreignConstraint Constraint to be joined.
209      * @param joinOp Join operator.
210      * @return True, if we need to add an additional "AND" constraint.
211      */

212     private boolean addAnd(Constraint foreignConstraint, int joinOp) {
213         // Add "AND" constraints for equi-joins only.
214
// * Don't add an "AND" constraint for outer joins. Outer joins
215
// don't contribute to the "where"-clause in ANSI-case. In the
216
// non-ANSI case, outer joins can be appended to the query w/o
217
// changing the semantic.
218
// * Don't add an "AND" constaint for non-relationship joins,
219
// as no additional join constraint is added in this case.
220
return (joinOp == ActionDesc.OP_EQUIJOIN)
221
222         // Never add an "AND" constraint, if the foreign stack is
223
// empty or contains "ORDER_BY" constraints only, as "ORDER_BY"
224
// constraints don't contribute to the "where"-clause.
225
&& !foreignConstraint.isEmptyOrOrderBy();
226     }
227
228     /**
229      * Checks, if the constraint stack is empty or contains "ORDER_BY"
230      * constraints only. "ORDER_BY" constraints are recognized as an
231      * ORDER_BY operation followed by a field name constraint, and
232      * an optional Value constraint giving the position of the Order
233      * By constraint.
234      * <em>NOTE:</em> The value constraints giving the position for
235      * the order by constraints are currently not generated by the
236      * query compiler. Order by constraints stay in the correct order
237      * because of two reasons
238      * <ul>
239      * <li>the way, constraints are processed by the query compiler</li>
240      * <li>the way constraint stacks are joined in
241      * {@link com.sun.jdo.spi.persistence.support.sqlstore.sql.generator.SelectQueryPlan#processForeignConstraints}.
242      * </li>
243      * </ul>
244      */

245     private boolean isEmptyOrOrderBy() {
246         boolean rc = true;
247         // Abort the loop at first possible opportunity.
248
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                     // Order By constraint.
258
i--;
259                     if ((i > 0) && (stack.get(i - 1) instanceof ConstraintValue)) {
260                         // Optional Value constraint.
261
i--;
262                     }
263                 } else {
264                     rc = false;
265                 }
266             } else {
267                 rc = false;
268             }
269
270             // Check the next constraint if any.
271
i--;
272         }
273         return rc;
274     }
275
276     /**
277       * Gets the where clause constraints for this Constraint
278       * @return The where clause constraints for this Constraint
279       */

280      public List JavaDoc getConstraints() {
281          return stack;
282      }
283
284     /**
285       * Gets the outer join constraints for this Constraint
286       * @return The outer join constraints for this Constraint
287       */

288      public List JavaDoc getOuterJoinConstraints() {
289          return outerJoinStack;
290      }
291
292     public Constraint() {
293         stack = new ArrayList JavaDoc();
294         outerJoinStack = new ArrayList JavaDoc();
295     }
296
297 }
298
Popular Tags