ModSQL
Class LiteralTable

java.lang.Object
  |
  +--ModSQL.LiteralTable
All Implemented Interfaces:
Table

class LiteralTable
extends Object
implements Table

A literal table is a list of rows consisting of RowConstructor or Function objects.

Author:
chris.studholme@utoronto.ca

Field Summary
private  int[] column_types
          Type of each column in table.
private  int current_row
          Index of current row (-1 for before first).
private  Object[] current_value
          Value of current row (null for before first or after last).
private  int ncolumns
          Number of columns in table.
private  Function[] rows
          Array of rows after optimize().
private  ArrayList rows_array
          Array of rows before optimize() has been called.
 
Constructor Summary
LiteralTable()
          Constructor.
 
Method Summary
 void addRow(Function row)
          Add a row to the table.
 void beforeFirst()
          Reset the table to before the first row.
 int getColumnCount()
          Returns the number of columns in the table.
 String getColumnName(int column)
          Get the name of a column.
 int getMaxResultSize(int column)
          Return the maximum number of characters String values in the specified column will have.
 Object getObject(int column)
          Get the value of a column in the current row as a Java object.
 Object[] getRow()
          Get the current row as an array of Objects.
 long getRowCount()
          Return number of rows.
 int getSQLType(int column)
          Return the SQL type of the specified column.
 boolean isConstant()
          Determine if this table is constant.
 boolean next()
          The table is initially positioned before its first row; the first call to next makes the first row the current row; the second call makes the second row the current row, etc.
 void optimize()
          Prepare the table for use.
 void registerWith(Object o)
          This method will pass the object on to all Function objects contained within.
 String toString()
          String description of table in the form "(row1,row2,...)".
 String toString(boolean with_brackets)
          Human readable description of table.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, wait, wait, wait
 

Field Detail

rows_array

private ArrayList rows_array
Array of rows before optimize() has been called.

rows

private Function[] rows
Array of rows after optimize().

column_types

private int[] column_types
Type of each column in table.

ncolumns

private int ncolumns
Number of columns in table.

current_row

private int current_row
Index of current row (-1 for before first).

current_value

private Object[] current_value
Value of current row (null for before first or after last).
Constructor Detail

LiteralTable

public LiteralTable()
Constructor.
Method Detail

addRow

public void addRow(Function row)
            throws SQLException

Add a row to the table. All rows of the table must have the same number of columns. If row is not of type RowConstructor, it is assumed to have exactly one column.

This method can only be called before optimize().

Parameters:
row - either a Function or RowConstructor
Throws:
SQLException - if an error occurs

registerWith

public void registerWith(Object o)
                  throws SQLException
This method will pass the object on to all Function objects contained within.
Specified by:
registerWith in interface Table
Parameters:
o - object to register with
Throws:
SQLException - if an error occurs

optimize

public void optimize()
              throws SQLException
Prepare the table for use. This method will call optimize() for each row, determine the types of each column, verify that the rows are compatible in type, and fix the rows array.
Specified by:
optimize in interface Table
Throws:
SQLException - if an error occurs

toString

public String toString(boolean with_brackets)
Human readable description of table. There are two styles here:
Specified by:
toString in interface Table
Parameters:
with_brackets - indicates whether the expression is bracketed
Returns:
human readable description of table

toString

public String toString()
String description of table in the form "(row1,row2,...)".
Overrides:
toString in class Object
Returns:
human readable description of table

isConstant

public boolean isConstant()
                   throws SQLException

Determine if this table is constant.

This method can only be called after optimize().

Specified by:
isConstant in interface Table
Returns:
true if all rows are constant
Throws:
SQLException - if an error occurs

getColumnCount

public int getColumnCount()

Returns the number of columns in the table. This value will be greater than zero.

This method can only be called after optimize().

Specified by:
getColumnCount in interface Table
Returns:
number of columns in table

getRowCount

public long getRowCount()

Return number of rows.

This method can only be called after optimize().

Specified by:
getRowCount in interface Table
Returns:
number of rows

getColumnName

public String getColumnName(int column)
Get the name of a column. This method always returns null since literal tables do not have named columns.
Specified by:
getColumnName in interface Table
Parameters:
column - column number (starting from zero)
Returns:
name of column (or null if column has no name)

getSQLType

public int getSQLType(int column)
Return the SQL type of the specified column.
Specified by:
getSQLType in interface Table
Parameters:
column - column number (starting from zero)
Returns:
SQL type of data to be returned

getMaxResultSize

public int getMaxResultSize(int column)
                     throws SQLException
Return the maximum number of characters String values in the specified column will have. If the column is not of String type or if the maximum length is not known, -1 should be returned.
Specified by:
getMaxResultSize in interface Table
Parameters:
column - column number (starting from zero)
Returns:
maximum size of String returned or -1 if unknown
Throws:
SQLException - if an error occurs

beforeFirst

public void beforeFirst()
Reset the table to before the first row. After a call to this method, next() will advance to the first row in the table.
Specified by:
beforeFirst in interface Table
Following copied from interface: ModSQL.Table
Throws:
SQLException - if an error occurs

next

public boolean next()
The table is initially positioned before its first row; the first call to next makes the first row the current row; the second call makes the second row the current row, etc.
Specified by:
next in interface Table
Returns:
true if the new current row is valid; false if there are no more rows

getRow

public Object[] getRow()
                throws SQLException,
                       EndOfTable
Get the current row as an array of Objects.
Specified by:
getRow in interface Table
Returns:
array of objects or null if current row is not valid
Throws:
SQLException - if an error occurs
EndOfTable - if thrown when evaluating the row

getObject

public Object getObject(int column)
                 throws SQLException,
                        EndOfTable
Get the value of a column in the current row as a Java object. This method will throw an exception if the current row is not valid.
Specified by:
getObject in interface Table
Parameters:
column - column number (starting from zero)
Returns:
an Object holding the column value
Throws:
SQLException - if an error occurs
EndOfTable - if thrown when evaluating the row