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

java.lang.Object
  extended by org.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 40726 2010-01-26 11:24:35Z michiel $
Author:
Rob van Maris

Constructor Summary
BasicSqlHandler()
          Constructor.
 
Method Summary
protected  void appendCompositeConstraintToSql(StringBuilder 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(StringBuilder 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(StringBuilder sb, Step step, String fieldName, boolean multipleSteps, int datePart)
           
protected  void appendDateValue(StringBuilder sb, Date value)
           
protected  void appendField(StringBuilder sb, SortOrder sortOrder, boolean includeTablePrefix)
          Creates an identifier for a field absed on adate from a sortorder, and appends it to a stringbuffer.
protected  void appendField(StringBuilder sb, StepField field, boolean includeTablePrefix)
           
protected  void appendField(StringBuilder sb, Step step, String fieldName, boolean includeTablePrefix)
          Creates an identifier for a field, and appends it to a stringbuffer.
 void appendFieldValue(StringBuilder sb, Object value, boolean toLowerCase, int fieldType)
          Represents field value as a string, appending the result to a stringbuilder
protected  StringBuilder appendLikeOperator(StringBuilder sb, FieldConstraint fieldConstraint)
          Appends the 'LIKE' operator for the given case sensitiviy.
protected  void appendLowerField(StringBuilder sb, Step step, String fieldName, boolean includeTablePrefix)
           
protected  String appendPreField(StringBuilder sb, FieldConstraint constraint, StepField field, boolean multiple)
           
 void appendQueryBodyToSql(StringBuilder sb, SearchQuery query, SqlHandler firstInChain)
          Represents body of a SearchQuery object as a string in SQL format, using the database configuration.
protected  StringBuilder appendRegularExpressionOperator(StringBuilder sb, FieldConstraint fieldConstraint)
           
protected  void appendRelationConstraints(StringBuilder sbRelations, RelationStep relationStep, boolean multipleSteps)
           
protected  StringBuilder appendSortOrderDirection(StringBuilder sb, SortOrder sortOrder)
           
protected  StringBuilder appendSortOrderField(StringBuilder sb, SortOrder sortOrder, boolean multipleSteps)
           
protected  StringBuilder appendSortOrderField(StringBuilder sb, SortOrder sortOrder, boolean multipleSteps, SearchQuery query)
           
protected  StringBuilder appendSortOrders(StringBuilder sb, SearchQuery query)
           
protected  void appendTableAlias(StringBuilder sb, Step step)
           
protected  void appendTableName(StringBuilder sb, Step step)
           
 String forceEncode(String st)
           
 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.
protected  String toSqlString(String str)
          Utility method, modifies strings for use in SQL statements.
protected  boolean useLower(FieldConstraint 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

protected 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.

forceEncode

public String forceEncode(String st)

useLower

protected boolean useLower(FieldConstraint 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(StringBuilder sb,
                               Date value)

appendFieldValue

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

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(StringBuilder sbRelations,
                                         RelationStep relationStep,
                                         boolean multipleSteps)
Since:
MMBase-1.8

appendQueryBodyToSql

public void appendQueryBodyToSql(StringBuilder 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(StringBuilder sb,
                               Step step)
Parameters:
sb -
step -
Since:
MMBase-1.8

appendTableAlias

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

appendSortOrderDirection

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

appendSortOrderField

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

appendSortOrderField

protected StringBuilder appendSortOrderField(StringBuilder sb,
                                             SortOrder sortOrder,
                                             boolean multipleSteps,
                                             SearchQuery query)
Since:
MMBase-1.8.2

appendSortOrders

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

appendLikeOperator

protected StringBuilder appendLikeOperator(StringBuilder sb,
                                           FieldConstraint fieldConstraint)
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.

appendRegularExpressionOperator

protected StringBuilder appendRegularExpressionOperator(StringBuilder sb,
                                                        FieldConstraint fieldConstraint)

appendDateField

protected void appendDateField(StringBuilder sb,
                               Step step,
                               String fieldName,
                               boolean multipleSteps,
                               int datePart)
Javadoc:

appendPreField

protected String appendPreField(StringBuilder sb,
                                FieldConstraint constraint,
                                StepField field,
                                boolean multiple)
Since:
MMBase-1.9.2

appendConstraintToSql

public void appendConstraintToSql(StringBuilder sb,
                                  Constraint constraint,
                                  SearchQuery query,
                                  boolean inverse,
                                  boolean inComposite)
                           throws SearchQueryException
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.
Throws:
SearchQueryException

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(StringBuilder 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(StringBuilder sb,
                           SortOrder sortOrder,
                           boolean includeTablePrefix)
Creates an identifier for a field absed on adate from a sortorder, 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.
sortOrder - The sortOrder object containing the field data.
includeTablePrefix - true when the fieldname must be prefixed with the tablename or tablealias (e.g. like in "images.number"), false otherwise.

appendField

protected void appendField(StringBuilder sb,
                           StepField field,
                           boolean includeTablePrefix)
Since:
MMBase-1.9.1

appendField

protected void appendField(StringBuilder 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.

appendLowerField

protected void appendLowerField(StringBuilder sb,
                                Step step,
                                String fieldName,
                                boolean includeTablePrefix)
Since:
MMBase-1.8.5


MMBase 2.0-SNAPSHOT - null