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, MySqlSqlHandler, PostgreSqlSqlHandler

public class BasicSqlHandler
extends java.lang.Object
implements SqlHandler

Basic implementation.

Since:
MMBase-1.7
Version:
$Id: BasicSqlHandler.java,v 1.32.2.4 2004/09/07 12:58:47 pierre Exp $
Author:
Rob van Maris

Constructor Summary
BasicSqlHandler(java.util.Map disallowedValues)
          Constructor.
 
Method Summary
protected  void appendCompositeConstraintToSql(java.lang.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(java.lang.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 appendField(java.lang.StringBuffer sb, Step step, java.lang.String fieldName, boolean includeTablePrefix)
          Creates an identifier for a field, and appends it to a stringbuffer.
 void appendFieldValue(java.lang.StringBuffer sb, java.lang.Object value, boolean toLowerCase, int fieldType)
          Represents field value as a string, appending the result to a stringbuffer.
protected  java.lang.StringBuffer appendLikeOperator(java.lang.StringBuffer sb, boolean caseSensitive)
          Appends the 'LIKE' operator for the given case sensitiviy.
 void appendQueryBodyToSql(java.lang.StringBuffer sb, SearchQuery query, SqlHandler firstInChain)
          Represents body of a SearchQuery object as a string in SQL format, using the database configuration.
 java.lang.String getAllowedValue(java.lang.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.
 java.lang.String toSql(SearchQuery query, SqlHandler firstInChain)
          Represents a SearchQuery object as a string in SQL format, using the database configuration.
static java.lang.String toSqlString(java.lang.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(java.util.Map disallowedValues)
Constructor.

Parameters:
disallowedValues - Map mapping disallowed table/fieldnames to allowed alternatives.
Method Detail

toSqlString

public static java.lang.String toSqlString(java.lang.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.


appendFieldValue

public void appendFieldValue(java.lang.StringBuffer sb,
                             java.lang.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.
fieldType - The field type.

toSql

public java.lang.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

appendQueryBodyToSql

public void appendQueryBodyToSql(java.lang.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.
Returns:
SQL string representation of the query.
Throws:
SearchQueryException

appendLikeOperator

protected java.lang.StringBuffer appendLikeOperator(java.lang.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.

appendConstraintToSql

public void appendConstraintToSql(java.lang.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 java.lang.String getAllowedValue(java.lang.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
Returns:
The mapped value.

appendCompositeConstraintToSql

protected void appendCompositeConstraintToSql(java.lang.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(java.lang.StringBuffer sb,
                           Step step,
                           java.lang.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.7.4.20050922