org.objectweb.cjdbc.common.sql
Class SelectRequest

java.lang.Object
  extended byorg.objectweb.cjdbc.common.sql.AbstractRequest
      extended byorg.objectweb.cjdbc.common.sql.SelectRequest
All Implemented Interfaces:
java.io.Serializable

public class SelectRequest
extends AbstractRequest
implements java.io.Serializable

A SelectRequest is an SQL request of the following syntax:

  SELECT [ALL|DISTINCT] select-item[,select-item]* 
  FROM table-specification[,table-specification]* 
  [WHERE search-condition] 
  [GROUP BY grouping-column[,grouping-column]] 
  [HAVING search-condition] 
  [ORDER BY sort-specification[,sort-specification]] 
  [LIMIT ignored]
 
Note that table-specification in the FROM clause can be a sub-select. Everything after the end of the WHERE clause is ignored.

Version:
1.0
Author:
Emmanuel Cecchet , Julie Marguerite , Mathieu Peltier , Sara Bouchenak
See Also:
Serialized Form

Field Summary
private  java.util.ArrayList aliasFrom
          ArrayList of AliasedTable objects
static int AVERAGE_FUNCTION
          Represents a SQL average() macro
static int COUNT_FUNCTION
          Represents a SQL count() macro
private  java.util.ArrayList from
          ArrayList of String objects.
 int funcType
          Need to keep track of type of query, e.g.
static int MAX_FUNCTION
          Represents a SQL max() macro
static int MIN_FUNCTION
          Represents a SQL min() macro
static int NO_FUNCTION
          Some values to keep track of function in the SELECT request
private  java.util.ArrayList order
          ArrayList of OrderBy objects
private  java.lang.String pkValue
          Primary key value in case of a unique selection
private  java.util.ArrayList select
          ArrayList of TableColumn objects.
private static long serialVersionUID
           
static int SUM_FUNCTION
          Represents a SQL sum() macro
private  java.util.ArrayList where
          ArrayList of TableColumn objects.
private  java.util.Hashtable whereValues
          Hashtable of String keys corresponding to column names and String values corresponding to the values associated with the UNIQUE columns of a UNIQUE SELECT.
 
Fields inherited from class org.objectweb.cjdbc.common.sql.AbstractRequest
cacheable, cursorName, escapeProcessing, fetchSize, id, isAutoCommit, isParsed, isReadOnly, login, maxRows, requestType, sqlQuery, sqlSkeleton, timeoutInSeconds, transactionId, transactionIsolation
 
Constructor Summary
SelectRequest(CJDBCInputStream in)
           
SelectRequest(java.lang.String sqlQuery, boolean escapeProcessing, int timeout, java.lang.String lineSeparator)
          Creates a new SelectRequest instance.
SelectRequest(java.lang.String sqlQuery, boolean escapeProcessing, int timeout, java.lang.String lineSeparator, DatabaseSchema schema, int granularity, boolean isCaseSensitive)
          Creates a new SelectRequest instance.
 
Method Summary
 void cloneParsing(AbstractRequest request)
          Clones the parsing of a request.
 void debug()
          Displays some debugging information about this request.
 java.util.ArrayList getAliasedFrom()
          Returns an ArrayList of AliasedDatabaseTable objects representing the table names found in the FROM clause of this request.
 java.util.ArrayList getFrom()
          Returns an ArrayList of String objects representing the table names found in the FROM clause of this request.
private  java.util.ArrayList getFromTables(java.lang.String fromClause, DatabaseSchema schema, boolean isCaseSensitive)
          Extracts the tables from the given FROM clause and retrieves their alias if any.
 java.util.ArrayList getOrderBy()
          Returns an ArrayList of OrderBy objects representing the columns involved in the ORDER BY clause of this request.
 java.lang.String getPkValue()
           
 java.util.ArrayList getSelect()
          Returns an ArrayList of DatabaseColumn objects representing the columns selected in the SELECT clause of this request.
