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.SQLBuffer;
24 import org.apache.openjpa.jdbc.sql.Select;
25 import org.apache.openjpa.kernel.exps.ExpressionVisitor;
26
27 /**
28 * Combines two expressions.
29 *
30 * @author Abe White
31 */
32 class AndExpression
33 implements Exp {
34
35 private final Exp _exp1;
36 private final Exp _exp2;
37
38 /**
39 * Constructor. Supply the expressions to combine.
40 */
41 public AndExpression(Exp exp1, Exp exp2) {
42 _exp1 = exp1;
43 _exp2 = exp2;
44 }
45
46 public ExpState initialize(Select sel, ExpContext ctx, Map contains) {
47 ExpState s1 = _exp1.initialize(sel, ctx, contains);
48 ExpState s2 = _exp2.initialize(sel, ctx, contains);
49 return new BinaryOpExpState(sel.and(s1.joins, s2.joins), s1, s2);
50 }
51
52 public void appendTo(Select sel, ExpContext ctx, ExpState state,
53 SQLBuffer buf) {
54 BinaryOpExpState bstate = (BinaryOpExpState) state;
55 boolean paren1 = _exp1 instanceof OrExpression;
56 boolean paren2 = _exp2 instanceof OrExpression;
57 if (paren1)
58 buf.append("(");
59 _exp1.appendTo(sel, ctx, bstate.state1, buf);
60 if (paren1)
61 buf.append(")");
62 buf.append(" AND ");
63 if (paren2)
64 buf.append("(");
65 _exp2.appendTo(sel, ctx, bstate.state2, buf);
66 if (paren2)
67 buf.append(")");
68 sel.append(buf, state.joins);
69 }
70
71 public void selectColumns(Select sel, ExpContext ctx, ExpState state,
72 boolean pks) {
73 BinaryOpExpState bstate = (BinaryOpExpState) state;
74 _exp1.selectColumns(sel, ctx, bstate.state1, pks);
75 _exp2.selectColumns(sel, ctx, bstate.state2, pks);
76 }
77
78 public void acceptVisit(ExpressionVisitor visitor) {
79 visitor.enter(this);
80 _exp1.acceptVisit(visitor);
81 _exp2.acceptVisit(visitor);
82 visitor.exit(this);
83 }
84 }