CursorImpl.java :  » Database-DBMS » Quadcap-Embeddable-Database » com » quadcap » sql » Java Open Source

Java Open Source » Database DBMS » Quadcap Embeddable Database 
Quadcap Embeddable Database » com » quadcap » sql » CursorImpl.java
package com.quadcap.sql;

/* Copyright 1999 - 2003 Quadcap Software.  All rights reserved.
 *
 * This software is distributed under the Quadcap Free Software License.
 * This software may be used or modified for any purpose, personal or
 * commercial.  Open Source redistributions are permitted.  Commercial
 * redistribution of larger works derived from, or works which bundle
 * this software requires a "Commercial Redistribution License"; see
 * http://www.quadcap.com/purchase.
 *
 * Redistributions qualify as "Open Source" under  one of the following terms:
 *   
 *    Redistributions are made at no charge beyond the reasonable cost of
 *    materials and delivery.
 *
 *    Redistributions are accompanied by a copy of the Source Code or by an
 *    irrevocable offer to provide a copy of the Source Code for up to three
 *    years at the cost of materials and delivery.  Such redistributions
 *    must allow further use, modification, and redistribution of the Source
 *    Code under substantially the same terms as this license.
 *
 * Redistributions of source code must retain the copyright notices as they
 * appear in each source code file, these license terms, and the
 * disclaimer/limitation of liability set forth as paragraph 6 below.
 *
 * Redistributions in binary form must reproduce this Copyright Notice,
 * these license terms, and the disclaimer/limitation of liability set
 * forth as paragraph 6 below, in the documentation and/or other materials
 * provided with the distribution.
 *
 * The Software is provided on an "AS IS" basis.  No warranty is
 * provided that the Software is free of defects, or fit for a
 * particular purpose.  
 *
 * Limitation of Liability. Quadcap Software shall not be liable
 * for any damages suffered by the Licensee or any third party resulting
 * from use of the Software.
 */

import java.sql.SQLException;

import com.quadcap.util.Debug;

/**
 * Base cursor implementation class.
 *
 * @author Stan Bailes
 */
abstract public class CursorImpl extends TupleImpl implements Cursor {
    protected Session      session    = null;  
    protected Cursor      outer;

    /**
     * Oh no, you don't...
     */
    private CursorImpl() {}

    /**
     * Construct a cursor from a session and a name
     */
    public CursorImpl(Session session, String name) {
  super(name);
  this.session = session;
    }

    /**
     * Construct a cursor from a session, name, and outer cursor
     */
    public CursorImpl(Session session, String name, Cursor outer) {
  super(name);
  this.session = session;
  this.outer = outer;
    }

    /**
     * Return the cursor's session
     */
    public Session getSession() { return session; }

    /**
     * Derived class implements this function to return the current
     * cursor row. Implementation required.
     */
    abstract public Row getRow() throws SQLException;

    /**
     * Handle insert
     */
    public void insertRow(Row row) throws SQLException {
  throw new SQLException("Insert not allowed for this cursor type");
    }
    abstract public void updateRow(Row row) throws SQLException;
    abstract public void deleteRow() throws SQLException;

    abstract public void beforeFirst() throws SQLException;
    abstract public void afterLast() throws SQLException;
    abstract public boolean next() throws SQLException;

    public boolean prev() throws SQLException {
        throw new SQLException("prev() not supported for this cursor type");
    }

    abstract public boolean isWritable(int column) throws SQLException;
    abstract public void close() throws SQLException;

    public void finalize() throws Throwable {
        try {
            close();
        } catch (Throwable t) {
        }
        super.finalize();
    }
    
    public Column getColumn(String columnName) throws SQLException {
  Column c = super.getColumn(columnName);
    if (c == null && outer != null) {
        c = outer.getColumn(columnName);
    }
  return c;
    }

    public void setOuterCursor(Cursor c) {
  outer = c;
    }

    public Cursor getOuterCursor() {
  return outer;
    }

    /**
     * Position the cursor to the specified absolute row.  The first row
     * is row '1', and the last row is '-1', as in JDBC.
     */
    public boolean absolute(int row) throws SQLException {
        if (row > 0) {
            beforeFirst();
            while (row-- > 0) if (!next()) return false;
        } else {
            afterLast();
            while (row++ < 0) if (!prev()) return false;
        }
        return true;
    }

    public void reset(Expression expr, Cursor outer) throws SQLException {}
    
    public Table getTable() { return null; }
    public long getRowId() { return 0; }

    //#ifdef DEBUG
    public String toString() {
  try {
      StringBuffer sb =
                new StringBuffer(Table.strip(getClass().getName()));
      sb.append(": ");
      sb.append(getName());
      if (outer != null) {
    sb.append(" (outer ");
    sb.append(outer.toString()); // Table.strip(outer.getClass().getName()));
    sb.append(")");
      }
            sb.append(" {");
      for (int i = 1; i <= getColumnCount(); i++) {
    Column c = getColumn(i);
                if (i > 1) sb.append(',');
                sb.append(c.getName());
            }
            sb.append('}');
      return sb.toString();
  } catch (Exception e) {
      Debug.print(e);
      return this.getClass().getName();
  }
    }

    static void dumpCursor(String label, Cursor c) throws SQLException {
        if (label.length() > 0) label = label + " ";
        int count = 0;
        c.beforeFirst();
        while (c.next()) {
            Debug.println(label + "Row " + (++count) + ": " + c.getRow());
        }
        c.beforeFirst();
    }
    //#endif
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.