KickJava   Java API By Example, From Geeks To Geeks.

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


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.Hashtable JavaDoc;
47 import java.util.Vector JavaDoc;
48
49 import java.sql.ResultSetMetaData JavaDoc;
50 import java.sql.SQLException JavaDoc;
51
52 import com.quadcap.sql.index.Btree;
53
54 import com.quadcap.sql.types.Value;
55 import com.quadcap.sql.types.ValueBoolean;
56
57 import com.quadcap.util.Debug;
58
59 /**
60  * A SQL <b>VIEW</b>.
61  *
62  * @author Stan Bailes
63  */

64 public class View extends TupleImpl implements Relation, Externalizable JavaDoc {
65     Vector JavaDoc viewColumns;
66     TableExpression select;
67
68     static final int NOCHECK = -1;
69     static final int CASCADED = 0;
70     static final int LOCAL = 1;
71     
72     int check;
73
74     /**
75      * Default constructor
76      */

77     public View() {}
78
79     /**
80      * Explicit constructor from name, columns, select expression,
81      * VIEW CHECK state
82      */

83     public View(String JavaDoc name, Vector JavaDoc columns, TableExpression select,
84         int check)
85     {
86     super(name);
87     this.viewColumns = columns;
88     this.select = select;
89     this.check = check;
90     }
91
92     /**
93      * Lazy build the view's columns list
94      */

95     public void addColumns(Session session) throws SQLException JavaDoc {
96     if (columns.size() == 0) {
97         Cursor c = getCursor(session, null, null, null);
98         try {
99         addColumns(session, c);
100         } finally {
101         c.close();
102         }
103     }
104     }
105
106     /**
107      * Check a base row to see if it's contained in the view
108      */

109     public void checkRow(Session session, Cursor cursor, Row row) throws SQLException JavaDoc {
110     if ((check != NOCHECK || session.getViewCheck())
111             && select.isUpdatable()) {
112             session.setViewCheck();
113         StaticCursor s = new StaticCursor(session, this, row);
114         s.next();
115         Expression w = select.getWhere();
116         if (w != null) {
117                 Value v = w.getValue(session, s);
118         if (v instanceof ValueBoolean && ((ValueBoolean)v).isTrue()) {
119             // ok
120
} else {
121             throw new SQLException JavaDoc("View check option violation", "44000");
122         }
123         }
124     }
125     }
126
127     /**
128      * Externalizable.readExternal(): Read me from a stream.
129      */

130     public void readExternal(ObjectInput JavaDoc in)
131     throws IOException JavaDoc, ClassNotFoundException JavaDoc
132     {
133     super.readExternal(in);
134     this.viewColumns = (Vector JavaDoc)in.readObject();
135     this.select = (TableExpression)in.readObject();
136     this.check = in.readInt();
137     }
138     
139     /**
140      * Externalizable.writeExternal(): Write me to a stream.
141      */

142     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
143     super.writeExternal(out);
144     out.writeObject(viewColumns);
145     out.writeObject(select);
146     out.writeInt(check);
147     }
148
149     /**
150      * Get the view's cursor
151      */

152     public Cursor getCursor(Session session, Expression where,
153                 String JavaDoc asName, Cursor cursor)
154     throws SQLException JavaDoc
155     {
156     Cursor c = select.getCursor(session, cursor);
157
158     String JavaDoc qualName = asName;
159     if (qualName == null) qualName = getName();
160     String JavaDoc qPrefix = qualName;
161     if (qPrefix.length() > 0) qPrefix = qPrefix + ".";
162
163     Vector JavaDoc cols = viewColumns;
164     if (cols == null) {
165         cols = new Vector JavaDoc();
166         Hashtable JavaDoc t = new Hashtable JavaDoc();
167         for (int i = 1; i <= c.getColumnCount(); i++) {
168         String JavaDoc name = c.getColumn(i).getName();
169                 int idx = name.lastIndexOf('.');
170                 if (idx >= 0) name = name.substring(idx+1);
171                 name = qPrefix + name;
172         if (t.get(name) != null) {
173             throw new SQLException JavaDoc(
174             "A view column list must be specified if multiple " +
175             "columns have the same name: " + name, "42000");
176         }
177         cols.addElement(name);
178         t.put(name, name);
179         }
180     } else {
181         cols = new Vector JavaDoc();
182         for (int i = 0; i < viewColumns.size(); i++) {
183         cols.addElement(qPrefix + viewColumns.elementAt(i).toString());
184         }
185     }
186     c = new ViewCursor(session, this, c, cols);
187     return c;
188     }
189
190     public void insertRow(Session session, Row row)
191     throws SQLException JavaDoc, IOException JavaDoc
192     {
193     Cursor c = getCursor(session, null, null, null);
194         try {
195             session.clearViewCheck();
196             c.insertRow(row);
197         } finally {
198             c.close();
199         }
200     }
201
202     public boolean isUpdatable() {
203     return select.isUpdatable();
204     }
205
206     public boolean hasBlobs() {
207         return false; // XXX test insert into view with blobs.
208
}
209     
210     public Vector JavaDoc getBaseTables() {
211     Vector JavaDoc v = new Vector JavaDoc();
212     select.getBaseTables(v);
213     return v;
214     }
215
216     public Expression getViewExpression() {
217         return select;
218     }
219
220     public String JavaDoc getType() { return "VIEW"; }
221
222     public void delete(Session session) {}
223
224     public String JavaDoc toString() {
225         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("VIEW ");
226         sb.append(getName());
227         if (columns != null) {
228             sb.append('(');
229             for (int i = 0; i < columns.size(); i++) {
230                 if (i > 0) sb.append(',');
231                 Column col = (Column)columns.get(i);
232                 sb.append(col.getShortName());
233             }
234             sb.append(')');
235         }
236         sb.append(" AS ");
237         sb.append(select.toString());
238         return sb.toString();
239     }
240     
241 }
242
Popular Tags