db_collation.py 548 B

123456789101112131415161718
  1. """MySQL 字符串比较/模糊匹配时统一排序规则,避免 Illegal mix of collations。"""
  2. _UNICODE_CI = "utf8mb4_unicode_ci"
  3. def unicode_ci(col):
  4. """将列显式设为 utf8mb4_unicode_ci。"""
  5. return col.collate(_UNICODE_CI)
  6. def eq_unicode(left, right):
  7. """跨表字符串等值比较(JOIN / WHERE)时统一为 utf8mb4_unicode_ci。"""
  8. return unicode_ci(left) == unicode_ci(right)
  9. def like_unicode(col, pattern: str):
  10. """LIKE 模糊匹配时统一排序规则。"""
  11. return unicode_ci(col).like(pattern)