Package net.helipilot50.stocktrade.genericdbms

Source Code of net.helipilot50.stocktrade.genericdbms.DBDataSet

/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package net.helipilot50.stocktrade.genericdbms;

import java.io.ByteArrayInputStream;
import java.sql.DatabaseMetaData;
import java.sql.Date;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import javax.sql.rowset.CachedRowSet;

import net.helipilot50.stocktrade.framework.BinaryData;
import net.helipilot50.stocktrade.framework.BooleanData;
import net.helipilot50.stocktrade.framework.DataValue;
import net.helipilot50.stocktrade.framework.DateTimeData;
import net.helipilot50.stocktrade.framework.DecimalData;
import net.helipilot50.stocktrade.framework.DoubleData;
import net.helipilot50.stocktrade.framework.ErrorMgr;
import net.helipilot50.stocktrade.framework.IntegerData;
import net.helipilot50.stocktrade.framework.TextData;
import net.helipilot50.stocktrade.framework.UsageException;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.SqlTypeValue;
import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator;


import com.sun.rowset.CachedRowSetImpl;

/**
* The DBDataSet class represents a description of a table, a set of place-holders or their actual data, or a result set or its actual values. When you use the DBSession class interface to construct and execute SQL statements at runtime, you can use this class to store rows either for input to a database or output from a database.
* @author Tim
*/
public class DBDataSet implements net.helipilot50.stocktrade.framework.Constants {
    private PreparedStatement statement = null;
    private ResultSet resultSet = null;
    private ResultSetHelper helper = null;
    private List<String> inputColMapping = null;
    private DatabaseMetaData metaData;
    private String tableName;
    protected PreparedStatementSetterWithNulls setter = new PreparedStatementSetterWithNulls();
    private int maxRows = 0;
    private int startReadingRow = 0;
    private boolean isFirstFetch = true;
    private int rowsFetchedLastRead = 0;

    /**
     * A cache of the number of rows in this dataset.
     */
    private int actualRows = -1;

    public class qq_Resolver
    {
        private qq_Resolver() {}
        public static final int cMAXROWS = 1;
    }
    public DBDataSet() {
        super();
    }

    /**
     * this constructor is not implemented
     * @param count
     * @param resolver
     */
    public DBDataSet(int count, int resolver) {
        this();
    }
    public DBDataSet(PreparedStatement pStatement) {
        this();
        this.statement = pStatement;
        if (pStatement != null) {
            try {
                this.resultSet = pStatement.getResultSet();
            }
            catch (Exception e) {
                this.resultSet = null;
            }
        }
        this.helper = null;
    }

    public DBDataSet(PreparedStatement pStatement, List<String> pInputColMapping) {
        this();
        this.statement = pStatement;
        this.inputColMapping = pInputColMapping;
    }

    public DBDataSet(PreparedStatement pStatement, ResultSet pResults) {
      this(pStatement, pResults, false);
    }
   
    /**
     * This constructor for a DBDataSet creates a new instance that takes the prepared statement,
     * the results from the result set, and a flag that indicates whether the results should be
     * accessible when the results are closed or not. Note that there is a cost associated with
     * setting the pAllowOffline to true -- a CachedResultSet is used to hold the results of the
     * query until it is garbage collected. Hence there is the full read of the results table, plus
     * a memory cost for keeping the resulting in memory
     * @param pStatement
     * @param pResults
     * @param pAllowOffline
     */
    public DBDataSet(PreparedStatement pStatement, ResultSet pResults, boolean pAllowOffline) {
        super();
        this.statement = pStatement;
        if (pAllowOffline) {
          try {
            CachedRowSet rowSet = new CachedRowSetImpl();
            rowSet.populate(pResults);
              this.resultSet = rowSet;
          }
          catch (SQLException e) {
                UsageException errorVar = new UsageException(e.getMessage(), SP_ER_USER, SP_ER_PARAMETERERROR, e);
                ErrorMgr.addError(errorVar);
                throw errorVar;
          }
        }
        else {
          this.resultSet = pResults;
        }
        this.helper = null;
    }

    public DBDataSet(TextData TableName, DatabaseMetaData meta){
        this(TableName.asString(), meta);
    }
    public DBDataSet(String TableName, DatabaseMetaData meta){
        this();
        this.tableName = TableName;
        this.metaData = meta;
    }
    public void setStatement(PreparedStatement pStatement) {
        this.statement = pStatement;
    }

    public PreparedStatement getStatement() {
        return this.statement;
    }

    /**
     * Get the results set associated with this statement. If the statement
     * hasn't been executed yet, null is returned.
     */
    public ResultSetHelper getResultSet() {
        if (this.helper == null) {
            if (this.resultSet == null) {
                try {
                    this.resultSet = this.statement.getResultSet();
                }
                catch (SQLException e){
                    return null;
                }
            }
            if (this.resultSet != null) {
                this.helper = new ResultSetHelper(this.resultSet);
            }
        }
        return this.helper;
    }

