org.mmbase.storage.search.implementation.database
Class BasicSqlHandler

java.lang.Object
  extended byorg.mmbase.storage.search.implementation.database.BasicSqlHandler
All Implemented Interfaces:
SqlHandler
Direct Known Subclasses:
HSqlSqlHandler, InformixSqlHandler, MSSqlSqlHandler, MySqlSqlHandler, PostgreSqlSqlHandler

public class BasicSqlHandler
extends Object
implements SqlHandler

Basic implementation.

Since:
MMBase-1.7
Version:
$Id: BasicSqlHandler.java,v 1.60 2006/07/05 20:06:16 michiel Exp $
Author:
Rob van Maris

Constructor Summary
BasicSqlHandler()
          Constructor.
 
Method Summary
protected  void appendCompositeConstraintToSql(StringBuffer sb, CompositeConstraint compositeConstraint, SearchQuery query, boolean inverse, boolean inComposite, SqlHandler firstInChain)
          Represents a CompositeConstraint object as a constraint in SQL format, appending the result to a stringbuffer.
 void appendConstraintToSql(StringBuffer sb, Constraint constraint, SearchQuery query, boolean inverse, boolean inComposite)
          Represents Constraint object, that is not a CompositeConstraint, as a constraint in SQL format, appending the result to a stringbuffer.
protected  void appendDateField(StringBuffer sb, Step step, String fieldName, boolean multipleSteps, int datePart)
           
protected  void appendDateValue(StringBuffer sb, Date value)
           
protected  void appendField(StringBuffer sb, Step step, String fieldName, boolean includeTablePrefix)
          Creates an identifier for a field, and appends it to a stringbuffer.
 void appendFieldValue(StringBuffer sb, Object value, boolean toLowerCase, int fieldType)
          Represents field value as a string, appending the result to a stringbuffer.
protected  StringBuffer appendLikeOperator(StringBuffer sb, boolean caseSensitive)
          Appends the 'LIKE' operator for the given case sensitiviy.
 void appendQueryBodyToSql(StringBuffer sb, SearchQuery query, SqlHandler firstInChain)
          Represents body of a SearchQuery object as a string in SQL format, using the database configuration.
protected  void appendRelationConstraints(StringBuffer sbRelations, RelationStep relationStep, boolean multipleSteps)
           
protected  StringBuffer appendSortOrderDirection(StringBuffer sb, SortOrder sortOrder)
           
protected  StringBuffer appendSortOrderField(StringBuffer sb, SortOrder sortOrder, boolean multipleSteps)
           
protected  StringBuffer appendSortOrders(StringBuffer sb, SearchQuery query)
           
protected  void appendTableAlias(StringBuffer sb, Step step)
           
protected  void appendTableName(StringBuffer sb, Step step)
           
 String getAllowedValue(String value)
          Maps string to value that is allowed as table or field name.
 int getSupportLevel(Constraint constraint, SearchQuery query)
          Gets the level at which a constraint is supported for a query by this handler.
 int getSupportLevel(int feature, SearchQuery query)
          Gets the level at which a feature is supported for a query by this handler.
 String toSql(SearchQuery query, SqlHandler firstInChain)
          Represents a SearchQuery object as a string in SQL format, using the database configuration.
static String toSqlString(String str)
          Utility method, modifies strings for use in SQL statements.
protected  boolean useLower(FieldCompareConstraint constraint)
          Wether the 'LOWER' function needs to be used to implement case insensitivity.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BasicSqlHandler

public BasicSqlHandler()
Constructor.

Method Detail

toSqlString

public static String toSqlString(String str)
Utility method, modifies strings for use in SQL statements. This amounts to replacing all single quotes by two single quotes.

Parameters:
str - The input string.
Returns:
The modified string.

useLower

protected boolean useLower(FieldCompareConstraint constraint)
Wether the 'LOWER' function needs to be used to implement case insensitivity. This is not always the case, because some database only match case insensitively, in which case it does not make sense to lowercase.


appendDateValue

protected void appendDateValue(StringBuffer sb,
                               Date value)

appendFieldValue

public void appendFieldValue(StringBuffer sb,
                             Object value,
                             boolean toLowerCase,
                             int fieldType)
Represents field value as a string, appending the result to a stringbuffer.

Depending on the fieldType:

Parameters:
sb - The stringbuffer to append to.
value - The field value.
toLowerCase - True when String must be converted to lower case.
fieldType - The field type.

toSql

public String toSql(SearchQuery query,
                    SqlHandler firstInChain)
             throws SearchQueryException
Description copied from interface: SqlHandler
Represents a SearchQuery object as a string in SQL format, using the database configuration.

Specified by:
toSql in interface SqlHandler
Parameters:
query - The searchquery.
firstInChain - The first element in the chain of handlers. At some point appendQueryBodyToSql() will have to be called on this handler, to generate the body of the query.
Returns:
SQL string representation of the query.
Throws:
SearchQueryException

appendRelationConstraints

protected void appendRelationConstraints(StringBuffer sbRelations,
                                         RelationStep relationStep,
                                         boolean multipleSteps)
Since:
MMBase-1.8

appendQueryBodyToSql

