Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Commit

Permalink
Update orderbook connection for sqlalchemy 2x
Browse files Browse the repository at this point in the history
  • Loading branch information
bram-vdberg committed Nov 12, 2024
1 parent 90e7c88 commit e546d86
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/fetch/orderbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,19 @@ def _pg_engine(db_env: OrderbookEnv) -> Engine:
db_string = f"postgresql+psycopg2://{db_url}"
return create_engine(db_string)

@staticmethod
def _get_connection(db_env: OrderbookEnv):
"""Returns a database connection"""
engine = OrderbookFetcher._pg_engine(db_env)
return engine.connect()

@classmethod
def _read_query_for_env(
cls, query: str, env: OrderbookEnv, data_types: Optional[dict[str, str]] = None
) -> DataFrame:
return pd.read_sql_query(query, con=cls._pg_engine(env), dtype=data_types)
"""Reads a query with a connection object"""
with cls._get_connection(env) as conn:
return pd.read_sql_query(query, con=conn, dtype=data_types)

@classmethod
def _query_both_dbs(
Expand Down Expand Up @@ -184,3 +192,4 @@ def get_price_feed(cls) -> DataFrame:
"""
prices_query = open_query("prices.sql")
return cls._read_query_for_env(prices_query, OrderbookEnv.ANALYTICS)

0 comments on commit e546d86

Please sign in to comment.