KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > smallsql > database > UnionAll


1 /* =============================================================
2  * SmallSQL : a free Java DBMS library for the Java(tm) platform
3  * =============================================================
4  *
5  * (C) Copyright 2004-2006, by Volker Berlin.
6  *
7  * Project Info: http://www.smallsql.de/
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ---------------
28  * UnionAll.java
29  * ---------------
30  * Author: Volker Berlin
31  *
32  * Created on 26.06.2004
33  */

34 package smallsql.database;
35
36
37 /**
38  * @author Volker Berlin
39  */

40 final class UnionAll extends DataSource {
41
42     private final DataSources dataSources = new DataSources();
43     private int dataSourceIdx;
44     private DataSource currentDS;
45     private int row;
46     
47     
48     void addDataSource(DataSource ds){
49         dataSources.add(ds);
50         currentDS = dataSources.get(0);
51     }
52     
53     
54 /*=======================================================================
55  
56     Methods for interface DataSource
57  
58 =======================================================================*/

59     
60     boolean init(SSConnection con) throws Exception JavaDoc{
61         boolean result = false;
62         int colCount = -1;
63         for(int i=0; i<dataSources.size(); i++){
64             DataSource ds = dataSources.get(i);
65             result |= ds.init(con);
66             int nextColCount = ds.getTableView().columns.size();
67             if(colCount == -1)
68                 colCount = nextColCount;
69             else
70                 if(colCount != nextColCount)
71                     throw Utils.createSQLException("Different SELECT of the UNION have different column count:"+colCount+" and "+nextColCount);
72         }
73         return result;
74     }
75     
76     
77     final boolean isNull(int colIdx) throws Exception JavaDoc {
78         return currentDS.isNull(colIdx);
79     }
80
81
82     final boolean getBoolean(int colIdx) throws Exception JavaDoc {
83         return currentDS.getBoolean(colIdx);
84     }
85
86
87     final int getInt(int colIdx) throws Exception JavaDoc {
88         return currentDS.getInt(colIdx);
89     }
90
91
92     final long getLong(int colIdx) throws Exception JavaDoc {
93         return currentDS.getLong(colIdx);
94     }
95
96
97     final float getFloat(int colIdx) throws Exception JavaDoc {
98         return currentDS.getFloat(colIdx);
99     }
100
101
102     final double getDouble(int colIdx) throws Exception JavaDoc {
103         return currentDS.getDouble(colIdx);
104     }
105
106
107     final long getMoney(int colIdx) throws Exception JavaDoc {
108         return currentDS.getMoney(colIdx);
109     }
110
111
112     final MutableNumeric getNumeric(int colIdx) throws Exception JavaDoc {
113         return currentDS.getNumeric(colIdx);
114     }
115
116
117     final Object JavaDoc getObject(int colIdx) throws Exception JavaDoc {
118         return currentDS.getObject(colIdx);
119     }
120
121
122     final String JavaDoc getString(int colIdx) throws Exception JavaDoc {
123         return currentDS.getString(colIdx);
124     }
125
126
127     final byte[] getBytes(int colIdx) throws Exception JavaDoc {
128         return currentDS.getBytes(colIdx);
129     }
130
131
132     final int getDataType(int colIdx) {
133         return currentDS.getDataType(colIdx);
134     }
135     
136
137     TableView getTableView(){
138         return currentDS.getTableView();
139     }
140     
141     
142
143 /*=======================================================================
144  
145     Methods for interface RowSource
146  
147 =======================================================================*/

148     
149     final boolean isScrollable(){
150         return false; //TODO performance, can implement it if all datasources implement it
151
}
152
153     
154     final void beforeFirst() throws Exception JavaDoc {
155         dataSourceIdx = 0;
156         currentDS = dataSources.get(0);
157         currentDS.beforeFirst();
158         row = 0;
159     }
160
161
162     final boolean first() throws Exception JavaDoc {
163         dataSourceIdx = 0;
164         currentDS = dataSources.get(0);
165         boolean b = currentDS.first();
166         row = b ? 1 : 0;
167         return b;
168     }
169
170
171     final boolean next() throws Exception JavaDoc {
172         boolean n = currentDS.next();
173         row++;
174         if(n) return true;
175         while(dataSources.size() > dataSourceIdx+1){
176             currentDS = dataSources.get(++dataSourceIdx);
177             currentDS.beforeFirst();
178             n = currentDS.next();
179             if(n) return true;
180         }
181         row = 0;
182         return false;
183     }
184
185
186     final void afterLast() throws Exception JavaDoc {
187         dataSourceIdx = dataSources.size()-1;
188         currentDS = dataSources.get(dataSourceIdx);
189         currentDS.afterLast();
190         row = 0;
191     }
192
193
194     final int getRow() throws Exception JavaDoc {
195         return row;
196     }
197     
198
199     private final int getBitCount(){
200         int size = dataSources.size();
201         int bitCount = 0;
202         while(size>0){
203             bitCount++;
204             size >>= 1;
205         }
206         return bitCount;
207     }
208
209     
210     final long getRowPosition() {
211         int bitCount = getBitCount();
212         return dataSourceIdx | currentDS.getRowPosition() << bitCount;
213     }
214
215
216     final void setRowPosition(long rowPosition) throws Exception JavaDoc {
217         int bitCount = getBitCount();
218         int mask = 0xFFFFFFFF >>> (32 - bitCount);
219         dataSourceIdx = (int)rowPosition & mask;
220         currentDS = dataSources.get(dataSourceIdx);
221         currentDS.setRowPosition( rowPosition >> bitCount );
222         //getRow() is only unse on the top level RowSource, setRowPosition is not used on the top level RowSource
223
//thats we not set row here
224
}
225
226
227     final boolean rowInserted(){
228         return currentDS.rowInserted();
229     }
230     
231     
232     final boolean rowDeleted(){
233         return currentDS.rowDeleted();
234     }
235     
236     
237     final void nullRow() {
238         currentDS.nullRow();
239         row = 0;
240     }
241
242
243     final void noRow() {
244         currentDS.noRow();
245         row = 0;
246     }
247
248     
249     final void execute() throws Exception JavaDoc{
250         for(int i=0; i<dataSources.size(); i++){
251             dataSources.get(i).execute();
252         }
253     }
254
255 }
256
Popular Tags