# -*- coding: utf-8 -*- """核查:lmes / norm_res 在测试库里的真实行数(只读,不写任何东西)。 2026-10-11:路径改走 gen_testdata.find_data_dir()(旧 glob 指向改名前的目录必 IndexError)。""" import os import sqlite3 import sys sys.stdout.reconfigure(errors="replace") HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, HERE) import gen_testdata as G dd = G.find_data_dir() if not dd: print("!! 找不到数据目录(history.db)") sys.exit(1) h, s = G.db_paths(dd)[1], G.db_paths(dd)[3] print("数据目录:", dd) if not os.path.exists(h): print("测试库 history-test.db 不存在——先在控制台生成一批。") sys.exit(1) print("history-test:", h) hc = sqlite3.connect(h, timeout=10) q = "FROM batches WHERE is_mock=1 AND Name LIKE '\u6d4b\u8bd5%'" print("\n== batches(is_mock=1)==") for k, in hc.execute("SELECT kind, COUNT(*) " + q + " GROUP BY kind"): print(" %-8s %d" % (k, hc.execute("SELECT COUNT(*) " + q + " AND kind=?", (k,)).fetchone()[0])) joins = { "lmes(工料机主表)": "FROM lmes l JOIN batches bt ON bt.id=l.batch_id WHERE bt.is_mock=1 AND bt.Name LIKE '\u6d4b\u8bd5%'", "norm_res(定额人材机)": "FROM norm_res r JOIN norms n ON n.id=r.norm_id JOIN bills b ON b.id=n.bill_id JOIN batches bt ON bt.id=b.batch_id WHERE bt.is_mock=1 AND bt.Name LIKE '\u6d4b\u8bd5%'", "lme_attach(工料机绑定)": "FROM lme_attach a JOIN batches bt ON bt.id=a.batch_id WHERE bt.is_mock=1 AND bt.Name LIKE '\u6d4b\u8bd5%'", "inquiry_files(询价附件)": "FROM inquiry_files f JOIN batches bt ON bt.id=f.batch_id WHERE bt.is_mock=1 AND bt.Name LIKE '\u6d4b\u8bd5%'", "prices(价格明细)": "FROM prices p JOIN batches bt ON bt.id=p.batch_id WHERE bt.is_mock=1 AND bt.Name LIKE '\u6d4b\u8bd5%'", "bills(清单)": "FROM bills b JOIN batches bt ON bt.id=b.batch_id WHERE bt.is_mock=1 AND bt.Name LIKE '\u6d4b\u8bd5%'", "norms(定额)": "FROM norms n JOIN bills b ON b.id=n.bill_id JOIN batches bt ON bt.id=b.batch_id WHERE bt.is_mock=1 AND bt.Name LIKE '\u6d4b\u8bd5%'", } print("\n== history-test.db 真实行数 ==") for name, sql in joins.items(): try: n = hc.execute("SELECT COUNT(*) " + sql).fetchone()[0] print(" %-22s %d" % (name, n)) except Exception as e: print(" %-22s ERR: %s" % (name, e)) # 抽一个测试项目看 lmes / norm_res 是否真有内容 bid = hc.execute("SELECT id, Name FROM batches WHERE is_mock=1 AND kind='project' ORDER BY id DESC LIMIT 1").fetchone() if bid: print("\n== 抽样项目 id=%d %s ==" % bid) print(" lmes 行数:", hc.execute("SELECT COUNT(*) FROM lmes WHERE batch_id=?", (bid[0],)).fetchone()[0]) print(" lme_attach 行数:", hc.execute("SELECT COUNT(*) FROM lme_attach WHERE batch_id=?", (bid[0],)).fetchone()[0]) row = hc.execute( "SELECT COUNT(*) FROM norm_res r JOIN norms n ON n.id=r.norm_id " "JOIN bills b ON b.id=n.bill_id WHERE b.batch_id=?", (bid[0],)).fetchone() print(" norm_res 行数:", row[0]) hc.close() if not os.path.exists(s): print("\nsession-test.db 不存在(工作台侧未生成),跳过。") sys.exit(0) print("\nsession-test:", s) sc = sqlite3.connect(s, timeout=10) print("\n== session-test.db 真实行数 ==") for name, sql in { "sessions": "FROM sessions WHERE name LIKE '\u6d4b\u8bd5%'", "s_lmes": "FROM s_lmes WHERE session_id IN (SELECT id FROM sessions WHERE name LIKE '\u6d4b\u8bd5%')", "s_norm_res": "FROM s_norm_res WHERE session_id IN (SELECT id FROM sessions WHERE name LIKE '\u6d4b\u8bd5%')", }.items(): try: print(" %-10s %d" % (name, sc.execute("SELECT COUNT(*) " + sql).fetchone()[0])) except Exception as e: print(" %-10s ERR: %s" % (name, e)) sc.close() print("\n核查完成(只读)")