com.edustructures.sifworks.tools.queries
Class SQLQueryFormatter

java.lang.Object
  extended by com.edustructures.sifworks.tools.queries.QueryFormatter
      extended by com.edustructures.sifworks.tools.queries.SQLQueryFormatter

public class SQLQueryFormatter
extends QueryFormatter

An implementation of a QueryFormatter that formats SIF_Query conditions as a string suitable for inclusion in an SQL WHERE clause.

When preparing a dictionary to be passed to the SQLQueryFormatter.format method, the caller must map SIFDTD ElementDef constants to instances of the SQLField class. The constructor to that class requires two parameters: the application-defined name of the field, and a type code constant from the java.sql.Types class. The type code is used to properly render the field value according to its data type (e.g. strings are quoted with a single quote, numeric fields are rendered as-is, etc.)

For example,

Map m = new HashMap();
m.put( SIFDTD.STUDENTPERSONAL_REFID,
    new SQLField( "Students.Foreign_ID", java.sql.Types.VARCHAR ) );
m.put( SIFDTD.NAME_LASTNAME,
    new SQLField( "Students.Last_Name", java.sql.Types.VARCHAR ) );
m.put( SIFDTD.NAME_FIRSTNAME,
    new SQLField( "First_Name", java.sql.Types.VARCHAR ) );
m.put( SIFDTD.DEMOGRAPHICS_CITIZENSHIPSTATUS,
    new SQLField( "Students.US_Citizen_Bool{04=1;=0}", java.sql.Types.NUMERIC ) );
The above example might result in a string such as "( Students.US_Citizen_Bool = 0 )" or "( Students.Foreign_ID = '898' ) OR ( Students.Last_Name = 'Cortez' AND First_Name = 'Robert' )"

Version:
ADK 1.0

Constructor Summary
SQLQueryFormatter()
          Constructs a SQLQueryFormatter
 
Method Summary
 void addField(ElementDef def, QueryField field)
          Adds a QueryField to use for rendering an SQL Where clause using the format(Query) method.
 void addField(ElementDef def, SQLField field)
          Adds an SQLField to use for rendering an SQL Where clause using the format(Query) method.
 void addField(java.lang.String xPath, QueryField field)
          Adds a QueryField to use for rendering an SQL Where clause using the format(Query) method.
 void addField(java.lang.String xPath, SQLField field)
          Adds an SQLField to use for rendering an SQL Where clause using the format(Query) method.
 void addFields(MappingsContext context)
          Adds SQLFields to represent each field rule in the specified MappingsContext.
 java.lang.String format(Query query)
          Builds a query string given a dictionary of field definitions and a Query instance.
 java.lang.String format(Query query, boolean explicit)
           
 java.lang.String getCloseBrace()
          Return the text that should be inserted for a closing brace
 java.util.Set<java.util.Map.Entry<java.lang.Object,java.lang.Object>> getFields()
          Returns all of the SQLFields that have been mapped for this instance
 java.lang.String getOpenBrace()
          Return the text that should be inserted for an opening brace
 java.lang.String getOperator(ComparisonOperators op)
          Return the text that should be inserted for the particular comparison operator, such as "Equals"
 java.lang.String getOperator(GroupOperators op)
          Return the text that should be inserted for the particular grouping operator, such as "AND"
 java.lang.String renderField(ElementDef field, java.lang.Object def)
          Return the text for a field name
 java.lang.String renderValue(java.lang.String value, java.lang.Object def)
          Return the text for a field value
 
Methods inherited from class com.edustructures.sifworks.tools.queries.QueryFormatter
format, format
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SQLQueryFormatter

public SQLQueryFormatter()
Constructs a SQLQueryFormatter

Method Detail

addField

public void addField(ElementDef def,
                     SQLField field)
Adds an SQLField to use for rendering an SQL Where clause using the format(Query) method.

Parameters:
def - The ElementDef that is represented by the field
field - The SQL representation of the field

addField

public void addField(java.lang.String xPath,
                     SQLField field)
Adds an SQLField to use for rendering an SQL Where clause using the format(Query) method.

