package org.postgresql.jdbc3;


import org.postgresql.PGConnection;


/** A real result set based on a ref cursor.
 *
 * @author Nic Ferrier <nferrier@tapsellferrier.co.uk>
 */
public class Jdbc3RefCursorResultSet extends Jdbc3ResultSet implements org.postgresql.PGRefCursorResultSet
{

   String refCursorHandle;

   // Indicates when the result set has activaly bound to the cursor.
   boolean isInitialized = false;

   Jdbc3RefCursorResultSet(java.sql.Statement statement, String refCursorName) throws java.sql.SQLException
   {
      super((PGConnection)statement.getConnection(), statement);
      this.refCursorHandle = refCursorName;
   }

   public String getRefCursor ()
   {
      return refCursorHandle;
   }

   public boolean next () throws java.sql.SQLException
   {
      if (isInitialized)
         return super.next();
      // Initialize this res set with the rows from the cursor.
      String[] toExec = { "FETCH ALL IN \"" + refCursorHandle + "\";" };
      Jdbc3Connection execr = (Jdbc3Connection) statement.getConnection();
      execr.execSQL(toExec, new String[0], statement, this);
      isInitialized = true;
      return super.next();
   }
}

