com.j_spaces.core.client
Class SQLQuery

java.lang.Object
  extended by com.j_spaces.core.client.SQLQuery
All Implemented Interfaces:
Query, Serializable, Entry
Direct Known Subclasses:
ContinousQuery

public class SQLQuery
extends Object
implements Query

The SQLQuery class is used to query the space using the SQL syntax. The query statement should only include the WHERE clause.

Supported JavaSpace operations
read/take/readMultiple/takeMultiple (with JavaSpace.NO_WAIT timeout)

UnSupported JavaSpace operations
Notify/readIfExists/takeIfExists and read/take/readMultiple/takeMultiple (with longer timeouts than JSpace.NO_WAIT)

Date and Time Formats
The following Date/TimeStamp Format is supported: 'yyyy-mm-dd hh:mm:ss' - i.e. '2004-12-20 20:40:10'
The following Time Format is supported: 'hh:mm:ss' - i.e. '20:40:10'

See Also:
Serialized Form

Field Summary
static String CALL
           
static String COUNT_PREFIX
           
static String DELETE_PREFIX
           
static String GROUP
           
static String ORDER
           
static String SELECT_PREFIX
           
 
Constructor Summary
SQLQuery()
          Empty constructor.
SQLQuery(Entry entry, String sqlQuery)
          Constructor for setting the entry as a template to query with the sql query expression.
SQLQuery(Object object, String sqlQuery)
          Constructor for setting the object as a template to query with the sql query expression.
SQLQuery(String className, String sqlQuery)
          Constructor for setting the className of the entry to query with the sql query expression.
 
Method Summary
 String getClassName()
          Extract the Entry class name for the current query.
 Entry getEntry()
          Extract the Entry for the current query.
 Object getObject()
          Extract the POJO for the current query.
 String getQuery()
          Returns the 'WHERE' part of this SQL Query.
 boolean hasWhereClause()
          Returns true if the query has a where clause.
 boolean isExternalEntry()
           
 boolean isNullExpression()
          This method should be used for check expression string.
 boolean isStoredProcedure()
          Returns true if this query is a stored procedure
 void setClassName(String className)
          This method should be used for new Entry classes introduced to the space.
 void setExternalEntry(boolean isExternalEntry)
           
 void setQuery(String wherePart)
          Sets the query statement.
 void setTemplate(Entry entry)
          Sets a new entry template.
 void setTemplate(Object object)
          Sets a new entry template.
 String toString()
          Returns a string representation of this SQLQuery, in the form of: SELECT * FROM table name WHERE query expression
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

SELECT_PREFIX

public static final String SELECT_PREFIX
See Also:
Constant Field Values

DELETE_PREFIX

public static final String DELETE_PREFIX
See Also:
Constant Field Values

COUNT_PREFIX

public static final String COUNT_PREFIX
See Also:
Constant Field Values

CALL

public static final String CALL
See Also:
Constant Field Values

GROUP

public static final String GROUP
See Also:
Constant Field Values

ORDER

public static final String ORDER
See Also:
Constant Field Values
Constructor Detail

SQLQuery

public SQLQuery()
Empty constructor.


SQLQuery

public SQLQuery(String className,
                String sqlQuery)
Constructor for setting the className of the entry to query with the sql query expression.

Parameters:
className - - Entry class name to be queried.
sqlQuery - - The SQL Query expression (contents of the WHERE part).

Usage example:

 SQLQuery query = new SQLQuery(MyEntry.class.getName(), "m_integer > 50");
 Entry result[] = space.readMultiple(query, null, Integer.MAX_VALUE);
 System.out.println("Found " + result.length + " Entries");
 for (int i = 0; i < result.length; i++) {
     System.out.println(i + " " + (MyEntry) result[i]);
 }
 

Note that a single read/take operation will return only the first match, if any.

SQLQuery

public SQLQuery(Object object,
                String sqlQuery)
Constructor for setting the object as a template to query with the sql query expression. The constructor behaves in a similar manner to the former, but incase of an ExternalEntry its class name is extracted implicitly.

Parameters:
object - - The POJO to query by
sqlQuery - - The SQL Query expression (contents of the WHERE part).

Usage Example:

 //let MyEntry extend ExternalEntry
 SQLQuery query = new SQLQuery(MyEntry(), "m_integer > 50");
 MyEntry result = (MyEntry)space.read(query, null, JavaSpace.NO_WAIT);
 if (result!=null)
     // if there are more then one, it will return the first entry found
     System.out.println("Found entry: " + result.toString());
 else
     System.out.println("None of the entries match the specified query: "+query);
 
