| 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 | * RowSource.java |
| 29 | * --------------- |
| 30 | * Author: Volker Berlin |
| 31 | * |
| 32 | */ |
| 33 | package smallsql.database; |
| 34 | |
| 35 | import java.sql.*; |
| 36 | /** |
| 37 | * This is the navigation through the rows of any source. |
| 38 | * This is an abstract class and not an interface because interfaces are ever public. |
| 39 | * All Implementations are used as a Source in the FROM clause. |
| 40 | * |
| 41 | * Know Implementations are: |
| 42 | * - Join |
| 43 | * - TableResult |
| 44 | * - ViewResult |
| 45 | * - MemoryResult |
| 46 | * - GroupResult (extends from MemoryResult) |
| 47 | * - SortedResult |
| 48 | * - UnionAll |
| 49 | * |
| 50 | * Future Implementations are: |
| 51 | * - Inner SELECT |
| 52 | * - row function |
| 53 | */ |
| 54 | abstract class RowSource { |
| 55 | |
| 56 | |
| 57 | /** |
| 58 | * If this RowSource is scrollable. It means it can scroll in all directions. |
| 59 | */ |
| 60 | abstract boolean isScrollable(); |
| 61 | |
| 62 | /** |
| 63 | * Equals to ResultSet.beforeFirst() |
| 64 | */ |
| 65 | abstract void beforeFirst() throws Exception; |
| 66 | |
| 67 | |
| 68 | |
| 69 | /** |
| 70 | * Equals to ResultSet.isBeforeFirst(). |
| 71 | */ |
| 72 | boolean isBeforeFirst() throws SQLException{ |
| 73 | throw Utils.createSQLException("ResultSet is forward only."); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Equals to ResultSet.isFirst(). |
| 78 | */ |
| 79 | boolean isFirst() throws SQLException{ |
| 80 | throw Utils.createSQLException("ResultSet is forward only."); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /** |
| 85 | * Equals to ResultSet.first() |
| 86 | * @return |
| 87 | */ |
| 88 | abstract boolean first() throws Exception; |
| 89 | |
| 90 | boolean previous() throws Exception{ |
| 91 | throw Utils.createSQLException("ResultSet is forward only."); |
| 92 | } |
| 93 | |
| 94 | abstract boolean next() throws Exception; |
| 95 | |
| 96 | |
| 97 | boolean last() throws Exception{ |
| 98 | throw Utils.createSQLException("ResultSet is forward only."); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | /** |
| 103 | * Equals to ResultSet.isLast(). |
| 104 | */ |
| 105 | boolean isLast() throws Exception{ |
| 106 | throw Utils.createSQLException("ResultSet is forward only."); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Equals to ResultSet.isAfterLast(). |
| 111 | */ |
| 112 | boolean isAfterLast() throws SQLException, Exception{ |
| 113 | throw Utils.createSQLException("ResultSet is forward only."); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | abstract void afterLast() throws Exception; |
| 118 | |
| 119 | |
| 120 | boolean absolute(int row) throws Exception{ |
| 121 | throw Utils.createSQLException("ResultSet is forward only."); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | boolean relative(int rows) throws Exception{ |
| 126 | throw Utils.createSQLException("ResultSet is forward only."); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | abstract int getRow() throws Exception; |
| 131 | |
| 132 | |
| 133 | |
| 134 | /** |
| 135 | * Get a marker for the current row. The method setRowPostion must be reconstruct |
| 136 | * the current row. The RowPosition can be a file offset for TableResult. |
| 137 | * This is using for SortedResult. |
| 138 | * @return The value need be >= 0. |
| 139 | * It can be a counter for MemoryResult. |
| 140 | */ |
| 141 | abstract long getRowPosition(); |
| 142 | |
| 143 | /** |
| 144 | * Restore the row that was marked with the value. This is using for SortedResult. |
| 145 | * @param rowPosition Only values that are return from getRowPosition are valid. |
| 146 | */ |
| 147 | abstract void setRowPosition(long rowPosition) throws Exception; |
| 148 | |
| 149 | /** |
| 150 | * Is used for OUTER JOIN to set the RowSource to NULL if the row exists |
| 151 | * only in the major RowSource (table) |
| 152 | */ |
| 153 | abstract void nullRow(); |
| 154 | |
| 155 | /** |
| 156 | * Is used for JOIN to set both site to "No current row". This is needed if |
| 157 | * one site has 0 rows that the getXXX() methods throw this exception. |
| 158 | */ |
| 159 | abstract void noRow(); |
| 160 | |
| 161 | |
| 162 | /** |
| 163 | * If the current row is inserted in this ResultSet. |
| 164 | */ |
| 165 | abstract boolean rowInserted(); |
| 166 | |
| 167 | /** |
| 168 | * If the current row is deleted. |
| 169 | */ |
| 170 | abstract boolean rowDeleted(); |
| 171 | |
| 172 | /** |
| 173 | * Returns true if a alias was set and no more alias can be set. |
| 174 | * This is used from the SQLParser |
| 175 | * @return |
| 176 | */ |
| 177 | boolean hasAlias(){ |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | |
| 182 | void setAlias(String name) throws SQLException{ |
| 183 | throw Utils.createSQLException("Alias not supported for this type of row source." ); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Perform some operation on some RowSources per ResultSet. For example the grouping |
| 188 | * on GroupResult and sorting on SortedResult. |
| 189 | */ |
| 190 | abstract void execute() throws Exception; |
| 191 | |
| 192 | } |