1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.openjpa.jdbc.kernel.exps;
20
21 import java.util.Map;
22
23 import org.apache.openjpa.jdbc.sql.Joins;
24 import org.apache.openjpa.jdbc.sql.SQLBuffer;
25 import org.apache.openjpa.jdbc.sql.Select;
26 import org.apache.openjpa.kernel.exps.ExpressionVisitor;
27
28 /**
29 * Tests whether a value is IN a subquery.
30 *
31 * @author Abe White
32 */
33 class InSubQExpression
34 implements Exp {
35
36 private final Val _val;
37 private final SubQ _sub;
38
39 /**
40 * Constructor. Supply the value to test and the subquery.
41 */
42 public InSubQExpression(Val val, SubQ sub) {
43 _val = val;
44 _sub = sub;
45 }
46
47 public ExpState initialize(Select sel, ExpContext ctx, Map contains) {
48 ExpState subqState = _sub.initialize(sel, ctx, 0);
49 ExpState valueState = _val.initialize(sel, ctx, 0);
50 return new InSubQExpState(valueState.joins, subqState, valueState);
51 }
52
53 /**
54 * Expression state.
55 */
56 private static class InSubQExpState
57 extends ExpState {
58
59 public final ExpState subqState;
60 public final ExpState valueState;
61
62 public InSubQExpState(Joins joins, ExpState subqState,
63 ExpState valueState) {
64 super(joins);
65 this.subqState = subqState;
66 this.valueState = valueState;
67 }
68 }
69
70 public void appendTo(Select sel, ExpContext ctx, ExpState state,
71 SQLBuffer buf) {
72 InSubQExpState istate = (InSubQExpState) state;
73 _sub.calculateValue(sel, ctx, istate.subqState, null, null);
74 _val.calculateValue(sel, ctx, istate.valueState, null, null);
75 _val.appendTo(sel, ctx, istate.valueState, buf, 0);
76 buf.append(" IN ");
77 _sub.appendTo(sel, ctx, istate.valueState, buf, 0);
78 }
79
80 public void selectColumns(Select sel, ExpContext ctx, ExpState state,
81 boolean pks) {
82 InSubQExpState istate = (InSubQExpState) state;
83 _sub.selectColumns(sel, ctx, istate.subqState, pks);
84 _val.selectColumns(sel, ctx, istate.valueState, true);
85 }
86
87 public void acceptVisit(ExpressionVisitor visitor) {
88 visitor.enter(this);
89 _val.acceptVisit(visitor);
90 _sub.acceptVisit(visitor);
91 visitor.exit(this);
92 }
93 }