KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.Externalizable JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.ObjectInput JavaDoc;
44 import java.io.ObjectOutput JavaDoc;
45
46 import java.util.Vector JavaDoc;
47
48 import java.sql.SQLException JavaDoc;
49
50 import antlr.RecognitionException;
51
52 import com.quadcap.sql.types.Value;
53
54 import com.quadcap.util.Debug;
55
56 /**
57  * This might be a vector (row value constructor) or a cursor (table value
58  * constructor)
59  *
60  * @author Stan Bailes
61  */

62 public class VectorExpression extends TableExpression
63     implements Externalizable JavaDoc
64 {
65     Vector JavaDoc expressions = new Vector JavaDoc();
66     int rank = 1;
67
68     public VectorExpression() {}
69
70     public void addElement(Expression expression) throws RecognitionException {
71     if (expressions.size() == 0) {
72         // determine rank
73
rank = expression.rank() + 1;
74     } else {
75         if (expression.rank() != rank-1) {
76         throw new RecognitionException(
77             "Mismatched ranks in table expression");
78         }
79     }
80     expressions.addElement(expression);
81     }
82     
83     public int rank() { return rank; }
84
85     public boolean isUpdatable() { return false; }
86
87     public void getBaseTables(Vector JavaDoc v) {}
88
89     public int size() { return expressions.size(); }
90
91     public final Expression get(int i) {
92         return (Expression)expressions.get(i);
93     }
94
95     public Row getValues(Session session, Cursor cursor) throws SQLException JavaDoc {
96     if (rank == 1) {
97         Row values = new Row();
98         for (int i = 0; i < expressions.size(); i++) {
99         Expression e = (Expression)expressions.elementAt(i);
100         values.addElement(e.getValue(session, cursor));
101         }
102         return values;
103     } else if (rank == 2) {
104         Expression e = (Expression)expressions.elementAt(0);
105         return e.getValues(session, cursor);
106     }
107     return null;
108     }
109
110     public Cursor getCursor(Session session, Cursor cursor)
111         throws SQLException JavaDoc
112     {
113     if (rank == 1) {
114         Vector JavaDoc v = new Vector JavaDoc();
115         v.addElement(getValues(session, cursor));
116         return new StaticCursor(session, v);
117     } else if (rank == 2) {
118         Vector JavaDoc cursorVec = new Vector JavaDoc();
119         for (int i = 0; i < expressions.size(); i++) {
120         Expression e = (Expression)expressions.elementAt(i);
121         cursorVec.addElement(e.getValues(session, cursor));
122         }
123         return new StaticCursor(session, cursorVec);
124     } else {
125         return null;
126     }
127     }
128
129     public void invert() {
130     throw new RuntimeException JavaDoc("Can't invert vector expression");
131     }
132
133     public void visitSubExpressions(ExpressionVisitor ev) {
134     for (int i = 0; i < expressions.size(); i++) {
135         Expression ex = (Expression)expressions.elementAt(i);
136         ev.visit(ex);
137     }
138     }
139
140     public String JavaDoc toString() {
141     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
142     for (int i = 0; i < expressions.size(); i++) {
143         if (i > 0) sb.append(", ");
144         sb.append(expressions.elementAt(i).toString());
145     }
146     return sb.toString();
147     }
148
149     public void readExternal(ObjectInput JavaDoc in)
150     throws IOException JavaDoc, ClassNotFoundException JavaDoc
151     {
152     expressions = (Vector JavaDoc)in.readObject();
153     }
154     
155     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
156     out.writeObject(expressions);
157     }
158
159     /**
160      * Return the (one-based) parameter from this VectorExpression which
161      * is assumed to be a simple list of parameterized expressions
162      */

163     public Value getParameter(Session session, int pos) {
164         try {
165             if (rank == 2) {
166                 VectorExpression v = (VectorExpression)expressions.get(0);
167                 Expression e = v.get(pos-1);
168                 return e.getValue(session, null);
169             }
170         } catch (Throwable JavaDoc t) {}
171         return null;
172     }
173
174     //#ifdef DEBUG
175
public String JavaDoc name() { return "[]"; }
176     //#endif
177
}
178
Popular Tags