KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > smallsql > database > SortedResult


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  * SortedResult.java
29  * ---------------
30  * Author: Volker Berlin
31  *
32  */

33 package smallsql.database;
34
35
36 /**
37  * Is used to implements the ORDER BY clause.
38  *
39  * @author Volker Berlin
40  */

41 final class SortedResult extends RowSource {
42
43     final private Expressions orderBy;
44     final private RowSource rowSource;
45     private IndexScrollStatus scrollStatus;
46     private int row;
47     private boolean useSetRowPosition;
48     
49     SortedResult(RowSource rowSource, Expressions orderBy){
50         this.rowSource = rowSource;
51         this.orderBy = orderBy;
52     }
53
54     
55     final boolean isScrollable(){
56         return true; //TODO can scrollable implement if Index implement it
57
}
58
59     
60     final void execute() throws Exception JavaDoc{
61         rowSource.execute();
62         Index index = new Index(false);
63         while(rowSource.next()){
64             index.addValues( rowSource.getRowPosition(), orderBy);
65         }
66         scrollStatus = index.createScrollStatus(orderBy);
67         useSetRowPosition = false;
68     }
69     
70
71     void beforeFirst() throws Exception JavaDoc {
72         scrollStatus.reset();
73         row = 0;
74         useSetRowPosition = false;
75     }
76     
77
78     boolean first() throws Exception JavaDoc {
79         beforeFirst();
80         return next();
81     }
82     
83
84     boolean previous() throws Exception JavaDoc{
85         return nextPrevious( -1 );
86     }
87     
88     
89     boolean next() throws Exception JavaDoc {
90         return nextPrevious( +1 );
91     }
92     
93     
94     final private boolean nextPrevious(int scroll) throws Exception JavaDoc{
95         if(useSetRowPosition) throw Utils.createSQLException("Internal Error with ORDER BY");
96         long rowPosition = scrollStatus.getRowOffset(scroll == 1);
97         if(rowPosition >= 0){
98             rowSource.setRowPosition( rowPosition );
99             row += scroll;
100             return true;
101         }else{
102             noRow();
103             return false;
104         }
105     }
106     
107     
108     boolean last() throws Exception JavaDoc{
109         afterLast();
110         return previous();
111     }
112     
113     
114     void afterLast(){
115         scrollStatus.afterLast();
116         noRow();
117         useSetRowPosition = false;
118     }
119     
120     
121     int getRow(){
122         return row;
123     }
124     
125     
126     final long getRowPosition(){
127         return rowSource.getRowPosition();
128     }
129     
130     
131     final void setRowPosition(long rowPosition) throws Exception JavaDoc{
132         rowSource.setRowPosition(rowPosition);
133         useSetRowPosition = true;
134     }
135     
136
137     final boolean rowInserted(){
138         return rowSource.rowInserted();
139     }
140     
141     
142     final boolean rowDeleted(){
143         return rowSource.rowDeleted();
144     }
145     
146     
147     void nullRow() {
148         rowSource.nullRow();
149         row = 0;
150         
151     }
152     
153
154     void noRow() {
155         rowSource.noRow();
156         row = 0;
157     }
158
159 }
160
Popular Tags