KickJava   Java API By Example, From Geeks To Geeks.

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


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.Enumeration JavaDoc;
47 import java.util.Hashtable JavaDoc;
48 import java.util.Vector JavaDoc;
49
50 import java.sql.SQLException JavaDoc;
51
52 import com.quadcap.sql.types.Type;
53 import com.quadcap.sql.types.TypeAny;
54 import com.quadcap.sql.types.Value;
55
56 import com.quadcap.util.Debug;
57
58 /**
59  * This cursor performs the name-mapping function associated with the
60  * <code>SELECT</code> clause.
61  *
62  * @author Stan Bailes
63  */

64 public class ItemsCursor extends CursorImpl {
65     Cursor cursor;
66     ItemsRow currentRow = null;
67     int[] map = null;
68     Vector JavaDoc items;
69
70     public ItemsCursor(Session session, Cursor cursor, Vector JavaDoc gitems)
71     throws SQLException JavaDoc
72     {
73     super(session, cursor.getName());
74     this.session = session;
75     this.cursor = cursor;
76     this.items = expandWildCard(session, cursor, gitems);
77     this.map = new int[items.size()];
78     for (int i = 0; i < items.size(); i++) {
79         SelectItem item = (SelectItem)items.elementAt(i);
80         Expression e = item.getExpression();
81         if (e instanceof NameExpression) {
82         String JavaDoc name = ((NameExpression)e).getName();
83                 String JavaDoc asName = item.getAsName();
84                 if (asName == null) asName = name;
85         Column col = cursor.getColumn(name);
86         if (col == null) {
87             //#ifdef DEBUG
88
Debug.println(0, cursor.toString());
89             //#endif
90
throw new SQLException JavaDoc("Bad column name: " + name,
91                        "42000");
92         }
93         addColumn(new Column(asName, col));
94         map[i] = col.getColumn();
95         } else {
96                 String JavaDoc asName = item.getAsName();
97                 if (asName == null) {
98                     asName = "Column" + (i+1);
99                 }
100         addColumn(new Column(asName, e.getType(session, cursor)));
101         map[i] = -1;
102         }
103     }
104     }
105
106     final Vector JavaDoc expandWildCard(Session session, Cursor cursor, Vector JavaDoc items)
107     throws SQLException JavaDoc
108     {
109     Vector JavaDoc nitems = new Vector JavaDoc();
110     for (int i = 0; i < items.size(); i++) {
111         SelectItem item = (SelectItem)items.elementAt(i);
112         Expression e = item.getExpression();
113         String JavaDoc name = "Column" + (i + 1);
114         if (e instanceof NameExpression) {
115         name = ((NameExpression)e).getName();
116         if (name.endsWith(".*")) {
117             String JavaDoc rname = name.substring(0, name.length() - 2);
118             String JavaDoc qname =
119                         session.getConnection().resolveName(rname) + ".";
120             rname = rname + ".";
121                     boolean found = false;
122             for (int j = 1; j <= cursor.getColumnCount(); j++) {
123             Column col = cursor.getColumn(j);
124             if (col.isJoinColumn()) continue;
125             String JavaDoc sname = col.getShortName();
126             String JavaDoc cname = col.getName();
127             if (sname.startsWith(rname) || sname.startsWith(qname)) {
128                             found = true;
129                 nitems.addElement(
130                 new SelectItem(new NameExpression(sname)));
131             } else if (cname.startsWith(qname) || cname.startsWith(rname)) {
132                             found = true;
133                 nitems.addElement(
134                 new SelectItem(new NameExpression(cname)));
135             }
136             }
137                     if (!found) {
138                         throw new SQLException JavaDoc("Bad column name: " + name,
139                                                "42000");
140                     }
141         } else {
142             nitems.addElement(item);
143         }
144         } else {
145         nitems.addElement(item);
146         }
147     }
148     return nitems;
149     }
150
151     public Row getEmptyAggregate() throws SQLException JavaDoc {
152     return new ItemsRow(session, items, null, null, map);
153     }
154
155     public void updateRow(Row row) throws SQLException JavaDoc {
156     for (int i = 1; i <= map.length; i++) {
157         Value v = row.item(i);
158         currentRow.set(i, v);
159     }
160     cursor.updateRow(currentRow.getBaseRow());
161     }
162
163     public void insertRow(Row row) throws SQLException JavaDoc {
164     if (currentRow == null) {
165         makeCurrentRow();
166     }
167     for (int i = 1; i <= map.length; i++) {
168         Value v = row.item(i);
169         currentRow.set(i, v);
170     }
171     cursor.insertRow(currentRow.getBaseRow());
172     }
173
174     public void deleteRow() throws SQLException JavaDoc {
175     cursor.deleteRow();
176     }
177
178     public boolean isWritable(int column) throws SQLException JavaDoc {
179     return cursor.isWritable(map[column-1]);
180     }
181
182     public void beforeFirst() throws SQLException JavaDoc {
183     cursor.beforeFirst();
184     }
185
186     public void afterLast() throws SQLException JavaDoc {
187     cursor.afterLast();
188     }
189
190     final void makeFirstRow() throws SQLException JavaDoc {
191     Row r = cursor.getRow();
192     currentRow = new ItemsRow(session, items, cursor, r, map);
193     for (int i = 0; i < map.length; i++) {
194         if (map[i] == -1) {
195         Column col = getColumn(i+1);
196         Value v = currentRow.item(i+1);
197         col.setType(v.getType());
198         }
199     }
200     }
201
202     final void makeCurrentRow() throws SQLException JavaDoc {
203     currentRow = new ItemsRow(session, items, cursor, null, map);
204     }
205     
206     public boolean next() throws SQLException JavaDoc {
207         return cursor.next();
208     }
209
210     public Row getRow() throws SQLException JavaDoc {
211         if (currentRow == null) {
212             makeFirstRow();
213         } else {
214             currentRow.nextRow(cursor.getRow());
215         }
216     return currentRow;
217     }
218
219     public void setOuterCursor(Cursor c) {
220     super.setOuterCursor(c);
221     if (cursor != null) cursor.setOuterCursor(c);
222     }
223
224     public void close() throws SQLException JavaDoc {
225     cursor.close();
226     }
227
228     public long size() throws SQLException JavaDoc { return cursor.size(); }
229     
230     public Cursor getBaseCursor() { return cursor; }
231
232     //#ifdef DEBUG
233
public String JavaDoc toString() {
234         return super.toString() + " : (cursor = " + cursor + ")";
235     }
236     //#endif
237

238 }
239
Popular Tags