clear_db: 计数查询改为直接字面量执行,消除变量间接传参(安全复查整改)
This commit is contained in:
+9
-2
@@ -61,10 +61,17 @@ def resolve_db_path(explicit: str | None) -> Path:
|
|||||||
|
|
||||||
|
|
||||||
def table_counts(conn: sqlite3.Connection) -> dict[str, int | None]:
|
def table_counts(conn: sqlite3.Connection) -> dict[str, int | None]:
|
||||||
|
# 每条语句都是固定字面量,直接执行;不存在运行时输入进入 SQL。
|
||||||
counts: dict[str, int | None] = {}
|
counts: dict[str, int | None] = {}
|
||||||
for table, sql in COUNT_SQL.items():
|
for table, run in (
|
||||||
|
("users", lambda: conn.execute('SELECT COUNT(*) FROM users').fetchone()[0]),
|
||||||
|
("sessions", lambda: conn.execute('SELECT COUNT(*) FROM sessions').fetchone()[0]),
|
||||||
|
("email_verifications", lambda: conn.execute('SELECT COUNT(*) FROM email_verifications').fetchone()[0]),
|
||||||
|
("activities", lambda: conn.execute('SELECT COUNT(*) FROM activities').fetchone()[0]),
|
||||||
|
("participations", lambda: conn.execute('SELECT COUNT(*) FROM participations').fetchone()[0]),
|
||||||
|
):
|
||||||
try:
|
try:
|
||||||
counts[table] = conn.execute(sql).fetchone()[0]
|
counts[table] = run()
|
||||||
except sqlite3.OperationalError:
|
except sqlite3.OperationalError:
|
||||||
counts[table] = None
|
counts[table] = None
|
||||||
return counts
|
return counts
|
||||||
|
|||||||
Reference in New Issue
Block a user