73 lines
1.9 KiB
Python
73 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import fdb
|
|
|
|
CODE = "0255910"
|
|
|
|
con = fdb.connect(
|
|
host='192.168.1.4',
|
|
database=r'z:\MEDICUS 3\data\medicus.FDB',
|
|
user='sysdba',
|
|
password='masterkey',
|
|
charset='WIN1250'
|
|
)
|
|
cur = con.cursor()
|
|
|
|
# --- find every user-table column defined as CHAR(7) ---
|
|
cur.execute("""
|
|
SELECT
|
|
TRIM(rf.rdb$relation_name) AS table_name,
|
|
TRIM(rf.rdb$field_name) AS column_name
|
|
FROM rdb$relation_fields rf
|
|
JOIN rdb$fields f
|
|
ON rf.rdb$field_source = f.rdb$field_name
|
|
JOIN rdb$relations r
|
|
ON rf.rdb$relation_name = r.rdb$relation_name
|
|
WHERE
|
|
r.rdb$system_flag = 0 -- only user tables
|
|
AND r.rdb$view_blr IS NULL -- exclude views
|
|
AND f.rdb$field_type = 14 -- CHAR
|
|
AND f.rdb$field_length = 7 -- exactly CHAR(7)
|
|
ORDER BY rf.rdb$relation_name, rf.rdb$field_position
|
|
""")
|
|
|
|
columns = cur.fetchall()
|
|
matches = []
|
|
|
|
# --- check each CHAR(7) column for exact match ---
|
|
for table_name, column_name in columns:
|
|
sql_count = f'''
|
|
SELECT COUNT(*)
|
|
FROM "{table_name}"
|
|
WHERE "{column_name}" = ?
|
|
'''
|
|
try:
|
|
cur.execute(sql_count, (CODE,))
|
|
cnt = cur.fetchone()[0]
|
|
except Exception as e:
|
|
print(f"Skipped {table_name}.{column_name}: {e}")
|
|
continue
|
|
|
|
if cnt > 0:
|
|
sql_sample = f'''
|
|
SELECT FIRST 5 "{column_name}"
|
|
FROM "{table_name}"
|
|
WHERE "{column_name}" = ?
|
|
'''
|
|
cur.execute(sql_sample, (CODE,))
|
|
samples = [s.strip() if s else "" for (s,) in cur.fetchall()]
|
|
matches.append((table_name, column_name, int(cnt), samples))
|
|
|
|
# --- report ---
|
|
if not matches:
|
|
print(f'No exact matches for "{CODE}" in any CHAR(7) column.')
|
|
else:
|
|
print(f'Exact matches for "{CODE}" in CHAR(7) columns:')
|
|
for t, c, cnt, samples in matches:
|
|
print(f' - {t}.{c}: {cnt} row(s)')
|
|
for s in samples:
|
|
print(f' sample: {s}')
|
|
|
|
con.close()
|