KickJava   Java API By Example, From Geeks To Geeks.

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


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.ByteArrayOutputStream JavaDoc;
42 import java.io.Externalizable JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.io.ObjectInput JavaDoc;
45 import java.io.ObjectOutput JavaDoc;
46
47 import java.util.Enumeration JavaDoc;
48 import java.util.Hashtable JavaDoc;
49
50 import java.util.Vector JavaDoc;
51
52 import java.sql.SQLException JavaDoc;
53
54 import com.quadcap.sql.io.ObjectInputStream;
55 import com.quadcap.sql.io.ObjectOutputStream;
56
57 import com.quadcap.sql.file.ByteUtil;
58
59 import com.quadcap.util.Debug;
60 import com.quadcap.util.Util;
61
62 /**
63  * Cursor that implements the <b>DISTINCT</b> modifier by creating
64  * temporary table, where the entire row is used as the index key.
65  *
66  * @author Stan Bailes
67  */

68 public class DistinctCursor extends BC_Cursor {
69     Cursor sourceCursor;
70     Vector JavaDoc orderBy = null;
71     TempTable tempTable;
72     boolean distinct = true;
73
74     /**
75      * Constructor on session and cursor
76      */

77     public DistinctCursor(Session session, Cursor cursor) throws SQLException JavaDoc {
78     super(session, null, null);
79     this.sourceCursor = cursor;
80     addColumns(session, cursor);
81     }
82
83     public final void checkCursor() throws SQLException JavaDoc, IOException JavaDoc {
84         if (bc == null) {
85             try {
86                 makeTempTable();
87             } finally {
88                 try {
89                     sourceCursor.close();
90                 } catch (Throwable JavaDoc t) {
91                 } finally {
92                     sourceCursor = null;
93                 }
94             }
95         }
96     }
97
98
99     private void makeTempTable() throws IOException JavaDoc, SQLException JavaDoc {
100     int size = getColumnCount();
101         if (orderBy == null) {
102             if (distinct) {
103                 tempTable = new TempTable(session, new Key(size), null);
104             } else {
105                 tempTable = new TempTable(session, new Key(1), new int[0]);
106             }
107         } else {
108             tempTable = makeObTemp();
109         }
110         tempTable.addRows(sourceCursor);
111     row = new LazyRow(size);
112         bc = tempTable.getCursor();
113     }
114
115     TempTable makeObTemp() throws SQLException JavaDoc, IOException JavaDoc {
116     int[] map = new int[getColumnCount()];
117     boolean[] rmap = new boolean[map.length+1];
118     boolean[] asc = new boolean[map.length];
119     for (int i = 0; i < orderBy.size(); i++) {
120         OrderElement elem = (OrderElement)orderBy.elementAt(i);
121         int num = elem.getColumn();
122         if (num < 0) {
123         String JavaDoc name = elem.getColName();
124         Column col = getColumn(name);
125         if (col == null) {
126             throw new SQLException JavaDoc("Bad column name: " + name);
127         }
128         num = col.getColumn();
129         elem.setColumn(num);
130         }
131         map[i] = num;
132         rmap[num] = true;
133         asc[i] = elem.isAscending();
134     }
135     int cnt = orderBy.size();
136     for (int i = 1; i < rmap.length; i++) {
137         if (!rmap[i]) {
138         map[cnt] = i;
139         asc[cnt] = true;
140         cnt++;
141         }
142     }
143     Key compare = new Key(asc);
144     return new TempTable(session, compare, map);
145     }
146
147     public void setOrder(Vector JavaDoc v) {
148     this.orderBy = v;
149     }
150
151     public void setDistinct(boolean b) {
152         this.distinct = b;
153     }
154
155     public long getCurrentRowId() throws SQLException JavaDoc {
156         return ByteUtil.getLong(bc.getValBuf(), 1);
157     }
158
159     public void fetchCurrentRow() throws SQLException JavaDoc, IOException JavaDoc {
160         tempTable.getRow(bc.getValBuf(), row);
161         rowValid = true;
162     }
163     
164     /**
165      * On close, we release the resources.
166      */

167     public void close() throws SQLException JavaDoc {
168     try {
169             super.close();
170         } finally {
171             try {
172                 if (sourceCursor != null) sourceCursor.close();
173             } finally {
174                 sourceCursor = null;
175                 try {
176                     if (tempTable != null) tempTable.release();
177                 } catch (IOException JavaDoc ex) {
178                     throw DbException.wrapThrowable(ex);
179                 } finally {
180                     tempTable = null;
181                 }
182             }
183         }
184     }
185
186     //#ifdef DEBUG
187
public String JavaDoc toString() {
188         return super.toString() + "\n\t[source cursor = " + sourceCursor + "]\n";
189     }
190     //#endif
191
}
192
Popular Tags