01: /**********************************************************************
02: Copyright (c) 2002 Mike Martin (TJDO) and others. All rights reserved.
03: Licensed under the Apache License, Version 2.0 (the "License");
04: you may not use this file except in compliance with the License.
05: You may obtain a copy of the License at
06:
07: http://www.apache.org/licenses/LICENSE-2.0
08:
09: Unless required by applicable law or agreed to in writing, software
10: distributed under the License is distributed on an "AS IS" BASIS,
11: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: See the License for the specific language governing permissions and
13: limitations under the License.
14:
15: Contributors:
16: 2003 Andy Jefferson - coding standards
17: ...
18: **********************************************************************/package org.jpox.store.expression;
19:
20: /**
21: * Perform a exists function on a Query Expression returning a true boolean value if one or more elements are retrieved.
22: *
23: * @version $Revision: 1.6 $
24: **/
25: public class ExistsExpression extends BooleanExpression {
26: private final QueryExpression argumentExpression;
27: private final boolean truthTest;
28:
29: /**
30: * Constructs a expression for existence of the argument expression.
31: * @param qs the QueryExpression
32: * @param argumentExpression the QueryExpression returning records to the exists function
33: */
34: public ExistsExpression(QueryExpression qs,
35: QueryExpression argumentExpression) {
36: this (qs, argumentExpression, true);
37: }
38:
39: /**
40: * Constructs a expression for existence (or not) of the argument expression.
41: * Create an "EXISTS", or "NOT EXISTS" depending on the truth flag.
42: * @param qs the QueryExpression
43: * @param argumentExpression the QueryExpression returning records to the exists function
44: * @param truthTest true to perform a "exists", and false to perform a "not exists"
45: */
46: public ExistsExpression(QueryExpression qs,
47: QueryExpression argumentExpression, boolean truthTest) {
48: super (qs);
49:
50: this .argumentExpression = argumentExpression;
51: this .truthTest = truthTest;
52: if (!truthTest) {
53: st.append("NOT ");
54: }
55:
56: argumentExpression.setExistsSubQuery(true);
57: st.append("EXISTS (").append(argumentExpression).append(')');
58: }
59:
60: public BooleanExpression not() {
61: return new ExistsExpression(qs, argumentExpression, !truthTest);
62: }
63:
64: public BooleanExpression and(ScalarExpression expr) {
65: if (expr instanceof ExistsExpression) {
66: ExistsExpression expr2 = (ExistsExpression) expr;
67: if (expr2.qs == this .argumentExpression) {
68: // TODO What condition is this trying to cater for ???? Example???
69: this .argumentExpression.andCondition(expr2);
70: return this ;
71: } else if (this .qs == expr2.argumentExpression) {
72: // TODO What condition is this trying to cater for ???? Example???
73: expr2.argumentExpression.andCondition(this );
74: return expr2;
75: }
76: return super .and(expr);
77: } else if (expr.qs != this .qs
78: && expr.qs != this .argumentExpression) {
79: // TODO What condition is this trying to cater for ???? Example???
80: return super .and(expr);
81: } else {
82: // TODO What condition is this trying to cater for ???? Example???
83: argumentExpression.andCondition((BooleanExpression) expr,
84: true);
85: return this;
86: }
87: }
88: }
|