Quadcap Embeddable Database

com/quadcap/sql/BC_Cursor.java

Go to the documentation of this file.
00001 package com.quadcap.sql; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.Externalizable; 00042 import java.io.IOException; 00043 import java.io.ObjectInput; 00044 import java.io.ObjectOutput; 00045 00046 import java.sql.SQLException; 00047 00048 import com.quadcap.sql.io.ObjectInputStream; 00049 import com.quadcap.sql.io.ObjectOutputStream; 00050 00051 import com.quadcap.sql.file.SubPageManager; 00052 import com.quadcap.sql.index.BCursor; 00053 00054 import com.quadcap.util.Debug; 00055 00056 /** 00057 * Cursor that implements the <b>DISTINCT</b> modifier by creating 00058 * temporary table, where the entire row is used as the index key. 00059 * 00060 * @author Stan Bailes 00061 */ 00062 public abstract class BC_Cursor extends CursorImpl { 00063 protected BCursor bc = null; 00064 protected LazyRow row = null; 00065 protected boolean rowValid = false; 00066 protected long rowId = 0; 00067 00068 /** 00069 * Constructor on session and cursor 00070 */ 00071 public BC_Cursor(Session session, String name, Cursor outer) 00072 throws SQLException 00073 { 00074 super(session, name, outer); 00075 } 00076 00077 /** 00078 * Cursor.getRow(): return the current table row, fetching it if necessary 00079 * based on the rowId 00080 */ 00081 public Row getRow() throws SQLException { 00082 Row ret = null; 00083 if (rowId != 0) { 00084 if (!rowValid) { 00085 try { 00086 fetchCurrentRow(); 00087 } catch (IOException ex) { 00088 throw DbException.wrapThrowable(ex); 00089 } 00090 } 00091 if (rowValid) { 00092 ret = row; 00093 } 00094 } 00095 return row; 00096 } 00097 00098 /** 00099 * Make the row too, while you're at it... 00100 */ 00101 public abstract void checkCursor() throws IOException, SQLException; 00102 00103 public abstract long getCurrentRowId() throws IOException, SQLException; 00104 00105 public abstract void fetchCurrentRow() throws IOException, SQLException; 00106 00107 public boolean isWritable(int column) { return false; } 00108 00109 public boolean absolute(int x) throws SQLException { 00110 try { 00111 rowValid = false; 00112 rowId = 0; 00113 checkCursor(); 00114 boolean ret = bc.absolute(x); 00115 if (ret) { 00116 rowId = getCurrentRowId(); 00117 } 00118 return ret; 00119 } catch (IOException e) { 00120 throw DbException.wrapThrowable(e); 00121 } 00122 } 00123 00124 public void beforeFirst() throws SQLException { 00125 try { 00126 rowValid = false; 00127 rowId = 0; 00128 checkCursor(); 00129 bc.beforeFirst(); 00130 } catch (IOException e) { 00131 throw DbException.wrapThrowable(e); 00132 } 00133 } 00134 00135 public void afterLast() throws SQLException { 00136 try { 00137 rowValid = false; 00138 rowId = 0; 00139 checkCursor(); 00140 bc.afterLast(); 00141 } catch (IOException e) { 00142 throw DbException.wrapThrowable(e); 00143 } 00144 } 00145 00146 public boolean next() throws SQLException { 00147 try { 00148 rowValid = false; 00149 rowId = 0; 00150 checkCursor(); 00151 boolean ret = bc.next(); 00152 if (ret) { 00153 rowId = getCurrentRowId(); 00154 } 00155 return ret; 00156 } catch (IOException e) { 00157 throw DbException.wrapThrowable(e); 00158 } 00159 } 00160 00161 public boolean prev() throws SQLException { 00162 try { 00163 rowValid = false; 00164 rowId = 0; 00165 checkCursor(); 00166 boolean ret = bc.prev(); 00167 if (ret) rowId = getCurrentRowId(); 00168 return ret; 00169 } catch (IOException e) { 00170 throw DbException.wrapThrowable(e); 00171 } 00172 } 00173 00174 /** 00175 * On close, we release the resources. 00176 */ 00177 public void close() throws SQLException { 00178 try { 00179 if (bc != null) bc.release(); 00180 } finally { 00181 bc = null; 00182 row = null; 00183 } 00184 } 00185 00186 /** 00187 * Return the row id 00188 */ 00189 public long getRowId() { 00190 return rowId; 00191 } 00192 00193 /** 00194 * Return the size of the index backing this cursor. 00195 */ 00196 public long size() throws SQLException { 00197 try { 00198 return bc.size(); 00199 } catch (IOException e) { 00200 throw DbException.wrapThrowable(e); 00201 } 00202 } 00203 00204 public void updateRow(Row row) throws SQLException { 00205 throw new SQLException("Cursor not updateable"); 00206 } 00207 00208 public void deleteRow() throws SQLException { 00209 throw new SQLException("Cursor not updateable"); 00210 } 00211 }