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