-
Notifications
You must be signed in to change notification settings - Fork 7
/
snowflake_connector.py
71 lines (53 loc) · 2.09 KB
/
snowflake_connector.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import asyncio
import snowflake.connector
from snowflake.connector.constants import QueryStatus
class SnowflakeConnector:
def __init__(self, account_name: str, username: str, password: str):
self.account_name = account_name
self.username = username
self.password = password
def __enter__(self):
self.con = snowflake.connector.connect(
user=self.username,
password=self.password,
account=self.account_name)
return self
def __exit__(self, *exc):
self.con.close()
def set_db_warehouse(self, warehouse: str):
results = self.query(f'USE WAREHOUSE {warehouse}')
return asyncio.run(results.fetch_results())
def set_user_role(self, role: str):
results = self.query(f'USE ROLE {role}')
return asyncio.run(results.fetch_results())
def query(self, query_str: str):
cursor = self.con.cursor()
cursor.execute_async(query_str)
return QueryResult(self.con, cursor.sfqid)
class QueryResult:
def __init__(self, con: SnowflakeConnector, query_id: str):
self.query_id = query_id
self.con = con
self.cursor = con.cursor()
def is_query_done_successfully(self):
return self.con.get_query_status(self.query_id) == QueryStatus.SUCCESS
def is_query_running(self):
return (self.con.get_query_status(self.query_id) == QueryStatus.RUNNING or
self.con.get_query_status(self.query_id) == QueryStatus.NO_DATA)
def _fetch_results(self):
"""
Raises ProgrammingError in case of SQL error,
by get_results_from_sfqid
"""
self.cursor.get_results_from_sfqid(self.query_id)
row = self.cursor.fetchone()
while row is not None:
yield row
row = self.cursor.fetchone()
async def fetch_results(self):
while self.is_query_running():
await asyncio.sleep(0.1)
return self._fetch_results()
def fetch_results_sync(self):
self.cursor.get_results_from_sfqid(self.query_id)
return self.cursor.fetchall()