Skip to content

Commit 524e7f3

Browse files
committed
Resolving comments
1 parent 24dadf5 commit 524e7f3

File tree

2 files changed

+177
-164
lines changed

2 files changed

+177
-164
lines changed

mssql_python/connection.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -443,9 +443,17 @@ def setencoding(
443443

444444
# Enforce UTF-16 encoding restriction for SQL_WCHAR
445445
if ctype == ConstantsDDBC.SQL_WCHAR.value and encoding not in UTF16_ENCODINGS:
446-
log('warning', "SQL_WCHAR only supports UTF-16 encodings. Attempted encoding '%s' is not allowed. Using default 'utf-16le' instead.",
446+
error_msg = (
447+
f"SQL_WCHAR only supports UTF-16 encodings (utf-16, utf-16le, utf-16be). "
448+
f"Encoding '{encoding}' is not compatible with SQL_WCHAR. "
449+
f"Either use a UTF-16 encoding, or use SQL_CHAR ({ConstantsDDBC.SQL_CHAR.value}) instead."
450+
)
451+
log('error', "Invalid encoding/ctype combination: %s with SQL_WCHAR",
447452
sanitize_user_input(encoding))
448-
encoding = 'utf-16le'
453+
raise ProgrammingError(
454+
driver_error=error_msg,
455+
ddbc_error=error_msg,
456+
)
449457

450458
# Store the encoding settings
451459
self._encoding_settings = {"encoding": encoding, "ctype": ctype}
@@ -569,9 +577,17 @@ def setdecoding(
569577
# Enforce UTF-16 encoding restriction for SQL_WCHAR and SQL_WMETADATA
570578
if (sqltype == ConstantsDDBC.SQL_WCHAR.value or sqltype == SQL_WMETADATA) and encoding not in UTF16_ENCODINGS:
571579
sqltype_name = "SQL_WCHAR" if sqltype == ConstantsDDBC.SQL_WCHAR.value else "SQL_WMETADATA"
572-
log('warning', "%s only supports UTF-16 encodings. Attempted encoding '%s' is not allowed. Using default 'utf-16le' instead.",
573-
sqltype_name, sanitize_user_input(encoding))
574-
encoding = 'utf-16le'
580+
error_msg = (
581+
f"{sqltype_name} only supports UTF-16 encodings (utf-16, utf-16le, utf-16be). "
582+
f"Encoding '{encoding}' is not compatible with {sqltype_name}. "
583+
f"Either use a UTF-16 encoding, or use SQL_CHAR ({ConstantsDDBC.SQL_CHAR.value}) "
584+
f"for the ctype parameter."
585+
)
586+
log('error', "Invalid encoding for %s: %s", sqltype_name, sanitize_user_input(encoding))
587+
raise ProgrammingError(
588+
driver_error=error_msg,
589+
ddbc_error=error_msg,
590+
)
575591

576592
# Set default ctype based on encoding if not provided
577593
if ctype is None:
@@ -582,9 +598,17 @@ def setdecoding(
582598

583599
# Additional validation: if user explicitly sets ctype to SQL_WCHAR but encoding is not UTF-16
584600
if ctype == ConstantsDDBC.SQL_WCHAR.value and encoding not in UTF16_ENCODINGS:
585-
log('warning', "SQL_WCHAR ctype only supports UTF-16 encodings. Attempted encoding '%s' is not compatible. Using default 'utf-16le' instead.",
586-
sanitize_user_input(encoding))
587-
encoding = 'utf-16le'
601+
error_msg = (
602+
f"SQL_WCHAR ctype only supports UTF-16 encodings (utf-16, utf-16le, utf-16be). "
603+
f"Encoding '{encoding}' is not compatible with SQL_WCHAR ctype. "
604+
f"Either use a UTF-16 encoding, or use SQL_CHAR ({ConstantsDDBC.SQL_CHAR.value}) "
605+
f"for the ctype parameter."
606+
)
607+
log('error', "Invalid encoding for SQL_WCHAR ctype: %s", sanitize_user_input(encoding))
608+
raise ProgrammingError(
609+
driver_error=error_msg,
610+
ddbc_error=error_msg,
611+
)
588612

589613
# Validate ctype
590614
valid_ctypes = [ConstantsDDBC.SQL_CHAR.value, ConstantsDDBC.SQL_WCHAR.value]

0 commit comments

Comments
 (0)