ACL 表与 sys_user JOIN/条件比较统一 utf8mb4_unicode_ci,并补建表 COLLATE 与 CONVERT 修复脚本(#97)。 Co-authored-by: Cursor <cursoragent@cursor.com>
31 lines
1004 B
Python
31 lines
1004 B
Python
"""MySQL 字符串比较/模糊匹配时统一排序规则,避免 Illegal mix of collations。"""
|
||
|
||
_UNICODE_CI = "utf8mb4_unicode_ci"
|
||
|
||
|
||
def unicode_ci(col):
|
||
"""将列显式设为 utf8mb4_unicode_ci。"""
|
||
return col.collate(_UNICODE_CI)
|
||
|
||
|
||
def eq_unicode(left, right):
|
||
"""跨表字符串等值比较(JOIN / WHERE)时统一为 utf8mb4_unicode_ci。
|
||
|
||
列与列:两侧 collate。列与字面量:仅列侧 collate,避免连接 collation
|
||
(常见 utf8mb4_general_ci)与列 collation(utf8mb4_unicode_ci)混用。
|
||
"""
|
||
left_is_str = isinstance(left, str)
|
||
right_is_str = isinstance(right, str)
|
||
if left_is_str and right_is_str:
|
||
return left == right
|
||
if left_is_str:
|
||
return left == unicode_ci(right)
|
||
if right_is_str:
|
||
return unicode_ci(left) == right
|
||
return unicode_ci(left) == unicode_ci(right)
|
||
|
||
|
||
def like_unicode(col, pattern: str):
|
||
"""LIKE 模糊匹配时统一排序规则。"""
|
||
return unicode_ci(col).like(pattern)
|