See Also:
SQLQuery(String className, String sqlQuery)

SQLQuery

public SQLQuery(Entry entry,
                String sqlQuery)
Constructor for setting the entry as a template to query with the sql query expression. The constructor behaves in a similar manner to the former, but incase of an ExternalEntry its class name is extracted implicitly.

Parameters:
entry - - The entry to query by
sqlQuery - - The SQL Query expression (contents of the WHERE part).

Usage Example:

 //let MyEntry extend ExternalEntry
 SQLQuery query = new SQLQuery(MyEntry(), "m_integer > 50");
 MyEntry result = (MyEntry)space.read(query, null, JavaSpace.NO_WAIT);
 if (result!=null)
     // if there are more then one, it will return the first entry found
     System.out.println("Found entry: " + result.toString());
 else
     System.out.println("None of the entries match the specified query: "+query);
 
See Also:
SQLQuery(String className, String sqlQuery)
Method Detail

isNullExpression

public boolean isNullExpression()
This method should be used for check expression string.

Returns:
true if expression is null or empty

isExternalEntry

public boolean isExternalEntry()

getEntry

public Entry getEntry()
Extract the Entry for the current query.

Returns:
Returns the Entry or null value if this sql query represent POJO.

getObject

public Object getObject()
Extract the POJO for the current query.

Returns:
Returns the POJO or null value if this sql query represent Entry object.

getClassName

public String getClassName()
Extract the Entry class name for the current query.

Returns:
Returns the Entry class Name.

setClassName

public void setClassName(String className)
This method should be used for new Entry classes introduced to the space. For usage example, see setQuery method.

Specified by:
setClassName in interface Query
Parameters:
className - The Entry class name to be set.

setQuery

public void setQuery(String wherePart)
Sets the query statement. This should be the content of the SQL WHERE clause (without the WHERE identifier).

Specified by:
setQuery in interface Query
Parameters:
wherePart - - The where contents of the query

Usage Example:

 // let EmployeeEntry and AccountEntry be instances of ExternalEntry

 SQLQuery query = new SQLQuery(EmployeeEntry.class.getName(), "m_employeeId"+employeeId);
 EmployeeEntry employee = (EmployeeEntry)space.read(query,null,JavaSpace.NO_WAIT);
 if (employee==null) throw new EmployeeNotFoundException("Employee "+employeeId+" not found");
 query.setClassName(AccountingEntry.class.getName());
 query.setQuery("m_accountNo ="+ employee.accNo);
 AccountEntry account = (AccountEntry)space.take(query,null,JavaSpace.NO_WAIT);
 

setTemplate

public void setTemplate(Entry entry)
Sets a new entry template. Extracts the class name from the entry to be used in a subsequent query.

Parameters:
entry - - The Entry or an ExternalEntry

Usage Example:

 SQLQuery query = new SQLQuery(new Entry(),null);
 query.setTemplate(new MyDateEntry());
 query.setQuery("m_date <"+ new Date(System.currentTimeMillis()));
 Entry[] results = space.takeMultiple(query, null, Integer.MAX_VALUE);
 System.out.println("Found " + result.length + " Entries");
 for (int i = 0; i < result.length; i++) {
    System.out.println(i + " " + (MyEntry) result[i]);
 }
 

setTemplate

public void setTemplate(Object object)
Sets a new entry template. Extracts the class name from the entry to be used in a subsequent query.

Parameters:
object - - The POJO

Usage Example:

 SQLQuery query = new SQLQuery(new POJO(),null);
 query.setTemplate(new POJO());
 query.setQuery("m_date <"+ new Date(System.currentTimeMillis()));
 Object[] results = space.takeMultiple(query, null, Integer.MAX_VALUE);
 System.out.println("Found " + result.length + " Entries");
 for (int i = 0; i < result.length; i++) {
    System.out.println(i + " " + (MyEntry) result[i]);
 }
 

getQuery

public String getQuery()
Returns the 'WHERE' part of this SQL Query.

Returns:
'WHERE' part expression.

toString

public String toString()
Returns a string representation of this SQLQuery, in the form of:

SELECT * FROM table name WHERE query expression

Overrides:
toString in class Object
Returns:
a string representation of the object.

setExternalEntry

public void setExternalEntry(boolean isExternalEntry)

isStoredProcedure

public boolean isStoredProcedure()
Returns true if this query is a stored procedure

Returns:

hasWhereClause

public boolean hasWhereClause()
Returns true if the query has a where clause. Used in partial SQLQuery

Returns: