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 14.08.2004 |
33 | */ |
34 | package smallsql.database; |
35 | |
36 | |
37 | class Where extends RowSource { |
38 | |
39 | final private RowSource rowSource; |
40 | final private Expression where; |
41 | private int row = 0; |
42 | private boolean isCurrentRow; |
43 | |
44 | Where(RowSource rowSource, Expression where){ |
45 | this.rowSource = rowSource; |
46 | this.where = where; |
47 | } |
48 | |
49 | RowSource getFrom(){ |
50 | return rowSource; |
51 | } |
52 | |
53 | /** |
54 | * Verify if the valid row of the underlying RowSource (Variable join) |
55 | * is valid for the current ResultSet. |
56 | * @return |
57 | */ |
58 | final private boolean isValidRow() throws Exception{ |
59 | return where == null || rowSource.rowInserted() || where.getBoolean(); |
60 | } |
61 | |
62 | |
63 | final boolean isScrollable() { |
64 | return rowSource.isScrollable(); |
65 | } |
66 | |
67 | |
68 | final boolean isBeforeFirst(){ |
69 | return row == 0; |
70 | } |
71 | |
72 | |
73 | final boolean isFirst(){ |
74 | return row == 1 && isCurrentRow; |
75 | } |
76 | |
77 | |
78 | final boolean isLast() throws Exception{ |
79 | if(!isCurrentRow) return false; |
80 | long rowPos = rowSource.getRowPosition(); |
81 | boolean isNext = next(); |
82 | rowSource.setRowPosition(rowPos); |
83 | return !isNext; |
84 | } |
85 | |
86 | |
87 | final boolean isAfterLast(){ |
88 | return row > 0 && !isCurrentRow; |
89 | } |
90 | |
91 | |
92 | final void beforeFirst() throws Exception { |
93 | rowSource.beforeFirst(); |
94 | row = 0; |
95 | } |
96 | |
97 | |
98 | final boolean first() throws Exception { |
99 | isCurrentRow = rowSource.first(); |
100 | while(isCurrentRow && !isValidRow()){ |
101 | isCurrentRow = rowSource.next(); |
102 | } |
103 | row = 1; |
104 | return isCurrentRow; |
105 | } |
106 | |
107 | |
108 | final boolean previous() throws Exception { |
109 | boolean oldIsCurrentRow = isCurrentRow; |
110 | do{ |
111 | isCurrentRow = rowSource.previous(); |
112 | }while(isCurrentRow && !isValidRow()); |
113 | if(oldIsCurrentRow || isCurrentRow) row--; |
114 | return isCurrentRow; |
115 | } |
116 | |
117 | |
118 | final boolean next() throws Exception { |
119 | boolean oldIsCurrentRow = isCurrentRow; |
120 | do{ |
121 | isCurrentRow = rowSource.next(); |
122 | }while(isCurrentRow && !isValidRow()); |
123 | if(oldIsCurrentRow || isCurrentRow) row++; |
124 | return isCurrentRow; |
125 | } |
126 | |
127 | |
128 | final boolean last() throws Exception{ |
129 | while(next()); |
130 | return previous(); |
131 | } |
132 | |
133 | |
134 | final void afterLast() throws Exception { |
135 | while(next()); |
136 | } |
137 | |
138 | |
139 | final int getRow() throws Exception { |
140 | return isCurrentRow ? row : 0; |
141 | } |
142 | |
143 | |
144 | final long getRowPosition() { |
145 | return rowSource.getRowPosition(); |
146 | } |
147 | |
148 | |
149 | final void setRowPosition(long rowPosition) throws Exception { |
150 | rowSource.setRowPosition(rowPosition); |
151 | } |
152 | |
153 | |
154 | final void nullRow() { |
155 | rowSource.nullRow(); |
156 | row = 0; |
157 | } |
158 | |
159 | |
160 | final void noRow() { |
161 | rowSource.noRow(); |
162 | row = 0; |
163 | } |
164 | |
165 | |
166 | final boolean rowInserted() { |
167 | return rowSource.rowInserted(); |
168 | } |
169 | |
170 | |
171 | final boolean rowDeleted() { |
172 | return rowSource.rowDeleted(); |
173 | } |
174 | |
175 | |
176 | final void execute() throws Exception{ |
177 | rowSource.execute(); |
178 | } |
179 | |
180 | } |