private  java.util.ArrayList getSelectedColumns(java.lang.String selectClause, java.util.ArrayList aliasedFrom, boolean isCaseSensitive)
          Gets all the columns selected in the given SELECT clause.
 java.util.ArrayList getWhere()
          Returns an ArrayList of TableColumn objects representing the columns involved in the WHERE clause of this request.
private  java.util.ArrayList getWhereColumns(java.lang.String whereClause, java.util.ArrayList aliasedFrom, boolean setUniqueCacheable, boolean isCaseSensitive)
          Gets all the columns involved in the given WHERE clause.
 java.util.Hashtable getWhereValues()
          Returns an Hashtable of String keys representing unique column names and String values associated with the columns involved in this request.
private  boolean isSqlFunction(java.lang.String str)
          Checks if the string parameter represents an SQL function, e. g., MAX, COUNT, SUM, etc.
 boolean needsMacroProcessing()
          Returns true if this request requires macro (RAND(), NOW(), ...) processing.
 void parse(DatabaseSchema schema, int granularity, boolean isCaseSensitive)
           The result of the parsing is accessible through the getSelect(), getFrom()and getWhere()functions.
 boolean returnsResultSet()
          Does this request returns a ResultSet?
 void sendToStream(CJDBCOutputStream out, boolean needSqlSkeleton)
          Serialize the request on the output stream by sending only the needed parameters to reconstruct it on the controller.
 void setPkValue(java.lang.String pkValue)
           
 
Methods inherited from class org.objectweb.cjdbc.common.sql.AbstractRequest
equals, getCacheAbility, getCursorName, getEscapeProcessing, getFetchSize, getId, getLineSeparator, getLogin, getMaxRows, getSQL, getSQLShortForm, getSqlSkeleton, getTimeout, getTransactionId, getTransactionIsolation, isAlter, isAutoCommit, isCreate, isDDL, isDelete, isDML, isDriverProcessed, isDrop, isInsert, isParsed, isReadOnly, isSelect, isUpdate, receiveResultSetParams, sendResultSetParams, setCacheAbility, setCursorName, setDriverProcessed, setFetchSize, setId, setIsAutoCommit, setIsReadOnly, setLogin, setMaxRows, setSQL, setSqlSkeleton, setTimeout, setTransactionId, setTransactionIsolation, trimCarriageReturnAndTabs
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

select

private transient java.util.ArrayList select
ArrayList of TableColumn objects.


from

private transient java.util.ArrayList from
ArrayList of String objects.


aliasFrom

private transient java.util.ArrayList aliasFrom
ArrayList of AliasedTable objects


where

private transient java.util.ArrayList where
ArrayList of TableColumn objects.


order

private transient java.util.ArrayList order
ArrayList of OrderBy objects


NO_FUNCTION

public static final int NO_FUNCTION
Some values to keep track of function in the SELECT request

See Also:
Constant Field Values

MAX_FUNCTION

public static final int MAX_FUNCTION
Represents a SQL max() macro

See Also:
Constant Field Values

MIN_FUNCTION

public static final int MIN_FUNCTION
Represents a SQL min() macro

See Also:
Constant Field Values

AVERAGE_FUNCTION

public static final int AVERAGE_FUNCTION
Represents a SQL average() macro

See Also:
Constant Field Values

COUNT_FUNCTION

public static final int COUNT_FUNCTION
Represents a SQL count() macro

See Also:
Constant Field Values

SUM_FUNCTION

public static final int SUM_FUNCTION
Represents a SQL sum() macro

See Also:
Constant Field Values

funcType

public transient int funcType
Need to keep track of type of query, e.g. MAX, COUNT, etc.


pkValue

private transient java.lang.String pkValue
Primary key value in case of a unique selection


whereValues

private transient java.util.Hashtable whereValues
Hashtable of String keys corresponding to column names and String values corresponding to the values associated with the UNIQUE columns of a UNIQUE SELECT.

