Quadcap Embeddable Database

com/quadcap/sql/CursorImpl.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.sql.SQLException; 00042 00043 import com.quadcap.util.Debug; 00044 00045 /** 00046 * Base cursor implementation class. 00047 * 00048 * @author Stan Bailes 00049 */ 00050 abstract public class CursorImpl extends TupleImpl implements Cursor { 00051 protected Session session = null; 00052 protected Cursor outer; 00053 00054 /** 00055 * Oh no, you don't... 00056 */ 00057 private CursorImpl() {} 00058 00059 /** 00060 * Construct a cursor from a session and a name 00061 */ 00062 public CursorImpl(Session session, String name) { 00063 super(name); 00064 this.session = session; 00065 } 00066 00067 /** 00068 * Construct a cursor from a session, name, and outer cursor 00069 */ 00070 public CursorImpl(Session session, String name, Cursor outer) { 00071 super(name); 00072 this.session = session; 00073 this.outer = outer; 00074 } 00075 00076 /** 00077 * Return the cursor's session 00078 */ 00079 public Session getSession() { return session; } 00080 00081 /** 00082 * Derived class implements this function to return the current 00083 * cursor row. Implementation required. 00084 */ 00085 abstract public Row getRow() throws SQLException; 00086 00087 /** 00088 * Handle insert 00089 */ 00090 public void insertRow(Row row) throws SQLException { 00091 throw new SQLException("Insert not allowed for this cursor type"); 00092 } 00093 abstract public void updateRow(Row row) throws SQLException; 00094 abstract public void deleteRow() throws SQLException; 00095 00096 abstract public void beforeFirst() throws SQLException; 00097 abstract public void afterLast() throws SQLException; 00098 abstract public boolean next() throws SQLException; 00099 00100 public boolean prev() throws SQLException { 00101 throw new SQLException("prev() not supported for this cursor type"); 00102 } 00103 00104 abstract public boolean isWritable(int column) throws SQLException; 00105 abstract public void close() throws SQLException; 00106 00107 public void finalize() throws Throwable { 00108 try { 00109 close(); 00110 } catch (Throwable t) { 00111 } 00112 super.finalize(); 00113 } 00114 00115 public Column getColumn(String columnName) throws SQLException { 00116 Column c = super.getColumn(columnName); 00117 if (c == null && outer != null) { 00118 c = outer.getColumn(columnName); 00119 } 00120 return c; 00121 } 00122 00123 public void setOuterCursor(Cursor c) { 00124 outer = c; 00125 } 00126 00127 public Cursor getOuterCursor() { 00128 return outer; 00129 } 00130 00131 /** 00132 * Position the cursor to the specified absolute row. The first row 00133 * is row '1', and the last row is '-1', as in JDBC. 00134 */ 00135 public boolean absolute(int row) throws SQLException { 00136 if (row > 0) { 00137 beforeFirst(); 00138 while (row-- > 0) if (!next()) return false; 00139 } else { 00140 afterLast(); 00141 while (row++ < 0) if (!prev()) return false; 00142 } 00143 return true; 00144 } 00145 00146 public void reset(Expression expr, Cursor outer) throws SQLException {} 00147 00148 public Table getTable() { return null; } 00149 public long getRowId() { return 0; } 00150 00151 //#ifdef DEBUG 00152 public String toString() { 00153 try { 00154 StringBuffer sb = 00155 new StringBuffer(Table.strip(getClass().getName())); 00156 sb.append(": "); 00157 sb.append(getName()); 00158 if (outer != null) { 00159 sb.append(" (outer "); 00160 sb.append(outer.toString()); // Table.strip(outer.getClass().getName())); 00161 sb.append(")"); 00162 } 00163 sb.append(" {"); 00164 for (int i = 1; i <= getColumnCount(); i++) { 00165 Column c = getColumn(i); 00166 if (i > 1) sb.append(','); 00167 sb.append(c.getName()); 00168 } 00169 sb.append('}'); 00170 return sb.toString(); 00171 } catch (Exception e) { 00172 Debug.print(e); 00173 return this.getClass().getName(); 00174 } 00175 } 00176 00177 static void dumpCursor(String label, Cursor c) throws SQLException { 00178 if (label.length() > 0) label = label + " "; 00179 int count = 0; 00180 c.beforeFirst(); 00181 while (c.next()) { 00182 Debug.println(label + "Row " + (++count) + ": " + c.getRow()); 00183 } 00184 c.beforeFirst(); 00185 } 00186 //#endif 00187 }