feat: migrate configuration to .env environment variables

This commit implements a complete migration from JSON-based configuration
to .env environment variables, providing better security and flexibility.

Key Changes:
- Add python-dotenv dependency for environment variable support
- Create config/env_loader.py with type conversion utilities
- Add from_env() class methods to all config dataclasses
- Update ConfigLoader to prioritize environment variables
- Add save_to_env() method for .env file management
- Implement database connection factory pattern
- Add base DAO and connection classes for better abstraction
- Support both SQL Server and MySQL with unified interface
- Create migration script (scripts/migrate_to_env.py)
- Update GUI to read/write .env files
- Add comprehensive migration documentation

New Files:
- config/env_loader.py - Environment variable loader
- db/base_connection.py - Base database connection interface
- db/base_dao.py - Base DAO with common utilities
- db/connection_factory.py - Factory for creating connections
- db/mysql_connection.py - MySQL-specific connection
- db/sqlserver_connection.py - SQL Server-specific connection
- db/table_name_converter.py - SQL dialect converter
- scripts/migrate_to_env.py - Configuration migration tool
- docs/ENV_MIGRATION.md - Complete migration guide
- .env.example - Environment variable template

Testing:
- Verified MySQL connection (8.0.44)
- Tested all DAO operations
- Confirmed 150 tables accessible
- Validated configuration loading

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Misaka
2026-02-09 22:39:14 +08:00
parent 04b99292ad
commit aaa46ef282
22 changed files with 2545 additions and 563 deletions

100
db/base_dao.py Normal file
View File

@@ -0,0 +1,100 @@
"""
DAO 基类
提供数据访问对象的通用方法和辅助函数
"""
from typing import Optional
from config.schema import DatabaseType
from db.base_connection import BaseDatabaseConnection
from db.connection import get_connection
from db.table_name_converter import TableNameConverter
class BaseDAO:
"""数据访问对象基类"""
def __init__(self):
"""初始化 DAO"""
self.db: Optional[BaseDatabaseConnection] = None
# 从配置文件加载数据库类型
from config.loader import ConfigLoader
app_config = ConfigLoader.load()
self._db_type = app_config.database.db_type
def __enter__(self):
"""进入上下文管理器,建立数据库连接"""
self.db = get_connection()
self.db.connect()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
"""退出上下文管理器,关闭数据库连接"""
if self.db:
self.db.disconnect()
def close(self):
"""关闭数据库连接"""
if self.db:
self.db.disconnect()
def _convert_sql(self, sql: str) -> str:
"""
根据当前数据库类型转换 SQL 语句中的表名
Args:
sql: 原始 SQL 语句SQL Server 格式)
Returns:
转换后的 SQL 语句
"""
if self._db_type == DatabaseType.MYSQL:
# SQL Server → MySQL
return TableNameConverter.convert_sql(sql, 'mysql')
return sql
def _get_placeholder(self) -> str:
"""
获取当前数据库类型的参数占位符
Returns:
SQL Server 返回 "?"MySQL 返回 "%s"
"""
if self._db_type == DatabaseType.MYSQL:
return "%s"
return "?"
def _build_placeholders(self, count: int) -> str:
"""
构建参数占位符字符串
Args:
count: 占位符数量
Returns:
占位符字符串,如 "?, ?, ?""%s, %s, %s"
"""
placeholder = self._get_placeholder()
return ", ".join([placeholder for _ in range(count)])
def _build_in_clause_placeholders(self, count: int) -> str:
"""
构建 IN 子句的参数占位符字符串
Args:
count: 占位符数量
Returns:
IN 子句占位符字符串,如 "?, ?, ?""%s, %s, %s"
"""
placeholder = self._get_placeholder()
return ", ".join([placeholder for _ in range(count)])
def _get_connection(self):
"""
获取数据库连接
Returns:
数据库连接对象
"""
return get_connection()