Used with the COLUMN_UNIQUE_DELETE granularity.

See Also:
CachingGranularities
Constructor Detail

SelectRequest

public SelectRequest(java.lang.String sqlQuery,
                     boolean escapeProcessing,
                     int timeout,
                     java.lang.String lineSeparator,
                     DatabaseSchema schema,
                     int granularity,
                     boolean isCaseSensitive)
              throws java.sql.SQLException
Creates a new SelectRequest instance. The caller must give an SQL request, without any leading or trailing spaces and beginning with 'select ' (it will not be checked).

The SQL request is parsed and selected tables and columns are retrieved using the given DatabaseSchema.

If the syntax is incorrect an exception is thrown.

Parameters:
sqlQuery - the SQL query
escapeProcessing - should the driver to escape processing before sending to the database ?
timeout - an int value
lineSeparator - the line separator used in the query
schema - a DatabaseSchema value
granularity - parsing granularity as defined in ParsingGranularities
isCaseSensitive - true if parsing is case sensitive
Throws:
java.sql.SQLException - if an error occurs

SelectRequest

public SelectRequest(java.lang.String sqlQuery,
                     boolean escapeProcessing,
                     int timeout,
                     java.lang.String lineSeparator)
Creates a new SelectRequest instance. The caller must give an SQL request, without any leading or trailing spaces and beginning with the 'select' keyword (it will not be checked).

The request is not parsed but it can be done later by a call to parse(DatabaseSchema, int, boolean).

Parameters:
sqlQuery - the SQL query
escapeProcessing - should the driver to escape processing before sending to the database ?
timeout - an int value
lineSeparator - the line separator used in the query
See Also:
parse(org.objectweb.cjdbc.common.sql.schema.DatabaseSchema, int, boolean)

SelectRequest

public SelectRequest(CJDBCInputStream in)
              throws java.io.IOException
See Also:
AbstractRequest.AbstractRequest(CJDBCInputStream, int)
Method Detail

sendToStream

public void sendToStream(CJDBCOutputStream out,
                         boolean needSqlSkeleton)
                  throws java.io.IOException
Description copied from class: AbstractRequest
Serialize the request on the output stream by sending only the needed parameters to reconstruct it on the controller. Has to mirror the deserialization method above.

Overrides:
sendToStream in class AbstractRequest
Parameters:
out - destination CJDBCOutputStream
needSqlSkeleton - true if controller wants SQL templates
Throws:
java.io.IOException - if fails
See Also:
AbstractRequest

parse

public void parse(DatabaseSchema schema,
                  int granularity,
                  boolean isCaseSensitive)
           throws java.sql.SQLException

The result of the parsing is accessible through the getSelect(), getFrom()and getWhere()functions.

Specified by:
parse in class AbstractRequest
Parameters:
schema - a DatabaseSchema value
granularity - parsing granularity as defined in ParsingGranularities
isCaseSensitive - true if parsing must be case sensitive
Throws:
java.sql.SQLException - if the parsing fails
See Also:
AbstractRequest.parse(org.objectweb.cjdbc.common.sql.schema.DatabaseSchema, int, boolean)

cloneParsing

public void cloneParsing(AbstractRequest request)
Description copied from class: AbstractRequest
Clones the parsing of a request.

Specified by:
cloneParsing in class AbstractRequest
Parameters:
request - the parsed request to clone
See Also:
AbstractRequest.cloneParsing(AbstractRequest)

getFromTables

private java.util.ArrayList getFromTables(java.lang.String fromClause,
                                          DatabaseSchema schema,
                                          boolean isCaseSensitive)
                                   throws java.sql.SQLException
Extracts the tables from the given FROM clause and retrieves their alias if any.

Parameters:
fromClause - the FROM clause of the request (without the FROM keyword)
schema - the DatabaseSchema this request refers to
isCaseSensitive - true if table name parsing is case sensitive
Returns:
an ArrayList of AliasedDatabaseTable objects
Throws:
java.sql.SQLException - if an error occurs

