Files
feiqiu-ETL/query_db.py

154 lines
4.2 KiB
Python

import psycopg2
# Database connection
dsn = 'postgresql://local-Python:Neo-local-1991125@100.64.0.4:5432/LLZQ-test'
conn = psycopg2.connect(dsn)
cur = conn.cursor()
print('=' * 80)
print('1. ALL SCHEMAS')
print('=' * 80)
cur.execute("""
SELECT schema_name FROM information_schema.schemata
WHERE schema_name NOT IN ('pg_catalog', 'information_schema', 'pg_toast')
ORDER BY schema_name;
""")
for row in cur.fetchall():
print(row[0])
print()
print('=' * 80)
print('2. TABLES IN BUSINESS SCHEMAS')
print('=' * 80)
cur.execute("""
SELECT table_schema, table_name, table_type
FROM information_schema.tables
WHERE table_schema IN ('billiards', 'billiards_ods', 'billiards_dwd', 'billiards_dws', 'etl_admin')
ORDER BY table_schema, table_name;
""")
print(f"{'Schema':<20} {'Table Name':<50} {'Type':<15}")
print('-' * 85)
for row in cur.fetchall():
print(f"{row[0]:<20} {row[1]:<50} {row[2]:<15}")
print()
print('=' * 80)
print('3. COLUMN DETAILS FOR billiards_dwd SCHEMA')
print('=' * 80)
cur.execute("""
SELECT
c.table_schema,
c.table_name,
c.column_name,
c.data_type,
c.character_maximum_length,
c.numeric_precision,
c.numeric_scale,
c.is_nullable,
c.column_default,
c.ordinal_position
FROM information_schema.columns c
WHERE c.table_schema = 'billiards_dwd'
ORDER BY c.table_name, c.ordinal_position;
""")
current_table = None
for row in cur.fetchall():
if current_table != row[1]:
current_table = row[1]
print()
print(f"Table: {row[0]}.{row[1]}")
print(f"{'#':<4} {'Column':<35} {'Data Type':<25} {'Nullable':<10} {'Default':<30}")
print('-' * 110)
data_type = row[3]
if row[4]: # character_maximum_length
data_type += f"({row[4]})"
elif row[5] and row[6]: # numeric precision and scale
data_type += f"({row[5]},{row[6]})"
elif row[5]:
data_type += f"({row[5]})"
default_val = str(row[8])[:28] if row[8] else ''
print(f"{row[9]:<4} {row[2]:<35} {data_type:<25} {row[7]:<10} {default_val:<30}")
print()
print('=' * 80)
print('4. PRIMARY KEYS IN billiards_dwd')
print('=' * 80)
cur.execute("""
SELECT
tc.table_schema,
tc.table_name,
kcu.column_name
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage kcu
ON tc.constraint_name = kcu.constraint_name
AND tc.table_schema = kcu.table_schema
WHERE tc.constraint_type = 'PRIMARY KEY'
AND tc.table_schema = 'billiards_dwd'
ORDER BY tc.table_name, kcu.ordinal_position;
""")
print(f"{'Schema':<20} {'Table Name':<40} {'PK Column':<30}")
print('-' * 90)
for row in cur.fetchall():
print(f"{row[0]:<20} {row[1]:<40} {row[2]:<30}")
print()
print('=' * 80)
print('5. TABLE COMMENTS IN billiards_dwd')
print('=' * 80)
cur.execute("""
SELECT
n.nspname AS schema_name,
c.relname AS table_name,
d.description AS table_comment
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
LEFT JOIN pg_description d ON d.objoid = c.oid AND d.objsubid = 0
WHERE n.nspname = 'billiards_dwd' AND c.relkind = 'r'
ORDER BY c.relname;
""")
print(f"{'Schema':<20} {'Table Name':<40} {'Comment':<50}")
print('-' * 110)
for row in cur.fetchall():
comment = row[2] if row[2] else '(no comment)'
print(f"{row[0]:<20} {row[1]:<40} {comment:<50}")
print()
print('=' * 80)
print('6. COLUMN COMMENTS IN billiards_dwd')
print('=' * 80)
cur.execute("""
SELECT
n.nspname AS schema_name,
c.relname AS table_name,
a.attname AS column_name,
d.description AS column_comment
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
JOIN pg_attribute a ON a.attrelid = c.oid
LEFT JOIN pg_description d ON d.objoid = c.oid AND d.objsubid = a.attnum
WHERE n.nspname = 'billiards_dwd'
AND c.relkind = 'r'
AND a.attnum > 0
AND NOT a.attisdropped
ORDER BY c.relname, a.attnum;
""")
current_table = None
for row in cur.fetchall():
if current_table != row[1]:
current_table = row[1]
print()
print(f"Table: {row[0]}.{row[1]}")
print(f"{'Column':<35} {'Comment':<60}")
print('-' * 95)
comment = row[3] if row[3] else '(no comment)'
print(f"{row[2]:<35} {comment:<60}")
conn.close()
print()
print('=' * 80)
print('Query completed successfully!')
print('=' * 80)