gabriel / musehub public
utils.py python
18 lines 755 B
Raw
sha256:7d6dd8f4a89e2d1fef2d84f6e65feaff51385d382f466766b7f690a22ec18e32 fix: fall back to DB ancestry check when mpack-only fast-fo… Sonnet 4.6 patch 7 days ago
1 """Database query utilities."""
2
3 def escape_like(s: str) -> str:
4 """Escape SQL LIKE metacharacters in a user-supplied string.
5
6 Use this before embedding user input as a *literal substring* inside a
7 LIKE / ILIKE pattern. Always pair with ``escape="\\\\"`` on the ORM call::
8
9 column.ilike(f"%{escape_like(q)}%", escape="\\\\")
10 column.contains(escape_like(q), escape="\\\\")
11
12 Without escaping, a user can supply ``%`` (matches everything) or ``_``
13 (matches any single character), turning a substring search into a wildcard
14 query. Escaping makes those characters literal.
15
16 The backslash is escaped first to avoid double-escaping.
17 """
18 return s.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")
File History 1 commit
sha256:7d6dd8f4a89e2d1fef2d84f6e65feaff51385d382f466766b7f690a22ec18e32 fix: fall back to DB ancestry check when mpack-only fast-fo… Sonnet 4.6 patch 7 days ago