org.geotools.filter
Class SQLEncoder

java.lang.Object
  extended byorg.geotools.filter.SQLEncoder
All Implemented Interfaces:
FilterVisitor

public class SQLEncoder
extends java.lang.Object
implements FilterVisitor

Encodes a filter into a SQL WHERE statement. It should hopefully be generic enough that any SQL database will work with it, though it has only been tested with MySQL and Postgis. This generic SQL encoder should eventually be able to encode all filters except Geometry Filters (currently LikeFilters are not yet fully implemented, but when they are they should be generic enough). This is because the OGC's SFS for SQL document specifies two ways of doing SQL databases, one with native geometry types and one without. To implement an encoder for one of the two types simply subclass off of this encoder and put in the proper GeometryFilter visit method. Then add the filter types supported to the capabilities in the static capabilities.addType block.

Author:
Chris Holmes, TOPP

Field Summary
protected  java.io.Writer out
          where to write the constructed string from visiting the filters.
 
Constructor Summary
SQLEncoder()
          Empty constructor
SQLEncoder(java.io.Writer out, Filter filter)
          Convenience constructor to perform the whole encoding process at once.
 
Method Summary
protected  FilterCapabilities createFilterCapabilities()
          Sets the capabilities of this filter.
 java.lang.String encode(Filter filter)
          Performs the encoding, returns a string of the encoded SQL.
 void encode(java.io.Writer out, Filter filter)
          Performs the encoding, sends the encoded sql to the writer passed in.
 FilterCapabilities getCapabilities()
          Describes the capabilities of this encoder.
 void visit(AttributeExpression expression)
          Writes the SQL for the attribute Expression.
 void visit(BetweenFilter filter)
          Writes the SQL for the Between Filter.
 void visit(CompareFilter filter)
          Writes the SQL for a Compare Filter.
 void visit(Expression expression)
          Writes the SQL for the attribute Expression.
 void visit(FidFilter filter)
          This only exists the fulfill the interface - unless There is a way of determining the FID column in the database...
 void visit(Filter filter)
          This should never be called.
 void visit(FunctionExpression expression)
          Writes sql for a function expression.
 void visit(GeometryFilter filter)
          Writes the SQL for the Geometry Filter.
 void visit(LikeFilter filter)
          Writes the SQL for the Like Filter.
 void visit(LiteralExpression expression)
          Export the contents of a Literal Expresion
 void visit(LogicFilter filter)
          Writes the SQL for the Logic Filter.
 void visit(MathExpression expression)
          Writes the SQL for the Math Expression.
 void visit(NullFilter filter)
          Writes the SQL for the Null Filter.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

out

protected java.io.Writer out
where to write the constructed string from visiting the filters.

Constructor Detail

SQLEncoder

public SQLEncoder()
Empty constructor


SQLEncoder

public SQLEncoder(java.io.Writer out,
                  Filter filter)
           throws SQLEncoderException
Convenience constructor to perform the whole encoding process at once.

Parameters:
out - the writer to encode the SQL to.
filter - the Filter to be encoded.
Throws:
SQLEncoderException - If there were problems encoding
Method Detail

createFilterCapabilities

protected FilterCapabilities createFilterCapabilities()
Sets the capabilities of this filter.

Returns:
FilterCapabilities for this Filter

encode

public void encode(java.io.Writer out,
                   Filter filter)
            throws SQLEncoderException
Performs the encoding, sends the encoded sql to the writer passed in.

Parameters:
out - the writer to encode the SQL to.
filter - the Filter to be encoded.
Throws:
SQLEncoderException - If filter type not supported, or if there were io problems.

encode

public java.lang.String encode(Filter filter)
                        throws SQLEncoderException
Performs the encoding, returns a string of the encoded SQL.

Parameters:
filter - the Filter to be encoded.
Returns:
the string of the SQL where statement.
Throws:
SQLEncoderException - If filter type not supported, or if there were io problems.

getCapabilities

