|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Object | +--ModSQL.Select
Class to parse and execute an SQL SELECT statement. This class implements Table, RowConstructor and Query so that it can behave as any one of these interfaces dictates, although some queries may not be compatable with some of these behaviours.
In the case of Query, the constuctor does the parsing, and execute will call optimize and may actually execute the query too.
In the case of RowConstructor or Table, the constructor still does the parsing, but the user is expected to explicitly call optimize before attempting to evaluate rows.
| Inner Class Summary | |
(package private) class |
Select.GroupByList
Simple list of ColumnValue's in a SELECT query's GROUP BY list. |
(package private) class |
Select.OrderByComparator
Comparator for sorting rows when evaluating ORDER BY. |
| Field Summary | |
private boolean |
aggregate
Are we grouping or aggregating? |
private String[] |
column_names
Names of columns. |
private Object[] |
column_values
Column values for current row. |
private Function[] |
columns
Array of columns to select. |
private int |
current_row
Current row in cached rows. |
private boolean |
distinct
True if SELECT DISTINCT. |
private HashSet |
distinct_set
HashSet used to detect and remove duplicate rows. |
private Function[] |
group_by
Functions to GROUP BY. |
private String[] |
group_by_names
Names of columns to GROUP BY. |
private Function |
having
HAVING clause used to select groups of interest. |
private DatabaseManager |
manager
Table manager used to open tables. |
private Object[] |
null_row
Array of null references to return as a null row. |
private boolean |
optimized
Has optimize been called? |
private Function[] |
order_by
List of functions to ORDER BY. |
private boolean[] |
order_by_dir
ORDER BY order: true for ascending, false for decending. |
private boolean[] |
order_by_dup
Flag indicating that order_by entry is dup of columns entry. |
private int[] |
order_by_indices
Indices into columns array. |
private TableReader |
reader
Reader to get database rows from. |
private List |
rows
Cached rows. |
private int |
start_group
Index in columns of start of GROUP BY. |
private int |
start_having
Index in columns of start of HAVING. |
private int |
start_order
Index in columns of start of ORDER BY. |
private int |
start_where
Index in columns of start of WHERE. |
private String[] |
table_aliases
Alternate names for tables (aliases). |
private DatabaseTable[] |
table_list
List of tables from which rows are read. |
static Boolean |
TRUE_VALUE
Constant true value as Boolean object. |
private Function |
where
WHERE clause used to select rows of interest. |
| Fields inherited from interface ModSQL.Function |
MATCH_BEGINS, MATCH_EQU, MATCH_GT, MATCH_GTE, MATCH_LT, MATCH_LTE, MATCH_NE |
| Constructor Summary | |
protected |
Select(StreamTokenizer tokenizer,
DatabaseManager manager)
Contructor to parse query. |
| Method Summary | |
void |
addParameter(Function item)
Adds a value (function) to the list of parameters maintained by this function. |
void |
beforeFirst()
Reset the table to before the first row. |
void |
close()
Close all tables. |
Object |
evaluate(boolean aggregate)
Evaluate parameters and compute the function. |
Object |
evaluate(int match_op,
Object match_value)
Same as evaluate(), except that a specific result is desired. |
void |
evaluateOrder(int index,
int order)
Specify the order in which the parameters should be evaluated. |
Object[] |
evaluateRow(boolean aggregate)
Evaluate each column of the row and return the results as an array of length equal to the value returned by numColumns(). |
int |
execute()
Optimizes query in preperation for calls to next(). |
protected void |
executeAggregate()
Execute simple aggregate query. |
protected void |
executeAndCacheResults()
Execute query and store results in rows array. |
protected void |
executeBasic()
Execute basic queries (no GROUP BY and no aggregates). |
protected void |
executeGroupBy()
Execute queries involving grouping (ie. |
protected void |
finalize()
Close all tables. |
int |
getColumnCount()
Returns the number of columns in the table. |
String |
getColumnName(int column)
Get the name of a column. |
int |
getMaxResultSize()
This version of getMaxResultSize() can only be used if the query returns only one 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. |
Function |
getParameter(int index)
Get a particular parameter. |
int |
getParameterCount()
Get the number pf parameters currently supplied to this function. |
Object[] |
getRow()
Get the current row as an array of Objects. |
long |
getRowCount()
Return number of rows if known. |
int |
getSQLType()
This version of getSQLType() can only be used if the query returns only one column. |
int |
getSQLType(int column)
Return the SQL type of the specified column. |
boolean |
isAggregate()
Select queries are never aggregate. |
boolean |
isConstant()
A query is non-constant if it is a subquery that references columns from its parent. |
Function |
lookupFunction(String description)
Lookup description in column_names array and return matching columns entry if match is found. |
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()
Optimize query and prepare for execution. |
private void |
parseSelect(StreamTokenizer tokenizer)
Parse entire SELECT query. |
void |
registerWith(Object o)
Most objects passed to this method will be passed on to all contained function objects. |
void |
reset()
In the case of aggregate functions, this method resets the state of the function to the state it was in before the first call to evaluate(). |
void |
setupTableReader()
Create and setup TableReader object (with no parent). |
void |
setupTableReader(TableReader parent)
Create and setup TableReader object. |
String |
toString()
Returns human-readable string version of query (with surrounding brackets). |
String |
toString(boolean with_brackets)
Returns human-readable string version of query. |
| Methods inherited from class java.lang.Object |
|
| Field Detail |
public static final Boolean TRUE_VALUE
private boolean distinct
private HashSet distinct_set
private Function[] columns
private String[] column_names
private Object[] column_values
private DatabaseTable[] table_list
private String[] table_aliases
private Function where
private Function[] group_by
private String[] group_by_names
private Function having
private Function[] order_by
private boolean[] order_by_dir
private int[] order_by_indices
private boolean[] order_by_dup
private int start_order
private int start_having
private int start_group
private int start_where
private transient DatabaseManager manager
private transient TableReader reader
private boolean aggregate
private boolean optimized
private List rows
private int current_row
private Object[] null_row
| Constructor Detail |
protected Select(StreamTokenizer tokenizer,
DatabaseManager manager)
throws SQLException,
IOException
tokenizer - StreamTokenizer that SQL tokens should be read frommanager - manager to use when looking up tablesSQLException - if an error occursIOException - if there is a problem reading the query| Method Detail |
public void close()
close in interface QueryModSQL.QuerySQLException - if an error occursprotected void finalize()
finalize in class Object
private void parseSelect(StreamTokenizer tokenizer)
throws SQLException,
IOException
tokenizer - StreamTokenizer that SQL tokens should be read fromSQLException - if an error occursIOException - if there is a problem reading the querypublic int getParameterCount()
getParameterCount in interface Functionpublic Function getParameter(int index)
getParameter in interface Functionindex - index of parameter to getIndexOutOfBoundsException - always
public void addParameter(Function item)
throws SQLException
addParameter in interface Functionitem - function to addSQLException - always
public void evaluateOrder(int index,
int order)
evaluateOrder in interface Functionindex - index of parameterorder - number indicating order in which parameter is evaluatedIndexOutOfBoundsException - alwayspublic Function lookupFunction(String description)
lookupFunction in interface FunctionProviderdescription - name of SELECT column to look for
public void registerWith(Object o)
throws SQLException
registerWith in interface Functiono - object to register withSQLException - if a database error occurs
public void setupTableReader()
throws SQLException
SQLException - if an error occurs
public void setupTableReader(TableReader parent)
throws SQLException
parent - parent TableReader objectSQLException - if an error occurs
public void optimize()
throws SQLException
optimize in interface FunctionSQLException - if an error occurspublic String toString()
toString in class Objectpublic String toString(boolean with_brackets)
toString in interface Tablewith_brackets - true to include surrounding bracketspublic boolean isAggregate()
isAggregate in interface Functionpublic boolean isConstant()
isConstant in interface Functionpublic int getColumnCount()
getColumnCount in interface RowConstructor
public long getRowCount()
throws SQLException
getRowCount in interface Tablepublic String getColumnName(int column)
getColumnName in interface Tablecolumn - column number (starting from zero)
public int getSQLType(int column)
throws SQLException
getSQLType in interface RowConstructorcolumn - column number (starting from zero)SQLException - if an error occurs
public int getSQLType()
throws SQLException
getSQLType in interface FunctionSQLException - if an error occurs
public int getMaxResultSize(int column)
throws SQLException
getMaxResultSize in interface RowConstructorcolumn - column number (starting from zero)SQLException - if an error occurs
public int getMaxResultSize()
throws SQLException
getMaxResultSize in interface FunctionSQLException - if an error occurs
protected void executeAggregate()
throws SQLException,
EndOfTable
SQLException - if an error occursEndOfTable - if the table is advanced beyond that last row
protected void executeGroupBy()
throws SQLException,
EndOfTable
SQLException - if an error occursEndOfTable - if the table is advanced beyond that last row
protected void executeBasic()
throws SQLException,
EndOfTable
SQLException - if an error occursEndOfTable - if the table is advanced beyond that last row
protected void executeAndCacheResults()
throws SQLException,
EndOfTable
SQLException - if an error occursEndOfTable - if the table is advanced beyond that last row
public int execute()
throws SQLException
execute in interface QuerySQLException - if a database-access error occurspublic void reset()
In the case of aggregate functions, this method resets the state of the function to the state it was in before the first call to evaluate().
reset in interface FunctionModSQL.FunctionSQLException - if an error occurs
public void beforeFirst()
throws SQLException
beforeFirst in interface TableSQLException - if a database-access error occurs
public boolean next()
throws SQLException,
EndOfTable
next in interface TableSQLException - if an error occursEndOfTable - if thrown by a parameter
public Object[] getRow()
throws SQLException
getRow in interface TableSQLException - if a database-access error occurs
public Object getObject(int column)
throws SQLException
getObject in interface Tablecolumn - column number (starting from zero)SQLException - if an error occurs
public Object[] evaluateRow(boolean aggregate)
throws SQLException,
EndOfTable
evaluateRow in interface RowConstructoraggregate - final evaluation of aggregatesSQLException - if an error occursEndOfTable - if thrown by a parameter
public Object evaluate(boolean aggregate)
throws SQLException,
EndOfTable
Evaluate parameters and compute the function. The parameters should be evaluated in the order indicated with evaluateOrder() for optimal query efficiency.
If the function is an aggregate, this method may return null to indicate that the aggregate is not yet available. The method will be called later with the aggregate parameter set to true to indicate that the final aggregate value is required. If the function is non-constant and non-aggregate, it may throw an exception if the aggregate parameter is true. New rows should not be read from the database if aggregate is set to true.
evaluate in interface Functionaggregate - true to return final aggregate valueSQLException - if an error occursEndOfTable - if thrown by a parameter
public Object evaluate(int match_op,
Object match_value)
throws SQLException,
EndOfTable
Same as evaluate(), except that a specific result is desired. If the desired result can only be obtained when a particular parameter evaluates to a particular value, the evaulate() method for that parameter should be passed that particular value in the hopes that it will be returned. Otherwise, if knowledge of a desired result does not sufficiently constrain the parameters, this method can simply call the evaluate() method above and return what it returns.
The match_op parameter will be one of the MATCH_* constants.
The match condition can be considered a relation of the form:
desired_value match_op match_value. For example,
desired_value MATCH_LT match_value implies
desired_value < match_value.
evaluate in interface Functionmatch_op - how desired value is matchedmatch_value - value to match toSQLException - if an error occursEndOfTable - if thrown by a parameter
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||