    /**
     * Fetch the next 0 or 1 row into this DBDataSet
     * <p>
     * This method will simulate up to 1 being read from the database. Under JDBC, all the rows are
     * transparently fetched, but to maintain compatibility with Forte this method sets up a window into this
     * data set, pretending there are between 0 and 1 there. Subsequent calls to fetch will scroll this
     * window further through the returned data set until the end of the data is reached, in which case 0 will
     * be returned.
     * @param maxRows
     * @return the number of rows in the current data set
     */
    public int fetch() {
      return this.fetch(1);
    }
   
    /**
     * Fetch the next set of rows into this DBDataSet
     * <p>
     * This method will simulate up to maxRows being read from the database. Under JDBC, all the rows are
     * transparently fetched, but to maintain compatibility with Forte this method sets up a window into this
     * data set, pretending there are between 0 and maxRows there. Subsequent calls to fetch will scroll this
     * window further through the returned data set until the end of the data is reached, in which case 0 will
     * be returned.
     * @param maxRows
     * @return the number of rows in the current data set
     */
    public int fetch(int maxRows) {
      if (isFirstFetch) {
          isFirstFetch = false;
      }
      else {
        startReadingRow += rowsFetchedLastRead;
      }
      if (maxRows <= 1) {
        maxRows = 1;
      }
      // If there are no rows to return, skip this altogether
      if (startReadingRow >= trueRowsReturned()) {
        return 0;
      }
      rowsFetchedLastRead = Math.min(maxRows, trueRowsReturned() - startReadingRow);
      // Forte always positioned the cursor on the first line with
      // a fetch (if there were any lines)
      int rowToSelect = 1 + this.startReadingRow;
    this.getResultSet().absolute(rowToSelect);
      return rowsFetchedLastRead;
    }

    /**
     * Return the number of rows in this result set. This method adjusts the returned number of rows so that
     * it is always in the range 0 <= rows <= lastFetchRows where lastFetchRows is the number of rows returned
     * from the last call to Fetch().
     * <p>
     * Note that JDBC has no explicit way of doing this other
     * than finding the last row in the result set and asking for it's index number. This can be a costly
     * operation, and should be used judiciously.<p>
     * <p>
     * <b>Note:</b> Since this implementation seeks the last row in the result set, it cannot be used for result
     * sets that are non-scrollable. That is, the underlying result set must have a Type of TYPE_SCROLL_INSENSITIVE
     * or TYPE_SCROLL_SENSITIVE. If not, this method will always return 1.
     * <p>
     * This method will not affect the row of the result set being considered.
     * @return the number of rows in the result set, if this can be determined. 1 will be returned if the result set
     * is non-scrollable, and 0 will be returned if the result set is null or an exception is thrown
     */
    public int rowsReturned() {
      if (isFirstFetch) {
        // They're not using scrollable results sets, or haven't called fetch yet,
        // just return the real number of rows
        return trueRowsReturned();
      }
      else {
        // We need to cater for the offset of how many rows we've currently read
        int rows = trueRowsReturned();
        return Math.max(0, rows - startReadingRow);
      }
    }
    /**
     * Return the number of rows in this result set. Note that JDBC has no explicit way of doing this other
     * than finding the last row in the result set and asking for it's index number. This can be a costly
     * operation, and should be used judiciously.<p>
     * <p>
     * <b>Note:</b> Since this implementation seeks the last row in the result set, it cannot be used for result
     * sets that are non-scrollable. That is, the underlying result set must have a Type of TYPE_SCROLL_INSENSITIVE
     * or TYPE_SCROLL_SENSITIVE. If not, this method will always return 1.
     * <p>
     * This method will not affect the row of the result set being considered.
     * @return the number of rows in the result set, if this can be determined. 1 will be returned if the result set
     * is non-scrollable, and 0 will be returned if the result set is null or an exception is thrown
     */
    public int trueRowsReturned(){
        if (this.resultSet == null) {
            try {
                this.resultSet = this.statement.getResultSet();
                // Clear out the cached number of rows
                this.actualRows = -1;
            }
            catch (SQLException e){
                return 0;
            }
        }
        if (this.resultSet != null) {
            try {
              // TF:Aug 12, 2008:Added a check to ensure that the result set is scrollable. If it's not,
              // there's no way of telling how many rows we can get back, so just return one.
              if (this.resultSet.getType() == ResultSet.TYPE_FORWARD_ONLY) {
                return 1;
              }
              else if (this.actualRows >= 0) {
                // Get the value out of the class if possible
                return this.actualRows;
              }
              int oldRow = this.resultSet.getRow();
                this.resultSet.last();
                int rowCount = this.resultSet.getRow();
                if (oldRow == 0) {
                  this.resultSet.beforeFirst();
                }
                else if (oldRow > rowCount) {
                  this.resultSet.afterLast();
                }
                else {
                  this.resultSet.absolute(oldRow);
                }
                // Store the value in the cache for next time
                this.actualRows = rowCount;
                return rowCount;
            } catch (SQLException e) {
                return 0;
            }
        }
        return 0;
    }

