commit c11ef161f4a5a5aaaa6b6a6fb2a8920b3c9c06c7 Author: bangae1 Date: Fri Aug 22 06:50:28 2025 +0900 first diff --git a/__pycache__/models.cpython-313.pyc b/__pycache__/models.cpython-313.pyc new file mode 100644 index 0000000..f52ad52 Binary files /dev/null and b/__pycache__/models.cpython-313.pyc differ diff --git a/api.iml b/api.iml new file mode 100644 index 0000000..63002b6 --- /dev/null +++ b/api.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/models.py b/models.py new file mode 100644 index 0000000..b47c7e8 --- /dev/null +++ b/models.py @@ -0,0 +1,59 @@ +from sqlalchemy import create_engine, Table, Column, Integer, String, Float, Double, DateTime +from sqlalchemy.orm import registry, sessionmaker + +# 메타데이터 객체 관리 +mapper_registry = registry() + +# DB 유저 테이블 +ticker_table = Table( + "ticker", + mapper_registry.metadata, + Column("contract", String(20), primary_key=True), + Column("clast", String(50)), + Column("change_percentage", Double), + Column("total_size", Integer), + Column("volume_24h", Integer), + Column("volume_24h_base", Integer), + Column("volume_24h_quote", Integer), + Column("volume_24h_settle", Integer), + Column("funding_rate", Double), + Column("funding_rate_indicative", Double), + Column("quanto_base_rate", String(100)), + Column("low_24h", Double), + Column("high_24h", Double), + Column("price_type", String(10)), + Column("change_from", String(20)), + Column("change_price", Double), + Column("ts", DateTime, primary_key=True), +) + +# Python 유저 객체 +class Ticker: + contract: str + clast:float + change_percentage:float + total_size:int + volume_24h:int + volume_24h_base:int + volume_24h_quote:int + volume_24h_settle:int + funding_rate:float + funding_rate_indicative:float + quanto_base_rate:str + low_24h:float + high_24h:float + price_type:str + change_from:str + change_price:float + ts:str + +# 테이블과 클래스의 매핑 설정 +mapper_registry.map_imperatively(Ticker, ticker_table) + +def get_session(): + # 데이터베이스 설정 + engine = create_engine('postgresql+psycopg2://bangae1:fpdlwms1@hmsn.ink:35432/coin') + mapper_registry.metadata.create_all(engine) # 테이블 생성 + Session = sessionmaker(bind=engine) + session = Session() + return session \ No newline at end of file diff --git a/test.py b/test.py new file mode 100644 index 0000000..d120dff --- /dev/null +++ b/test.py @@ -0,0 +1,23 @@ +from sqlalchemy.orm import Session +from sqlalchemy import and_, or_, desc, asc + +from models import get_session, Ticker + + +def get_ticker(session: Session, contract: str): + return session.query(Ticker).filter(Ticker.contract == contract).limit(500).all() + +def get_ticker_count(session: Session, contract: str): + return session.query(Ticker).filter(Ticker.contract == contract).count() + +# 사용 예시 + +session = get_session() +try: + cnt = get_ticker_count(session, "BTC_USDT") + print(cnt) + tickers = get_ticker(session, "BTC_USDT") + for ticker in tickers: + print(ticker.clast) +finally: + session.close() \ No newline at end of file