RowDataDynamic.java
上传用户:tanyanyong
上传日期:2013-06-23
资源大小:1355k
文件大小:10k
源码类别:

电子政务应用

开发平台:

MultiPlatform

  1. /*
  2.    Copyright (C) 2002 MySQL AB
  3.       This program is free software; you can redistribute it and/or modify
  4.       it under the terms of the GNU General Public License as published by
  5.       the Free Software Foundation; either version 2 of the License, or
  6.       (at your option) any later version.
  7.       This program is distributed in the hope that it will be useful,
  8.       but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.       GNU General Public License for more details.
  11.       You should have received a copy of the GNU General Public License
  12.       along with this program; if not, write to the Free Software
  13.       Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  14.  */
  15. package com.mysql.jdbc;
  16. import java.io.IOException;
  17. import java.sql.SQLException;
  18. /**
  19.  * Allows streaming of MySQL data.
  20.  *
  21.  * @author dgan
  22.  * @version $Id: RowDataDynamic.java,v 1.8.2.6 2003/12/24 05:16:24 mmatthew Exp $
  23.  */
  24. public class RowDataDynamic implements RowData {
  25.     private MysqlIO io;
  26.     private byte[][] nextRow;
  27.     private boolean isAfterEnd = false;
  28.     private boolean isAtEnd = false;
  29.     private boolean streamerClosed = false;
  30.     private int columnCount;
  31.     private int index = -1;
  32.     private long lastSuccessfulReadTimeMs = 0;
  33.     private long netWriteTimeoutMs = 0;
  34.     private ResultSet owner;
  35.     /**
  36.      * Creates a new RowDataDynamic object.
  37.      *
  38.      * @param io DOCUMENT ME!
  39.      * @param colCount DOCUMENT ME!
  40.      *
  41.      * @throws SQLException DOCUMENT ME!
  42.      */
  43.     public RowDataDynamic(MysqlIO io, int colCount) throws SQLException {
  44.         this.io = io;
  45.         this.columnCount = colCount;
  46.         nextRecord();
  47.     }
  48.     /**
  49.      * Returns true if we got the last element.
  50.      *
  51.      * @return true if after last row
  52.      *
  53.      * @throws SQLException if a database error occurs
  54.      */
  55.     public boolean isAfterLast() throws SQLException {
  56.         return isAfterEnd;
  57.     }
  58.     /**
  59.      * Only works on non dynamic result sets.
  60.      *
  61.      * @param index row number to get at
  62.      *
  63.      * @return row data at index
  64.      *
  65.      * @throws SQLException if a database error occurs
  66.      */
  67.     public byte[][] getAt(int index) throws SQLException {
  68.         notSupported();
  69.         return null;
  70.     }
  71.     /**
  72.      * Returns if iteration has not occured yet.
  73.      *
  74.      * @return true if before first row
  75.      *
  76.      * @throws SQLException if a database error occurs
  77.      */
  78.     public boolean isBeforeFirst() throws SQLException {
  79.         return index < 0;
  80.     }
  81.     /**
  82.      * Moves the current position in the result set to the given row number.
  83.      *
  84.      * @param rowNumber row to move to
  85.      *
  86.      * @throws SQLException if a database error occurs
  87.      */
  88.     public void setCurrentRow(int rowNumber) throws SQLException {
  89.         notSupported();
  90.     }
  91.     
  92. /**
  93.  * @see com.mysql.jdbc.RowData#setOwner(com.mysql.jdbc.ResultSet)
  94.  */
  95. public void setOwner(ResultSet rs) {
  96. this.owner = rs;
  97. }
  98. /**
  99.  * @see com.mysql.jdbc.RowData#getOwner()
  100.  */
  101. public ResultSet getOwner() {
  102. return this.owner;
  103. }
  104.     /**
  105.      * Returns the current position in the result set as a row number.
  106.      *
  107.      * @return the current row number
  108.      *
  109.      * @throws SQLException if a database error occurs
  110.      */
  111.     public int getCurrentRowNumber() throws SQLException {
  112.         notSupported();
  113.         return -1;
  114.     }
  115.     /**
  116.      * Returns true if the result set is dynamic. This means that move back and
  117.      * move forward won't work because we do not hold on to the records.
  118.      *
  119.      * @return true if this result set is streaming from the server
  120.      */
  121.     public boolean isDynamic() {
  122.         return true;
  123.     }
  124.     /**
  125.      * Has no records.
  126.      *
  127.      * @return true if no records
  128.      *
  129.      * @throws SQLException if a database error occurs
  130.      */
  131.     public boolean isEmpty() throws SQLException {
  132.         notSupported();
  133.         return false;
  134.     }
  135.     /**
  136.      * Are we on the first row of the result set?
  137.      *
  138.      * @return true if on first row
  139.      *
  140.      * @throws SQLException if a database error occurs
  141.      */
  142.     public boolean isFirst() throws SQLException {
  143.         notSupported();
  144.         return false;
  145.     }
  146.     /**
  147.      * Are we on the last row of the result set?
  148.      *
  149.      * @return true if on last row
  150.      *
  151.      * @throws SQLException if a database error occurs
  152.      */
  153.     public boolean isLast() throws SQLException {
  154.         notSupported();
  155.         return false;
  156.     }
  157.     /**
  158.      * Adds a row to this row data.
  159.      *
  160.      * @param row the row to add
  161.      *
  162.      * @throws SQLException if a database error occurs
  163.      */
  164.     public void addRow(byte[][] row) throws SQLException {
  165.         notSupported();
  166.     }
  167.     /**
  168.      * Moves to after last.
  169.      *
  170.      * @throws SQLException if a database error occurs
  171.      */
  172.     public void afterLast() throws SQLException {
  173.         notSupported();
  174.     }
  175.     /**
  176.      * Moves to before first.
  177.      *
  178.      * @throws SQLException if a database error occurs
  179.      */
  180.     public void beforeFirst() throws SQLException {
  181.         notSupported();
  182.     }
  183.     /**
  184.      * Moves to before last so next el is the last el.
  185.      *
  186.      * @throws SQLException if a database error occurs
  187.      */
  188.     public void beforeLast() throws SQLException {
  189.         notSupported();
  190.     }
  191.     /**
  192.      * We're done.
  193.      *
  194.      * @throws SQLException if a database error occurs
  195.      */
  196.     public void close() throws SQLException {
  197.         //drain the rest of the records.
  198.         int count = 0;
  199.         
  200.         while (this.hasNext()) {
  201.             this.next();
  202.             
  203.             count++;
  204.             
  205.             if (count == 100) {
  206.              Thread.yield();
  207.              count = 0;
  208.             }
  209.         }
  210.     }
  211.     /**
  212.      * Returns true if another row exsists.
  213.      *
  214.      * @return true if more rows
  215.      *
  216.      * @throws SQLException if a database error occurs
  217.      */
  218.     public boolean hasNext() throws SQLException {
  219.         boolean hasNext = (nextRow != null);
  220.         if (!hasNext && !streamerClosed) {
  221.             io.closeStreamer(this);
  222.             streamerClosed = true;
  223.         }
  224.         return hasNext;
  225.     }
  226.     /**
  227.      * Moves the current position relative 'rows' from the current position.
  228.      *
  229.      * @param rows the relative number of rows to move
  230.      *
  231.      * @throws SQLException if a database error occurs
  232.      */
  233.     public void moveRowRelative(int rows) throws SQLException {
  234.         notSupported();
  235.     }
  236.     /**
  237.      * Returns the next row.
  238.      *
  239.      * @return the next row value
  240.      *
  241.      * @throws SQLException if a database error occurs
  242.      */
  243.     public byte[][] next() throws SQLException {
  244.         index++;
  245.         byte[][] ret = nextRow;
  246.         nextRecord();
  247.         return ret;
  248.     }
  249.     /**
  250.      * Removes the row at the given index.
  251.      *
  252.      * @param index the row to move to
  253.      *
  254.      * @throws SQLException if a database error occurs
  255.      */
  256.     public void removeRow(int index) throws SQLException {
  257.         notSupported();
  258.     }
  259.     /**
  260.      * Only works on non dynamic result sets.
  261.      *
  262.      * @return the size of this row data
  263.      */
  264.     public int size() {
  265.         return RESULT_SET_SIZE_UNKNOWN;
  266.     }
  267.     private void nextRecord() throws SQLException {
  268.         try {
  269.             if (!isAtEnd) {
  270.                 nextRow = io.nextRow((int) columnCount);
  271.                 if (nextRow == null) {
  272.                     isAtEnd = true;
  273.                 }
  274.                 
  275.                 this.lastSuccessfulReadTimeMs = System.currentTimeMillis();
  276.             } else {
  277.                 isAfterEnd = true;
  278.             }
  279.         } catch (SQLException sqlEx) {
  280.             // don't wrap SQLExceptions
  281.             throw sqlEx;
  282.         } catch (IOException ioEx) {
  283.          long timeSinceLastReadMs = System.currentTimeMillis() - this.lastSuccessfulReadTimeMs;
  284.         
  285.             String exceptionType = ioEx.getClass().getName();
  286.             String exceptionMessage = ioEx.getMessage();
  287.             exceptionMessage += "nnNested Stack Trace:n";
  288.             exceptionMessage += Util.stackTraceToString(ioEx);
  289.             throw new java.sql.SQLException(
  290.                 "IOException while retrieving next record in streaming result set."
  291.                 + "(Check for deadlock "
  292.                 + " or retrieval exceeding 'net_write_timeout' seconds. Last "
  293.                 + "successful record read was " + timeSinceLastReadMs + " ms ago, and"                 + "'net_write_timeout' is configured in the server as " + this.netWriteTimeoutMs 
  294.                 + " ms.) : "
  295.                 + exceptionType + " message given: " + exceptionMessage, SQLError.SQL_STATE_GENERAL_ERROR);
  296.         } catch (Exception ex) {
  297.             String exceptionType = ex.getClass().getName();
  298.             String exceptionMessage = ex.getMessage();
  299.             exceptionMessage += "nnNested Stack Trace:n";
  300.             exceptionMessage += Util.stackTraceToString(ex);
  301.             throw new java.sql.SQLException(
  302.                 "Error retrieving record: Unexpected Exception: "
  303.                 + exceptionType + " message given: " + exceptionMessage, SQLError.SQL_STATE_GENERAL_ERROR);
  304.         }
  305.     }
  306.     private void notSupported() throws SQLException {
  307.         throw new OperationNotSupportedException();
  308.     }
  309.     class OperationNotSupportedException extends SQLException {
  310.         OperationNotSupportedException() {
  311.             super("Operation not supported for streaming result sets", SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
  312.         }
  313.     }
  314. }