refactor: Update database client methods to use command and query for SQL execution

This commit is contained in:
Bobby (aider) 2025-02-11 18:15:54 -08:00
parent 41de719250
commit 0ac499ae8c

View File

@ -79,7 +79,7 @@ def create_trading_plan_table():
with create_client() as client: with create_client() as client:
try: try:
# Drop the table if it exists to ensure a clean creation # Drop the table if it exists to ensure a clean creation
client.execute("DROP TABLE IF EXISTS trading_plans") client.command("DROP TABLE IF EXISTS trading_plans")
# Create new table with a simple structure # Create new table with a simple structure
query = """ query = """
@ -125,7 +125,7 @@ def create_trading_plan_table():
) )
ENGINE = TinyLog ENGINE = TinyLog
""" """
client.execute(query) client.command(query)
print("Table 'trading_plans' created successfully.") print("Table 'trading_plans' created successfully.")
except Exception as e: except Exception as e:
@ -136,7 +136,7 @@ def save_trading_plan(plan: TradingPlan) -> int:
with create_client() as client: with create_client() as client:
if not plan.id: if not plan.id:
# Generate new ID for new plans # Generate new ID for new plans
result = client.execute("SELECT max(id) FROM trading_plans") result = client.query("SELECT max(id) FROM trading_plans")
plan.id = (result[0][0] or 0) + 1 plan.id = (result[0][0] or 0) + 1
plan.created_at = datetime.now() plan.created_at = datetime.now()
@ -198,13 +198,13 @@ def save_trading_plan(plan: TradingPlan) -> int:
'options_strategy_details': plan.options_strategy_details 'options_strategy_details': plan.options_strategy_details
} }
client.execute(query, params) client.command(query, params)
return plan.id return plan.id
def get_trading_plan(plan_id: int) -> Optional[TradingPlan]: def get_trading_plan(plan_id: int) -> Optional[TradingPlan]:
"""Get a trading plan by ID""" """Get a trading plan by ID"""
with create_client() as client: with create_client() as client:
result = client.execute(""" result = client.query("""
SELECT * FROM trading_plans WHERE id = %(id)s SELECT * FROM trading_plans WHERE id = %(id)s
""", {'id': plan_id}) """, {'id': plan_id})
@ -265,7 +265,7 @@ def get_all_trading_plans(status: Optional[PlanStatus] = None) -> List[TradingPl
query += " ORDER BY updated_at DESC" query += " ORDER BY updated_at DESC"
results = client.execute(query, params) results = client.query(query, params)
return [get_trading_plan(result[0]) for result in results] return [get_trading_plan(result[0]) for result in results]
except Exception as e: except Exception as e:
@ -360,5 +360,5 @@ def update_trading_plan(plan: TradingPlan) -> bool:
'options_strategy_details': plan.options_strategy_details 'options_strategy_details': plan.options_strategy_details
} }
client.execute(query, params) client.command(query, params)
return True return True