(
conn: psycopg.Connection,
sql: LiteralString | bytes | SQL | Composed,
timeout: int | None = None,
)
| 44 | |
| 45 | |
| 46 | def query( |
| 47 | conn: psycopg.Connection, |
| 48 | sql: LiteralString | bytes | SQL | Composed, |
| 49 | timeout: int | None = None, |
| 50 | ) -> list[Any]: |
| 51 | if VERBOSE: |
| 52 | print(f"\n> {to_sql_string(sql, conn)}") |
| 53 | |
| 54 | cancel_timer: threading.Timer | None = None |
| 55 | |
| 56 | try: |
| 57 | if timeout is not None: |
| 58 | cancel_timer = threading.Timer(timeout, conn.cancel) |
| 59 | cancel_timer.start() |
| 60 | |
| 61 | with conn.cursor() as cur: |
| 62 | cur.execute(sql) |
| 63 | return cur.fetchall() |
| 64 | except psycopg.errors.QueryCanceled: |
| 65 | if VERBOSE: |
| 66 | print("Too slow") |
| 67 | return [] |
| 68 | except KeyboardInterrupt: |
| 69 | conn.cancel() |
| 70 | if VERBOSE: |
| 71 | raise |
| 72 | print(f"\n> {to_sql_string(sql, conn)}") |
| 73 | with conn.cursor() as cur: |
| 74 | cur.execute(sql) |
| 75 | return cur.fetchall() |
| 76 | finally: |
| 77 | if cancel_timer is not None: |
| 78 | cancel_timer.cancel() |
| 79 | |
| 80 | |
| 81 | def attach_source_statistics( |
no test coverage detected