You can use sqlLike in OCL and OCL-PS to select objects whose string value matches a SQL LIKE-style pattern.
Match text in a string
Use sqlLike on a string expression and pass the pattern to match. The operator returns a Boolean value, so it is commonly used as the condition in select.
stringValue.sqlLike(pattern)
The percent sign (%) in a pattern means that any text can occur at that position.
| Pattern | Matches | Example use |
|---|---|---|
'Anna'
|
The exact text Anna
|
Match a complete value. |
'Anna%'
|
Text that starts with Anna
|
Find names beginning with a search value. |
'%Anna'
|
Text that ends with Anna
|
Find values ending with a search value. |
'%Anna%'
|
Text that contains Anna
|
Find a search value anywhere in a string. |
Search for text entered by a user
The following expression selects all Person objects whose Identity contains the value in vSeekParam. It then orders the result by identity.
Person.allinstances
->select(a|a.Identity.sqlLike('%'+vSeekParam+'%'))
->orderby(identity)
For example, when vSeekParam is 'ann', the constructed pattern is '%ann%'. This matches an Identity value where ann occurs somewhere in the value, subject to the case behavior of the database.
Case sensitivity
Do not rely on sqlLike having the same case behavior in every database. Whether it ignores case in a persistence-server search depends on the database server and its settings. For example, the Turnkey built-in SQL Server Compact database does not ignore case.
When users should be able to find text without matching its capitalization, use sqlLikeCaseInsensitive instead:
Person.allinstances
->select(a|a.Name.sqlLikeCaseInsensitive('%'+vSeekParam+'%'))
Use sqlLike in a selection
- Start with the collection to search, such as
Person.allinstances. - Add
->select(variable|condition). - Call
sqlLikeon the string attribute in the condition. - Build the pattern with
%before, after, or on both sides of the search value according to the match you need. - Use sqlLikeCaseInsensitive if the search must ignore case across database configurations.
