"""get_kar_sortby_idlist.py – přečte definici stored procedure KAR_SORTBY_IDLIST z Firebirdu.
Spustit na Windows.
"""
import fdb

conn = fdb.connect(
    dsn=r'localhost:c:\medicus 3\data\medicus.fdb',
    user='SYSDBA', password='masterkey', charset='win1250')

cur = conn.cursor()
cur.execute("""
    SELECT RDB$PROCEDURE_SOURCE FROM RDB$PROCEDURES
    WHERE RDB$PROCEDURE_NAME = 'KAR_SORTBY_IDLIST'
""")
row = cur.fetchone()
if row and row[0]:
    print(row[0])
else:
    # Možná je to funkce (FUNCTION), ne procedure
    cur.execute("""
        SELECT RDB$FUNCTION_SOURCE FROM RDB$FUNCTIONS
        WHERE RDB$FUNCTION_NAME = 'KAR_SORTBY_IDLIST'
    """)
    row = cur.fetchone()
    if row and row[0]:
        print(row[0])
    else:
        print("Nenalezeno ani jako PROCEDURE ani jako FUNCTION.")

conn.close()
