com.sas.storage.jdbc
Class JDBCRowCountAdapter

com.sas.storage.jdbc.JDBCRowCountAdapter
All Implemented Interfaces:
com.sas.table.StaticColumnInfoInterface, com.sas.util.transforms.DisplayTransformInterface
Direct Known Subclasses:
JDBCToTableModelAdapter

public abstract class JDBCRowCountAdapter

In many cases it may not be necessary or may be an expensive operation to determine the true row count of a data source. For example, one way to calculate the true row count of a forward only result set is to increment a counter while calling next() on the result set until the end of the result set is reached. This could be inefficient for large result sets.

This class optionally provides an alternative way for calculating the row count, by calculating a "current maximum row count". This value, specified by the currentMaxRowCount property is calculated in the calculateCurrentMaxRowCount method by advancing the result set cursor ahead of the input specified row, or the currentMaxRowCount, whichever is greater. The amount the cursor will be advanced is specified by the readAheadAmount property. The default read ahead amount is 50 rows. When the end of the result set is reached, the maxRowsFound property is set to true, and the currentMaxRowCount is equal to the true row count.


Field Summary
protected  int currentMaxRowCount
          The current number of rows that have been read from the data source
protected  boolean maxRowsFound
          A flag indicating if the true row count has been determined
static java.lang.String RB_KEY
           
protected static int READ_AHEAD_AMOUNT
          The default number of row to read ahead in the result set
protected  int readAheadAmount
          The number of rows to read ahead in the data source
protected  boolean trueRowCountCalculated
          If true, the actual row count is calculated.
 
Fields inherited from class com.sas.storage.jdbc.JDBCAdapter
autoCommit, CLASS_NAME, columnInfoUsed, conn, designTime, displayTransform, forwardOnly, initialized, isRowCountKnown, LABEL, meta, NAME, printWarnings, queryRowCountDisabled, queryStatement, readOnly, result, resultSetRequeryUsed, resultSetType, resultSetUsed, retrieveNumericTypesAsDouble, rowCount, rowCountQueryString, rowNumber, spcs, stmt, trimUsed
 
Constructor Summary
JDBCRowCountAdapter()
          Default constructor called when the adapter is made in a webAF project.
JDBCRowCountAdapter(java.sql.Connection conn, java.lang.String queryStatement)
          Constructor used to create an initialized adapter.
JDBCRowCountAdapter(java.sql.ResultSet result)
          Constructor used to connect an adapter directly to a previously created ResultSet.
 
Method Summary
protected  void calculateCurrentMaxRowCount(int requestedRow)
          Calculates the currentMaxRowCount by attempting to advance the result set cursor ahead of the requestedRow or currentMaxRowCount, whichever is greater.
protected abstract  void fireModelInsertEvent(int startRow, int endRow)
          Fire a insert event for a model signaling that an insert has occurred.
 int getReadAheadAmount()
          Returns the number of rows the adapter will read ahead in the result set.
 boolean isTrueRowCountCalculated()
          Returns true if the true row count is calcualted.
 void setConnection(java.sql.Connection conn)
          Sets the connection object on the adapter.
 void setQueryStatement(java.lang.String queryStatement)
          The queryStatement should be any SQL query that is valid for the JDBC data source.
 void setReadAheadAmount(int readAheadAmount)
          Sets the amount of rows that the adapter will attempt to read ahead in the result set.
 void setTrueRowCountCalculated(boolean trueRowCountCalculated)
          Sets whether to calculate the true row count or the current maximum row count.
 
Methods inherited from class com.sas.storage.jdbc.JDBCAdapter
addPropertyChangeListener, addPropertyChangeListener, calculateRowCount, close, doAbsolute, doCountColumns, doUpdateCell, finalize, fireCellEvent, fireModelEvent, fireModelEvent, getAutoCommit, getColumnInfo, getColumnInfoNames, getConnection, getDisplayTransform, getModel, getPrintWarnings, getQueryStatement, getResultSetConcurrency, getResultSetRowCount, getResultSetType, getRowCountByCountFunction, getRowCountQueryString, init, initializeData, isQueryRowCountDisabled, isReadOnly, isResultSetRequeryUsed, isRetrieveNumericTypesAsDouble, isTrimUsed, printSQLWarning, putColumnInfoUsed, putValueAt, refresh, removePropertyChangeListener, removePropertyChangeListener, retrieveColumnClass, retrieveColumnCount, retrieveColumnIndex, retrieveColumnInfoUsed, retrieveColumnLabel, retrieveRawSASNumericValue, retrieveRawSASNumericValue, retrieveValueAt, retrieveValueAt, setAutoCommit, setDisplayTransform, setModel, setPrintWarnings, setQueryRowCountDisabled, setReadOnly, setResultSetRequeryUsed, setResultSetType, setRetrieveNumericTypesAsDouble, setRowCountQueryString, setTrimUsed
 