    /**
     * Moves the cursor to the given row number in
     * this <code>ResultSet</code> object.
     *
     * <p>If the row number is positive, the cursor moves to
     * the given row number with respect to the
     * beginning of the result set.  The first row is row 1, the second
     * is row 2, and so on.
     *
     * <p>If the given row number is negative, the cursor moves to
     * an absolute row position with respect to
     * the end of the result set.  For example, calling the method
     * <code>absolute(-1)</code> positions the
     * cursor on the last row; calling the method <code>absolute(-2)</code>
     * moves the cursor to the next-to-last row, and so on.
     *
     * <p>An attempt to position the cursor beyond the first/last row in
     * the result set leaves the cursor before the first row or after
     * the last row.
     *
     * <p>This method is just an alias for getResultSet().absolute(row)
     *
     * @param row the number of the row to which the cursor should move.
     *        A positive number indicates the row number counting from the
     *        beginning of the result set; a negative number indicates the
     *        row number counting from the end of the result set
     * @return <code>true</code> if the cursor is moved to a position in this
     * <code>ResultSet</code> object;
     * <code>false</code> if the cursor is before the first row or after the
     * last row
     * @exception UsageException if a database access error
     * occurs; this method is called on a closed result set
     * or the result set type is <code>TYPE_FORWARD_ONLY</code>
     * @exception UsageException if the JDBC driver does not support
     * this method
     */
    public boolean setCurrentRow(int row) {
      if (isFirstFetch) {
        // Use the parameter as the real number of rows, we're either not using
        // a scrollable result set or the calls are out of the expected order
          return this.getResultSet().absolute(row);
      }
      else {
        return this.getResultSet().absolute(row + this.startReadingRow);
      }
    }
   
    /**
     * Retrieves the current row number.  The first row is number 1, the
     * second number 2, and so on. 
     * <p>
     * <strong>Note:</strong>Support for the <code>getRow</code> method
     * is optional for <code>ResultSet</code>s with a result
     * set type of <code>TYPE_FORWARD_ONLY</code>
     *
     * <p>This method is just an alias for getResultSet().getRow()
     *
     * @return the current row number; <code>0</code> if there is no current row
     * @exception UsageException if a database access error occurs
     * or this method is called on a closed result set
     * @exception UsageException if the JDBC driver does not support
     * this method
     */
    public int getCurrentRow() {
      if (isFirstFetch) {
        // Use the parameter as the real number of rows, we're either not using
        // a scrollable result set or the calls are out of the expected order
          return this.getResultSet().getRow();
      }
      else {
          return this.getResultSet().getRow() - this.startReadingRow;
      }
    }
   
    public int getColOrdinal(String pColName) {
      if (resultSet == null) {
        if (pColName.charAt(0) == ':')
          pColName = pColName.substring(1);
        if (this.inputColMapping != null) {
          for (int i = 0; i < this.inputColMapping.size(); i++) {
            if (this.inputColMapping.get(i).compareTo(pColName) == 0)
              // Don't forget to compensate for 1-based index, not 0-based
              return i+1;
          }
        }
      } else {
        try {
        return resultSet.findColumn(pColName);
      } catch (SQLException e) {
        // Ignore the exception because one will be raised below
      }
      }
        UsageException errorVar = new UsageException("Could not find ordinal for " + pColName);
        ErrorMgr.addError(errorVar);
        throw errorVar;
    }
    /**
     * The ColumnDesc list that describes the columns of this dataset.
     * @return
     */
    //PM:25/4/08 added generics
    public Array_Of_DBColumnDesc<DBColumnDesc> getColumnList(){
        Array_Of_DBColumnDesc<DBColumnDesc> cds = new Array_Of_DBColumnDesc<DBColumnDesc>();
        try {
          if (metaData != null){
            // AD:24/09/08 Return the actual columns of the table
            ResultSet result = this.metaData.getColumns(null, null, this.tableName, null);
            int columnCount = 0;
            while (result.next()){
              // At this stage setting the UDS Data Type to Unknown. May need to create a mapping from the JDBC DataType to a DB_DT_ constant
              cds.add(new DBColumnDesc(result.getInt("COLUMN_SIZE"), 0, result.getInt("DATA_TYPE"), result.getString("COLUMN_NAME"), result.getBoolean("NULLABLE"), result.getInt("DECIMAL_DIGITS")));
              columnCount++;
            }
            if (columnCount == 0) {
              UsageException errorVar = new UsageException("Cannot get column List from table: " + this.tableName + ", Table does not exist.");
              ErrorMgr.addError(errorVar);
              throw errorVar;
            }
            }
            // TF:24/07/2008:Added support for columns descriptors on input data sets too
            else if (statement != null && this.inputColMapping != null) { //PM:Aug 15, 2008:added null check on inputColMapping
              // It's possibly a request for the column list off the input metadata
              ParameterMetaData data = null;
              try {
                data = statement.getParameterMetaData();
              }
              catch (Exception e) {
                // No dice, go with what we've got
              }
              int numCols = this.inputColMapping.size();
              for (int i = 0; i < numCols; i++) {
                String name =  ":" + this.inputColMapping.get(i);
                int type = Types.OTHER;
                try {
                  if (data != null) {
                    type = data.getParameterType(i);
                  }
                }
                catch (Exception e) {}
                int scale = 2;
                try {
                  if (data != null) {
                    scale = data.getPrecision(i);
                  }
                }
                catch (Exception e) {};
                cds.add(new DBColumnDesc(4, type, type, name, false, scale));
              }
            }
            else if (resultSet != null) {
              ResultSetMetaData data = this.resultSet.getMetaData();
                int numCols = data.getColumnCount();
                // AD:24/09/08 Column Count starts at 1 not 0
                for (int i = 1; i <= numCols; i++) {
                    cds.add(new DBColumnDesc(data, i));
                }
            }
        } catch (SQLException e) {
            DataAccessException errorVar = new SQLErrorCodeSQLExceptionTranslator().translate("Cannot get column List from result set\n" + e.getMessage(), "", e );
            ErrorMgr.addError(errorVar);
            throw errorVar;
            // PM:16/07/2008
        } catch (NullPointerException e){
          String msg = (e.getMessage() == null) ? e.getClass().getName() : e.getMessage();
            UsageException errorVar = new UsageException("Cannot get column List from result set\n" + msg , e );
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }
        return cds;
    }

