KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > smallsql > database > Distinct


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  * Where.java
29  * ---------------
30  * Author: Volker Berlin
31  *
32  * Created on 31.07.2004
33  */

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

40 final class Distinct extends RowSource {
41
42     final private Expressions columns;
43     final private RowSource rowSource;
44     private Index index;
45     private int row;
46     
47     Distinct(RowSource rowSource, Expressions columns){
48         this.rowSource = rowSource;
49         this.columns = columns;
50     }
51
52     
53     final void execute() throws Exception JavaDoc{
54         rowSource.execute();
55         index = new Index(true);
56     }
57     
58
59     final boolean isScrollable() {
60         return false;
61     }
62
63
64     final void beforeFirst() throws Exception JavaDoc {
65         rowSource.beforeFirst();
66         row = 0;
67     }
68
69
70     final boolean first() throws Exception JavaDoc {
71         beforeFirst();
72         return next();
73     }
74     
75
76     final boolean next() throws Exception JavaDoc {
77         while(true){
78             boolean isNext = rowSource.next();
79             if(!isNext) return false;
80
81             Long JavaDoc oldRowOffset = (Long JavaDoc)index.findRows(columns, null);
82             long newRowOffset = rowSource.getRowPosition();
83             if(oldRowOffset == null){
84                 index.addValues( newRowOffset, columns);
85                 row++;
86                 return true;
87             }else
88             if(oldRowOffset.longValue() == newRowOffset){
89                 row++;
90                 return true;
91             }
92         }
93     }
94
95
96     final void afterLast() throws Exception JavaDoc {
97         rowSource.afterLast();
98         row = 0;
99     }
100
101
102     final int getRow() throws Exception JavaDoc {
103         return row;
104     }
105
106
107     final long getRowPosition() {
108         return rowSource.getRowPosition();
109     }
110
111
112     final void setRowPosition(long rowPosition) throws Exception JavaDoc {
113         rowSource.setRowPosition(rowPosition);
114     }
115
116
117     final void nullRow() {
118         rowSource.nullRow();
119         row = 0;
120     }
121
122
123     final void noRow() {
124         rowSource.noRow();
125         row = 0;
126     }
127
128
129     final boolean rowInserted(){
130         return rowSource.rowInserted();
131     }
132     
133     
134     final boolean rowDeleted() {
135         return rowSource.rowDeleted();
136     }
137 }
138
Popular Tags