Enum Class DefaultSqlContainsPolicy
- All Implemented Interfaces:
SqlContainsPolicy,Serializable,Comparable<DefaultSqlContainsPolicy>,Constable
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
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Enum
Enum.EnumDesc<E extends Enum<E>>Nested classes/interfaces inherited from interface de.calamanari.adl.sql.config.SqlContainsPolicy
SqlContainsPolicy.CreatorFunction, SqlContainsPolicy.PreparatorFunction -
Enum Constant Summary
Enum ConstantsEnum ConstantDescriptionCreates a standard like with pattern concatenation using CONCAT.Creates a standard LIKE with pattern concatenation using '+', e.g., Microsoft SQL-Server, also removes any '['-bracket from the search snippet.Creates a CHARINDEX-based CONTAINS, only Microsoft SQL-Server, accepts any snippet as there is no wildcard removal.Creates a standard LIKE with pattern concatenation using ANSI-'||', should work for many databases such as Oracle, SQLite, PostgreSQLSpecial case policy: will reject any attempt to translate a CONTAINS and throwContainsNotSupportedException -
Method Summary
Modifier and TypeMethodDescriptioncreateInstruction(String columnName, String patternParameter) Returns the SQL-instruction to compare a column against a pattern.prepareSearchSnippet(String value) This method allows to modify the snippet to conform to a specific database.static DefaultSqlContainsPolicyReturns the enum constant of this class with the specified name.static DefaultSqlContainsPolicy[]values()Returns an array containing the constants of this enum class, in the order they are declared.Methods inherited from class java.lang.Enum
clone, compareTo, describeConstable, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOfMethods inherited from interface de.calamanari.adl.sql.config.SqlContainsPolicy
name, withCreatorFunction, withCreatorFunction, withPreparatorFunction, withPreparatorFunction
-
Enum Constant Details
-
MYSQL
Creates a standard like with pattern concatenation using CONCAT. -
SQL92
Creates a standard LIKE with pattern concatenation using ANSI-'||', should work for many databases such as Oracle, SQLite, PostgreSQL -
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
Creates a CHARINDEX-based CONTAINS, only Microsoft SQL-Server, accepts any snippet as there is no wildcard removal. -
UNSUPPORTED
Special case policy: will reject any attempt to translate a CONTAINS and throwContainsNotSupportedException
-
-
Method Details
-
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
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 nameNullPointerException- if the argument is null
-
prepareSearchSnippet
Description copied from interface:SqlContainsPolicyThis 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:
prepareSearchSnippetin interfaceSqlContainsPolicy- Parameters:
value- the text value (snippet) of a CONTAINSMatchExpression- Returns:
- cleaned value
-
createInstruction
Description copied from interface:SqlContainsPolicyReturns the SQL-instruction to compare a column against a pattern.Here are some examples:
COL123 LIKE '%' || ${PATTERN_1011} || '%'should work well with Oracle, SQLite, PostgreSQLCOL123 LIKE CONCAT('%', ${PATTERN_1011}, '%')should do with MySQLCOL123 LIKE '%' + ${PATTERN_1011} + '%'should work with SQL ServerCHARINDEX(${PATTERN_1011}, COL123, 0) > 0may also work with SQL Server
- 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:
createInstructionin interfaceSqlContainsPolicy- Parameters:
columnName- this is the SQL column or alias to perform the CONTAINS/LIKE operation on, e.g.COL123patternParameter- placeholder (later argument to prepared statement) that will hold the pattern (e.g.,${PATTERN_1011}
-