KickJava   Java API By Example, From Geeks To Geeks.

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


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 'and' expression.
21  */

22 public class AndExp extends SqlExp {
23
24     public AndExp() {
25     }
26
27     public AndExp(SqlExp children) {
28         super(children);
29     }
30
31     public SqlExp createInstance() {
32         return new AndExp();
33     }
34
35     /**
36      * Append SQL for this node to s.
37      *
38      * @param driver The driver being used
39      * @param s Append the SQL here
40      * @param leftSibling
41      */

42     public void appendSQLImp(SqlDriver driver, CharBuf s, SqlExp leftSibling) {
43         appendSQL(childList, driver, s, null);
44         SqlExp prev = childList;
45         for (SqlExp e = childList.next; e != null; prev = e, e = e.next) {
46             s.append(" AND ");
47             appendSQL(e, driver, s, prev);
48         }
49     }
50
51     private void appendSQL(SqlExp e, SqlDriver driver, CharBuf s,
52             SqlExp leftSibling) {
53         boolean p = e.requiresParensInAnd();
54         if (p) s.append('(');
55         e.appendSQL(driver, s, leftSibling);
56         if (p) s.append(')');
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 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                 }
81                 Join j = new Join();
82                 j.selectExp = sub;
83                 j.exp = sub.subSelectJoinExp;
84                 sub.subSelectJoinExp = null;
85                 sel.addJoinMerge(j);
86                 if (!sel.distinct) {
87                     sel.distinct = cj >= SqlExp.YES_DISTINCT;
88                 }
89                 if (not) {
90                     sub.setOuterRec();
91                     if (sub.whereExp != null) {
92                         throw BindingSupportImpl.getInstance().fatalDatastore("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                     }
109                     // replace us in childlist with ne
110
if (p == null) {
111                         p = childList = ne;
112                     } else {
113                         p = p.next = ne;
114                     }
115                     e = ne.next = e.next;
116                 }
117             } else {
118                 p = e;
119                 e = e.next;
120             }
121         }
122
123         // see if we can get rid of the AndExp
124
if (childList == null) return null; // should return dummy exp
125
if (childList.next == null) return childList;
126         return null;
127     }
128
129 }
130
Popular Tags