Interface SqlContainsPolicy

All Superinterfaces:
Serializable
All Known Implementing Classes:
DefaultSqlContainsPolicy, SqlContainsPolicyDecorator

public interface SqlContainsPolicy extends Serializable
A concrete SqlContainsPolicy formats an SQL LIKE instruction (or anything similar) to realize a CONTAINS condition.

Unfortunately, this cannot be done database-independently because major databases support different syntax.
There can even be subtle differences related to the JDBC-driver. Some databases work nicely with like patterns as arguments to a prepared statement, other will escape the percentage signs and this way break the pattern.
This makes it difficult to format a LIKE in a generic way.

The SqlContainsPolicy takes care of both problems, the search pattern preparation and the SQL-syntax to express the LIKE.

Author:
Karl Eilebrecht
  • Method Details

    • name

      String name()
      Returns:
      unique name (identifier) of this policy
    • prepareSearchSnippet

      String prepareSearchSnippet(String value)
      This method allows to modify the snippet to conform to a specific database.

      E.g., given as a prepared statement parameter one would assume that further percentage signs or underscores in the snippet would be escaped by the driver. However, some databases interpret them as further wildcards.

      This method allows intercepting the snippet preparation (e.g., remove such problematic characters).

      Important:
      The return value of this method is still a text snippet and not yet the SQL-pattern to be included in the statement.
      So, typically, you should NOT surround the text here with percentage signs but. Otherwise the driver will most likely escape them and this way destroy the search pattern.

      Parameters:
      value - the text value (snippet) of a CONTAINS MatchExpression
      Returns:
      cleaned value
    • createInstruction

      String createInstruction(String columnName, String patternParameter)
      Returns the SQL-instruction to compare a column against a pattern.

      Here are some examples:

      • COL123 LIKE '%' || ${PATTERN_1011} || '%' should work well with Oracle, SQLite, PostgreSQL
      • COL123 LIKE CONCAT('%', ${PATTERN_1011}, '%') should do with MySQL
      • COL123 LIKE '%' + ${PATTERN_1011} + '%' should work with SQL Server
      • CHARINDEX(${PATTERN_1011}, COL123, 0) > 0 may also work with SQL Server
      Notes:
      • The percentage signs are not part of the prepared statement parameter. This will ensure they do their job, no matter if the driver escapes percentage signs in prepared statement parameters or not.
      • ${PATTERN_1011} is a placeholder without any meaning and not to be included in quotes here.
      • The best way to realize CONTAINS depends on your DB, and there might be multiple roads to success.
      Parameters:
      columnName - this is the SQL column or alias to perform the CONTAINS/LIKE operation on, e.g. COL123
      patternParameter - placeholder (later argument to prepared statement) that will hold the pattern (e.g., ${PATTERN_1011}
    • withPreparatorFunction

      default SqlContainsPolicy withPreparatorFunction(String name, SqlContainsPolicy.PreparatorFunction prepareSearchSnippetFunction)
      This method derives a new policy from the given one by replacing the preparation strategy with a new one.
      Parameters:
      name - identifier of the policy
      prepareSearchSnippetFunction -
      Returns:
      new policy instance
      See Also:
    • withPreparatorFunction

      default SqlContainsPolicy withPreparatorFunction(SqlContainsPolicy.PreparatorFunction prepareSearchSnippetFunction)
      This method derives a new policy from the given one by replacing the preparation strategy with a new one with an auto-generated name.
      Parameters:
      prepareSearchSnippetFunction -
      Returns:
      new policy instance
      See Also:
    • withCreatorFunction

      default SqlContainsPolicy withCreatorFunction(String name, SqlContainsPolicy.CreatorFunction createInstructionFunction)
      This method derives a new policy from the given one by replacing the creation strategy with a new one.
      Parameters:
      name - identifier of the policy
      createInstructionFunction -
      Returns:
      new policy instance
      See Also:
    • withCreatorFunction

      default SqlContainsPolicy withCreatorFunction(SqlContainsPolicy.CreatorFunction createInstructionFunction)
      This method derives a new policy from the given one by replacing the creation strategy with a new one with an auto-generated name.
      Parameters:
      createInstructionFunction -
      Returns:
      new policy instance
      See Also: