01: /*
02: * Copyright (c) 1998 - 2005 Versant Corporation
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * Versant Corporation - initial API and implementation
10: */
11: package com.versant.core.jdbc.sql.exp;
12:
13: import com.versant.core.jdbc.sql.SqlDriver;
14: import com.versant.core.util.CharBuf;
15: import com.versant.core.jdo.query.VarNodeIF;
16:
17: import java.util.Map;
18:
19: /**
20: * An exists (....) expression.
21: */
22: public class ExistsExp extends UnaryExp {
23: /**
24: * If this exp was create for a oneToMany scenario.
25: */
26: private boolean distinct;
27: private VarNodeIF forVariable;
28:
29: public ExistsExp(SqlExp child, boolean requiresDistinct) {
30: super (child);
31: this .distinct = requiresDistinct;
32: }
33:
34: public ExistsExp(SqlExp child, boolean requiresDistinct,
35: VarNodeIF var) {
36: this (child, requiresDistinct);
37: this .forVariable = var;
38: }
39:
40: public ExistsExp() {
41: }
42:
43: public SqlExp createInstance() {
44: return new ExistsExp();
45: }
46:
47: public SqlExp getClone(SqlExp clone, Map cloneMap) {
48: super .getClone(clone, cloneMap);
49:
50: ((ExistsExp) clone).distinct = distinct;
51:
52: return clone;
53: }
54:
55: /**
56: * Append SQL for this node to s.
57: *
58: * @param driver The driver being used
59: * @param s Append the SQL here
60: * @param leftSibling
61: */
62: public void appendSQLImp(SqlDriver driver, CharBuf s,
63: SqlExp leftSibling) {
64: s.append("EXISTS (");
65: childList.appendSQL(driver, s, null);
66: s.append(')');
67: }
68:
69: /**
70: * Can this expression be removed and its child be converted into a join?
71: * @see ExistsExp
72: */
73: public int getConvertToJoin() {
74: if (!distinct)
75: return YES;
76: if (forVariable != null
77: && ((VarNodeIF) forVariable).isUsedInProjection())
78: return YES;
79: return YES_DISTINCT;
80: }
81:
82: public String toString() {
83: return super .toString() + " oneToMany " + distinct;
84: }
85:
86: }
|