"""Adapter for the data warehouse.""" from snowflake import connector from accounting import config class SnowflakeAdapter(): """Creates a snowflake connector. Allows select statements to be executed against snowflake. The connection is lazy loaded. """ def __init__(self): """Set lazy load flag.""" self._init = False def __del__(self): """Teardown connection.""" try: self._cursor.close() except AttributeError: pass try: self._connection.close() except AttributeError: pass def _connect(self): """Configure a usable connection for querying.""" self._connection = self._get_connection() self._cursor = self._connection.cursor() def _get_connection(self): """Return a raw connection to snowflake.""" return connector.connect( user=config.WAREHOUSE_USER, password=config.WAREHOUSE_PASS, account=config.WAREHOUSE_ACCOUNT) def execute(self, sql): """Execute a select statement against snowflake. Args: sql (str): A valid snowflake sql select statement. Returns: Tuple[]: A list of Tuples with values ordered according to the sql. """ if not self._init: self._connect() self._init = True return self._cursor.execute(sql).fetchall()