KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > sql > exp > ExistsExp


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc.sql.exp;
13
14 import com.versant.core.jdbc.sql.SqlDriver;
15 import com.versant.core.util.CharBuf;
16 import com.versant.core.jdo.query.VarNodeIF;
17
18 import java.util.Map JavaDoc;
19
20 /**
21  * An exists (....) expression.
22  */

23 public class ExistsExp extends UnaryExp {
24     /**
25      * If this exp was create for a oneToMany scenario.
26      */

27     private boolean distinct;
28     private VarNodeIF forVariable;
29
30     public ExistsExp(SqlExp child, boolean requiresDistinct) {
31         super(child);
32         this.distinct = requiresDistinct;
33     }
34
35     public ExistsExp(SqlExp child, boolean requiresDistinct, 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 JavaDoc 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, SqlExp leftSibling) {
63         s.append("EXISTS (");
64         childList.appendSQL(driver, s, null);
65         s.append(')');
66     }
67
68     /**
69      * Can this expression be removed and its child be converted into a join?
70      * @see ExistsExp
71      */

72     public int getConvertToJoin() {
73         if (!distinct) return YES;
74         if (forVariable != null && ((VarNodeIF)forVariable).isUsedInProjection()) return YES;
75         return YES_DISTINCT;
76     }
77
78     public String JavaDoc toString() {
79         return super.toString() + " oneToMany " + distinct;
80     }
81
82 }
83
84
Popular Tags