fix: add SQL Server to MySQL syntax conversion for parameter placeholders and table names

Automatically convert SQL Server syntax to MySQL-compatible format in
execute_query() and execute_update() methods:

- Parameter placeholders: ? -> %s
- Table names: [dbo].[TableName] -> dbo_TableName
- Column names: [ColumnName] -> ColumnName

This fixes the "Not all parameters were used in the SQL statement" error
that occurred when running queries originally written for SQL Server against
MySQL database.

The conversion is transparent to existing code, allowing SQL queries
throughout the codebase to work with MySQL without modification.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Misaka
2026-02-24 21:49:08 +08:00
parent f2bbcfc426
commit a520c9a05c

View File

@@ -83,10 +83,14 @@ class MySQLConnection(BaseDatabaseConnection):
cursor = None cursor = None
try: try:
cursor = self.connection.cursor(dictionary=True) cursor = self.connection.cursor(dictionary=True)
# Convert SQL Server placeholders (?) to MySQL placeholders (%s)
# Convert SQL Server table names to MySQL format
converted_sql = self._convert_placeholders(sql)
converted_sql = self._convert_table_names(converted_sql)
if params: if params:
cursor.execute(sql, params) cursor.execute(converted_sql, params)
else: else:
cursor.execute(sql) cursor.execute(converted_sql)
# 直接获取字典列表 # 直接获取字典列表
results = cursor.fetchall() results = cursor.fetchall()
@@ -116,10 +120,14 @@ class MySQLConnection(BaseDatabaseConnection):
cursor = None cursor = None
try: try:
cursor = self.connection.cursor() cursor = self.connection.cursor()
# Convert SQL Server placeholders (?) to MySQL placeholders (%s)
# Convert SQL Server table names to MySQL format
converted_sql = self._convert_placeholders(sql)
converted_sql = self._convert_table_names(converted_sql)
if params: if params:
cursor.execute(sql, params) cursor.execute(converted_sql, params)
else: else:
cursor.execute(sql) cursor.execute(converted_sql)
self.connection.commit() self.connection.commit()
return cursor.rowcount return cursor.rowcount
@@ -132,6 +140,44 @@ class MySQLConnection(BaseDatabaseConnection):
if cursor: if cursor:
cursor.close() cursor.close()
def _convert_placeholders(self, sql: str) -> str:
"""
Convert SQL Server placeholders (?) to MySQL placeholders (%s)
This is necessary because the codebase was originally designed for SQL Server,
which uses '?' as parameter placeholders. MySQL uses '%s' instead.
Args:
sql: SQL query with potential SQL Server placeholders
Returns:
str: SQL query with MySQL-compatible placeholders
"""
return sql.replace('?', '%s')
def _convert_table_names(self, sql: str) -> str:
"""
Convert SQL Server table names to MySQL format
Converts [dbo].[TableName] to dbo_TableName and removes square brackets
from column names.
Args:
sql: SQL query with SQL Server table/column names
Returns:
str: SQL query with MySQL-compatible table/column names
"""
import re
# Convert [dbo].[TableName] to dbo_TableName
sql = re.sub(r'\[dbo\]\.\[([^\]]+)\]', r'dbo_\1', sql)
# Remove square brackets from column names (e.g., [Column] -> Column)
sql = re.sub(r'\[([^\]]+)\]', r'\1', sql)
return sql
def get_placeholder(self) -> str: def get_placeholder(self) -> str:
""" """
获取参数占位符 获取参数占位符