So, after the recent Twitch leak everyone is discussing their favorite bits of bad code. One bit that stood out was a single monster SQL statement to spot "illegal terms". That got me thinking about how to implement this "correctly".
So you have a table of strings that you want to match sub-strings against, how you write this in PL/pgSQL? I am assuming any SQL implementation should have procedural capabilities to create functions/procedures for such meta-programming, basically creating and executing SQL like the following:
admin@localhost:words> SELECT 'XabcY' LIKE '%abc%' OR 'XabcY' LIKE '%xyz%' as matches;
+-----------+
| matches |
|-----------|
| True |
+-----------+
So to be more specific, given a list of strings in table disallowed
:
| illegal_string |
|----------------|
| stupid |
| witless |
| moron |
| commie-lover |
How would you create a dynamic query in PL/pgSQL that when executed returned true if any of these matched a given string? It does not need to use ILIKE
in the Twitch like to check if the given word contained the substring, so using position
is fine too, but it should be performant/tuneable using gin
indices and whatnot.