MCPcopy
hub / github.com/HKUDS/AI-Trader / get_db_connection

Function get_db_connection

service/server/database.py:305–325  ·  view source on GitHub ↗

Get database connection. Supports both SQLite and PostgreSQL.

()

Source from the content-addressed store, hash-verified

303
304
305def get_db_connection():
306 """Get database connection. Supports both SQLite and PostgreSQL."""
307 if using_postgres():
308 if psycopg is None:
309 raise RuntimeError(
310 "PostgreSQL support requires psycopg. Install service requirements first."
311 )
312 conn = psycopg.connect(DATABASE_URL, row_factory=dict_row)
313 return DatabaseConnection(conn, "postgres")
314
315 db_path = _SQLITE_DB_PATH
316 os.makedirs(os.path.dirname(db_path), exist_ok=True)
317
318 conn = sqlite3.connect(db_path, timeout=30.0)
319 conn.row_factory = sqlite3.Row
320
321 # Enable WAL mode for better concurrent access
322 conn.execute("PRAGMA journal_mode=WAL")
323 conn.execute("PRAGMA busy_timeout=30000")
324
325 return DatabaseConnection(conn, "sqlite")
326
327
328def get_database_status() -> dict[str, Any]:

Calls 3

using_postgresFunction · 0.85
DatabaseConnectionClass · 0.85
executeMethod · 0.80

Tested by

no test coverage detected