KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
47 import java.util.Iterator JavaDoc;
48 import java.util.Vector JavaDoc;
49
50 import java.sql.SQLException JavaDoc;
51
52 import com.quadcap.sql.types.Type;
53
54 import com.quadcap.util.Debug;
55
56 /**
57  * Base class for tuple implementations.
58  *
59  * @author Stan Bailes
60  */

61 public class TupleImpl implements Tuple, Externalizable JavaDoc {
62     protected String JavaDoc qual;
63     protected String JavaDoc qualifier;
64     protected Vector JavaDoc columns = new Vector JavaDoc();
65     protected HashMap JavaDoc names = new HashMap JavaDoc();
66
67     /**
68      * Default constructor
69      */

70     public TupleImpl() {
71     this.qualifier = "";
72     this.qual = "";
73     }
74
75     /**
76      * Named tuple constructor
77      */

78     public TupleImpl(String JavaDoc qualifier) {
79     setName(qualifier);
80     }
81
82     /**
83      * Set the tuple's name
84      */

85     public void setName(String JavaDoc name) {
86     if (name == null) name = "";
87     this.qualifier = name;
88     if (qualifier.length() > 0) {
89         this.qual = qualifier + ".";
90     } else {
91         this.qual = "";
92     }
93     }
94
95     /**
96      * Add, in order, the columns from the definition of tuple 't' to
97      * the column list for this tuple.
98      */

99     public void addColumns(Session session, Tuple t) throws SQLException JavaDoc {
100     addColumns(session, t, true);
101     }
102
103     /**
104      * Add all the columns from the other tuple to this one.
105      */

106     public void addColumns(Session session, Tuple t, boolean resolve)
107         throws SQLException JavaDoc
108     {
109         Connection conn = session.getConnection();
110     boolean as = qual.length() > 0;
111     for (int i = 1; i <= t.getColumnCount(); i++) {
112         Column col = t.getColumn(i);
113         String JavaDoc name = conn.resolveColname(col.getName(), this);
114             //Debug.println("[" + getClass().getName() + "].addColumns " + i + ": " +
115
// col.getName() + "/" + col.getShortName() + "/" + name);
116
Column ncol = new Column(name, col);
117         if (col.isJoinColumn()) ncol.setJoinColumn(true);
118         addColumn(ncol);
119     }
120     if (resolve) resolveColumns();
121     }
122
123     /**
124      * Once all of the column names are available, we can look for duplicates
125      * and shortest-unique names, etc.
126      */

127     public void resolveColumns() throws SQLException JavaDoc {
128     for (int i = 1; i <= getColumnCount(); i++) {
129         Column col = getColumn(i);
130         String JavaDoc name = col.getName();
131         for (int j = name.length() - 1; j > 0; j--) {
132         if (name.charAt(j) == '.') {
133             String JavaDoc qname = name.substring(j+1);
134             Object JavaDoc obj = names.get(qname);
135             if (obj == col) {
136             col.setShortName(qname);
137             break;
138             }
139         }
140         }
141     }
142         //Debug.println("resolveColumns: " + this);
143
}
144
145     /**
146      * addColumn helper
147      */

148     private final void addName(String JavaDoc name, Column col) {
149     if (names.get(name) != null) {
150         names.put(name, "duplicate");
151     } else {
152         names.put(name, col);
153     }
154     }
155
156     /**
157      * Return the number of columns in this tuple
158      */

159     public int getColumnCount() throws SQLException JavaDoc {
160     return columns.size();
161     }
162
163     /**
164      * Return the specified (one-based) column
165      */

166     public Column getColumn(int i) throws SQLException JavaDoc {
167     return (Column)columns.elementAt(i-1);
168     }
169
170     /**
171      * Return the column with the specified name/shortname
172      */

173     public Column getColumn(String JavaDoc name) throws SQLException JavaDoc {
174     Column c = null;
175     Object JavaDoc obj = names.get(name);
176     if (obj != null && obj instanceof Column) {
177         c = (Column)obj;
178     }
179     return c;
180     }
181
182     /**
183      * Map the specified column names into an array of integer (one-based)
184      * column indexes
185      */

186     public int[] mapColumns(Vector JavaDoc names) throws SQLException JavaDoc {
187     int[] ret;
188     if (names == null) {
189         ret = new int[columns.size()];
190         for (int i = 0; i < ret.length; i++) {
191         ret[i] = i+1;
192         }
193     } else {
194         ret = new int[names.size()];
195         for (int i = 0; i < ret.length; i++) {
196         String JavaDoc name = (String JavaDoc)names.elementAt(i);
197         ret[i] = getColumnIndex(name);
198                 if (ret[i] <= 0) {
199                     throw new SQLException JavaDoc("No such column: " + name, "42000");
200                 }
201         }
202     }
203     return ret;
204     }
205
206     /**
207      * Return the tuple's name
208      */

209     public String JavaDoc getName() { return qualifier; }
210
211     /**
212      * Find the first occurence of the '.' name separator in
213      * the given SQL identifier string.
214      */

215     static int nextUnquotedPeriod(String JavaDoc s) {
216         boolean q = false;
217         for (int i = 0; i < s.length(); i++) {
218             char c = s.charAt(i);
219             if (c == '"') q = !q;
220             if (!q && c == '.') return i;
221         }
222         return -1;
223     }
224
225     /**
226      * Add a new column to the tuple
227      */

228     public void addColumn(Column col) throws SQLException JavaDoc {
229     columns.addElement(col);
230     col.setColumn(columns.size());
231     col.setTable(this);
232     String JavaDoc name = col.getName();
233     addName(name, col);
234     int idx = nextUnquotedPeriod(name);
235     while (idx >= 0) {
236         name = name.substring(idx+1);
237         addName(name, col);
238         idx = nextUnquotedPeriod(name);
239     }
240     }
241
242     /**
243      * Add a new column to the tuple in a specified position,
244      * moving columns at that position and greater on position
245      * to the right.
246      */

247     public void addColumn(Column col, int pos) throws SQLException JavaDoc {
248         columns.addElement(col);
249         int np = columns.size();
250         while (np > pos) {
251             Column col2 = (Column)columns.get(np-2);
252             col2.setColumn(np);
253             columns.set(np-1, col2);
254             np--;
255         }
256         columns.set(pos-1, col);
257         col.setColumn(pos);
258     col.setTable(this);
259     String JavaDoc name = col.getName();
260     addName(name, col);
261     int idx = nextUnquotedPeriod(name);
262     while (idx >= 0) {
263         name = name.substring(idx+1);
264         addName(name, col);
265         idx = nextUnquotedPeriod(name);
266     }
267     }
268
269     /**
270      * Add a column of the specified name and type to the tuple
271      */

272     public void addColumn(String JavaDoc name, Type type) throws SQLException JavaDoc {
273     addColumn(new Column(name, type));
274     }
275
276     /**
277      * Delete the specified column
278      */

279     public void deleteColumn(int col) throws SQLException JavaDoc, IOException JavaDoc {
280         Column delCol = getColumn(col);
281         int del = 0;
282         for (int i = 1; i <= columns.size(); i++) {
283             if (i == col) del++;
284             else {
285                 Column cx = getColumn(i);
286                 cx.setColumn(i-del);
287                 columns.set(i-del-1, cx);
288             }
289         }
290     columns.setSize(columns.size()-1);
291
292         Iterator JavaDoc iter = names.keySet().iterator();
293         while (iter.hasNext()) {
294             String JavaDoc name = iter.next().toString();
295             if (names.get(name) == delCol) iter.remove();
296         }
297     }
298
299     /**
300      * Return the (one-based) column index of the column with the
301      * specified name
302      */

303     public int getColumnIndex(String JavaDoc name) throws SQLException JavaDoc {
304     Column col = getColumn(name);
305     if (col == null) {
306             return -1;
307     }
308     return col.getColumn();
309     }
310
311     /**
312      * Externalizable.readExternal(): Read me from a stream.
313      */

314     public void readExternal(ObjectInput JavaDoc in)
315     throws IOException JavaDoc, ClassNotFoundException JavaDoc
316     {
317     qualifier = (String JavaDoc)in.readObject();
318     if (qualifier.length() > 0) {
319         this.qual = qualifier + ".";
320     } else {
321         this.qual = "";
322     }
323     Vector JavaDoc c = (Vector JavaDoc)in.readObject();
324     this.columns = new Vector JavaDoc();
325     this.names = new HashMap JavaDoc();
326     for (int i = 0; i < c.size(); i++) {
327         try {
328         addColumn((Column)c.elementAt(i));
329         } catch (SQLException JavaDoc e) {
330                 //#ifdef DEBUG
331
Debug.print(e);
332                 //#endif
333
throw new IOException JavaDoc(e.toString());
334         }
335     }
336     try {
337         resolveColumns();
338     } catch (SQLException JavaDoc e) {
339             //#ifdef DEBUG
340
Debug.print(e);
341             //#endif
342
throw new IOException JavaDoc(e.toString());
343     }
344     }
345     
346     /**
347      * Externalizable.writeExternal(): Write me to a stream.
348      */

349     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
350     out.writeObject(qualifier);
351     out.writeObject(columns);
352     }
353
354     //#ifdef DEBUG
355
/**
356      * Return a string representation for debugging
357      */

358     public String JavaDoc toString() {
359     try {
360         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(
361                 Table.strip(getClass().getName()));
362             sb.append(" ");
363             sb.append(qualifier);
364         sb.append("\n");
365         for (int i = 1; i <= getColumnCount(); i++) {
366         Column c = getColumn(i);
367         if (i > 1) sb.append("\n");
368         sb.append(" col " + i + ": ");
369         sb.append(c.toString());
370         }
371         sb.append("\n");
372         //sb.append(names.toString());
373
return sb.toString();
374     } catch (Exception JavaDoc e) {
375         Debug.print(e);
376         return e.toString();
377     }
378     }
379     //#endif
380

381 }
382
383
Popular Tags