001    package hep.aida.ref.sql;
002    
003    // FreeHEP
004    import hep.aida.ref.tuple.FTupleCursor;
005    
006    // Log4J
007    import org.apache.log4j.Logger;
008    
009    /** <code>SQLTupleCursor</code> extends {@link FTupleCursor}
010      * with SQL backend.
011      * <p><font color="#880088">
012      * <pre>
013      * $Log: SQLTupleCursor.java,v $
014      * Revision 1.7  2007/05/23 16:38:44  hrivnac
015      * logical connections for Plotter; better UML
016      *
017      * Revision 1.6  2005/09/02 14:10:31  hrivnac
018      * moved to JAS3 0.8.2
019      *
020      * Revision 1.5  2004/10/29 22:27:25  hrivnac
021      * imports corrected
022      *
023      * Revision 1.4  2004/10/12 19:26:59  hrivnac
024      * MySQL Connector 3.0.14
025      *
026      * Revision 1.3  2004/05/22 15:12:59  hrivnac
027      * class id reformated
028      *
029      * Revision 1.2  2004/05/04 08:50:33  hrivnac
030      * can run with JAS3
031      *
032      * Revision 1.1  2004/05/03 19:22:30  hrivnac
033      * basic support for JAS3
034      *
035      * </pre>
036      * </font></p>
037      * @opt attributes
038      * @opt operations
039      * @opt types
040      * @opt visibility
041      * @version $Id: SQLTupleCursor.java,v 1.7 2007/05/23 16:38:44 hrivnac Exp $
042      * @author <a href="mailto:Julius.Hrivnac@cern.ch">J.Hrivnac</a> */
043    public class SQLTupleCursor implements FTupleCursor {
044    
045      /** Create with reference to {@link SQLTuple}.
046        * @param tuple The {@link SQLTuple} containing requested column. */
047      public SQLTupleCursor(SQLTuple tuple) {
048        _tuple  = tuple;
049        _closed = false;
050        }
051    
052      /** Give the current row number
053        * @return The current row number, or -1 if before the first row. */
054      public int row() throws IllegalStateException {
055        if (_closed) throw new IllegalStateException("Cursor is closed");
056        return _tuple.row();
057        }
058    
059      /** Rewind the Cursor to before the first row. */
060      public void start() throws IllegalStateException {
061        if (_closed) throw new IllegalStateException("Cursor is closed");
062        _tuple.start();
063        }
064    
065      /** Step to the next row.
066        * @return False if there are no more rows. */
067      public boolean next() throws IllegalStateException {
068        if (_closed) throw new IllegalStateException("Cursor is closed");
069        return _tuple.next();
070        }
071    
072      /** Skips rows.
073        * @param rows The number of rows to skip, greater than 0. */
074      public void skip(int rows) throws IllegalStateException {
075        if (_closed) throw new IllegalStateException("Cursor is closed");
076        _tuple.skip(rows);
077        }
078    
079      /** Step to a specific row.
080       * @param n The row to move to.
081       * @throws IndexOutOfBoundsException     if the index is < 0 or >= number of rows.
082       * @throws UnsupportedOperationException if the Tuple does not support random access. */    
083      public void setRow(int n) throws IllegalStateException {
084        if (_closed)                     throw new IllegalStateException("Cursor is closed");
085        if (n < 0 || n >= _tuple.rows()) throw new IllegalStateException("Requested row " + n + " isn't within available rows < 0, " + _tuple.rows() + ")");
086        _tuple.setRow(n);
087        }
088    
089      /** Close this cursor. Any further attempts to use the cursor will result in 
090        * an {@link IllegalStateException}. */
091      public void close() {
092        _closed = true;
093        }
094    
095      private SQLTuple _tuple;
096    
097      private boolean _closed;
098    
099      /** Logging . */
100      private static Logger log = Logger.getLogger(SQLTupleCursor.class);
101    
102      }