Skip to content

Commit e36a3e6

Browse files
committed
Resolving comments
1 parent ac620f8 commit e36a3e6

File tree

4 files changed

+1086
-2200
lines changed

4 files changed

+1086
-2200
lines changed

mssql_python/connection.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ def __init__(self, connection_str: str = "", autocommit: bool = False, attrs_bef
152152
# Initialize encoding settings with defaults for Python 3
153153
# Python 3 only has str (which is Unicode), so we use utf-16le by default
154154
self._encoding_settings = {
155-
'encoding': 'utf-16le',
156-
'ctype': ConstantsDDBC.SQL_WCHAR.value
155+
'encoding': 'utf-8',
156+
'ctype': ConstantsDDBC.SQL_CHAR.value
157157
}
158158

159159
# Initialize decoding settings with Python 3 defaults
@@ -373,9 +373,10 @@ def setencoding(self, encoding=None, ctype=None):
373373

374374
# Enforce UTF-16LE for SQL_WCHAR
375375
if ctype == ConstantsDDBC.SQL_WCHAR.value and encoding not in UTF16_ENCODINGS:
376-
raise ValueError(
377-
f"SQL_WCHAR must use UTF-16LE encoding. '{encoding}' is not supported for SQL_WCHAR. "
378-
f"Use SQL_CHAR if you need to use '{encoding}' encoding."
376+
raise ProgrammingError(
377+
driver_error=f"SQL_WCHAR requires UTF-16LE encoding",
378+
ddbc_error=f"SQL_WCHAR must use UTF-16LE encoding. '{encoding}' is not supported for SQL_WCHAR. "
379+
f"Use SQL_CHAR if you need to use '{encoding}' encoding."
379380
)
380381

381382
# Store the encoding settings
@@ -462,9 +463,9 @@ def setdecoding(self, sqltype, encoding=None, ctype=None):
462463
# For SQL_WCHAR and SQL_WMETADATA, enforce UTF-16LE encoding and SQL_WCHAR ctype
463464
if sqltype in (ConstantsDDBC.SQL_WCHAR.value, SQL_WMETADATA):
464465
if encoding is not None and encoding.lower() not in UTF16_ENCODINGS:
465-
raise ValueError(
466-
f"SQL_WCHAR and SQL_WMETADATA must use UTF-16LE encoding. '{encoding}' is not supported. "
467-
f"Custom encodings are only supported for SQL_CHAR."
466+
raise ProgrammingError(
467+
driver_error=f"SQL_WCHAR and SQL_WMETADATA must use UTF-16LE encoding. '{encoding}' is not supported.",
468+
ddbc_error=f"Custom encodings are only supported for SQL_CHAR. '{encoding}' is not valid for SQL_WCHAR or SQL_WMETADATA."
468469
)
469470
# Always enforce UTF-16LE for wide character types
470471
encoding = 'utf-16le'

mssql_python/cursor.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,13 @@ def _get_encoding_settings(self):
117117
except:
118118
# Return default encoding settings if there's an error
119119
return {
120-
'encoding': 'utf-16le',
121-
'ctype': ddbc_sql_const.SQL_WCHAR.value
120+
'encoding': 'utf-8',
121+
'ctype': ddbc_sql_const.SQL_CHAR.value
122122
}
123123
# Return default encoding settings if getencoding is not available
124124
return {
125-
'encoding': 'utf-16le',
126-
'ctype': ddbc_sql_const.SQL_WCHAR.value
125+
'encoding': 'utf-8',
126+
'ctype': ddbc_sql_const.SQL_CHAR.value
127127
}
128128

129129
def _get_decoding_settings(self, sql_type):

0 commit comments

Comments
 (0)