Parameters:
def - The XPath representing the SIF Element being mapped
field - The SQL representation of the field

addField

public void addField(ElementDef def,
                     QueryField field)
Adds a QueryField to use for rendering an SQL Where clause using the format(Query) method.

Parameters:
def - The ElementDef that is represented by the field
field - The SQL representation of the field

addField

public void addField(java.lang.String xPath,
                     QueryField field)
Adds a QueryField to use for rendering an SQL Where clause using the format(Query) method.

Parameters:
def - The xPath representing the SIF Element being mapped
field - The SQL representation of the field

addFields

public void addFields(MappingsContext context)
Adds SQLFields to represent each field rule in the specified MappingsContext.

Parameters:
context - The MappingsContext containing FieldMappings

getFields

public java.util.Set<java.util.Map.Entry<java.lang.Object,java.lang.Object>> getFields()
Returns all of the SQLFields that have been mapped for this instance

Returns:

format

public java.lang.String format(Query query)
                        throws QueryFormatterException
Builds a query string given a dictionary of field definitions and a Query instance. This method evaluates the conditions of that Query to produce a textual query string in the format determined by the implementation.

The dictionary should contain application-defined field values that map to ElementDef key elements. Whenever a SIF element or attribute is found in the Query, the corresponding application-defined field is used in its place.

A special convention allows agents to define an in-line translation table for replacing SIF element/attribute values with values defined in the table. If a field is expressed in the form "field-name{value1=cons1;value2=cons2;..}", the comma-delimited list of values within the curly braces is applied to the value of the SIF_Value element in the SIF_Query, such that "value1" is represented as "cons1". For example, the acceptable values for LibraryPatronStatus/@SifRefIdType attribute are "StudentPersonal" and "StaffPersonal". If in your application you represent these values as numeric types - say, 1 and 2, respectively - you could create the following in-line translation table to instruct the QueryFormatter to substitute "StudentPersonal" with "1" and "StaffPersonal" with "2": "CircRecord.PatronType{StudentPersonal=1;StaffPersonal=2}"

Parameters:
query - An ADK Query object, usually obtained during the processing of a SIF_Request by a Publisher message handler
Throws:
QueryFormatterException

format

public java.lang.String format(Query query,
                               boolean explicit)
                        throws QueryFormatterException
Throws:
QueryFormatterException

getOpenBrace

public java.lang.String getOpenBrace()
Return the text that should be inserted for an opening brace

Specified by:
getOpenBrace in class QueryFormatter

getCloseBrace

public java.lang.String getCloseBrace()
Return the text that should be inserted for a closing brace

Specified by:
getCloseBrace in class QueryFormatter

getOperator

public java.lang.String getOperator(ComparisonOperators op)
Return the text that should be inserted for the particular comparison operator, such as "Equals"

Specified by:
getOperator in class QueryFormatter

getOperator

public java.lang.String getOperator(GroupOperators op)
Return the text that should be inserted for the particular grouping operator, such as "AND"

Specified by:
getOperator in class QueryFormatter

renderField

public java.lang.String renderField(ElementDef field,
                                    java.lang.Object def)
                             throws QueryFormatterException
Return the text for a field name

Specified by:
renderField in class QueryFormatter
Parameters:
field - The field
def - The corresponding field definition from the Map passed to the format method
Returns:
The implementation returns the field name in whatever form is appropriate to the implementation, using the supplied def Object if necessary to obtain additional field information.
Throws:
QueryFormatterException

renderValue

public java.lang.String renderValue(java.lang.String value,
                                    java.lang.Object def)
                             throws QueryFormatterException
Return the text for a field value

Specified by:
renderValue in class QueryFormatter
Parameters:
field - The field value
def - The corresponding field definition from the Map passed to the format method
Returns:
The implementation returns the field value in whatever form is appropriate to the implementation, using the supplied def Object if necessary to obtain additional field information
Throws:
QueryFormatterException


Copyright © 2001-2007 Edustructures LLC. All Rights Reserved. SIFWorks® and ADK® are registered trademarks of Edustructures LLC. SIF™ and Schools Interoperability Framework are trademarks of the Schools Interoperability Framework Association.