the code specifically I am referring to is:
AND (
(','||to_char('~[gpv:lt]')||',' LIKE '%,' || to_char(log.logtypeid) || '.' ||
CASE WHEN log.subtype is null
THEN ' '
ELSE log.subtype
END || ',%')
OR (','||to_char('~[gpv:lt]')||',' LIKE '%,' || to_char(log.logtypeid) || '.-1,%')
OR (to_char(log.logtypeid) LIKE
CASE
WHEN to_char('~[gpv:lt]') = '-1'
THEN '%'
ELSE ','||to_char('~[gpv:lt]')||','
END)
)
Any clarification would be great. Thank you!
Let's start with this expression:
It is an example of this idiom:
in which
ais yourltparameter, andba "type dot subtype" string. This comparison can be used whenever you have anastring that is a comma-separated list of values, and abstring that is a single value, and you want to know whether the listacontains the valueb.To see why it's written that way, first look at this attempt to do it more simply:
We take
b, put a wildcard at the front and back, and matcha. This will be true ifais for example1,2,3andbis2. It will also unfortunately be true ifais12,34,56andbis2.LIKEdoesn't do comma-separated list parsing, just string matching.So next you might try this:
Now if
bis2, the pattern is%,2,%- which will match any string containing,2,so it is true fora=1,2,3and false fora=12,34,56. Unfortunately it's also false fora=2,3,4because the 2 doesn't have a comma before it and false fora=0,1,2because the 2 doesn't have a comma after it.For the next improvement there are 2 ways to go. You can use separate pattern cases to match
bat the beginning, middle, and end ofa(if you go this way, using a regular expression will help make it readable!)The other way is to modify
ato match the existing pattern. We didn't match0,1,2or2,3,4because the first element of the list and the last element of the list aren't surrounded by commas. But if we add a comma to the beginning ofabefore matching, then the first element of the list will be surrounded by commas! And add another comma to the end ofato make sure the last element is also surrounded by commas.Now when
ais0,1,2andbis2, theLIKEexpression becomes:which is a match! The first wildcard absorbs the
,0,1and the,2,is found. The last wildcard matches a zero-length substring at the end, which is allowed.