/
query_history.py
39 lines (26 loc) · 1023 Bytes
/
query_history.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#
# Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved.
#
from typing import List, NamedTuple
import snowflake.snowpark
class QueryRecord(NamedTuple):
"""Contains the query information returned from the Snowflake database after the query is run."""
query_id: str
sql_text: str
class QueryHistory:
"""A context manager that listens to and records SQL queries that are pushed down to the Snowflake database.
See also:
:meth:`snowflake.snowpark.Session.query_history`.
"""
def __init__(self, session: "snowflake.snowpark.session.Session") -> None:
self.session = session
self._queries: List[QueryRecord] = []
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.session._conn.remove_query_listener(self)
def _add_query(self, query_record: QueryRecord):
self._queries.append(query_record)
@property
def queries(self) -> List[QueryRecord]:
return self._queries