KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > Analyze


1 package com.quadcap.sql;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.util.Vector JavaDoc;
42
43 import java.sql.SQLException JavaDoc;
44
45 import com.quadcap.sql.types.Op;
46
47 import com.quadcap.util.Debug;
48
49 /**
50  * Join planner.
51  *
52  * @author Stan Bailes
53  */

54 public class Analyze {
55     Session session;
56     Expression expr;
57     BinaryExpression b = null;
58     NameExpression n = null;
59     ValueExpression v = null;
60     //#ifdef DEBUG
61
String JavaDoc prefix = "";
62     //#endif
63

64     public Analyze(Session session, Expression expr) {
65         this.session = session;
66         this.expr = expr;
67         if (expr instanceof BinaryExpression) b = (BinaryExpression)expr;
68         if (expr instanceof NameExpression) n = (NameExpression)expr;
69         if (expr instanceof ValueExpression) v = (ValueExpression)expr;
70     }
71
72     public void getConjunctives(Vector JavaDoc v) {
73         if (b != null) {
74             if (b.not) {
75                 // XXX could get fancy and try to DeMorgan'ize it.
76
v.addElement(expr);
77             } else {
78                 if (b.op == Op.AND) {
79                     analyze(b.e).getConjunctives(v);
80                     analyze(b.f).getConjunctives(v);
81                 } else {
82                     v.addElement(expr);
83                 }
84             }
85         } else {
86             v.addElement(expr);
87         }
88     }
89
90     public boolean isConstant() {
91         return expr instanceof ValueExpression;
92     }
93
94     public boolean isName() {
95         return n != null;
96     }
97
98     public boolean refersTo(Tuple table) throws SQLException JavaDoc {
99         boolean ret = false;
100         if (n != null) {
101             if (table.getColumn(n.name) != null) ret = true;
102         }
103         return ret;
104     }
105
106     public Analyze analyze(Expression e) {
107         Analyze z = new Analyze(session, e);
108         //#ifdef DEBUG
109
z.prefix = prefix + " ";
110         //#endif
111
return z;
112     }
113
114     public boolean isConstantCompareToTable(Tuple table) throws SQLException JavaDoc {
115         boolean ret = false;
116         if (b != null) {
117             Analyze be = analyze(b.e);
118             Analyze bf = analyze(b.f);
119             if (be.isConstant()) {
120                 if (bf.refersTo(table)) {
121                     ret = true;
122                 }
123             } else if (bf.isConstant()) {
124                 if (be.refersTo(table)) {
125                     ret = true;
126                 }
127             }
128         }
129         //Debug.println("Analyze[" + expr + "].isConstantCompareToTable(" +
130
// table.getName() + ") = " + ret);
131
return ret;
132     }
133
134     public boolean isTermOrConstant(Tuple table) throws SQLException JavaDoc {
135         boolean ret = false;
136         if (n != null) {
137             ret = refersTo(table);
138         } else if (b != null) {
139             Analyze be = analyze(b.e);
140             Analyze bf = analyze(b.f);
141             ret = be.isTermOrConstant(table) && bf.isTermOrConstant(table);
142         } else if (v != null) {
143             ret = true;
144         }
145         return ret;
146     }
147
148     public int[][] getJoinColumns(Cursor ca, Cursor cb) throws SQLException JavaDoc {
149         Vector JavaDoc vec = new Vector JavaDoc();
150         Vector JavaDoc aCols = new Vector JavaDoc();
151         Vector JavaDoc bCols = new Vector JavaDoc();
152         int cnt = 0;
153         int[][] ret = null;
154         getConjunctives(vec);
155         for (int i = 0; i < vec.size(); i++) {
156             Expression ei = (Expression)vec.elementAt(i);
157             if (analyze(ei).isJoinColumn(ca, aCols, cb, bCols)) {
158                 cnt++;
159             }
160         }
161         if (cnt > 0) {
162             ret = new int[2][cnt];
163             for (int i = 0; i < cnt; i++) {
164                 ret[0][i] = ((Column)aCols.elementAt(i)).getColumn();
165                 ret[1][i] = ((Column)bCols.elementAt(i)).getColumn();
166             }
167         }
168         return ret;
169     }
170
171     boolean isJoinColumn(Cursor ca, Vector JavaDoc aCols, Cursor cb, Vector JavaDoc bCols)
172         throws SQLException JavaDoc
173     {
174         if (b == null) return false;
175         if (b.op != Op.EQ) return false;
176         if (!(b.e instanceof NameExpression)) return false;
177         String JavaDoc ename = ((NameExpression)b.e).name;
178         if (!(b.f instanceof NameExpression)) return false;
179         String JavaDoc fname = ((NameExpression)b.f).name;
180         Column cea = ca.getColumn(ename);
181         if (cea != null) {
182             Column cfb = cb.getColumn(fname);
183             if (cfb != null) {
184                 aCols.addElement(cea);
185                 bCols.addElement(cfb);
186                 return true;
187             }
188         }
189         Column ceb = cb.getColumn(ename);
190         if (ceb != null) {
191             Column cfa = ca.getColumn(fname);
192             if (cfa != null) {
193                 aCols.addElement(cfa);
194                 bCols.addElement(ceb);
195                 return true;
196             }
197         }
198         return false;
199     }
200
201     public Expression factorTable(Tuple table) throws SQLException JavaDoc {
202         Expression ret = null;
203         if (table != null) {
204             Vector JavaDoc vec = new Vector JavaDoc();
205             getConjunctives(vec);
206             for (int i = 0; i < vec.size(); i++) {
207                 Expression ei = (Expression)vec.elementAt(i);
208                 if (analyze(ei).isTermOrConstant(table)) {
209                     if (ret == null) {
210                         ret = ei;
211                     } else {
212                         ret = new BinaryExpression(Op.AND, ret, ei);
213                     }
214                 }
215             }
216         }
217 // Debug.println("Analyze[" + expr + "].factorTable(" +
218
// (table == null ? "null" : table.getName()) + ") => " +
219
// ret);
220
return ret;
221         
222     }
223
224     public boolean isJoinExpression(Tuple c) throws SQLException JavaDoc {
225         boolean ret = false;
226         if (isConstant()) {
227             ret = true;
228         } else if (n != null) {
229             if (c.getColumn(n.name) != null) ret = true;
230         } else if (b != null) {
231             if (analyze(b.e).isJoinExpression(c) &&
232                 analyze(b.f).isJoinExpression(c)) ret = true;
233         }
234         return ret;
235     }
236
237     public Expression factorJoinExpression(Tuple c) throws SQLException JavaDoc {
238         Expression ret = null;
239         Vector JavaDoc vec = new Vector JavaDoc();
240         getConjunctives(vec);
241         for (int i = 0; i < vec.size(); i++) {
242             Expression ei = (Expression)vec.elementAt(i);
243             if (analyze(ei).isJoinExpression(c)) {
244                 if (ret == null) {
245                     ret = ei;
246                 } else {
247                     ret = new BinaryExpression(Op.AND, ret, ei);
248                 }
249             }
250         }
251         return ret;
252     }
253
254     //#ifdef DEBUG
255
public String JavaDoc toString() {
256         return prefix + "Analyze[" + expr + "]";
257     }
258     //#endif
259
}
260
Popular Tags