Field Detail

RB_KEY

public static final java.lang.String RB_KEY
See Also:
Constant Field Values

maxRowsFound

protected boolean maxRowsFound
A flag indicating if the true row count has been determined


currentMaxRowCount

protected int currentMaxRowCount
The current number of rows that have been read from the data source


readAheadAmount

protected int readAheadAmount
The number of rows to read ahead in the data source


trueRowCountCalculated

protected boolean trueRowCountCalculated
If true, the actual row count is calculated. If false, the currentMaxRowCount value will attempt to be at least the readAheadAmount number of rows higher than the highest row number retrieved.


READ_AHEAD_AMOUNT

protected static final int READ_AHEAD_AMOUNT
The default number of row to read ahead in the result set

See Also:
Constant Field Values
Constructor Detail

JDBCRowCountAdapter

public JDBCRowCountAdapter()
Default constructor called when the adapter is made in a webAF project.


JDBCRowCountAdapter

public JDBCRowCountAdapter(java.sql.ResultSet result)
Constructor used to connect an adapter directly to a previously created ResultSet. In order for the adapter to work properly, the ResultSet should be scrollable.

Parameters:
result - ResultSet to use.

JDBCRowCountAdapter

public JDBCRowCountAdapter(java.sql.Connection conn,
                           java.lang.String queryStatement)
Constructor used to create an initialized adapter.

Parameters:
conn - A java.sql.Connection object.
queryStatement - A SQL query that is valid for the database represented by conn.
Method Detail

setTrueRowCountCalculated

public void setTrueRowCountCalculated(boolean trueRowCountCalculated)
Sets whether to calculate the true row count or the current maximum row count. The default is false.

The current maximum row count is calculated by attempting to read ahead in the result set by readAheadAmount rows when a value is retrieved. When the end of the result set is reached the current max row count is equal to the true row count and the maxRowsFound is set to true.

Parameters:
trueRowCountCalculated - true if the true row count should be calculated, false if the current maximum row count should be calculated
See Also:
isTrueRowCountCalculated()

isTrueRowCountCalculated

public boolean isTrueRowCountCalculated()
Returns true if the true row count is calcualted. Returns false if the current maximum row count is calculated.

See Also:
setTrueRowCountCalculated(boolean)

getReadAheadAmount

public int getReadAheadAmount()
Returns the number of rows the adapter will read ahead in the result set.

See Also:
setReadAheadAmount(int)

setReadAheadAmount

public void setReadAheadAmount(int readAheadAmount)
Sets the amount of rows that the adapter will attempt to read ahead in the result set. If this value is not greater than 0 an IllegalArgumentException is thrown. The default value is 50.

Parameters:
readAheadAmount - the number of rows the adapter will read ahead in the result set
See Also:
getReadAheadAmount()

calculateCurrentMaxRowCount

protected void calculateCurrentMaxRowCount(int requestedRow)
                                    throws java.sql.SQLException
Calculates the currentMaxRowCount by attempting to advance the result set cursor ahead of the requestedRow or currentMaxRowCount, whichever is greater. The amount the cursor will be advanced is specified by the readAheadAmount property. When the end of the result set is found, the maxRowsFound property is set to true.

Parameters:
requestedRow - the row number to read ahead of
Throws:
java.sql.SQLException

setQueryStatement

public void setQueryStatement(java.lang.String queryStatement)
The queryStatement should be any SQL query that is valid for the JDBC data source. It is used to extract the desired data from the database.

Calling this method has no affect if the constructer taking a ResultSet was used to create the adapter and the ResultSet is not a RowSet.

Overrides:
setQueryStatement in class JDBCAdapter
Parameters:
queryStatement - The SQL query to apply.

setConnection

public void setConnection(java.sql.Connection conn)
Sets the connection object on the adapter.

Overrides:
setConnection in class JDBCAdapter
Parameters:
conn - A java.sql.Connection object.

fireModelInsertEvent

protected abstract void fireModelInsertEvent(int startRow,
                                             int endRow)
Fire a insert event for a model signaling that an insert has occurred. Subclasses must override this method to fire a default events for their model.




Copyright © 2009 SAS Institute Inc. All Rights Reserved.