-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathcursor.py
More file actions
3277 lines (2821 loc) · 132 KB
/
cursor.py
File metadata and controls
3277 lines (2821 loc) · 132 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Copyright (c) Microsoft Corporation.
Licensed under the MIT license.
This module contains the Cursor class, which represents a database cursor.
Resource Management:
- Cursors are tracked by their parent connection.
- Closing the connection will automatically close all open cursors.
- Do not use a cursor after it is closed, or after its parent connection is closed.
- Use close() to release resources held by the cursor as soon as it is no longer needed.
"""
# pylint: disable=too-many-lines # Large file due to comprehensive DB-API 2.0 implementation
import decimal
import uuid
import datetime
import warnings
from typing import List, Union, Any, Optional, Tuple, Sequence, TYPE_CHECKING, Iterable
from mssql_python.constants import ConstantsDDBC as ddbc_sql_const, SQLTypes
from mssql_python.helpers import check_error, connstr_to_pycore_params
from mssql_python.logging import logger
from mssql_python import ddbc_bindings
from mssql_python.exceptions import (
InterfaceError,
NotSupportedError,
ProgrammingError,
OperationalError,
DatabaseError,
)
from mssql_python.row import Row
from mssql_python import get_settings
from mssql_python.parameter_helper import (
detect_and_convert_parameters,
parse_pyformat_params,
convert_pyformat_to_qmark,
)
if TYPE_CHECKING:
import pyarrow # type: ignore
from mssql_python.connection import Connection
else:
pyarrow = None
# Constants for string handling
MAX_INLINE_CHAR: int = (
4000 # NVARCHAR/VARCHAR inline limit; this triggers NVARCHAR(MAX)/VARCHAR(MAX) + DAE
)
SMALLMONEY_MIN: decimal.Decimal = decimal.Decimal("-214748.3648")
SMALLMONEY_MAX: decimal.Decimal = decimal.Decimal("214748.3647")
MONEY_MIN: decimal.Decimal = decimal.Decimal("-922337203685477.5808")
MONEY_MAX: decimal.Decimal = decimal.Decimal("922337203685477.5807")
class Cursor: # pylint: disable=too-many-instance-attributes,too-many-public-methods
"""
Represents a database cursor, which is used to manage the context of a fetch operation.
Attributes:
connection: Database connection object.
description: Sequence of 7-item sequences describing one result column.
rowcount: Number of rows produced or affected by the last execute operation.
arraysize: Number of rows to fetch at a time with fetchmany().
rownumber: Track the current row index in the result set.
Methods:
__init__(connection_str) -> None.
callproc(procname, parameters=None) ->
Modified copy of the input sequence with output parameters.
close() -> None.
execute(operation, parameters=None) -> Cursor.
executemany(operation, seq_of_parameters) -> None.
fetchone() -> Single sequence or None if no more data is available.
fetchmany(size=None) -> Sequence of sequences (e.g. list of tuples).
fetchall() -> Sequence of sequences (e.g. list of tuples).
nextset() -> True if there is another result set, None otherwise.
next() -> Fetch the next row from the cursor.
setinputsizes(sizes) -> None.
setoutputsize(size, column=None) -> None.
"""
# TODO(jathakkar): Thread safety considerations
# The cursor class contains methods that are not thread-safe due to:
# 1. Methods that mutate cursor state (_reset_cursor, self.description, etc.)
# 2. Methods that call ODBC functions with shared handles (self.hstmt)
#
# These methods should be properly synchronized or redesigned when implementing
# async functionality to prevent race conditions and data corruption.
# Consider using locks, redesigning for immutability, or ensuring
# cursor objects are never shared across threads.
def __init__(self, connection: "Connection", timeout: int = 0) -> None:
"""
Initialize the cursor with a database connection.
Args:
connection: Database connection object.
timeout: Query timeout in seconds
"""
self._connection: "Connection" = connection # Store as private attribute
self._timeout: int = timeout
self._inputsizes: Optional[List[Union[int, Tuple[Any, ...]]]] = None
# self.connection.autocommit = False
self.hstmt: Optional[Any] = None
self._initialize_cursor()
self.description: Optional[
List[
Tuple[
str,
Any,
Optional[int],
Optional[int],
Optional[int],
Optional[int],
Optional[bool],
]
]
] = None
self.rowcount: int = -1
self.arraysize: int = (
1 # Default number of rows to fetch at a time is 1, user can change it
)
self.buffer_length: int = 1024 # Default buffer length for string data
self.closed: bool = False
self._result_set_empty: bool = False # Add this initialization
self.last_executed_stmt: str = "" # Stores the last statement executed by this cursor
self.is_stmt_prepared: List[bool] = [
False
] # Indicates if last_executed_stmt was prepared by ddbc shim.
# Is a list instead of a bool coz bools in Python are immutable.
# Initialize attributes that may be defined later to avoid pylint warnings
# Note: _original_fetch* methods are not initialized here as they need to be
# conditionally set based on hasattr() checks
# Hence, we can't pass around bools by reference & modify them.
# Therefore, it must be a list with exactly one bool element.
self._rownumber = -1 # DB-API extension: last returned row index, -1 before first
self._cached_column_map = None
self._cached_converter_map = None
self._uuid_str_indices = None # Pre-computed UUID column indices for str conversion
# Cache the effective native_uuid setting for this cursor's connection.
# Resolution order: connection._native_uuid (if not None) → module-level setting.
self._conn_native_uuid = getattr(self.connection, "_native_uuid", None)
self._next_row_index = 0 # internal: index of the next row the driver will return (0-based)
self._has_result_set = False # Track if we have an active result set
self._skip_increment_for_next_fetch = (
False # Track if we need to skip incrementing the row index
)
self.messages = [] # Store diagnostic messages
def _is_unicode_string(self, param: str) -> bool:
"""
Check if a string contains non-ASCII characters.
Args:
param: The string to check.
Returns:
True if the string contains non-ASCII characters, False otherwise.
"""
try:
param.encode("ascii")
return False # Can be encoded to ASCII, so not Unicode
except UnicodeEncodeError:
return True # Contains non-ASCII characters, so treat as Unicode
def _parse_date(self, param: str) -> Optional[datetime.date]:
"""
Attempt to parse a string as a date.
Args:
param: The string to parse.
Returns:
A datetime.date object if parsing is successful, else None.
"""
formats = ["%Y-%m-%d"]
for fmt in formats:
try:
return datetime.datetime.strptime(param, fmt).date()
except ValueError:
continue
return None
def _parse_datetime(self, param: str) -> Optional[datetime.datetime]:
"""
Attempt to parse a string as a datetime, smalldatetime, datetime2, timestamp.
Args:
param: The string to parse.
Returns:
A datetime.datetime object if parsing is successful, else None.
"""
formats = [
"%Y-%m-%dT%H:%M:%S.%f", # ISO 8601 datetime with fractional seconds
"%Y-%m-%dT%H:%M:%S", # ISO 8601 datetime
"%Y-%m-%d %H:%M:%S.%f", # Datetime with fractional seconds
"%Y-%m-%d %H:%M:%S", # Datetime without fractional seconds
]
for fmt in formats:
try:
return datetime.datetime.strptime(param, fmt) # Valid datetime
except ValueError:
continue # Try next format
return None # If all formats fail, return None
def _parse_time(self, param: str) -> Optional[datetime.time]:
"""
Attempt to parse a string as a time.
Args:
param: The string to parse.
Returns:
A datetime.time object if parsing is successful, else None.
"""
formats = [
"%H:%M:%S", # Time only
"%H:%M:%S.%f", # Time with fractional seconds
]
for fmt in formats:
try:
return datetime.datetime.strptime(param, fmt).time()
except ValueError:
continue
return None
def _get_numeric_data(self, param: decimal.Decimal) -> Any:
"""
Get the data for a numeric parameter.
Args:
param: The numeric parameter.
Returns:
numeric_data: A NumericData struct containing
the numeric data.
"""
decimal_as_tuple = param.as_tuple()
digits_tuple = decimal_as_tuple.digits
num_digits = len(digits_tuple)
exponent = decimal_as_tuple.exponent
# Handle special values (NaN, Infinity, etc.)
if isinstance(exponent, str):
# For special values like 'n' (NaN), 'N' (sNaN), 'F' (Infinity)
# Return default precision and scale
precision = 38 # SQL Server default max precision
scale = 0
else:
# Calculate the SQL precision & scale
# precision = no. of significant digits
# scale = no. digits after decimal point
if exponent >= 0:
# digits=314, exp=2 ---> '31400' --> precision=5, scale=0
precision = num_digits + exponent
scale = 0
elif (-1 * exponent) <= num_digits:
# digits=3140, exp=-3 ---> '3.140' --> precision=4, scale=3
precision = num_digits
scale = exponent * -1
else:
# digits=3140, exp=-5 ---> '0.03140' --> precision=5, scale=5
# TODO: double check the precision calculation here with SQL documentation
precision = exponent * -1
scale = exponent * -1
if precision > 38:
raise ValueError(
"Precision of the numeric value is too high - "
+ str(param)
+ ". Should be less than or equal to 38"
)
Numeric_Data = ddbc_bindings.NumericData
numeric_data = Numeric_Data()
numeric_data.scale = scale
numeric_data.precision = precision
numeric_data.sign = 1 if decimal_as_tuple.sign == 0 else 0
# strip decimal point from param & convert the significant digits to integer
# Ex: 12.34 ---> 1234
int_str = "".join(str(d) for d in digits_tuple)
if exponent > 0:
int_str = int_str + ("0" * exponent)
elif exponent < 0:
if -exponent > num_digits:
int_str = ("0" * (-exponent - num_digits)) + int_str
if int_str == "":
int_str = "0"
# Convert decimal base-10 string to python int, then to 16 little-endian bytes
big_int = int(int_str)
byte_array = bytearray(16) # SQL_MAX_NUMERIC_LEN
for i in range(16):
byte_array[i] = big_int & 0xFF
big_int >>= 8
if big_int == 0:
break
numeric_data.val = bytes(byte_array)
return numeric_data
def _get_encoding_settings(self):
"""
Get the encoding settings from the connection.
Returns:
dict: A dictionary with 'encoding' and 'ctype' keys, or default settings if not available
Raises:
OperationalError, DatabaseError: If there are unexpected database connection issues
that indicate a broken connection state. These should not be silently ignored
as they can lead to data corruption or inconsistent behavior.
"""
if hasattr(self._connection, "getencoding"):
try:
return self._connection.getencoding()
except (OperationalError, DatabaseError) as db_error:
# Log the error for debugging but re-raise for fail-fast behavior
# Silently returning defaults can lead to data corruption and hard-to-debug issues
logger.error(
"Failed to get encoding settings from connection due to database error: %s. "
"This indicates a broken connection state that should not be ignored.",
db_error,
)
# Re-raise to fail fast - users should know their connection is broken
raise
except Exception as unexpected_error:
# Handle other unexpected errors (connection closed, programming errors, etc.)
logger.error("Unexpected error getting encoding settings: %s", unexpected_error)
# Re-raise unexpected errors as well
raise
# Return default encoding settings if getencoding is not available
# This is the only case where defaults are appropriate (method doesn't exist)
return {"encoding": "utf-16le", "ctype": ddbc_sql_const.SQL_WCHAR.value}
def _get_decoding_settings(self, sql_type):
"""
Get decoding settings for a specific SQL type.
Args:
sql_type: SQL type constant (SQL_CHAR, SQL_WCHAR, etc.)
Returns:
Dictionary containing the decoding settings.
Raises:
OperationalError, DatabaseError: If there are unexpected database connection issues
that indicate a broken connection state. These should not be silently ignored
as they can lead to data corruption or inconsistent behavior.
"""
try:
# Get decoding settings from connection for this SQL type
return self._connection.getdecoding(sql_type)
except (OperationalError, DatabaseError) as db_error:
# Log the error for debugging but re-raise for fail-fast behavior
# Silently returning defaults can lead to data corruption and hard-to-debug issues
logger.error(
"Failed to get decoding settings for SQL type %s due to database error: %s. "
"This indicates a broken connection state that should not be ignored.",
sql_type,
db_error,
)
# Re-raise to fail fast - users should know their connection is broken
raise
except Exception as unexpected_error:
# Handle other unexpected errors (connection closed, programming errors, etc.)
logger.error(
"Unexpected error getting decoding settings for SQL type %s: %s",
sql_type,
unexpected_error,
)
# Re-raise unexpected errors as well
raise
def _map_sql_type( # pylint: disable=too-many-arguments,too-many-positional-arguments,too-many-locals,too-many-return-statements,too-many-branches
self,
param: Any,
parameters_list: List[Any],
i: int,
min_val: Optional[Any] = None,
max_val: Optional[Any] = None,
) -> Tuple[int, int, int, int, bool]:
"""
Map a Python data type to the corresponding SQL type,
C type, Column size, and Decimal digits.
Takes:
- param: The parameter to map.
- parameters_list: The list of parameters to bind.
- i: The index of the parameter in the list.
Returns:
- A tuple containing the SQL type, C type, column size, and decimal digits.
"""
logger.debug("_map_sql_type: Mapping param index=%d, type=%s", i, type(param).__name__)
if param is None:
logger.debug("_map_sql_type: NULL parameter - index=%d", i)
return (
ddbc_sql_const.SQL_UNKNOWN_TYPE.value,
ddbc_sql_const.SQL_C_DEFAULT.value,
1,
0,
False,
)
if isinstance(param, bool):
logger.debug("_map_sql_type: BOOL detected - index=%d", i)
return (
ddbc_sql_const.SQL_BIT.value,
ddbc_sql_const.SQL_C_BIT.value,
1,
0,
False,
)
if isinstance(param, int):
# Use min_val/max_val if available
value_to_check = max_val if max_val is not None else param
min_to_check = min_val if min_val is not None else param
logger.debug(
"_map_sql_type: INT detected - index=%d, min=%s, max=%s",
i,
str(min_to_check)[:50],
str(value_to_check)[:50],
)
if 0 <= min_to_check and value_to_check <= 255:
logger.debug("_map_sql_type: INT -> TINYINT - index=%d", i)
return (
ddbc_sql_const.SQL_TINYINT.value,
ddbc_sql_const.SQL_C_TINYINT.value,
3,
0,
False,
)
if -32768 <= min_to_check and value_to_check <= 32767:
logger.debug("_map_sql_type: INT -> SMALLINT - index=%d", i)
return (
ddbc_sql_const.SQL_SMALLINT.value,
ddbc_sql_const.SQL_C_SHORT.value,
5,
0,
False,
)
if -2147483648 <= min_to_check and value_to_check <= 2147483647:
logger.debug("_map_sql_type: INT -> INTEGER - index=%d", i)
return (
ddbc_sql_const.SQL_INTEGER.value,
ddbc_sql_const.SQL_C_LONG.value,
10,
0,
False,
)
logger.debug("_map_sql_type: INT -> BIGINT - index=%d", i)
return (
ddbc_sql_const.SQL_BIGINT.value,
ddbc_sql_const.SQL_C_SBIGINT.value,
19,
0,
False,
)
if isinstance(param, float):
logger.debug("_map_sql_type: FLOAT detected - index=%d", i)
return (
ddbc_sql_const.SQL_DOUBLE.value,
ddbc_sql_const.SQL_C_DOUBLE.value,
15,
0,
False,
)
if isinstance(param, decimal.Decimal):
logger.debug("_map_sql_type: DECIMAL detected - index=%d", i)
# First check precision limit for all decimal values
decimal_as_tuple = param.as_tuple()
digits_tuple = decimal_as_tuple.digits
num_digits = len(digits_tuple)
exponent = decimal_as_tuple.exponent
# Handle special values (NaN, Infinity, etc.)
if isinstance(exponent, str):
logger.debug(
"_map_sql_type: DECIMAL special value - index=%d, exponent=%s", i, exponent
)
# For special values like 'n' (NaN), 'N' (sNaN), 'F' (Infinity)
# Return default precision and scale
precision = 38 # SQL Server default max precision
else:
# Calculate the SQL precision (same logic as _get_numeric_data)
if exponent >= 0:
precision = num_digits + exponent
elif (-1 * exponent) <= num_digits:
precision = num_digits
else:
precision = exponent * -1
logger.debug(
"_map_sql_type: DECIMAL precision calculated - index=%d, precision=%d",
i,
precision,
)
if precision > 38:
logger.debug(
"_map_sql_type: DECIMAL precision too high - index=%d, precision=%d",
i,
precision,
)
raise ValueError(
f"Precision of the numeric value is too high. "
f"The maximum precision supported by SQL Server is 38, but got {precision}."
)
# Detect MONEY / SMALLMONEY range
if SMALLMONEY_MIN <= param <= SMALLMONEY_MAX:
logger.debug("_map_sql_type: DECIMAL -> SMALLMONEY - index=%d", i)
# smallmoney
parameters_list[i] = format(param, "f")
return (
ddbc_sql_const.SQL_VARCHAR.value,
ddbc_sql_const.SQL_C_CHAR.value,
len(parameters_list[i]),
0,
False,
)
if MONEY_MIN <= param <= MONEY_MAX:
logger.debug("_map_sql_type: DECIMAL -> MONEY - index=%d", i)
# money
parameters_list[i] = format(param, "f")
return (
ddbc_sql_const.SQL_VARCHAR.value,
ddbc_sql_const.SQL_C_CHAR.value,
len(parameters_list[i]),
0,
False,
)
# fallback to generic numeric binding
logger.debug("_map_sql_type: DECIMAL -> NUMERIC - index=%d", i)
parameters_list[i] = self._get_numeric_data(param)
logger.debug(
"_map_sql_type: NUMERIC created - index=%d, precision=%d, scale=%d",
i,
parameters_list[i].precision,
parameters_list[i].scale,
)
return (
ddbc_sql_const.SQL_NUMERIC.value,
ddbc_sql_const.SQL_C_NUMERIC.value,
parameters_list[i].precision,
parameters_list[i].scale,
False,
)
if isinstance(param, uuid.UUID):
logger.debug("_map_sql_type: UUID detected - index=%d", i)
parameters_list[i] = param.bytes_le
return (
ddbc_sql_const.SQL_GUID.value,
ddbc_sql_const.SQL_C_GUID.value,
16,
0,
False,
)
if isinstance(param, str):
logger.debug("_map_sql_type: STR detected - index=%d, length=%d", i, len(param))
if (
param.startswith("POINT")
or param.startswith("LINESTRING")
or param.startswith("POLYGON")
):
logger.debug("_map_sql_type: STR is geometry type - index=%d", i)
return (
ddbc_sql_const.SQL_WVARCHAR.value,
ddbc_sql_const.SQL_C_WCHAR.value,
len(param),
0,
False,
)
# String mapping logic here
is_unicode = self._is_unicode_string(param)
# Computes UTF-16 code units (handles surrogate pairs)
utf16_len = sum(2 if ord(c) > 0xFFFF else 1 for c in param)
logger.debug(
"_map_sql_type: STR analysis - index=%d, is_unicode=%s, utf16_len=%d",
i,
str(is_unicode),
utf16_len,
)
if utf16_len > MAX_INLINE_CHAR: # Long strings -> DAE
logger.debug("_map_sql_type: STR exceeds MAX_INLINE_CHAR, using DAE - index=%d", i)
if is_unicode:
return (
ddbc_sql_const.SQL_WVARCHAR.value,
ddbc_sql_const.SQL_C_WCHAR.value,
0,
0,
True,
)
return (
ddbc_sql_const.SQL_VARCHAR.value,
ddbc_sql_const.SQL_C_CHAR.value,
0,
0,
True,
)
# Short strings
if is_unicode:
return (
ddbc_sql_const.SQL_WVARCHAR.value,
ddbc_sql_const.SQL_C_WCHAR.value,
utf16_len,
0,
False,
)
return (
ddbc_sql_const.SQL_VARCHAR.value,
ddbc_sql_const.SQL_C_CHAR.value,
len(param),
0,
False,
)
if isinstance(param, (bytes, bytearray)):
length = len(param)
if length > 8000: # Use VARBINARY(MAX) for large blobs
return (
ddbc_sql_const.SQL_VARBINARY.value,
ddbc_sql_const.SQL_C_BINARY.value,
0,
0,
True,
)
# Small blobs → direct binding
return (
ddbc_sql_const.SQL_VARBINARY.value,
ddbc_sql_const.SQL_C_BINARY.value,
max(length, 1),
0,
False,
)
if isinstance(param, datetime.datetime):
if param.tzinfo is not None:
# Timezone-aware datetime -> DATETIMEOFFSET
return (
ddbc_sql_const.SQL_DATETIMEOFFSET.value,
ddbc_sql_const.SQL_C_SS_TIMESTAMPOFFSET.value,
34,
7,
False,
)
# Naive datetime -> TIMESTAMP
return (
ddbc_sql_const.SQL_TIMESTAMP.value,
ddbc_sql_const.SQL_C_TYPE_TIMESTAMP.value,
26,
6,
False,
)
if isinstance(param, datetime.date):
return (
ddbc_sql_const.SQL_DATE.value,
ddbc_sql_const.SQL_C_TYPE_DATE.value,
10,
0,
False,
)
if isinstance(param, datetime.time):
return (
ddbc_sql_const.SQL_TIME.value,
ddbc_sql_const.SQL_C_TYPE_TIME.value,
8,
0,
False,
)
# For safety: unknown/unhandled Python types should not silently go to SQL
raise TypeError(
"Unsupported parameter type: The driver cannot safely convert it to a SQL type."
)
def _initialize_cursor(self) -> None:
"""
Initialize the DDBC statement handle.
"""
self._allocate_statement_handle()
self._set_timeout()
def _allocate_statement_handle(self) -> None:
"""
Allocate the DDBC statement handle.
"""
self.hstmt = self._connection._conn.alloc_statement_handle()
def _set_timeout(self) -> None:
"""
Set the query timeout attribute on the statement handle.
This is called once when the cursor is created and after any handle reallocation.
Following pyodbc's approach for better performance.
"""
if self._timeout > 0:
logger.debug("_set_timeout: Setting query timeout=%d seconds", self._timeout)
try:
timeout_value = int(self._timeout)
ret = ddbc_bindings.DDBCSQLSetStmtAttr(
self.hstmt,
ddbc_sql_const.SQL_ATTR_QUERY_TIMEOUT.value,
timeout_value,
)
check_error(ddbc_sql_const.SQL_HANDLE_STMT.value, self.hstmt, ret)
logger.debug("Query timeout set to %d seconds", timeout_value)
except Exception as e: # pylint: disable=broad-exception-caught
logger.warning("Failed to set query timeout: %s", str(e))
def _reset_cursor(self) -> None:
"""
Reset the DDBC statement handle.
"""
if self.hstmt:
self.hstmt.free()
self.hstmt = None
logger.debug("SQLFreeHandle succeeded")
self._clear_rownumber()
# Reinitialize the statement handle
self._initialize_cursor()
def close(self) -> None:
"""
Close the connection now (rather than whenever .__del__() is called).
Idempotent: subsequent calls have no effect and will be no-ops.
The cursor will be unusable from this point forward; an InterfaceError
will be raised if any operation (other than close) is attempted with the cursor.
This is a deviation from pyodbc, which raises an exception if the cursor is already closed.
"""
if self.closed:
# Do nothing - not calling _check_closed() here since we want this to be idempotent
return
# Clear messages per DBAPI
self.messages = []
# Remove this cursor from the connection's tracking
if hasattr(self, "connection") and self.connection and hasattr(self.connection, "_cursors"):
try:
self.connection._cursors.discard(self)
except Exception as e: # pylint: disable=broad-exception-caught
logger.warning("Error removing cursor from connection tracking: %s", e)
if self.hstmt:
self.hstmt.free()
self.hstmt = None
logger.debug("SQLFreeHandle succeeded")
self._clear_rownumber()
self.closed = True
def _check_closed(self) -> None:
"""
Check if the cursor is closed and raise an exception if it is.
Raises:
ProgrammingError: If the cursor is closed.
"""
if self.closed:
raise ProgrammingError(
driver_error="Operation cannot be performed: The cursor is closed.",
ddbc_error="",
)
def _ensure_pyarrow(self) -> Any:
"""
Import and return pyarrow or raise ImportError accordingly.
"""
try:
import pyarrow
return pyarrow
except ImportError as e:
raise ImportError(
"pyarrow is required for Arrow fetch methods. Please install pyarrow."
) from e
def setinputsizes(self, sizes: List[Union[int, tuple]]) -> None:
"""
Sets the type information to be used for parameters in execute and executemany.
This method can be used to explicitly declare the types and sizes of query parameters.
For example:
sql = "INSERT INTO product (item, price) VALUES (?, ?)"
params = [('bicycle', 499.99), ('ham', 17.95)]
# specify that parameters are for NVARCHAR(50) and DECIMAL(18,4) columns
cursor.setinputsizes([(SQL_WVARCHAR, 50, 0), (SQL_DECIMAL, 18, 4)])
cursor.executemany(sql, params)
Args:
sizes: A sequence of tuples, one for each parameter. Each tuple contains
(sql_type, size, decimal_digits) where size and decimal_digits are optional.
"""
# Get valid SQL types from centralized constants
valid_sql_types = SQLTypes.get_valid_types()
self._inputsizes = []
if sizes:
for size_info in sizes:
if isinstance(size_info, tuple):
# Handle tuple format (sql_type, size, decimal_digits)
if len(size_info) == 1:
sql_type = size_info[0]
column_size = 0
decimal_digits = 0
elif len(size_info) == 2:
sql_type, column_size = size_info
decimal_digits = 0
elif len(size_info) >= 3:
sql_type, column_size, decimal_digits = size_info
# Validate SQL type
if not isinstance(sql_type, int) or sql_type not in valid_sql_types:
raise ValueError(
f"Invalid SQL type: {sql_type}. Must be a valid SQL type constant."
)
# Validate size and precision
if not isinstance(column_size, int) or column_size < 0:
raise ValueError(
f"Invalid column size: {column_size}. Must be a non-negative integer."
)
if not isinstance(decimal_digits, int) or decimal_digits < 0:
raise ValueError(
f"Invalid decimal digits: {decimal_digits}. "
f"Must be a non-negative integer."
)
self._inputsizes.append((sql_type, column_size, decimal_digits))
else:
# Handle single value (just sql_type)
sql_type = size_info
# Validate SQL type
if not isinstance(sql_type, int) or sql_type not in valid_sql_types:
raise ValueError(
f"Invalid SQL type: {sql_type}. Must be a valid SQL type constant."
)
self._inputsizes.append((sql_type, 0, 0))
def _reset_inputsizes(self) -> None:
"""Reset input sizes after execution"""
self._inputsizes = None
def _get_c_type_for_sql_type(self, sql_type: int) -> int:
"""Map SQL type to appropriate C type for parameter binding."""
sql_to_c_type = {
ddbc_sql_const.SQL_CHAR.value: ddbc_sql_const.SQL_C_CHAR.value,
ddbc_sql_const.SQL_VARCHAR.value: ddbc_sql_const.SQL_C_CHAR.value,
ddbc_sql_const.SQL_LONGVARCHAR.value: ddbc_sql_const.SQL_C_CHAR.value,
ddbc_sql_const.SQL_WCHAR.value: ddbc_sql_const.SQL_C_WCHAR.value,
ddbc_sql_const.SQL_WVARCHAR.value: ddbc_sql_const.SQL_C_WCHAR.value,
ddbc_sql_const.SQL_WLONGVARCHAR.value: ddbc_sql_const.SQL_C_WCHAR.value,
ddbc_sql_const.SQL_DECIMAL.value: ddbc_sql_const.SQL_C_NUMERIC.value,
ddbc_sql_const.SQL_NUMERIC.value: ddbc_sql_const.SQL_C_NUMERIC.value,
ddbc_sql_const.SQL_BIT.value: ddbc_sql_const.SQL_C_BIT.value,
ddbc_sql_const.SQL_TINYINT.value: ddbc_sql_const.SQL_C_TINYINT.value,
ddbc_sql_const.SQL_SMALLINT.value: ddbc_sql_const.SQL_C_SHORT.value,
ddbc_sql_const.SQL_INTEGER.value: ddbc_sql_const.SQL_C_LONG.value,
ddbc_sql_const.SQL_BIGINT.value: ddbc_sql_const.SQL_C_SBIGINT.value,
ddbc_sql_const.SQL_REAL.value: ddbc_sql_const.SQL_C_FLOAT.value,
ddbc_sql_const.SQL_FLOAT.value: ddbc_sql_const.SQL_C_DOUBLE.value,
ddbc_sql_const.SQL_DOUBLE.value: ddbc_sql_const.SQL_C_DOUBLE.value,
ddbc_sql_const.SQL_BINARY.value: ddbc_sql_const.SQL_C_BINARY.value,
ddbc_sql_const.SQL_VARBINARY.value: ddbc_sql_const.SQL_C_BINARY.value,
ddbc_sql_const.SQL_LONGVARBINARY.value: ddbc_sql_const.SQL_C_BINARY.value,
ddbc_sql_const.SQL_SS_UDT.value: ddbc_sql_const.SQL_C_BINARY.value,
# ODBC 3.x date/time types (reported by ODBC 18 driver)
ddbc_sql_const.SQL_TYPE_DATE.value: ddbc_sql_const.SQL_C_TYPE_DATE.value,
ddbc_sql_const.SQL_TYPE_TIME.value: ddbc_sql_const.SQL_C_TYPE_TIME.value,
ddbc_sql_const.SQL_TYPE_TIMESTAMP.value: ddbc_sql_const.SQL_C_TYPE_TIMESTAMP.value,
ddbc_sql_const.SQL_SS_TIME2.value: ddbc_sql_const.SQL_C_TYPE_TIME.value,
ddbc_sql_const.SQL_DATETIMEOFFSET.value: ddbc_sql_const.SQL_C_SS_TIMESTAMPOFFSET.value,
# ODBC 2.x aliases (accepted by setinputsizes via SQLTypes)
ddbc_sql_const.SQL_DATE.value: ddbc_sql_const.SQL_C_TYPE_DATE.value,
ddbc_sql_const.SQL_TIME.value: ddbc_sql_const.SQL_C_TYPE_TIME.value,
ddbc_sql_const.SQL_TIMESTAMP.value: ddbc_sql_const.SQL_C_TYPE_TIMESTAMP.value,
# Other types
ddbc_sql_const.SQL_GUID.value: ddbc_sql_const.SQL_C_GUID.value,
ddbc_sql_const.SQL_SS_XML.value: ddbc_sql_const.SQL_C_WCHAR.value,
ddbc_sql_const.SQL_SS_VARIANT.value: ddbc_sql_const.SQL_C_BINARY.value,
}
return sql_to_c_type.get(sql_type, ddbc_sql_const.SQL_C_DEFAULT.value)
def _create_parameter_types_list( # pylint: disable=too-many-arguments,too-many-positional-arguments
self,
parameter: Any,
param_info: Optional[Tuple[Any, ...]],
parameters_list: List[Any],
i: int,
min_val: Optional[Any] = None,
max_val: Optional[Any] = None,
) -> Tuple[int, int, int, int, bool]:
"""
Maps parameter types for the given parameter.
Args:
parameter: parameter to bind.
Returns:
paraminfo.
"""
paraminfo = param_info()
# Check if we have explicit type information from setinputsizes
if self._inputsizes and i < len(self._inputsizes):
# Use explicit type information
sql_type, column_size, decimal_digits = self._inputsizes[i]
# Default is_dae to False for explicit types, but set to True for large strings/binary
is_dae = False
if parameter is None:
# For NULL parameters, always use SQL_C_DEFAULT regardless of SQL type
c_type = ddbc_sql_const.SQL_C_DEFAULT.value
else:
# For non-NULL parameters, determine the appropriate C type based on SQL type
c_type = self._get_c_type_for_sql_type(sql_type)
# Check if this should be a DAE (data at execution) parameter
# For string types with large column sizes
if isinstance(parameter, str) and column_size > MAX_INLINE_CHAR:
is_dae = True
# For binary types with large column sizes
elif isinstance(parameter, (bytes, bytearray)) and column_size > 8000:
is_dae = True
# Sanitize precision/scale for numeric types
if sql_type in (
ddbc_sql_const.SQL_DECIMAL.value,
ddbc_sql_const.SQL_NUMERIC.value,
):
column_size = max(1, min(int(column_size) if column_size > 0 else 18, 38))
decimal_digits = min(max(0, decimal_digits), column_size)
else:
# Fall back to automatic type inference
sql_type, c_type, column_size, decimal_digits, is_dae = self._map_sql_type(
parameter, parameters_list, i, min_val=min_val, max_val=max_val
)
paraminfo.paramCType = c_type
paraminfo.paramSQLType = sql_type
paraminfo.inputOutputType = ddbc_sql_const.SQL_PARAM_INPUT.value
paraminfo.columnSize = column_size
paraminfo.decimalDigits = decimal_digits
paraminfo.isDAE = is_dae
if is_dae:
paraminfo.dataPtr = parameter # Will be converted to py::object* in C++
return paraminfo
def _initialize_description(self, column_metadata: Optional[Any] = None) -> None:
"""Initialize the description attribute from column metadata."""
if not column_metadata:
self.description = None
return
description = []
for _, col in enumerate(column_metadata):
# Get column name - lowercase it if the lowercase flag is set
column_name = col["ColumnName"]
# Use the current global setting to ensure tests pass correctly
if get_settings().lowercase:
column_name = column_name.lower()
# Add to description tuple (7 elements as per PEP-249)
description.append(
(
column_name, # name
self._map_data_type(col["DataType"]), # type_code
None, # display_size
col["ColumnSize"], # internal_size
col["ColumnSize"], # precision - should match ColumnSize
col["DecimalDigits"], # scale
col["Nullable"] == ddbc_sql_const.SQL_NULLABLE.value, # null_ok
)
)
self.description = description