# -*- coding: utf-8 -*- """ Hledani tabulky pro eDavky / Kniha podani v Medicus DB. """ import sys import io sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace') import fdb conn = fdb.connect( dsn=r'localhost:c:\medicus 3\data\medicus.fdb', user='SYSDBA', password='masterkey', charset='win1250' ) cur = conn.cursor() print("=" * 60) print("1. Tabulky s relevantními názvy") print("=" * 60) cur.execute(""" SELECT RDB$RELATION_NAME FROM RDB$RELATIONS WHERE RDB$SYSTEM_FLAG = 0 AND ( TRIM(RDB$RELATION_NAME) LIKE '%DAV%' OR TRIM(RDB$RELATION_NAME) LIKE '%PORTAL%' OR TRIM(RDB$RELATION_NAME) LIKE '%PODANI%' OR TRIM(RDB$RELATION_NAME) LIKE '%EDAVK%' OR TRIM(RDB$RELATION_NAME) LIKE '%KNIHA%' OR TRIM(RDB$RELATION_NAME) LIKE '%PODAC%' OR TRIM(RDB$RELATION_NAME) LIKE '%ELEK%' ) ORDER BY RDB$RELATION_NAME """) tables = [row[0].strip() for row in cur.fetchall()] for t in tables: print(f" {t}") print() print("=" * 60) print("2. Počty záznamů a max datum v relevantních tabulkách") print("=" * 60) for table in tables: try: # Zkus najít datumový sloupec cur.execute(f""" SELECT RDB$FIELD_NAME FROM RDB$RELATION_FIELDS WHERE RDB$RELATION_NAME = '{table}' ORDER BY RDB$FIELD_POSITION """) cols = [r[0].strip() for r in cur.fetchall()] cur.execute(f"SELECT COUNT(*) FROM {table}") count = cur.fetchone()[0] # Najdi datum sloupec date_col = None for c in cols: if any(x in c for x in ['DAT', 'ODE', 'VYT', 'CAS', 'TIME']): date_col = c break if date_col: try: cur.execute(f"SELECT MAX({date_col}) FROM {table}") max_date = cur.fetchone()[0] print(f" {table:<30} {count:>6} záznamů max {date_col}={max_date}") except Exception: print(f" {table:<30} {count:>6} záznamů cols: {cols[:5]}") else: print(f" {table:<30} {count:>6} záznamů cols: {cols[:5]}") except Exception as e: print(f" {table:<30} chyba: {e}") print() print("=" * 60) print("3. Aktuální stav PORTAL") print("=" * 60) try: cur.execute("SELECT COUNT(*), MAX(ODESLANO) FROM PORTAL") cnt, mx = cur.fetchone() print(f" PORTAL: {cnt} záznamů, max ODESLANO={mx}") cur.execute("SELECT FIRST 5 ID, IDFAK, ODESLANO, STAV, ID_PODANI, DAVKA_ROK FROM PORTAL ORDER BY ID DESC") for row in cur.fetchall(): print(f" {row}") except Exception as e: print(f" PORTAL chyba: {e}") print() print("=" * 60) print("4. Hledání tabulek s datem >= 2026-02-01 (čerstvá data)") print("=" * 60) # Projdi všechny tabulky a hledej ty které mají záznamy z 2026 suspicious = [] cur.execute(""" SELECT RDB$RELATION_NAME FROM RDB$RELATIONS WHERE RDB$SYSTEM_FLAG = 0 ORDER BY RDB$RELATION_NAME """) all_tables = [r[0].strip() for r in cur.fetchall()] for table in all_tables: try: # Najdi datum sloupce cur.execute(f""" SELECT RDB$FIELD_NAME FROM RDB$RELATION_FIELDS WHERE RDB$RELATION_NAME = '{table}' AND ( TRIM(RDB$FIELD_NAME) LIKE '%ODESLANO%' OR TRIM(RDB$FIELD_NAME) LIKE '%VYTVORENO%' OR TRIM(RDB$FIELD_NAME) LIKE '%DATUM_ODE%' ) """) date_cols = [r[0].strip() for r in cur.fetchall()] for dc in date_cols: cur.execute(f"SELECT COUNT(*) FROM {table} WHERE {dc} >= '2026-02-01'") cnt = cur.fetchone()[0] if cnt > 0: suspicious.append((table, dc, cnt)) except Exception: pass for table, dc, cnt in suspicious: print(f" {table:<35} {dc}: {cnt} záznamů od 2026-02-01") conn.close() print() print("Hotovo.")