GigaSpaces XAP 7.0 API

com.j_spaces.core.client
Class SQLQuery<T>

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

public class SQLQuery<T>
extends Object
implements Query<T>, Serializable

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:
thrown when space operations with SQLQuery fails., Serialized Form

Constructor Summary
SQLQuery()
          Empty constructor.
SQLQuery(Class<T> clazz, String sqlQuery)
          Constructor for setting the className of the entry to query with the sql query expression.
SQLQuery(Class<T> clazz, String sqlQuery, Object... parameters)
          Constructor for setting the class of the entry to query with the sql query expression.
SQLQuery(net.jini.core.entry.Entry entry, String sqlQuery)
          Constructor for setting the entry as a template to query with the sql query expression.
SQLQuery(String className, String sqlQuery)
          Constructor for setting the class of the entry to query with the sql query expression.
SQLQuery(String className, String sqlQuery, Object... parameters)
          Constructor for setting the class of the entry to query with the sql query expression.
SQLQuery(T object, String sqlQuery)
          Constructor for setting the object as a template to query with the sql query expression.
SQLQuery(T object, String sqlQuery, Object... parameters)
          Constructor for setting the object as a template to query with the sql query expression.
 
Method Summary
 boolean equals(Object obj)
          Equals ignore the template member.
 String getClassName()
          Extract the Entry class name for the current query.
 net.jini.core.entry.Entry getEntry()
          Deprecated. use getObject() instead.
 String getFromQuery()
          Returns a string representation of this SQLQuery, in the form of: FROM table name WHERE query expression
 T getObject()
          Extract the POJO for the current query.
 Object[] getParameters()
           
 String getQuery()
          Returns the 'WHERE' part of this SQL Query.
 String getSelectAllQuery()
          Returns a string representation of this SQLQuery, in the form of: SELECT * FROM table name WHERE query expression
 String getSelectCountQuery()
          Returns a string representation of this SQLQuery, in the form of: SELECT count(*) FROM table name WHERE query expression
 int hashCode()
           
 boolean hasParameters()
           
 boolean hasWhereClause()
          Returns true if the query has a where clause.
 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 setParameter(int index, Object value)
          Set the query parameter value.
 void setParameters(Object... parameters)
           
 void setQuery(String wherePart)
          Sets the query statement.
 void setTemplate(net.jini.core.entry.Entry entry)
          Sets a new entry template.
 void setTemplate(T 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
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SQLQuery

public SQLQuery()
Empty constructor.


SQLQuery

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

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

SQLQuery

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

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

SQLQuery

public SQLQuery(Class<T> clazz,
                String sqlQuery,
                Object... parameters)
Constructor for setting the class of the entry to query with the sql query expression.

Parameters:
clazz - Entry class to be queried.
sqlQuery - The SQL Query expression (contents of the WHERE part).
parameters - Parameters for the sql query

SQLQuery

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

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

Usage example:

 SQLQuery query = new SQLQuery(MyEntry.class, "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(T object,
                String sqlQuery,
                Object... parameters)
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).
parameters - Parameters for the sql query

Usage Example:


  // The following query returns all the objects that have m_integer between 50 and 100
 SQLQuery query = new SQLQuery(MyPojo(), "m_integer > ? and m_integer < ?",50,100);
 MyPojo result = (MyPojo)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 object: " + result.toString());
 else
     System.out.println("None of the objects  match the specified query: "+query);
 
See Also:
SQLQuery(String className, String sqlQuery)

SQLQuery

public SQLQuery(T 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:


 SQLQuery query = new SQLQuery(MyPojo(), "m_integer > 50");
 MyPojo result = (MyPojo)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 object: " + result.toString());
 else
     System.out.println("None of the objects  match the specified query: "+query);
 
See Also:
SQLQuery(String className, String sqlQuery)

SQLQuery

public SQLQuery(net.jini.core.entry.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

getObject

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

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

getEntry

@Deprecated
public net.jini.core.entry.Entry getEntry()
Deprecated. use getObject() instead.

Extract the Entry for the current query.

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

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<T>
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<T>
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(net.jini.core.entry.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(T 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.

getFromQuery

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

FROM table name WHERE query expression

Returns:
the string representation of the query

getSelectAllQuery

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

SELECT * FROM table name WHERE query expression

Returns:
a string representation of the object.

getSelectCountQuery

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

SELECT count(*) FROM table name WHERE query expression

Returns:
a string representation of the object.

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.

isStoredProcedure

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

Returns:
true if represents a stored procedure

hasWhereClause

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

Returns:
true has a "where" part

getParameters

public Object[] getParameters()
Returns:
the parameters

hasParameters

public boolean hasParameters()
Returns:
true if the SQLQuery has parameters

setParameters

public void setParameters(Object... parameters)
Parameters:
parameters - the parameters to set

setParameter

public void setParameter(int index,
                         Object value)
Set the query parameter value.

Parameters:
index - parameter index - start with 1
value - parameter value

hashCode

public int hashCode()
Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)
Equals ignore the template member. Since it is only used for SharedDataIterator and when a .NET data source is in use the equals of the template will never return true

Overrides:
equals in class Object

GigaSpaces XAP 7.0 API

Copyright © GigaSpaces.