feat: add MySQL database support alongside SQL Server
This commit implements multi-database support, allowing the system to switch
between SQL Server and MySQL databases seamlessly.
## New Features
- Database type selection (SQL Server or MySQL) via configuration
- Automatic table name conversion between formats ([dbo].[table] → dbo_table)
- Automatic parameter placeholder handling (? for SQL Server, %s for MySQL)
- GUI settings tab now includes database type dropdown and MySQL configuration
## Database Abstraction Layer
- db/base_connection.py: Abstract base class for database connections
- db/sqlserver_connection.py: SQL Server implementation
- db/mysql_connection.py: MySQL implementation using mysql-connector-python
- db/connection_factory.py: Factory pattern for creating connections
- db/table_name_converter.py: Table name format conversion utility
## DAO Base Class
- db/base_dao.py: Base DAO with helper methods for SQL conversion and placeholders
## Updated Components
- config/schema.py: Extended with DatabaseType enum and MySQL/SQLServer config classes
- config/defaults.py: Added MySQL default configuration
- config/loader.py: Updated to handle new database structure
- db/connection.py: Refactored to use factory pattern and load user config
- All DAO files: Updated to inherit from BaseDAO with automatic conversion
## Dependencies
- Added mysql-connector-python>=8.0.0 to requirements.txt
## Configuration
To use MySQL, set db_type to "mysql" in config/user_settings.json:
{
"database": {
"db_type": "mysql",
"mysql": {
"host": "192.168.31.83",
"port": 3306,
"database": "BLD_DB",
"username": "remote_user",
"password": "3.1415926Beeke"
}
}
}
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
219
db/connection.py
219
db/connection.py
@@ -1,10 +1,9 @@
|
||||
"""
|
||||
SQL Server 数据库连接组件
|
||||
数据库连接组件
|
||||
|
||||
提供数据库连接和查询接口
|
||||
提供数据库连接和查询接口,支持 SQL Server 和 MySQL
|
||||
"""
|
||||
|
||||
import pyodbc
|
||||
from typing import List, Dict, Any, Optional
|
||||
import sys
|
||||
import os
|
||||
@@ -14,186 +13,78 @@ project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
if project_root not in sys.path:
|
||||
sys.path.insert(0, project_root)
|
||||
|
||||
from config.defaults import DEFAULT_APP_CONFIG
|
||||
|
||||
# 从默认配置获取数据库配置
|
||||
SQL_SERVER_CONFIG = {
|
||||
"driver": DEFAULT_APP_CONFIG.database.driver,
|
||||
"server": DEFAULT_APP_CONFIG.database.server,
|
||||
"database": DEFAULT_APP_CONFIG.database.database,
|
||||
"username": DEFAULT_APP_CONFIG.database.username,
|
||||
"password": DEFAULT_APP_CONFIG.database.password,
|
||||
"TrustServerCertificate": DEFAULT_APP_CONFIG.database.trust_server_certificate,
|
||||
}
|
||||
from config.schema import DatabaseType
|
||||
from db.connection_factory import ConnectionFactory
|
||||
from db.base_connection import BaseDatabaseConnection
|
||||
|
||||
|
||||
class DatabaseConnection:
|
||||
"""SQL Server 数据库连接类"""
|
||||
def get_connection(config=None) -> BaseDatabaseConnection:
|
||||
"""
|
||||
获取数据库连接实例
|
||||
|
||||
def __init__(self, config: Optional[Dict[str, Any]] = None):
|
||||
"""
|
||||
初始化数据库连接
|
||||
Args:
|
||||
config: 可选的数据库配置对象,默认从用户配置文件加载
|
||||
|
||||
Args:
|
||||
config: 数据库配置字典,默认使用 SQL_SERVER_CONFIG
|
||||
"""
|
||||
self.config = config or SQL_SERVER_CONFIG
|
||||
self.connection = None
|
||||
Returns:
|
||||
BaseDatabaseConnection: 数据库连接对象
|
||||
"""
|
||||
if config is not None:
|
||||
# 使用提供的配置
|
||||
database_config = config
|
||||
else:
|
||||
# 从用户配置文件加载
|
||||
from config.loader import ConfigLoader
|
||||
app_config = ConfigLoader.load()
|
||||
database_config = app_config.database
|
||||
|
||||
def connect(self) -> pyodbc.Connection:
|
||||
"""
|
||||
建立数据库连接
|
||||
|
||||
Returns:
|
||||
pyodbc.Connection: 数据库连接对象
|
||||
"""
|
||||
if self.connection is not None:
|
||||
return self.connection
|
||||
|
||||
# 构建连接字符串
|
||||
conn_str = (
|
||||
f"DRIVER={{{self.config['driver']}}};"
|
||||
f"SERVER={self.config['server']};"
|
||||
f"DATABASE={self.config['database']};"
|
||||
f"UID={self.config['username']};"
|
||||
f"PWD={self.config['password']};"
|
||||
f"TrustServerCertificate={self.config['TrustServerCertificate']};"
|
||||
)
|
||||
|
||||
try:
|
||||
self.connection = pyodbc.connect(conn_str)
|
||||
print(
|
||||
f"成功连接到数据库: {self.config['server']}/{self.config['database']}"
|
||||
)
|
||||
return self.connection
|
||||
except pyodbc.Error as e:
|
||||
print(f"数据库连接失败: {e}")
|
||||
raise
|
||||
|
||||
def disconnect(self):
|
||||
"""关闭数据库连接"""
|
||||
if self.connection:
|
||||
self.connection.close()
|
||||
self.connection = None
|
||||
print("数据库连接已关闭")
|
||||
|
||||
def execute_query(
|
||||
self, sql: str, params: Optional[tuple] = None
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
执行查询语句并返回结果
|
||||
|
||||
Args:
|
||||
sql: SQL 查询语句
|
||||
params: 查询参数(可选)
|
||||
|
||||
Returns:
|
||||
List[Dict[str, Any]]: 查询结果列表,每个元素为一行数据的字典
|
||||
"""
|
||||
if not self.connection:
|
||||
self.connect()
|
||||
|
||||
cursor = self.connection.cursor()
|
||||
|
||||
try:
|
||||
if params:
|
||||
cursor.execute(sql, params)
|
||||
else:
|
||||
cursor.execute(sql)
|
||||
|
||||
# 获取列名
|
||||
columns = [column[0] for column in cursor.description]
|
||||
|
||||
# 将结果转换为字典列表
|
||||
results = []
|
||||
for row in cursor.fetchall():
|
||||
results.append(dict(zip(columns, row)))
|
||||
|
||||
return results
|
||||
|
||||
except pyodbc.Error as e:
|
||||
print(f"查询执行失败: {e}")
|
||||
raise
|
||||
finally:
|
||||
cursor.close()
|
||||
|
||||
def execute_update(self, sql: str, params: Optional[tuple] = None) -> int:
|
||||
"""
|
||||
执行更新/插入/删除语句
|
||||
|
||||
Args:
|
||||
sql: SQL 语句
|
||||
params: 参数(可选)
|
||||
|
||||
Returns:
|
||||
int: 受影响的行数
|
||||
"""
|
||||
if not self.connection:
|
||||
self.connect()
|
||||
|
||||
cursor = self.connection.cursor()
|
||||
|
||||
try:
|
||||
if params:
|
||||
cursor.execute(sql, params)
|
||||
else:
|
||||
cursor.execute(sql)
|
||||
|
||||
self.connection.commit()
|
||||
return cursor.rowcount
|
||||
|
||||
except pyodbc.Error as e:
|
||||
self.connection.rollback()
|
||||
print(f"执行失败,已回滚: {e}")
|
||||
raise
|
||||
finally:
|
||||
cursor.close()
|
||||
|
||||
def __enter__(self):
|
||||
"""支持 with 语句的上下文管理器入口"""
|
||||
self.connect()
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||
"""支持 with 语句的上下文管理器出口"""
|
||||
self.disconnect()
|
||||
return ConnectionFactory.create_from_config(database_config)
|
||||
|
||||
|
||||
# 便捷函数
|
||||
def query_production_orders(总排号_list: List[str]) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
根据总排号列表查询生产订单号
|
||||
|
||||
支持两种数据库格式:
|
||||
- SQL Server: [productionContractData].[26年压力表合同数据]
|
||||
- MySQL: productionContractData_26年压力表合同数据
|
||||
|
||||
Args:
|
||||
总排号_list: 总排号列表
|
||||
|
||||
Returns:
|
||||
List[Dict[str, Any]]: 查询结果
|
||||
"""
|
||||
db = DatabaseConnection()
|
||||
from db.table_name_converter import TableNameConverter
|
||||
from config.loader import ConfigLoader
|
||||
|
||||
# 构建占位符字符串
|
||||
placeholders = ",".join(["?" for _ in 总排号_list])
|
||||
# 获取当前数据库类型
|
||||
app_config = ConfigLoader.load()
|
||||
db_type = app_config.database.db_type
|
||||
|
||||
sql = f"""
|
||||
SELECT [总排号], [生产订单号], [序号], [订单号], [客户名称], [产品型号]
|
||||
FROM [productionContractData].[26年压力表合同数据]
|
||||
WHERE [总排号] IN ({placeholders})
|
||||
ORDER BY [序号]
|
||||
"""
|
||||
with get_connection() as db:
|
||||
# 获取正确的占位符
|
||||
placeholder = db.get_placeholder()
|
||||
|
||||
# 构建占位符字符串
|
||||
placeholders = ",".join([placeholder for _ in 总排号_list])
|
||||
|
||||
# 根据数据库类型选择表名格式
|
||||
if db_type == DatabaseType.MYSQL:
|
||||
table_name = "productionContractData_26年压力表合同数据"
|
||||
sql = f"""
|
||||
SELECT 总排号, 生产订单号, 序号, 订单号, 客户名称, 产品型号
|
||||
FROM {table_name}
|
||||
WHERE 总排号 IN ({placeholders})
|
||||
ORDER BY 序号
|
||||
"""
|
||||
else:
|
||||
table_name = "[productionContractData].[26年压力表合同数据]"
|
||||
sql = f"""
|
||||
SELECT [总排号], [生产订单号], [序号], [订单号], [客户名称], [产品型号]
|
||||
FROM {table_name}
|
||||
WHERE [总排号] IN ({placeholders})
|
||||
ORDER BY [序号]
|
||||
"""
|
||||
|
||||
try:
|
||||
results = db.execute_query(sql, tuple(总排号_list))
|
||||
return results
|
||||
finally:
|
||||
db.disconnect()
|
||||
|
||||
|
||||
def get_connection() -> DatabaseConnection:
|
||||
"""
|
||||
获取数据库连接实例
|
||||
|
||||
Returns:
|
||||
DatabaseConnection: 数据库连接对象
|
||||
"""
|
||||
return DatabaseConnection()
|
||||
|
||||
Reference in New Issue
Block a user