notebook
This commit is contained in:
71
20 Medicus all tables.py
Normal file
71
20 Medicus all tables.py
Normal file
@@ -0,0 +1,71 @@
|
||||
import fdb
|
||||
|
||||
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()
|
||||
|
||||
# Query all user tables and their columns
|
||||
cur.execute("""
|
||||
SELECT
|
||||
rf.rdb$relation_name AS table_name,
|
||||
rf.rdb$field_name AS column_name,
|
||||
f.rdb$field_type AS field_type,
|
||||
f.rdb$field_sub_type AS field_subtype,
|
||||
f.rdb$field_length AS field_length,
|
||||
f.rdb$field_precision AS field_precision,
|
||||
f.rdb$field_scale AS field_scale,
|
||||
f.rdb$null_flag AS not_null
|
||||
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
|
||||
ORDER BY rf.rdb$relation_name, rf.rdb$field_position
|
||||
""")
|
||||
|
||||
# helper to translate Firebird numeric codes to SQL types
|
||||
fb_types = {
|
||||
7: "SMALLINT",
|
||||
8: "INTEGER",
|
||||
10: "FLOAT",
|
||||
12: "DATE",
|
||||
13: "TIME",
|
||||
14: "CHAR",
|
||||
16: "BIGINT/NUMERIC",
|
||||
27: "DOUBLE",
|
||||
35: "TIMESTAMP",
|
||||
37: "VARCHAR",
|
||||
261:"BLOB"
|
||||
}
|
||||
|
||||
schema = {}
|
||||
for row in cur.fetchall():
|
||||
table = row[0].strip()
|
||||
column = row[1].strip()
|
||||
ftype = fb_types.get(row[2], f"TYPE{row[2]}")
|
||||
subtype = row[3]
|
||||
length = row[4]
|
||||
precision = row[5]
|
||||
scale = row[6]
|
||||
not_null = bool(row[7])
|
||||
|
||||
# Build human-readable type
|
||||
if ftype in ("CHAR","VARCHAR"):
|
||||
dtype = f"{ftype}({length})"
|
||||
elif ftype == "BIGINT/NUMERIC" and precision:
|
||||
dtype = f"NUMERIC({precision},{-scale})"
|
||||
else:
|
||||
dtype = ftype
|
||||
|
||||
coldef = f"{column} {dtype}{' NOT NULL' if not_null else ''}"
|
||||
schema.setdefault(table, []).append(coldef)
|
||||
|
||||
# Pretty print
|
||||
for t, cols in schema.items():
|
||||
print(f"\nTable: {t}")
|
||||
for c in cols:
|
||||
print(" ", c)
|
||||
Reference in New Issue
Block a user