Enum Class DefaultSqlContainsPolicy

java.lang.Object
java.lang.Enum<DefaultSqlContainsPolicy>
de.calamanari.adl.sql.config.DefaultSqlContainsPolicy
All Implemented Interfaces:
SqlContainsPolicy, Serializable, Comparable<DefaultSqlContainsPolicy>, Constable

public enum DefaultSqlContainsPolicy extends Enum<DefaultSqlContainsPolicy> implements SqlContainsPolicy
The implementations in this enumerations cover the CONTAINS translation (typically SQL LIKE) for a couple of databases.

Notes:
Be aware that the default behavior for most of the policies below is to remove any potentially conflicting pattern matching characters (such as '%', '_') if they are part of a given search text.
This creates the limitation that users won't be able to search for them. Should you have the need to support these characters it depends on the behavior of your JDBC-driver what you can do. First check what happens if you decorate your current policy with SqlContainsPolicy.PreparatorFunction.none() to pass the full (unfiltered) search snippet as the parameter of the PreparedStatement. Test this with different user-inputs.
Should the result not look as expected, try pattern escaping with database-specific SQL-syntax, e.g. LIKE ... ESCAPE ... using characters that won't be handled by the driver. An interesting discussion regarding this topic can be found here.
If the result still does not look as desired, you might solve this issue by configuring a custom AdlSqlType that should deal with the problem in a custom QueryParameterCreator in combination with a custom ArgValueFormatter, where you have the information MatchOperator.CONTAINS available indicating that the current parameter value should be a pattern for a LIKE. However, because of the complexity this should be the last option.

Author:
Karl Eilebrecht
  • Enum Constant Details

    • MYSQL

      public static final DefaultSqlContainsPolicy MYSQL
      Creates a standard like with pattern concatenation using CONCAT.
    • SQL92

      public static final DefaultSqlContainsPolicy SQL92
      Creates a standard LIKE with pattern concatenation using ANSI-'||', should work for many databases such as Oracle, SQLite, PostgreSQL
    • SQL_SERVER

      public static final DefaultSqlContainsPolicy SQL_SERVER
      Creates a standard LIKE with pattern concatenation using '+', e.g., Microsoft SQL-Server, also removes any '['-bracket from the search snippet.
    • SQL_SERVER2

      public static final DefaultSqlContainsPolicy SQL_SERVER2
      Creates a CHARINDEX-based CONTAINS, only Microsoft SQL-Server, accepts any snippet as there is no wildcard removal.
    • UNSUPPORTED

      public static final DefaultSqlContainsPolicy UNSUPPORTED
      Special case policy: will reject any attempt to translate a CONTAINS and throw ContainsNotSupportedException
  • Method Details

    • values

      public static DefaultSqlContainsPolicy[] values()
      Returns an array containing the constants of this enum class, in the order they are declared.
      Returns:
      an array containing the constants of this enum class, in the order they are declared
    • valueOf

      public static DefaultSqlContainsPolicy valueOf(String name)
      Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)
      Parameters:
      name - the name of the enum constant to be returned.
      Returns:
      the enum constant with the specified name
      Throws:
      IllegalArgumentException - if this enum class has no constant with the specified name
      NullPointerException - if the argument is null
    • prepareSearchSnippet

      public String prepareSearchSnippet(String value)
      Description copied from interface: SqlContainsPolicy
      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.

      Specified by:
      prepareSearchSnippet in interface SqlContainsPolicy
      Parameters:
      value - the text value (snippet) of a CONTAINS MatchExpression
      Returns:
      cleaned value
    • createInstruction

      public String createInstruction(String columnName, String patternParameter)
      Description copied from interface: SqlContainsPolicy
      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.
      Specified by:
      createInstruction in interface SqlContainsPolicy
      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}