KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.CharBuf;
15 import com.versant.core.jdbc.sql.SqlDriver;
16
17 import com.versant.core.common.BindingSupportImpl;
18
19 /**
20  * An 'or' expression.
21  */

22 public class OrExp extends SqlExp {
23
24     public OrExp(SqlExp children) {
25         super(children);
26     }
27
28     public OrExp() {
29     }
30
31     public SqlExp createInstance() {
32         return new OrExp();
33     }
34
35     /**
36      * If this expression is added to an AndExp should it be enclosed in
37      * parenthesis?
38      */

39     public boolean requiresParensInAnd() {
40         return true;
41     }
42
43     /**
44      * Append SQL for this node to s.
45      *
46      * @param driver The driver being used
47      * @param s Append the SQL here
48      * @param leftSibling
49      */

50     public void appendSQLImp(SqlDriver driver, CharBuf s, SqlExp leftSibling) {
51         childList.appendSQL(driver, s, null);
52         SqlExp prev = childList;
53         for (SqlExp e = childList.next; e != null; prev = e, e = e.next) {
54             s.append(" OR ");
55             e.appendSQL(driver, s, prev);
56         }
57     }
58
59     /**
60      * Normalize this node i.e. transform it into its simplist possible form.
61      * This will turn sub selects into joins and so on. Return expression to
62      * replace us with or null if no change.
63      */

64     public SqlExp normalize(SqlDriver driver, SelectExp sel, boolean convertExists) {
65         super.normalize(driver, sel, convertExists);
66
67         if (!driver.isConvertExistsToDistinctJoin()) return null;
68
69         SqlExp p = null;
70         for (SqlExp e = childList; e != null;) {
71             int cj = e.getConvertToJoin();
72             if (cj >= SqlExp.YES) {
73                 // convert to outer join with distinct
74
boolean not = cj == SqlExp.YES_DISTINCT_NOT;
75                 SelectExp sub;
76                 if (not)
77                     sub = (SelectExp) (e.childList.childList);
78                 else
79                     sub = (SelectExp) (e.childList);
80                 Join j = new Join();
81                 j.selectExp = sub;
82                 j.exp = sub.subSelectJoinExp;
83                 sub.subSelectJoinExp = null;
84                 sel.addJoinMerge(j);
85                 if (!sel.distinct) {
86                     sel.distinct = cj >= SqlExp.YES_DISTINCT;
87                 }
88                 sub.setOuterRec();
89                 if (not) {
90                     if (sub.whereExp != null) {
91                         throw BindingSupportImpl.getInstance().fatalDatastore(
92                                 "Query too complex for " + driver.getName());
93                     }
94                     // replace us in childlist with exp to check row not matched
95
SqlExp ne = sub.getOuterJoinNotMatchedExp();
96                     if (p == null) {
97                         p = childList = ne;
98                     } else {
99                         p = p.next = ne;
100                     }
101                     e = ne.next = e.next;
102                 } else {
103                     SqlExp ne = sub.whereExp;
104                     if (ne == null)
105                         ne = sub.getOuterJoinMatchedExp();
106                     else
107                         sub.whereExp = null;
108                     // replace us in childlist with ne
109
if (p == null)
110                         p = childList = ne;
111                     else
112                         p = p.next = ne;
113                     e = ne.next = e.next;
114                 }
115             } else {
116                 p = e;
117                 e = e.next;
118             }
119         }
120
121         // see if we can get rid of the OrExp
122
if (childList == null) return null; // should return dummy exp
123
if (childList.next == null) return childList;
124         return null;
125     }
126
127 }
128
129
Popular Tags