public void appendQueryBodyToSql(StringBuffer sb,
                                 SearchQuery query,
                                 SqlHandler firstInChain)
                          throws SearchQueryException
Description copied from interface: SqlHandler
Represents body of a SearchQuery object as a string in SQL format, using the database configuration. Appends this to a stringbuffer.
The body of the SQL query string is defined as the substring containing fields, tables, constraints and orders.

Specified by:
appendQueryBodyToSql in interface SqlHandler
Parameters:
sb - The stringbuffer to append to.
query - The searchquery.
firstInChain - The first element in the chain of handlers. At some point appendConstraintToSql() will have to be called on this handler, to generate the constraints in the query.
Throws:
SearchQueryException

appendTableName

protected void appendTableName(StringBuffer sb,
                               Step step)
Parameters:
sb -
step -
Since:
MMBase-1.8

appendTableAlias

protected void appendTableAlias(StringBuffer sb,
                                Step step)
Since:
MMBase-1.8

appendSortOrderDirection

protected StringBuffer appendSortOrderDirection(StringBuffer sb,
                                                SortOrder sortOrder)
                                         throws IllegalStateException
Throws:
IllegalStateException
Since:
MMBase-1.8

appendSortOrderField

protected StringBuffer appendSortOrderField(StringBuffer sb,
                                            SortOrder sortOrder,
                                            boolean multipleSteps)
Since:
MMBase-1.8

appendSortOrders

protected StringBuffer appendSortOrders(StringBuffer sb,
                                        SearchQuery query)
Since:
MMBase-1.8

appendLikeOperator

protected StringBuffer appendLikeOperator(StringBuffer sb,
                                          boolean caseSensitive)
Appends the 'LIKE' operator for the given case sensitiviy. Some databases support a case insensitive LIKE ('ILIKE'). Implementations for those database can override this method.

Returns:
The string buffer.

appendDateField

protected void appendDateField(StringBuffer sb,
                               Step step,
                               String fieldName,
                               boolean multipleSteps,
                               int datePart)
Warning: No Java Documentation Available.

appendConstraintToSql

public void appendConstraintToSql(StringBuffer sb,
                                  Constraint constraint,
                                  SearchQuery query,
                                  boolean inverse,
                                  boolean inComposite)
Description copied from interface: SqlHandler
Represents Constraint object, that is not a CompositeConstraint, as a constraint in SQL format, appending the result to a stringbuffer. When it is part of a composite expression, it will be surrounded by parenthesis when needed.

Specified by:
appendConstraintToSql in interface SqlHandler
Parameters:
sb - The stringbuffer to append to.
constraint - The (non-composite) constraint.
query - The searchquery containing the constraint.
inverse - True when the inverse constraint must be represented, false otherwise.
inComposite - True when the constraint is part of a composite expression.

getSupportLevel

public int getSupportLevel(int feature,
                           SearchQuery query)
                    throws SearchQueryException
Description copied from interface: SqlHandler
Gets the level at which a feature is supported for a query by this handler. This is one of either: Given the choice, the query handler with the highest level of support is prefered.

Specified by:
getSupportLevel in interface SqlHandler
Throws:
SearchQueryException

getSupportLevel

public int getSupportLevel(Constraint constraint,
                           SearchQuery query)
                    throws SearchQueryException
Description copied from interface: SqlHandler
Gets the level at which a constraint is supported for a query by this handler. This is one of either: Given the choice, the query handler with the highest level of support is prefered.

Specified by:
getSupportLevel in interface SqlHandler
Throws:
SearchQueryException

getAllowedValue

public String getAllowedValue(String value)
Description copied from interface: SqlHandler
Maps string to value that is allowed as table or field name.

Specified by:
getAllowedValue in interface SqlHandler
Parameters:
value - The string value.
Returns:
The mapped value.

appendCompositeConstraintToSql

protected void appendCompositeConstraintToSql(StringBuffer sb,
                                              CompositeConstraint compositeConstraint,
                                              SearchQuery query,
                                              boolean inverse,
                                              boolean inComposite,
                                              SqlHandler firstInChain)
                                       throws SearchQueryException
Represents a CompositeConstraint object as a constraint in SQL format, appending the result to a stringbuffer. When it is part of a composite expression, it will be surrounded by parenthesis when needed.

Parameters:
sb - The stringbuffer to append to.
compositeConstraint - The composite constraint.
query - The searchquery containing the constraint.
inverse - True when the inverse constraint must be represented, false otherwise.
inComposite - True when the constraint is part of a composite expression.
firstInChain - The first element in the chain of handlers. At some point appendConstraintToSql() will have to be called on this handler, to generate the constraints in the composite.
Throws:
SearchQueryException

appendField

protected void appendField(StringBuffer sb,
                           Step step,
                           String fieldName,
                           boolean includeTablePrefix)
Creates an identifier for a field, and appends it to a stringbuffer. The identifier is constructed from the fieldname, optionally prefixed by the tablename or the tablealias - when available.

Parameters:
sb - The stringbuffer to append to.
step - The Step the field belongs to.
fieldName - The fields fieldname.
includeTablePrefix - true when the fieldname must be prefixed with the tablename or tablealias (e.g. like in "images.number"), false otherwise.


MMBase build 1.8.1.20060716