    /**
     * This variation of the SetValue method sets the value of the column you specify by position.
     * @param position The position parameter specifies the number of the column you wish to set.
     * If there is no match, the SetValue method raises an exception of type UsageException
     * @param pValue Specifies the new value for the column. The value must be compatible with
     * the data type of the column, or an exception is thrown
     */
    public void setValue(int pColOrdinal, DataValue pValue) {
      if (resultSet == null) {
        try {
          setter.setParameterValue(this.statement, pColOrdinal, SqlTypeValue.TYPE_UNKNOWN, null, pValue);
        }
        catch (SQLException e) {
          DataAccessException errorVar = new SQLErrorCodeSQLExceptionTranslator().translate("DBDataSet.setValue(" + pColOrdinal +"," + pValue + ")", "", e );
          ErrorMgr.addError(errorVar);
          throw errorVar;
        }
      } else {
        // AD:20/11/2008: Allow for CachedRowSet to be updated if the DBDataSet has already been populated.
        if (resultSet instanceof CachedRowSet) {
          CachedRowSet cachedRowSet = (CachedRowSet) resultSet;
          try {
            if (pValue instanceof TextData) {
              TextData textValue = (TextData) pValue;
              cachedRowSet.updateString(pColOrdinal, textValue.toString());
            } else if (pValue instanceof IntegerData) {
              IntegerData integerValue = (IntegerData) pValue;
              cachedRowSet.updateInt(pColOrdinal, integerValue.getIntegerValue());
            } else if (pValue instanceof DoubleData) {
              DoubleData doubleValue = (DoubleData) pValue;
              cachedRowSet.updateDouble(pColOrdinal, doubleValue.getDoubleValue());
            } else if (pValue instanceof DecimalData) {
              DecimalData value = (DecimalData) pValue;
              cachedRowSet.updateBigDecimal(pColOrdinal, value.getBigDecimal());
            } else if (pValue instanceof BooleanData) {
              BooleanData value = (BooleanData) pValue;
              cachedRowSet.updateBoolean(pColOrdinal, value.getBooleanValue());
            } else if (pValue instanceof DateTimeData) {
              DateTimeData value = (DateTimeData) pValue;
              cachedRowSet.updateDate(pColOrdinal, new Date(value.asDate().getTime()));
            } else if (pValue instanceof BinaryData) {
              BinaryData value = (BinaryData) pValue;
              cachedRowSet.updateBinaryStream(pColOrdinal, new ByteArrayInputStream(value.getValue()), value.getActualSize());
            }
          } catch (SQLException e) {
            UsageException errorVar = new UsageException(e.getMessage(), net.helipilot50.stocktrade.framework.Constants.SP_ER_USER, net.helipilot50.stocktrade.framework.Constants.SP_ER_PARAMETERERROR, e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
          }
        }
      }
    }   
    /**
     * This variation of the SetValue method sets the value of the column you specify by name.
     * @param position The position parameter specifies the number of the column you wish to set.
     * If there is no match, the SetValue method raises an exception of type UsageException
     * @param pValue Specifies the new value for the column. The value must be compatible with
     * the data type of the column, or an exception is thrown
*/
    public void setValue(String pColumnName, DataValue pValue) {
      setValue(this.getColOrdinal(pColumnName), pValue);
    }
    /**
     * This variation of the SetValue method sets the value of the column you specify by name.
     * @param position The position parameter specifies the number of the column you wish to set.
     * If there is no match, the SetValue method raises an exception of type UsageException
     * @param pValue Specifies the new value for the column. The value must be compatible with
     * the data type of the column, or an exception is thrown
     */
    public void setValue(TextData pColumnName, DataValue pValue) {
        setValue(pColumnName.toString(), pValue);
    }

    /**
     * This variation of the SetValue method sets the value of the column you specify by position.
     * @param position The position parameter specifies the number of the column you wish to set.
     * If there is no match, the SetValue method raises an exception of type UsageException
     * @param pValue Specifies the new value for the column. The value must be compatible with
     * the data type of the column, or an exception is thrown
     */
    public void setValue(int pColOrdinal, int pValue) {
      if (resultSet == null) {
        try {
          this.statement.setInt(pColOrdinal, pValue);
        }
        catch (SQLException e) {
          DataAccessException errorVar = new SQLErrorCodeSQLExceptionTranslator().translate("DBDataSet.setValue(" + pColOrdinal +"," + pValue + ")", "", e );
          ErrorMgr.addError(errorVar);
          throw errorVar;
        }
      } else {
        // AD:20/11/2008: Allow for CachedRowSet to be updated if the DBDataSet has already been populated.
        if (resultSet instanceof CachedRowSet) {
          CachedRowSet cachedRowSet = (CachedRowSet) resultSet;
          try {
            cachedRowSet.updateInt(pColOrdinal, pValue);
          } catch (SQLException e) {
            UsageException errorVar = new UsageException(e.getMessage(), net.helipilot50.stocktrade.framework.Constants.SP_ER_USER, net.helipilot50.stocktrade.framework.Constants.SP_ER_PARAMETERERROR, e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
          }
        }
      }
    }
    /**
     * This variation of the SetValue method sets the value of the column you specify by name.
     * @param position The position parameter specifies the number of the column you wish to set.
     * If there is no match, the SetValue method raises an exception of type UsageException
     * @param pValue Specifies the new value for the column. The value must be compatible with
     * the data type of the column, or an exception is thrown
     */
    public void setValue(String pColumnName, int pValue) {
        setValue(this.getColOrdinal(pColumnName), pValue);
    }
    /**
     * This variation of the SetValue method sets the value of the column you specify by name.
     * @param position The position parameter specifies the number of the column you wish to set.
     * If there is no match, the SetValue method raises an exception of type UsageException
     * @param pValue Specifies the new value for the column. The value must be compatible with
     * the data type of the column, or an exception is thrown
     */
    public void setValue(TextData pColumnName, int pValue) {
        setValue(pColumnName.toString(), pValue);
    }

    /**
     * This variation of the SetValue method sets the value of the column you specify by position.
     * @param position The position parameter specifies the number of the column you wish to set.
     * If there is no match, the SetValue method raises an exception of type UsageException
     * @param pValue Specifies the new value for the column. The value must be compatible with
     * the data type of the column, or an exception is thrown
     */
    public void setValue(int pColOrdinal, float pValue) {
      if (resultSet == null) {
        try {
          this.statement.setFloat(pColOrdinal, pValue);
        }
        catch (SQLException e) {
          DataAccessException errorVar = new SQLErrorCodeSQLExceptionTranslator().translate("DBDataSet.setValue(" + pColOrdinal +"," + pValue + ")", "", e );
          ErrorMgr.addError(errorVar);
          throw errorVar;
        }
      } else {
        // AD:20/11/2008: Allow for CachedRowSet to be updated if the DBDataSet has already been populated.
        if (resultSet instanceof CachedRowSet) {
          CachedRowSet cachedRowSet = (CachedRowSet) resultSet;
          try {
            cachedRowSet.updateFloat(pColOrdinal, pValue);
          } catch (SQLException e) {
            UsageException errorVar = new UsageException(e.getMessage(), net.helipilot50.stocktrade.framework.Constants.SP_ER_USER, net.helipilot50.stocktrade.framework.Constants.SP_ER_PARAMETERERROR, e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
          }
        }
      }
    }
    /**
     * This variation of the SetValue method sets the value of the column you specify by name.
     * @param position The position parameter specifies the number of the column you wish to set.
     * If there is no match, the SetValue method raises an exception of type UsageException
     * @param pValue Specifies the new value for the column. The value must be compatible with
     * the data type of the column, or an exception is thrown
     */
    public void setValue(String pColumnName, float pValue) {
        setValue(this.getColOrdinal(pColumnName), pValue);
    }
    /**
     * This variation of the SetValue method sets the value of the column you specify by name.
     * @param position The position parameter specifies the number of the column you wish to set.
     * If there is no match, the SetValue method raises an exception of type UsageException
     * @param pValue Specifies the new value for the column. The value must be compatible with
     * the data type of the column, or an exception is thrown
     */
    public void setValue(TextData pColumnName, float pValue) {
        setValue(pColumnName.toString(), pValue);
    }

    /**
     * This variation of the SetValue method sets the value of the column you specify by position.
     * @param position The position parameter specifies the number of the column you wish to set.
     * If there is no match, the SetValue method raises an exception of type UsageException
     * @param pValue Specifies the new value for the column. The value must be compatible with
     * the data type of the column, or an exception is thrown
     */
    public void setValue(int pColOrdinal, double pValue) {
      if (resultSet == null) {
        try {
          this.statement.setDouble(pColOrdinal, pValue);
        }
        catch (SQLException e) {
          DataAccessException errorVar = new SQLErrorCodeSQLExceptionTranslator().translate("DBDataSet.setValue(" + pColOrdinal +"," + pValue + ")", "", e );
          ErrorMgr.addError(errorVar);
          throw errorVar;
        }
      } else {
        // AD:20/11/2008: Allow for CachedRowSet to be updated if the DBDataSet has already been populated.
        if (resultSet instanceof CachedRowSet) {
          CachedRowSet cachedRowSet = (CachedRowSet) resultSet;
          try {
            cachedRowSet.updateDouble(pColOrdinal, pValue);
          } catch (SQLException e) {
            UsageException errorVar = new UsageException(e.getMessage(), net.helipilot50.stocktrade.framework.Constants.SP_ER_USER, net.helipilot50.stocktrade.framework.Constants.SP_ER_PARAMETERERROR, e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
          }
        }
      }
    }
    /**
     * This variation of the SetValue method sets the value of the column you specify by name.
     * @param position The position parameter specifies the number of the column you wish to set.
     * If there is no match, the SetValue method raises an exception of type UsageException
     * @param pValue Specifies the new value for the column. The value must be compatible with
     * the data type of the column, or an exception is thrown
     */
    public void setValue(String pColumnName, double pValue) {
        setValue(this.getColOrdinal(pColumnName), pValue);
    }
    /**
     * This variation of the SetValue method sets the value of the column you specify by name.
     * @param position The position parameter specifies the number of the column you wish to set.
     * If there is no match, the SetValue method raises an exception of type UsageException
     * @param pValue Specifies the new value for the column. The value must be compatible with
     * the data type of the column, or an exception is thrown
     */
    public void setValue(TextData pColumnName, double pValue) {
        setValue(pColumnName.toString(), pValue);
    }

    /**
     * This variation of the SetValue method sets the value of the column you specify by position.
     * @param position The position parameter specifies the number of the column you wish to set.
     * If there is no match, the SetValue method raises an exception of type UsageException
     * @param pValue Specifies the new value for the column. The value must be compatible with
     * the data type of the column, or an exception is thrown
     */
    public void setValue(int pColOrdinal, long pValue) {
      if (resultSet == null) {
        try {
          this.statement.setLong(pColOrdinal, pValue);
        }
        catch (SQLException e) {
          DataAccessException errorVar = new SQLErrorCodeSQLExceptionTranslator().translate("DBDataSet.setValue(" + pColOrdinal +"," + pValue + ")", "", e );
          ErrorMgr.addError(errorVar);
          throw errorVar;
        }
      } else {
        // AD:20/11/2008: Allow for CachedRowSet to be updated if the DBDataSet has already been populated.
        if (resultSet instanceof CachedRowSet) {
          CachedRowSet cachedRowSet = (CachedRowSet) resultSet;
          try {
            cachedRowSet.updateLong(pColOrdinal, pValue);
          } catch (SQLException e) {
            UsageException errorVar = new UsageException(e.getMessage(), net.helipilot50.stocktrade.framework.Constants.SP_ER_USER, net.helipilot50.stocktrade.framework.Constants.SP_ER_PARAMETERERROR, e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
          }
        }
      }
    }
    /**
     * This variation of the SetValue method sets the value of the column you specify by name.
     * @param position The position parameter specifies the number of the column you wish to set.
     * If there is no match, the SetValue method raises an exception of type UsageException
     * @param pValue Specifies the new value for the column. The value must be compatible with
     * the data type of the column, or an exception is thrown
     */
    public void setValue(String pColumnName, long pValue) {
        setValue(this.getColOrdinal(pColumnName), pValue);
    }
    /**
     * This variation of the SetValue method sets the value of the column you specify by name.
     * @param position The position parameter specifies the number of the column you wish to set.
     * If there is no match, the SetValue method raises an exception of type UsageException
     * @param pValue Specifies the new value for the column. The value must be compatible with
     * the data type of the column, or an exception is thrown
     */
    public void setValue(TextData pColumnName, long pValue) {
        setValue(pColumnName.toString(), pValue);
    }

    /**
     * This variation of the SetValue method sets the value of the column you specify by position.
     * @param position The position parameter specifies the number of the column you wish to set.
     * If there is no match, the SetValue method raises an exception of type UsageException
     * @param pValue Specifies the new value for the column. The value must be compatible with
     * the data type of the column, or an exception is thrown
     */
    public void setValue(int pColOrdinal, boolean pValue) {
      if (resultSet == null) {
        try {
          this.statement.setBoolean(pColOrdinal, pValue);
        }
        catch (SQLException e) {
          DataAccessException errorVar = new SQLErrorCodeSQLExceptionTranslator().translate("DBDataSet.setValue(" + pColOrdinal +"," + pValue + ")", "", e );
          ErrorMgr.addError(errorVar);
          throw errorVar;
        }
      } else {
        // AD:20/11/2008: Allow for CachedRowSet to be updated if the DBDataSet has already been populated.
        if (resultSet instanceof CachedRowSet) {
          CachedRowSet cachedRowSet = (CachedRowSet) resultSet;
          try {
            cachedRowSet.updateBoolean(pColOrdinal, pValue);
          } catch (SQLException e) {
            UsageException errorVar = new UsageException(e.getMessage(), net.helipilot50.stocktrade.framework.Constants.SP_ER_USER, net.helipilot50.stocktrade.framework.Constants.SP_ER_PARAMETERERROR, e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
          }
        }
      }
    }
    /**
     * This variation of the SetValue method sets the value of the column you specify by name.
     * @param position The position parameter specifies the number of the column you wish to set.
     * If there is no match, the SetValue method raises an exception of type UsageException
     * @param pValue Specifies the new value for the column. The value must be compatible with
     * the data type of the column, or an exception is thrown
     */
    public void setValue(String pColumnName, boolean pValue) {
        setValue(this.getColOrdinal(pColumnName), pValue);
    }
    /**
     * This variation of the SetValue method sets the value of the column you specify by name.
     * @param position The position parameter specifies the number of the column you wish to set.
     * If there is no match, the SetValue method raises an exception of type UsageException
     * @param pValue Specifies the new value for the column. The value must be compatible with
     * the data type of the column, or an exception is thrown
     */
    public void setValue(TextData pColumnName, boolean pValue) {
        setValue(pColumnName.toString(), pValue);
    }

    /**
     * This variation of the SetValue method sets the value of the column you specify by position.
     * @param position The position parameter specifies the number of the column you wish to set.
     * If there is no match, the SetValue method raises an exception of type UsageException
     * @param pValue Specifies the new value for the column. The value must be compatible with
     * the data type of the column, or an exception is thrown
     */
    public void setValue(int pColOrdinal, short pValue) {
      if (resultSet == null) {
        try {
          this.statement.setShort(pColOrdinal, pValue);
        }
        catch (SQLException e) {
          DataAccessException errorVar = new SQLErrorCodeSQLExceptionTranslator().translate("DBDataSet.setValue(" + pColOrdinal +"," + pValue + ")", "", e );
          ErrorMgr.addError(errorVar);
          throw errorVar;
        }
      } else {
        // AD:20/11/2008: Allow for CachedRowSet to be updated if the DBDataSet has already been populated.
        if (resultSet instanceof CachedRowSet) {
          CachedRowSet cachedRowSet = (CachedRowSet) resultSet;
          try {
            cachedRowSet.updateShort(pColOrdinal, pValue);
          } catch (SQLException e) {
            UsageException errorVar = new UsageException(e.getMessage(), net.helipilot50.stocktrade.framework.Constants.SP_ER_USER, net.helipilot50.stocktrade.framework.Constants.SP_ER_PARAMETERERROR, e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
          }
        }
      }
    }
    /**
     * This variation of the SetValue method sets the value of the column you specify by name.
     * @param position The position parameter specifies the number of the column you wish to set.
     * If there is no match, the SetValue method raises an exception of type UsageException
     * @param pValue Specifies the new value for the column. The value must be compatible with
     * the data type of the column, or an exception is thrown
     */
    public void setValue(String pColumnName, short pValue) {
        setValue(this.getColOrdinal(pColumnName), pValue);
    }
    /**
     * This variation of the SetValue method sets the value of the column you specify by name.
     * @param position The position parameter specifies the number of the column you wish to set.
     * If there is no match, the SetValue method raises an exception of type UsageException
     * @param pValue Specifies the new value for the column. The value must be compatible with
     * the data type of the column, or an exception is thrown
     */
    public void setValue(TextData pColumnName, short pValue) {
        setValue(pColumnName.toString(), pValue);
    }

    /**
     * This variation of the SetValue method sets the value of the column you specify by position.
     * @param position The position parameter specifies the number of the column you wish to set.
     * If there is no match, the SetValue method raises an exception of type UsageException
     * @param pValue Specifies the new value for the column. The value must be compatible with
     * the data type of the column, or an exception is thrown
     */
    public void setValue(int pColOrdinal, String pValue) {
      if (resultSet == null) {
        try {
          this.statement.setString(pColOrdinal, pValue);
        }
        catch (SQLException e) {
          DataAccessException errorVar = new SQLErrorCodeSQLExceptionTranslator().translate("DBDataSet.setValue(" + pColOrdinal +"," + pValue + ")", "", e );
          ErrorMgr.addError(errorVar);
          throw errorVar;
        }
      } else {
        // AD:20/11/2008: Allow for CachedRowSet to be updated if the DBDataSet has already been populated.
        if (resultSet instanceof CachedRowSet) {
          CachedRowSet cachedRowSet = (CachedRowSet) resultSet;
          try {
            cachedRowSet.updateString(pColOrdinal, pValue);
          } catch (SQLException e) {
            UsageException errorVar = new UsageException(e.getMessage(), net.helipilot50.stocktrade.framework.Constants.SP_ER_USER, net.helipilot50.stocktrade.framework.Constants.SP_ER_PARAMETERERROR, e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
          }
        }
      }
    }

    /**
     * This variation of the SetValue method sets the value of the column you specify by name.
     * @param position The position parameter specifies the number of the column you wish to set.
     * If there is no match, the SetValue method raises an exception of type UsageException
     * @param pValue Specifies the new value for the column. The value must be compatible with
     * the data type of the column, or an exception is thrown
     */
    public void setValue(String pColumnName, String pValue) {
        setValue(this.getColOrdinal(pColumnName), pValue);
    }

    /**
     * This variation of the SetValue method sets the value of the column you specify by name.
     * @param position The position parameter specifies the number of the column you wish to set.
     * If there is no match, the SetValue method raises an exception of type UsageException
     * @param pValue Specifies the new value for the column. The value must be compatible with
     * the data type of the column, or an exception is thrown
     */
    public void setValue(TextData pColumnName, String pValue) {
        setValue(pColumnName.toString(), pValue);
    }
   
    /**
     * Set the maximum size of a column as a hint to the database driver about how much memory to allocate
     * when retrieving a value from the database.<br>
     * This method currently has no functionality as there is no equivalent in Java
     * @param pColumnOrdinal
     * @param pSize
     */
    public void setMaxSize(int pColumnOrdinal, int pSize) {
    }
   
    /**
     * Set the maximum size of a column as a hint to the database driver about how much memory to allocate
     * when retrieving a value from the database.<br>
     * This method currently has no functionality as there is no equivalent in Java
     * @param pColumnOrdinal
     * @param pSize
     */
    public void setMaxSize(String pColumnName, int pSize) {
    }

    /**
     * The maxRows (integer) property is the number of rows that can be put into a data set in a single fetch.
     * You typically use the MaxRows property when you use a DBDataSet as input to a SQL statement
     * prior to being executed. <p>
     * <p>
     * Note that this property is more akin to the FetchSize on a Statement rather than
     * the MaxRows property on a Statement, as it indicates the size of the row returned in a single fetch,
     * and is a hint to the underlying JDBC driver only.
     * @param pRows The maximum number of rows for this dataset
     */
    public void setMaxRows(int pRows) {
      if (pRows < 0) {
        UsageException err = new UsageException("pRows must be > 0 in call to DBDataSet.setMaxRows");
        ErrorMgr.addError(err);
        throw err;
      }
      this.maxRows = pRows;
      if (this.statement != null) {
        try {
        this.statement.setFetchSize(pRows);
      } catch (SQLException e) {
        // Do nothing, it may not be needed by the statement at this point
      }
      }
    }
   
    /**
     * The maxRows (integer) property is the number of rows that can be put into a data set in a single fetch.
     * You typically use the MaxRows property when you use a DBDataSet as input to a SQL statement
     * prior to being executed. <p>
     * <p>
     * Note that this property is more akin to the FetchSize on a Statement rather than
     * the MaxRows property on a Statement, as it indicates the size of the row returned in a single fetch,
     * and is a hint to the underlying JDBC driver only.
     * @return The number of rows the db dataset can hold, or 0 if this has not been set
     */
    public int getMaxRows() {
      if (this.statement != null) {
        int statementRows = 0;
        try {
        statementRows = this.statement.getFetchSize();
      } catch (SQLException e) {
        // Do nothing, use the default of 0
      }
        if (statementRows != this.maxRows && statementRows != 0) {
          // They're different values, and the statment has a non-zero rows.
          // Use it's value in preference.
          this.maxRows = statementRows;
        }
      }
      return this.maxRows;
    }
   
//    public DataValue getValue(int pPosition, ParameterHolder_DataValue<DataValue> parameter) {
//      DataValue result;
//      if (parameter.getObject() == null) {
//          result = this.getResultSet().getDataValue(pPosition);
//      }
//      else {
//          result = this.getResultSet().getDataValue(pPosition, parameter.get().getClass());
//          parameter.get().setValue(result);
//      }
//      return result;
//    }
//
//    public DataValue getValue(String pPosition, ParameterHolder_DataValue<DataValue> parameter) {
//      DataValue result;
//      if (parameter.getObject() == null) {
//          result = this.getResultSet().getDataValue(pPosition);
//      }
//      else {
//          result = this.getResultSet().getDataValue(pPosition, parameter.get().getClass());
//          parameter.get().setValue(result);
//      }
//      return result;
//    }   
}
TOP

Related Classes of net.helipilot50.stocktrade.genericdbms.DBDataSet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.