"""Text matched literally inside a LIKE / ILIKE pattern. stmt.where(Line.message.ilike(f"%{escape_like(text)}%", escape="\\")) """ from __future__ import annotations def escape_like(text: str) -> str: """Make `text` match literally inside a LIKE/ILIKE pattern (escape char \\).""" return text.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")