public FilterCapabilities getCapabilities()
Describes the capabilities of this encoder.

Performs lazy creation of capabilities.

Returns:
The capabilities supported by this encoder.

visit

public void visit(Filter filter)
This should never be called. This can only happen if a subclass of AbstractFilter failes to implement its own version of accept(FilterVisitor);

Specified by:
visit in interface FilterVisitor
Parameters:
filter - The filter to visit
Throws:
java.lang.RuntimeException - for IO Encoding problems.

visit

public void visit(BetweenFilter filter)
           throws java.lang.RuntimeException
Writes the SQL for the Between Filter.

Specified by:
visit in interface FilterVisitor
Parameters:
filter - the Filter to be visited.
Throws:
java.lang.RuntimeException - for io exception with writer

visit

public void visit(LikeFilter filter)
           throws java.lang.UnsupportedOperationException
Writes the SQL for the Like Filter. Assumes the current java implemented wildcards for the Like Filter: . for multi and .? for single. And replaces them with the SQL % and _, respectively. Currently does nothing, and should not be called, not included in the capabilities.

Specified by:
visit in interface FilterVisitor
Parameters:
filter - the Like Filter to be visited.
Throws:
java.lang.UnsupportedOperationException - always, as likes aren't implemented yet.

visit

public void visit(LogicFilter filter)
           throws java.lang.RuntimeException
Writes the SQL for the Logic Filter.

Specified by:
visit in interface FilterVisitor
Parameters:
filter - the logic statement to be turned into SQL.
Throws:
java.lang.RuntimeException - for io exception with writer

visit

public void visit(CompareFilter filter)
           throws java.lang.RuntimeException
Writes the SQL for a Compare Filter.

Specified by:
visit in interface FilterVisitor
Parameters:
filter - the comparison to be turned into SQL.
Throws:
java.lang.RuntimeException - for io exception with writer

visit

public void visit(GeometryFilter filter)
Writes the SQL for the Geometry Filter.

Specified by:
visit in interface FilterVisitor
Parameters:
filter - the geometry logic to be turned into SQL.

visit

public void visit(NullFilter filter)
           throws java.lang.RuntimeException
Writes the SQL for the Null Filter.

Specified by:
visit in interface FilterVisitor
Parameters:
filter - the null filter to be written to SQL.
Throws:
java.lang.RuntimeException - for io exception with writer

visit

public void visit(FidFilter filter)
This only exists the fulfill the interface - unless There is a way of determining the FID column in the database...

Specified by:
visit in interface FilterVisitor
Parameters:
filter - the Fid Filter.

visit

public void visit(AttributeExpression expression)
           throws java.lang.RuntimeException
Writes the SQL for the attribute Expression.

Specified by:
visit in interface FilterVisitor
Parameters:
expression - the attribute to turn to SQL.
Throws:
java.lang.RuntimeException - for io exception with writer

visit

public void visit(Expression expression)
Writes the SQL for the attribute Expression.

Specified by:
visit in interface FilterVisitor
Parameters:
expression - the attribute to turn to SQL.

visit

public void visit(LiteralExpression expression)
           throws java.lang.RuntimeException
Export the contents of a Literal Expresion

Specified by:
visit in interface FilterVisitor
Parameters:
expression - the Literal to export
Throws:
java.lang.RuntimeException - for io exception with writer

visit

public void visit(MathExpression expression)
           throws java.lang.RuntimeException
Writes the SQL for the Math Expression.

Specified by:
visit in interface FilterVisitor
Parameters:
expression - the Math phrase to be written.
Throws:
java.lang.RuntimeException - for io problems

visit

public void visit(FunctionExpression expression)
           throws java.lang.UnsupportedOperationException
Writes sql for a function expression. Not currently supported.

Specified by:
visit in interface FilterVisitor
Parameters:
expression - a function expression
Throws:
java.lang.UnsupportedOperationException - every time, this isn't supported.


Copyright © 1996-2003 GeoTools. All Rights Reserved.