ResultSet.java
上传用户:psq1974
上传日期:2007-01-06
资源大小:1195k
文件大小:27k
源码类别:

mpeg/mp3

开发平台:

C/C++

  1. package postgresql;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.math.*;
  5. import java.text.*;
  6. import java.util.*;
  7. import java.sql.*;
  8. import postgresql.largeobject.*;
  9. import postgresql.util.*;
  10. /**
  11.  * A ResultSet provides access to a table of data generated by executing a
  12.  * Statement.  The table rows are retrieved in sequence.  Within a row its
  13.  * column values can be accessed in any order.
  14.  *
  15.  * <P>A ResultSet maintains a cursor pointing to its current row of data.  
  16.  * Initially the cursor is positioned before the first row.  The 'next'
  17.  * method moves the cursor to the next row.
  18.  *
  19.  * <P>The getXXX methods retrieve column values for the current row.  You can
  20.  * retrieve values either using the index number of the column, or by using
  21.  * the name of the column.  In general using the column index will be more
  22.  * efficient.  Columns are numbered from 1.
  23.  *
  24.  * <P>For maximum portability, ResultSet columns within each row should be read
  25.  * in left-to-right order and each column should be read only once.
  26.  *
  27.  *<P> For the getXXX methods, the JDBC driver attempts to convert the
  28.  * underlying data to the specified Java type and returns a suitable Java
  29.  * value.  See the JDBC specification for allowable mappings from SQL types
  30.  * to Java types with the ResultSet getXXX methods.
  31.  *
  32.  * <P>Column names used as input to getXXX methods are case insenstive.  When
  33.  * performing a getXXX using a column name, if several columns have the same
  34.  * name, then the value of the first matching column will be returned.  The
  35.  * column name option is designed to be used when column names are used in the
  36.  * SQL Query.  For columns that are NOT explicitly named in the query, it is
  37.  * best to use column numbers.  If column names were used there is no way for
  38.  * the programmer to guarentee that they actually refer to the intended
  39.  * columns.
  40.  *
  41.  * <P>A ResultSet is automatically closed by the Statement that generated it 
  42.  * when that Statement is closed, re-executed, or is used to retrieve the 
  43.  * next result from a sequence of multiple results.
  44.  *
  45.  * <P>The number, types and properties of a ResultSet's columns are provided by
  46.  * the ResultSetMetaData object returned by the getMetaData method.
  47.  *
  48.  * @see ResultSetMetaData
  49.  * @see java.sql.ResultSet
  50.  */
  51. public class ResultSet implements java.sql.ResultSet 
  52. {
  53.   Vector rows; // The results
  54.   Field fields[]; // The field descriptions
  55.   String status; // Status of the result
  56.   int updateCount; // How many rows did we get back?
  57.   int current_row; // Our pointer to where we are at
  58.   byte[][] this_row; // the current row result
  59.   Connection connection; // the connection which we returned from
  60.   SQLWarning warnings = null; // The warning chain
  61.   boolean wasNullFlag = false; // the flag for wasNull()
  62.   
  63.   //  We can chain multiple resultSets together - this points to
  64.   // next resultSet in the chain.
  65.   private ResultSet next = null;
  66.   
  67.   /**
  68.    * Create a new ResultSet - Note that we create ResultSets to
  69.    * represent the results of everything.
  70.    *
  71.    * @param fields an array of Field objects (basically, the
  72.    * ResultSet MetaData)
  73.    * @param tuples Vector of the actual data
  74.    * @param status the status string returned from the back end
  75.    * @param updateCount the number of rows affected by the operation
  76.    * @param cursor the positioned update/delete cursor name
  77.    */
  78.   public ResultSet(Connection conn, Field[] fields, Vector tuples, String status, int updateCount)
  79.   {
  80.     this.connection = conn;
  81.     this.fields = fields;
  82.     this.rows = tuples;
  83.     this.status = status;
  84.     this.updateCount = updateCount;
  85.     this.this_row = null;
  86.     this.current_row = -1;
  87.   }
  88.   
  89.   /**
  90.    * A ResultSet is initially positioned before its first row,
  91.    * the first call to next makes the first row the current row;
  92.    * the second call makes the second row the current row, etc.
  93.    *
  94.    * <p>If an input stream from the previous row is open, it is
  95.    * implicitly closed.  The ResultSet's warning chain is cleared
  96.    * when a new row is read
  97.    *
  98.    * @return true if the new current is valid; false if there are no
  99.    * more rows
  100.    * @exception SQLException if a database access error occurs
  101.    */
  102.   public boolean next() throws SQLException
  103.   {
  104.     if (++current_row >= rows.size())
  105.       return false;
  106.     this_row = (byte [][])rows.elementAt(current_row);
  107.     return true;
  108.   }
  109.   
  110.   /**
  111.    * In some cases, it is desirable to immediately release a ResultSet
  112.    * database and JDBC resources instead of waiting for this to happen
  113.    * when it is automatically closed.  The close method provides this
  114.    * immediate release.
  115.    *
  116.    * <p><B>Note:</B> A ResultSet is automatically closed by the Statement
  117.    * the Statement that generated it when that Statement is closed,
  118.    * re-executed, or is used to retrieve the next result from a sequence
  119.    * of multiple results.  A ResultSet is also automatically closed 
  120.    * when it is garbage collected.
  121.    *
  122.    * @exception SQLException if a database access error occurs
  123.    */
  124.   public void close() throws SQLException
  125.   {
  126.     // No-op
  127.   }
  128.   
  129.   /**
  130.    * A column may have the value of SQL NULL; wasNull() reports whether
  131.    * the last column read had this special value.  Note that you must
  132.    * first call getXXX on a column to try to read its value and then
  133.    * call wasNull() to find if the value was SQL NULL
  134.    *
  135.    * @return true if the last column read was SQL NULL
  136.    * @exception SQLException if a database access error occurred
  137.    */
  138.   public boolean wasNull() throws SQLException
  139.   {
  140.     return wasNullFlag;
  141.   }
  142.   
  143.   /**
  144.    * Get the value of a column in the current row as a Java String
  145.    *
  146.    * @param columnIndex the first column is 1, the second is 2...
  147.    * @return the column value, null for SQL NULL
  148.    * @exception SQLException if a database access error occurs
  149.    */
  150.   public String getString(int columnIndex) throws SQLException
  151.   {
  152.     //byte[] bytes = getBytes(columnIndex);
  153.     //
  154.     //if (bytes == null)
  155.     //return null;
  156.     //return new String(bytes);
  157.     if (columnIndex < 1 || columnIndex > fields.length)
  158.       throw new SQLException("Column Index out of range");
  159.     wasNullFlag = (this_row[columnIndex - 1] == null);
  160.     if(wasNullFlag)
  161.       return null;
  162.     return new String(this_row[columnIndex - 1]);
  163.   }
  164.   
  165.   /**
  166.    * Get the value of a column in the current row as a Java boolean
  167.    *
  168.    * @param columnIndex the first column is 1, the second is 2...
  169.    * @return the column value, false for SQL NULL
  170.    * @exception SQLException if a database access error occurs
  171.    */
  172.   public boolean getBoolean(int columnIndex) throws SQLException
  173.   {
  174.     String s = getString(columnIndex);
  175.     
  176.     if (s != null)
  177.       {
  178. int c = s.charAt(0);
  179. return ((c == 't') || (c == 'T'));
  180.       }
  181.     return false; // SQL NULL
  182.   }
  183.   
  184.   /**
  185.    * Get the value of a column in the current row as a Java byte.
  186.    *
  187.    * @param columnIndex the first column is 1, the second is 2,...
  188.    * @return the column value; 0 if SQL NULL
  189.    * @exception SQLException if a database access error occurs
  190.    */
  191.   public byte getByte(int columnIndex) throws SQLException
  192.   {
  193.     String s = getString(columnIndex);
  194.     
  195.     if (s != null)
  196.       {
  197. try
  198.   {
  199.     return Byte.parseByte(s);
  200.   } catch (NumberFormatException e) {
  201.     throw new SQLException("Bad Byte Form: " + s);
  202.   }
  203.       }
  204.     return 0; // SQL NULL
  205.   }
  206.   
  207.   /**
  208.    * Get the value of a column in the current row as a Java short.
  209.    *
  210.    * @param columnIndex the first column is 1, the second is 2,...
  211.    * @return the column value; 0 if SQL NULL
  212.    * @exception SQLException if a database access error occurs
  213.    */
  214.   public short getShort(int columnIndex) throws SQLException
  215.   {
  216.     String s = getString(columnIndex);
  217.     
  218.     if (s != null)
  219.       {
  220. try
  221.   {
  222.     return Short.parseShort(s);
  223.   } catch (NumberFormatException e) {
  224.     throw new SQLException("Bad Short Form: " + s);
  225.   }
  226.       }
  227.     return 0; // SQL NULL
  228.   }
  229.   
  230.   /**
  231.    * Get the value of a column in the current row as a Java int.
  232.    *
  233.    * @param columnIndex the first column is 1, the second is 2,...
  234.    * @return the column value; 0 if SQL NULL
  235.    * @exception SQLException if a database access error occurs
  236.    */
  237.   public int getInt(int columnIndex) throws SQLException
  238.   {
  239.     String s = getString(columnIndex);
  240.     
  241.     if (s != null)
  242.       {
  243. try
  244.   {
  245.     return Integer.parseInt(s);
  246.   } catch (NumberFormatException e) {
  247.     throw new SQLException ("Bad Integer Form: " + s);
  248.   }
  249.       }
  250.     return 0; // SQL NULL
  251.   }
  252.   
  253.   /**
  254.    * Get the value of a column in the current row as a Java long.
  255.    *
  256.    * @param columnIndex the first column is 1, the second is 2,...
  257.    * @return the column value; 0 if SQL NULL
  258.    * @exception SQLException if a database access error occurs
  259.    */
  260.   public long getLong(int columnIndex) throws SQLException
  261.   {
  262.     String s = getString(columnIndex);
  263.     
  264.     if (s != null)
  265.       {
  266. try
  267.   {
  268.     return Long.parseLong(s);
  269.   } catch (NumberFormatException e) {
  270.     throw new SQLException ("Bad Long Form: " + s);
  271.   }
  272.       }
  273.     return 0; // SQL NULL
  274.   }
  275.   
  276.   /**
  277.    * Get the value of a column in the current row as a Java float.
  278.    *
  279.    * @param columnIndex the first column is 1, the second is 2,...
  280.    * @return the column value; 0 if SQL NULL
  281.    * @exception SQLException if a database access error occurs
  282.    */
  283.   public float getFloat(int columnIndex) throws SQLException
  284.   {
  285.     String s = getString(columnIndex);
  286.     
  287.     if (s != null)
  288.       {
  289. try
  290.   {
  291.     return Float.valueOf(s).floatValue();
  292.   } catch (NumberFormatException e) {
  293.     throw new SQLException ("Bad Float Form: " + s);
  294.   }
  295.       }
  296.     return 0; // SQL NULL
  297.   }
  298.   
  299.   /**
  300.    * Get the value of a column in the current row as a Java double.
  301.    *
  302.    * @param columnIndex the first column is 1, the second is 2,...
  303.    * @return the column value; 0 if SQL NULL
  304.    * @exception SQLException if a database access error occurs
  305.    */
  306.   public double getDouble(int columnIndex) throws SQLException
  307.   {
  308.     String s = getString(columnIndex);
  309.     
  310.     if (s != null)
  311.       {
  312. try
  313.   {
  314.     return Double.valueOf(s).doubleValue();
  315.   } catch (NumberFormatException e) {
  316.     throw new SQLException ("Bad Double Form: " + s);
  317.   }
  318.       }
  319.     return 0; // SQL NULL
  320.   }
  321.   
  322.   /**
  323.    * Get the value of a column in the current row as a 
  324.    * java.lang.BigDecimal object
  325.    *
  326.    * @param columnIndex  the first column is 1, the second is 2...
  327.    * @param scale the number of digits to the right of the decimal
  328.    * @return the column value; if the value is SQL NULL, null
  329.    * @exception SQLException if a database access error occurs
  330.    */
  331.   public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException
  332.   {
  333.     String s = getString(columnIndex);
  334.     BigDecimal val;
  335.     
  336.     if (s != null)
  337.       {
  338. try
  339.   {
  340.     val = new BigDecimal(s);
  341.   } catch (NumberFormatException e) {
  342.     throw new SQLException ("Bad BigDecimal Form: " + s);
  343.   }
  344.   try
  345.     {
  346.       return val.setScale(scale);
  347.     } catch (ArithmeticException e) {
  348.       throw new SQLException ("Bad BigDecimal Form: " + s);
  349.     }
  350.       }
  351.     return null; // SQL NULL
  352.   }
  353.   
  354.   /**
  355.    * Get the value of a column in the current row as a Java byte array.
  356.    *
  357.    * <p>In normal use, the bytes represent the raw values returned by the
  358.    * backend. However, if the column is an OID, then it is assumed to
  359.    * refer to a Large Object, and that object is returned as a byte array.
  360.    *
  361.    * <p><b>Be warned</b> If the large object is huge, then you may run out
  362.    * of memory.
  363.    *
  364.    * @param columnIndex the first column is 1, the second is 2, ...
  365.    * @return the column value; if the value is SQL NULL, the result
  366.    * is null
  367.    * @exception SQLException if a database access error occurs
  368.    */
  369.   public byte[] getBytes(int columnIndex) throws SQLException
  370.   {
  371.     if (columnIndex < 1 || columnIndex > fields.length)
  372.       throw new SQLException("Column Index out of range");
  373.     wasNullFlag = (this_row[columnIndex - 1] == null);
  374.     
  375.     // Handle OID's as BLOBS
  376.     if(!wasNullFlag)
  377.       if( fields[columnIndex - 1].getOID() == 26) {
  378. LargeObjectManager lom = connection.getLargeObjectAPI();
  379. LargeObject lob = lom.open(getInt(columnIndex));
  380. byte buf[] = lob.read(lob.size());
  381. lob.close();
  382. return buf;
  383.       }
  384.     
  385.     return this_row[columnIndex - 1];
  386.   }
  387.   
  388.   /**
  389.    * Get the value of a column in the current row as a java.sql.Date
  390.    * object
  391.    *
  392.    * @param columnIndex the first column is 1, the second is 2...
  393.    * @return the column value; null if SQL NULL
  394.    * @exception SQLException if a database access error occurs
  395.    */
  396.   public java.sql.Date getDate(int columnIndex) throws SQLException
  397.   {
  398.     String s = getString(columnIndex);
  399.     if(s==null)
  400.       return null;
  401.     SimpleDateFormat df = new SimpleDateFormat(connection.getDateStyle());
  402.     try {
  403.       return new java.sql.Date(df.parse(s).getTime());
  404.     } catch (ParseException e) {
  405.       throw new SQLException("Bad Date Format: at " + e.getErrorOffset() + " in " + s);
  406.     }
  407.   }
  408.   
  409.   /**
  410.    * Get the value of a column in the current row as a java.sql.Time
  411.    * object
  412.    *
  413.    * @param columnIndex the first column is 1, the second is 2...
  414.    * @return the column value; null if SQL NULL
  415.    * @exception SQLException if a database access error occurs
  416.    */
  417.   public Time getTime(int columnIndex) throws SQLException
  418.   {
  419.     String s = getString(columnIndex);
  420.     
  421.     if (s != null)
  422.       {
  423. try
  424.   {
  425.     if (s.length() != 5 && s.length() != 8)
  426.       throw new NumberFormatException("Wrong Length!");
  427.     int hr = Integer.parseInt(s.substring(0,2));
  428.     int min = Integer.parseInt(s.substring(3,5));
  429.     int sec = (s.length() == 5) ? 0 : Integer.parseInt(s.substring(6));
  430.     return new Time(hr, min, sec);
  431.   } catch (NumberFormatException e) {
  432.     throw new SQLException ("Bad Time Form: " + s);
  433.   }
  434.       }
  435.     return null; // SQL NULL
  436.   }
  437.   
  438.   /**
  439.    * Get the value of a column in the current row as a 
  440.    * java.sql.Timestamp object
  441.    *
  442.    * @param columnIndex the first column is 1, the second is 2...
  443.    * @return the column value; null if SQL NULL
  444.    * @exception SQLException if a database access error occurs
  445.    */
  446.   public Timestamp getTimestamp(int columnIndex) throws SQLException
  447.   {
  448.     String s = getString(columnIndex);
  449.     SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:sszzz");
  450.     
  451.     if (s != null)
  452.       {
  453. int TZ = new Float(s.substring(19)).intValue();
  454. TZ = TZ * 60 * 60 * 1000;
  455. TimeZone zone = TimeZone.getDefault();
  456. zone.setRawOffset(TZ);
  457. String nm = zone.getID();
  458. // s = s.substring(0,19) + nm;
  459. try {
  460.   java.util.Date d = df.parse(s);
  461.   return new Timestamp(d.getTime());
  462. } catch (ParseException e) {
  463.   throw new SQLException("Bad Timestamp Format: at " + e.getErrorOffset() + " in " + s);
  464. }
  465.       }
  466.     return null;                // SQL NULL
  467.   }
  468.   
  469.   /**
  470.    * A column value can be retrieved as a stream of ASCII characters
  471.    * and then read in chunks from the stream.  This method is 
  472.    * particular suitable for retrieving large LONGVARCHAR values.
  473.    * The JDBC driver will do any necessary conversion from the
  474.    * database format into ASCII.
  475.    *
  476.    * <p><B>Note:</B> All the data in the returned stream must be read
  477.    * prior to getting the value of any other column.  The next call
  478.    * to a get method implicitly closes the stream.  Also, a stream
  479.    * may return 0 for available() whether there is data available
  480.    * or not.
  481.    *
  482.    *<p> We implement an ASCII stream as a Binary stream - we should really
  483.    * do the data conversion, but I cannot be bothered to implement this
  484.    * right now.
  485.    *
  486.    * @param columnIndex the first column is 1, the second is 2, ...
  487.    * @return a Java InputStream that delivers the database column
  488.    *  value as a stream of one byte ASCII characters.  If the
  489.    * value is SQL NULL then the result is null
  490.    * @exception SQLException if a database access error occurs
  491.    * @see getBinaryStream
  492.    */
  493.   public InputStream getAsciiStream(int columnIndex) throws SQLException
  494.   {
  495.     return getBinaryStream(columnIndex);
  496.   }
  497.   
  498.   /**
  499.    * A column value can also be retrieved as a stream of Unicode
  500.    * characters. We implement this as a binary stream.
  501.    *
  502.    * @param columnIndex the first column is 1, the second is 2...
  503.    * @return a Java InputStream that delivers the database column value
  504.    *  as a stream of two byte Unicode characters.  If the value is
  505.    * SQL NULL, then the result is null
  506.    * @exception SQLException if a database access error occurs
  507.    * @see getAsciiStream
  508.    * @see getBinaryStream
  509.    */
  510.   public InputStream getUnicodeStream(int columnIndex) throws SQLException
  511.   {
  512.     return getBinaryStream(columnIndex);
  513.   }
  514.   
  515.   /**
  516.    * A column value can also be retrieved as a binary strea.  This
  517.    * method is suitable for retrieving LONGVARBINARY values.
  518.    *
  519.    * @param columnIndex the first column is 1, the second is 2...
  520.    * @return a Java InputStream that delivers the database column value
  521.    * as a stream of bytes.  If the value is SQL NULL, then the result
  522.    * is null
  523.    * @exception SQLException if a database access error occurs
  524.    * @see getAsciiStream
  525.    * @see getUnicodeStream
  526.    */
  527.   public InputStream getBinaryStream(int columnIndex) throws SQLException
  528.   {
  529.     byte b[] = getBytes(columnIndex);
  530.     
  531.     if (b != null)
  532.       return new ByteArrayInputStream(b);
  533.     return null; // SQL NULL
  534.   }
  535.   
  536.   /**
  537.    * The following routines simply convert the columnName into
  538.    * a columnIndex and then call the appropriate routine above.
  539.    *
  540.    * @param columnName is the SQL name of the column
  541.    * @return the column value
  542.    * @exception SQLException if a database access error occurs
  543.    */
  544.   public String getString(String columnName) throws SQLException
  545.   {
  546.     return getString(findColumn(columnName));
  547.   }
  548.   
  549.   public boolean getBoolean(String columnName) throws SQLException
  550.   {
  551.     return getBoolean(findColumn(columnName));
  552.   }
  553.   
  554.   public byte getByte(String columnName) throws SQLException
  555.   {
  556.     
  557.     return getByte(findColumn(columnName));
  558.   }
  559.   
  560.   public short getShort(String columnName) throws SQLException
  561.   {
  562.     return getShort(findColumn(columnName));
  563.   }
  564.   
  565.   public int getInt(String columnName) throws SQLException
  566.   {
  567.     return getInt(findColumn(columnName));
  568.   }
  569.   
  570.   public long getLong(String columnName) throws SQLException
  571.   {
  572.     return getLong(findColumn(columnName));
  573.   }
  574.   
  575.   public float getFloat(String columnName) throws SQLException
  576.   {
  577.     return getFloat(findColumn(columnName));
  578.   }
  579.   
  580.   public double getDouble(String columnName) throws SQLException
  581.   {
  582.     return getDouble(findColumn(columnName));
  583.   }
  584.   
  585.   public BigDecimal getBigDecimal(String columnName, int scale) throws SQLException
  586.   {
  587.     return getBigDecimal(findColumn(columnName), scale);
  588.   }
  589.   
  590.   public byte[] getBytes(String columnName) throws SQLException
  591.   {
  592.     return getBytes(findColumn(columnName));
  593.   }
  594.   
  595.   public java.sql.Date getDate(String columnName) throws SQLException
  596.   {
  597.     return getDate(findColumn(columnName));
  598.   }
  599.   
  600.   public Time getTime(String columnName) throws SQLException
  601.   {
  602.     return getTime(findColumn(columnName));
  603.   }
  604.   
  605.   public Timestamp getTimestamp(String columnName) throws SQLException
  606.   {
  607.     return getTimestamp(findColumn(columnName));
  608.   }
  609.   
  610.   public InputStream getAsciiStream(String columnName) throws SQLException
  611.   {
  612.     return getAsciiStream(findColumn(columnName));
  613.   }
  614.   
  615.   public InputStream getUnicodeStream(String columnName) throws SQLException
  616.   {
  617.     return getUnicodeStream(findColumn(columnName));
  618.   }
  619.   
  620.   public InputStream getBinaryStream(String columnName) throws SQLException
  621.   {
  622.     return getBinaryStream(findColumn(columnName));
  623.   }
  624.   
  625.   /**
  626.    * The first warning reported by calls on this ResultSet is
  627.    * returned.  Subsequent ResultSet warnings will be chained
  628.    * to this SQLWarning.
  629.    *
  630.    * <p>The warning chain is automatically cleared each time a new
  631.    * row is read.
  632.    *
  633.    * <p><B>Note:</B> This warning chain only covers warnings caused by
  634.    * ResultSet methods.  Any warnings caused by statement methods
  635.    * (such as reading OUT parameters) will be chained on the
  636.    * Statement object.
  637.    *
  638.    * @return the first SQLWarning or null;
  639.    * @exception SQLException if a database access error occurs.
  640.    */
  641.   public SQLWarning getWarnings() throws SQLException
  642.   {
  643.     return warnings;
  644.   }
  645.   
  646.   /**
  647.    * After this call, getWarnings returns null until a new warning
  648.    * is reported for this ResultSet
  649.    *
  650.    * @exception SQLException if a database access error occurs
  651.    */
  652.   public void clearWarnings() throws SQLException
  653.   {
  654.     warnings = null;
  655.   }
  656.   
  657.   /**
  658.    * Get the name of the SQL cursor used by this ResultSet
  659.    *
  660.    * <p>In SQL, a result table is retrieved though a cursor that is
  661.    * named.  The current row of a result can be updated or deleted
  662.    * using a positioned update/delete statement that references
  663.    * the cursor name.
  664.    *
  665.    * <p>JDBC supports this SQL feature by providing the name of the
  666.    * SQL cursor used by a ResultSet.  The current row of a ResulSet
  667.    * is also the current row of this SQL cursor.
  668.    *
  669.    * <p><B>Note:</B> If positioned update is not supported, a SQLException
  670.    * is thrown.
  671.    *
  672.    * @return the ResultSet's SQL cursor name.
  673.    * @exception SQLException if a database access error occurs
  674.    */
  675.   public String getCursorName() throws SQLException
  676.   {
  677.     return connection.getCursorName();
  678.   }
  679.   
  680.   /**
  681.    * The numbers, types and properties of a ResultSet's columns are
  682.    * provided by the getMetaData method
  683.    *
  684.    * @return a description of the ResultSet's columns
  685.    * @exception SQLException if a database access error occurs
  686.    */
  687.   public java.sql.ResultSetMetaData getMetaData() throws SQLException
  688.   {
  689.     return new ResultSetMetaData(rows, fields);
  690.   }
  691.   
  692.   /**
  693.    * Get the value of a column in the current row as a Java object
  694.    *
  695.    * <p>This method will return the value of the given column as a
  696.    * Java object.  The type of the Java object will be the default
  697.    * Java Object type corresponding to the column's SQL type, following
  698.    * the mapping specified in the JDBC specification.
  699.    *
  700.    * <p>This method may also be used to read database specific abstract
  701.    * data types.
  702.    *
  703.    * @param columnIndex the first column is 1, the second is 2...
  704.    * @return a Object holding the column value
  705.    * @exception SQLException if a database access error occurs
  706.    */
  707.   public Object getObject(int columnIndex) throws SQLException
  708.   {
  709.     Field field;
  710.     
  711.     if (columnIndex < 1 || columnIndex > fields.length)
  712.       throw new SQLException("Column index out of range");
  713.     field = fields[columnIndex - 1];
  714.     
  715.     // some fields can be null, mainly from those returned by MetaData methods
  716.     if(field==null) {
  717.       wasNullFlag=true;
  718.       return null;
  719.     }
  720.     
  721.     switch (field.getSQLType())
  722.       {
  723.       case Types.BIT:
  724. return new Boolean(getBoolean(columnIndex));
  725.       case Types.SMALLINT:
  726. return new Integer(getInt(columnIndex));
  727.       case Types.INTEGER:
  728. return new Integer(getInt(columnIndex));
  729.       case Types.BIGINT:
  730. return new Long(getLong(columnIndex));
  731.       case Types.NUMERIC:
  732. return getBigDecimal(columnIndex, 0);
  733.       case Types.REAL:
  734. return new Float(getFloat(columnIndex));
  735.       case Types.DOUBLE:
  736. return new Double(getDouble(columnIndex));
  737.       case Types.CHAR:
  738.       case Types.VARCHAR:
  739. return getString(columnIndex);
  740.       case Types.DATE:
  741. return getDate(columnIndex);
  742.       case Types.TIME:
  743. return getTime(columnIndex);
  744.       case Types.TIMESTAMP:
  745. return getTimestamp(columnIndex);
  746.       default:
  747. return connection.getObject(field.getTypeName(), getString(columnIndex));
  748.       }
  749.   }
  750.   
  751.   /**
  752.    * Get the value of a column in the current row as a Java object
  753.    *
  754.    *<p> This method will return the value of the given column as a
  755.    * Java object.  The type of the Java object will be the default
  756.    * Java Object type corresponding to the column's SQL type, following
  757.    * the mapping specified in the JDBC specification.
  758.    *
  759.    * <p>This method may also be used to read database specific abstract
  760.    * data types.
  761.    *
  762.    * @param columnName is the SQL name of the column
  763.    * @return a Object holding the column value
  764.    * @exception SQLException if a database access error occurs
  765.    */
  766.   public Object getObject(String columnName) throws SQLException
  767.   {
  768.     return getObject(findColumn(columnName));
  769.   }
  770.   
  771.   /**
  772.    * Map a ResultSet column name to a ResultSet column index
  773.    *
  774.    * @param columnName the name of the column
  775.    * @return the column index
  776.    * @exception SQLException if a database access error occurs
  777.    */
  778.   public int findColumn(String columnName) throws SQLException
  779.   {
  780.     int i;
  781.     
  782.     for (i = 0 ; i < fields.length; ++i)
  783.       if (fields[i].name.equalsIgnoreCase(columnName))
  784. return (i+1);
  785.     throw new SQLException ("Column name not found");
  786.   }
  787.   
  788.   // ************************************************************
  789.   // END OF PUBLIC INTERFACE
  790.   // ************************************************************
  791.   
  792.   /**
  793.    * We at times need to know if the resultSet we are working
  794.    * with is the result of an UPDATE, DELETE or INSERT (in which
  795.    * case, we only have a row count), or of a SELECT operation
  796.    * (in which case, we have multiple fields) - this routine
  797.    * tells us.
  798.    *
  799.    * @return true if we have tuples available
  800.    */
  801.   public boolean reallyResultSet()
  802.   {
  803.     return (fields != null);
  804.   }
  805.   
  806.   /**
  807.    * Since ResultSets can be chained, we need some method of
  808.    * finding the next one in the chain.  The method getNext()
  809.    * returns the next one in the chain.
  810.    *
  811.    * @return the next ResultSet, or null if there are none
  812.    */
  813.   public ResultSet getNext()
  814.   {
  815.     return next;
  816.   }
  817.   
  818.   /**
  819.    * This following method allows us to add a ResultSet object
  820.    * to the end of the current chain.
  821.    *
  822.    * @param r the resultset to add to the end of the chain.
  823.    */
  824.   public void append(ResultSet r)
  825.   {
  826.     if (next == null)
  827.       next = r;
  828.     else
  829.       next.append(r);
  830.   }
  831.   
  832.   /**
  833.    * If we are just a place holder for results, we still need
  834.    * to get an updateCount.  This method returns it.
  835.    *
  836.    * @return the updateCount
  837.    */
  838.   public int getResultCount()
  839.   {
  840.     return updateCount;
  841.   }
  842.   
  843.   /**
  844.    * We also need to provide a couple of auxiliary functions for
  845.    * the implementation of the ResultMetaData functions.  In
  846.    * particular, we need to know the number of rows and the
  847.    * number of columns.  Rows are also known as Tuples
  848.    *
  849.    * @return the number of rows
  850.    */
  851.   public int getTupleCount()
  852.   {
  853.     return rows.size();
  854.   }
  855.   
  856.   /**
  857.    * getColumnCount returns the number of columns
  858.    *
  859.    * @return the number of columns
  860.    */
  861.   public int getColumnCount()
  862.   {
  863.     return fields.length;
  864.   }
  865.    
  866.    /**
  867.     * Returns the status message from the backend.<p>
  868.     * It is used internally by the driver.
  869.     *
  870.     * @return the status string from the backend
  871.     */
  872.    public String getStatusString()
  873.    {
  874.      return status;
  875.    }
  876.    
  877.    /**
  878.     * returns the OID of a field.<p>
  879.     * It is used internally by the driver.
  880.     *
  881.     * @param field field id
  882.     * @return the oid of that field's type
  883.     */
  884.    public int getColumnOID(int field)
  885.    {
  886.      return fields[field-1].getOID();
  887.    }
  888. }