"""Offline SQLite verifier for MySQL Chapter 35: Operators & Precedence."""
import json
import re
import sqlite3
import sys
def make_case(name: str, summary: str, expected, actual) -> dict:
return {
"name": name,
"summary": summary,
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main() -> None:
conn = sqlite3.connect(":memory:")
conn.create_function(
"REGEXP",
2,
lambda pat, val: 0 if (pat is None or val is None) else (1 if re.search(pat, str(val)) else 0),
)
cur = conn.cursor()
# 1. Arithmetic operators (+, -, *, /, DIV, MOD) and divide-by-zero -> NULL
a_add, a_sub, a_mul, a_div_f, a_div_i, a_mod, a_zero_div, a_zero_mod = cur.execute(
"""
SELECT 1 + 2,
1 - 2,
2 * 3,
ROUND(2.0 / 3.0, 4),
10 / 4,
10 % 4,
10 / 0,
10 % 0
"""
).fetchone()
case1 = make_case(
"arithmetic_operators_and_div_zero",
"Verify +, -, *, /, integer DIV, MOD, and NULL result on division/modulo by zero",
[3, -1, 6, 0.6667, 2, 2, None, None],
[a_add, a_sub, a_mul, a_div_f, a_div_i, a_mod, a_zero_div, a_zero_mod],
)
# 2. Comparison operators (=, <>, <, <=, >, >=, BETWEEN, IN, NOT IN, IS, IS NULL, LIKE, REGEXP)
cmp_row = list(
cur.execute(
"""
SELECT (2 = 3),
(NULL = NULL),
(2 <> 3),
(2 IS 3),
(NULL IS NULL),
(NULL IS 1),
(2 < 3),
(2 <= 3),
(2 > 3),
(2 >= 3),
(5 BETWEEN 1 AND 10),
(5 NOT BETWEEN 1 AND 10),
(5 IN (1, 2, 3, 4, 5)),
(5 NOT IN (1, 2, 3, 4, 5)),
(NULL IS NULL),
('a' IS NULL),
(NULL IS NOT NULL),
('a' IS NOT NULL),
('12345' LIKE '12%'),
('12345' LIKE '12_'),
('beijing' REGEXP 'jing'),
('beijing' REGEXP 'xi')
"""
).fetchone()
)
case2 = make_case(
"comparison_operators_and_null_safe",
"Verify all SQL comparison, range, membership, null-safe IS (<=>), LIKE, and REGEXP operators",
[0, None, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0],
cmp_row,
)
# 3. Logical operators (AND, OR, NOT, XOR) and precedence
log_row = list(
cur.execute(
"""
SELECT (2 AND 0),
(2 AND 1),
(2 OR 0),
(0 OR 0),
(NOT 1),
(NOT 0),
(NULL OR 1),
(1 + 2 * 3),
((1 + 2) * 3)
"""
).fetchone()
)
xor_table = [int(bool(a) != bool(b)) for a, b in ((1, 1), (0, 0), (1, 0), (0, 1))]
case3 = make_case(
"logical_operators_and_precedence",
"Verify AND, OR, NOT, NULL OR 1, XOR truth table, and arithmetic/parentheses precedence",
{
"sql": [0, 1, 1, 0, 0, 1, 1, 7, 9],
"xor_table": [0, 0, 1, 1],
},
{
"sql": log_row,
"xor_table": xor_table,
},
)
# 4. Bitwise operators (&, |, ^, ~, <<, >>)
b_and, b_or, b_shr, b_shl = cur.execute(
"SELECT (3 & 5), (3 | 5), (3 >> 1), (3 << 1)"
).fetchone()
b_xor = 3 ^ 5
b_not_u64 = (~18446744073709551612) & ((1 << 64) - 1)
case4 = make_case(
"bitwise_operators_and_uint64_not",
"Verify &, |, ^, <<, >> and 64-bit unsigned bitwise NOT (~18446744073709551612 == 3)",
{
"and": 1,
"or": 7,
"xor": 6,
"not_u64": 3,
"shr": 1,
"shl": 6,
},
{
"and": b_and,
"or": b_or,
"xor": b_xor,
"not_u64": b_not_u64,
"shr": b_shr,
"shl": b_shl,
},
)
conn.close()
payload = {
"version": f"CPython {sys.version.split()[0]} / SQLite {sqlite3.sqlite_version} (offline MySQL verifier)",
"cases": [case1, case2, case3, case4],
}
print(json.dumps(payload, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、内置 sqlite3(SQLite 3.53.1)本地离线关系语义验证环境(全程内存库或临时目录运行,未连接外部 MySQL 服务端)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、内置 sqlite3(SQLite 3.53.1)本地离线关系语义验证环境(全程内存库或临时目录运行,未连接外部 MySQL 服务端)。
入口文件:projects/mysql-operator-lab/explore-operator.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-operator.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、内置 sqlite3(SQLite 3.53.1)本地离线关系语义验证环境(全程内存库或临时目录运行,未连接外部 MySQL 服务端)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联41章) · 含当前章节或实例 · mbskycom202612a1590.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1614.zip