getSelectedColumns

private java.util.ArrayList getSelectedColumns(java.lang.String selectClause,
                                               java.util.ArrayList aliasedFrom,
                                               boolean isCaseSensitive)
Gets all the columns selected in the given SELECT clause.

The selected columns or tables must be found in the given ArrayList of AliasedDatabaseTable representing the FROM clause of the same request.

Parameters:
selectClause - SELECT clause of the request (without the SELECT keyword)
aliasedFrom - an ArrayList of AliasedDatabaseTable
isCaseSensitive - true if column name parsing is case sensitive
Returns:
an ArrayList of TableColumn

isSqlFunction

private boolean isSqlFunction(java.lang.String str)
Checks if the string parameter represents an SQL function, e. g., MAX, COUNT, SUM, etc.

Parameters:
str - A lower-case string that may represent an SQL function
Returns:
boolean true if it is an SQL function and false otherwise.

getWhereColumns

private java.util.ArrayList getWhereColumns(java.lang.String whereClause,
                                            java.util.ArrayList aliasedFrom,
                                            boolean setUniqueCacheable,
                                            boolean isCaseSensitive)
Gets all the columns involved in the given WHERE clause.

The selected columns or tables must be found in the given ArrayList of AliasedDatabaseTable representing the FROM clause of the same request.

Parameters:
whereClause - WHERE clause of the request (without the WHERE keyword)
aliasedFrom - an ArrayList of AliasedDatabaseTable
setUniqueCacheable - true if we have to check is this select is UNIQUE or not
isCaseSensitive - true if column name parsing is case sensitive
Returns:
an ArrayList of TableColumn

getSelect

public java.util.ArrayList getSelect()
Returns an ArrayList of DatabaseColumn objects representing the columns selected in the SELECT clause of this request.

Returns:
an ArrayList of TableColumn

getFrom

public java.util.ArrayList getFrom()
Returns an ArrayList of String objects representing the table names found in the FROM clause of this request.

Returns:
an ArrayList of String

getAliasedFrom

public java.util.ArrayList getAliasedFrom()
Returns an ArrayList of AliasedDatabaseTable objects representing the table names found in the FROM clause of this request.

Returns:
an ArrayList of AliasedDatabaseTable

getWhere

public java.util.ArrayList getWhere()
Returns an ArrayList of TableColumn objects representing the columns involved in the WHERE clause of this request.

Returns:
an ArrayList of TableColumn

getOrderBy

public java.util.ArrayList getOrderBy()
Returns an ArrayList of OrderBy objects representing the columns involved in the ORDER BY clause of this request.

Returns:
an ArrayList of OrderBy

getWhereValues

public java.util.Hashtable getWhereValues()
Returns an Hashtable of String keys representing unique column names and String values associated with the columns involved in this request.

Returns:
an Hashtable value

needsMacroProcessing

public boolean needsMacroProcessing()
Description copied from class: AbstractRequest
Returns true if this request requires macro (RAND(), NOW(), ...) processing.

Specified by:
needsMacroProcessing in class AbstractRequest
Returns:
true
See Also:
AbstractRequest.needsMacroProcessing()

returnsResultSet

public boolean returnsResultSet()
Description copied from class: AbstractRequest
Does this request returns a ResultSet?

Specified by:
returnsResultSet in class AbstractRequest
Returns:
true is this request will return a ResultSet
See Also:
AbstractRequest.returnsResultSet()

getPkValue

public java.lang.String getPkValue()
Returns:
Returns the pkValue.

setPkValue

public void setPkValue(java.lang.String pkValue)
Parameters:
pkValue - The pkValue to set.

debug

public void debug()
Displays some debugging information about this request.

Overrides:
debug in class AbstractRequest


Copyright © 2002, 2005 - ObjectWeb Consortium - All Rights Reserved.