Files
playwrite/db/connection_factory.py
Misaka Server f02858d5dc feat: add MySQL database support with SQL translation
- Add MySQLConnection class with automatic SQL Server to MySQL translation
- Add connection factory to support both SQL Server and MySQL
- Update config schema to support MySQL configuration (host, port, db_type)
- Update default config to use MySQL (localhost:3306)
- Translate table names: [schema].[table] -> schema_table
- Translate placeholders: ? -> %s
- Translate MERGE statements to INSERT ... ON DUPLICATE KEY UPDATE

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-09 20:12:32 +08:00

41 lines
1008 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
数据库连接工厂
根据配置返回 MySQL 或 SQL Server 连接实例。
"""
from typing import Optional
from db.connection import DatabaseConnection
from db.mysql_connection import MySQLConnection
from config.defaults import DEFAULT_APP_CONFIG
def get_connection(db_type: Optional[str] = None):
"""
根据配置返回 MySQL 或 SQL Server 连接
Args:
db_type: 数据库类型,"mysql""sqlserver"
如果为 None则从 DEFAULT_APP_CONFIG 读取配置
Returns:
DatabaseConnection 或 MySQLConnection 实例
Example:
>>> # 使用默认配置
>>> conn = get_connection()
>>> # 强制使用 MySQL
>>> conn = get_connection("mysql")
>>> # 强制使用 SQL Server
>>> conn = get_connection("sqlserver")
"""
if db_type is None:
db_type = DEFAULT_APP_CONFIG.database.db_type
if db_type == "mysql":
return MySQLConnection()
return DatabaseConnection()