-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathcursor.py
More file actions
2245 lines (1902 loc) · 91.1 KB
/
cursor.py
File metadata and controls
2245 lines (1902 loc) · 91.1 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.
"""
import decimal
import uuid
import datetime
import warnings
from typing import List, Union, Any
from mssql_python.constants import ConstantsDDBC as ddbc_sql_const, SQLTypes
from mssql_python.helpers import check_error, log
from mssql_python import ddbc_bindings
from mssql_python.exceptions import InterfaceError, NotSupportedError, ProgrammingError
from mssql_python.row import Row
from mssql_python import get_settings
# Constants for string handling
MAX_INLINE_CHAR = 4000 # NVARCHAR/VARCHAR inline limit; this triggers NVARCHAR(MAX)/VARCHAR(MAX) + DAE
SMALLMONEY_MIN = decimal.Decimal('-214748.3648')
SMALLMONEY_MAX = decimal.Decimal('214748.3647')
MONEY_MIN = decimal.Decimal('-922337203685477.5808')
MONEY_MAX = decimal.Decimal('922337203685477.5807')
class Cursor:
"""
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, timeout: int = 0) -> None:
"""
Initialize the cursor with a database connection.
Args:
connection: Database connection object.
"""
self._connection = connection # Store as private attribute
self._timeout = timeout
self._inputsizes = None
# self.connection.autocommit = False
self.hstmt = None
self._initialize_cursor()
self.description = None
self.rowcount = -1
self.arraysize = (
1 # Default number of rows to fetch at a time is 1, user can change it
)
self.buffer_length = 1024 # Default buffer length for string data
self.closed = False
self._result_set_empty = False # Add this initialization
self.last_executed_stmt = (
"" # Stores the last statement executed by this cursor
)
self.is_stmt_prepared = [
False
] # Indicates if last_executed_stmt was prepared by ddbc shim.
# Is a list instead of a bool coz bools in Python are immutable.
# Hence, we can't pass around bools by reference & modify them.
# Therefore, it must be a list with exactly one bool element.
# rownumber attribute
self._rownumber = -1 # DB-API extension: last returned row index, -1 before first
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):
"""
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):
"""
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):
"""
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):
"""
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):
"""
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()
num_digits = len(decimal_as_tuple.digits)
exponent = decimal_as_tuple.exponent
# 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
# TODO: Revisit this check, do we want this restriction?
if precision > 15:
raise ValueError(
"Precision of the numeric value is too high - "
+ str(param)
+ ". Should be less than or equal to 15"
)
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
val = str(param)
if "." in val or "-" in val:
val = val.replace(".", "")
val = val.replace("-", "")
val = int(val)
numeric_data.val = val
return numeric_data
def _map_sql_type(self, param, parameters_list, i, min_val=None, max_val=None):
"""
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.
"""
if param is None:
return (
ddbc_sql_const.SQL_VARCHAR.value,
ddbc_sql_const.SQL_C_DEFAULT.value,
1,
0,
False,
)
if isinstance(param, bool):
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
if 0 <= min_to_check and value_to_check <= 255:
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:
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:
return (
ddbc_sql_const.SQL_INTEGER.value,
ddbc_sql_const.SQL_C_LONG.value,
10,
0,
False,
)
return (
ddbc_sql_const.SQL_BIGINT.value,
ddbc_sql_const.SQL_C_SBIGINT.value,
19,
0,
False,
)
if isinstance(param, float):
return (
ddbc_sql_const.SQL_DOUBLE.value,
ddbc_sql_const.SQL_C_DOUBLE.value,
15,
0,
False,
)
if isinstance(param, decimal.Decimal):
# Detect MONEY / SMALLMONEY range
if SMALLMONEY_MIN <= param <= SMALLMONEY_MAX:
# smallmoney
parameters_list[i] = str(param)
return (
ddbc_sql_const.SQL_VARCHAR.value,
ddbc_sql_const.SQL_C_CHAR.value,
len(parameters_list[i]),
0,
False,
)
elif MONEY_MIN <= param <= MONEY_MAX:
# money
parameters_list[i] = str(param)
return (
ddbc_sql_const.SQL_VARCHAR.value,
ddbc_sql_const.SQL_C_CHAR.value,
len(parameters_list[i]),
0,
False,
)
else:
# fallback to generic numeric binding
parameters_list[i] = self._get_numeric_data(param)
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):
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):
if (
param.startswith("POINT")
or param.startswith("LINESTRING")
or param.startswith("POLYGON")
):
return (
ddbc_sql_const.SQL_WVARCHAR.value,
ddbc_sql_const.SQL_C_WCHAR.value,
len(param),
0,
False,
)
try:
val = uuid.UUID(param)
parameters_list[i] = val.bytes_le
return (
ddbc_sql_const.SQL_GUID.value,
ddbc_sql_const.SQL_C_GUID.value,
16,
0,
False
)
except ValueError:
pass
# 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)
if utf16_len > MAX_INLINE_CHAR: # Long strings -> DAE
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
)
else: # 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,
)
else:
# 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()
def _allocate_statement_handle(self):
"""
Allocate the DDBC statement handle.
"""
self.hstmt = self._connection._conn.alloc_statement_handle()
def _reset_cursor(self) -> None:
"""
Reset the DDBC statement handle.
"""
if self.hstmt:
self.hstmt.free()
self.hstmt = None
log('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:
log('warning', "Error removing cursor from connection tracking: %s", e)
if self.hstmt:
self.hstmt.free()
self.hstmt = None
log('debug', "SQLFreeHandle succeeded")
self._clear_rownumber()
self.closed = True
def _check_closed(self):
"""
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 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}. 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):
"""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_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,
}
return sql_to_c_type.get(sql_type, ddbc_sql_const.SQL_C_DEFAULT.value)
def _create_parameter_types_list(self, parameter, param_info, parameters_list, i, min_val=None, max_val=None):
"""
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=None):
"""Initialize the description attribute from column metadata."""
if not column_metadata:
self.description = None
return
description = []
for i, 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
def _map_data_type(self, sql_type):
"""
Map SQL data type to Python data type.
Args:
sql_type: SQL data type.
Returns:
Corresponding Python data type.
"""
sql_to_python_type = {
ddbc_sql_const.SQL_INTEGER.value: int,
ddbc_sql_const.SQL_VARCHAR.value: str,
ddbc_sql_const.SQL_WVARCHAR.value: str,
ddbc_sql_const.SQL_CHAR.value: str,
ddbc_sql_const.SQL_WCHAR.value: str,
ddbc_sql_const.SQL_FLOAT.value: float,
ddbc_sql_const.SQL_DOUBLE.value: float,
ddbc_sql_const.SQL_DECIMAL.value: decimal.Decimal,
ddbc_sql_const.SQL_NUMERIC.value: decimal.Decimal,
ddbc_sql_const.SQL_DATE.value: datetime.date,
ddbc_sql_const.SQL_TIMESTAMP.value: datetime.datetime,
ddbc_sql_const.SQL_TIME.value: datetime.time,
ddbc_sql_const.SQL_BIT.value: bool,
ddbc_sql_const.SQL_TINYINT.value: int,
ddbc_sql_const.SQL_SMALLINT.value: int,
ddbc_sql_const.SQL_BIGINT.value: int,
ddbc_sql_const.SQL_BINARY.value: bytes,
ddbc_sql_const.SQL_VARBINARY.value: bytes,
ddbc_sql_const.SQL_LONGVARBINARY.value: bytes,
ddbc_sql_const.SQL_GUID.value: uuid.UUID,
# Add more mappings as needed
}
return sql_to_python_type.get(sql_type, str)
@property
def rownumber(self):
"""
DB-API extension: Current 0-based index of the cursor in the result set.
Returns:
int or None: The current 0-based index of the cursor in the result set,
or None if no row has been fetched yet or the index cannot be determined.
Note:
- Returns -1 before the first successful fetch
- Returns 0 after fetching the first row
- Returns -1 for empty result sets (since no rows can be fetched)
Warning:
This is a DB-API extension and may not be portable across different
database modules.
"""
# Use mssql_python logging system instead of standard warnings
log('warning', "DB-API extension cursor.rownumber used")
# Return None if cursor is closed or no result set is available
if self.closed or not self._has_result_set:
return -1
return self._rownumber # Will be None until first fetch, then 0, 1, 2, etc.
@property
def connection(self):
"""
DB-API 2.0 attribute: Connection object that created this cursor.
This is a read-only reference to the Connection object that was used to create
this cursor. This attribute is useful for polymorphic code that needs access
to connection-level functionality.
Returns:
Connection: The connection object that created this cursor.
Note:
This attribute is read-only as specified by DB-API 2.0. Attempting to
assign to this attribute will raise an AttributeError.
"""
return self._connection
def _reset_rownumber(self):
"""Reset the rownumber tracking when starting a new result set."""
self._rownumber = -1
self._next_row_index = 0
self._has_result_set = True
self._skip_increment_for_next_fetch = False
def _increment_rownumber(self):
"""
Called after a successful fetch from the driver. Keep both counters consistent.
"""
if self._has_result_set:
# driver returned one row, so the next row index increments by 1
self._next_row_index += 1
# rownumber is last returned row index
self._rownumber = self._next_row_index - 1
else:
raise InterfaceError("Cannot increment rownumber: no active result set.", "No active result set.")
# Will be used when we add support for scrollable cursors
def _decrement_rownumber(self):
"""
Decrement the rownumber by 1.
This could be used for error recovery or cursor positioning operations.
"""
if self._has_result_set and self._rownumber >= 0:
if self._rownumber > 0:
self._rownumber -= 1
else:
self._rownumber = -1
else:
raise InterfaceError("Cannot decrement rownumber: no active result set.", "No active result set.")
def _clear_rownumber(self):
"""
Clear the rownumber tracking.
This should be called when the result set is cleared or when the cursor is reset.
"""
self._rownumber = -1
self._has_result_set = False
self._skip_increment_for_next_fetch = False
def __iter__(self):
"""
Return the cursor itself as an iterator.
This allows direct iteration over the cursor after execute():
for row in cursor.execute("SELECT * FROM table"):
print(row)
"""
self._check_closed()
return self
def __next__(self):
"""
Fetch the next row when iterating over the cursor.
Returns:
The next Row object.
Raises:
StopIteration: When no more rows are available.
"""
self._check_closed()
row = self.fetchone()
if row is None:
raise StopIteration
return row
def next(self):
"""
Fetch the next row from the cursor.
This is an alias for __next__() to maintain compatibility with older code.
Returns:
The next Row object.
Raises:
StopIteration: When no more rows are available.
"""
return self.__next__()
def _buffer_intermediate_results(self):
"""
Buffer intermediate results automatically.
This method skips "rows affected" messages and empty result sets,
positioning the cursor on the first meaningful result set that contains
actual data. This eliminates the need for SET NOCOUNT ON detection.
"""
try:
# Keep advancing through result sets until we find one with actual data
# or reach the end
while True:
# Check if current result set has actual columns/data
if self.description and len(self.description) > 0:
# We have a meaningful result set with columns, stop here
break
# Try to advance to next result set
try:
ret = ddbc_bindings.DDBCSQLMoreResults(self.hstmt)
# If no more result sets, we're done
if ret == ddbc_sql_const.SQL_NO_DATA.value:
break
# Check for errors
check_error(ddbc_sql_const.SQL_HANDLE_STMT.value, self.hstmt, ret)
# Update description for the new result set
column_metadata = []
try:
ddbc_bindings.DDBCSQLDescribeCol(self.hstmt, column_metadata)
self._initialize_description(column_metadata)
except Exception:
# If describe fails, it's likely there are no results (e.g., for INSERT)
self.description = None
except Exception:
# If we can't advance further, stop
break
except Exception as e:
log('warning', "Exception occurred during `_buffer_intermediate_results` %s", e)
# If anything goes wrong during buffering, continue with current state
# This ensures we don't break existing functionality
pass
def execute(
self,
operation: str,
*parameters,
use_prepare: bool = True,
reset_cursor: bool = True
) -> 'Cursor':
"""
Prepare and execute a database operation (query or command).
Args:
operation: SQL query or command.
parameters: Sequence of parameters to bind.
use_prepare: Whether to use SQLPrepareW (default) or SQLExecDirectW.
reset_cursor: Whether to reset the cursor before execution.
"""
# Restore original fetch methods if they exist
if hasattr(self, '_original_fetchone'):
self.fetchone = self._original_fetchone
self.fetchmany = self._original_fetchmany
self.fetchall = self._original_fetchall
del self._original_fetchone
del self._original_fetchmany
del self._original_fetchall
self._check_closed() # Check if the cursor is closed
if reset_cursor:
self._reset_cursor()
# Clear any previous messages
self.messages = []
# Apply timeout if set (non-zero)
if self._timeout > 0:
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)
log('debug', f"Set query timeout to {timeout_value} seconds")
except Exception as e:
log('warning', f"Failed to set query timeout: {e}")
param_info = ddbc_bindings.ParamInfo
parameters_type = []
# Flatten parameters if a single tuple or list is passed
if len(parameters) == 1 and isinstance(parameters[0], (tuple, list)):
parameters = parameters[0]
parameters = list(parameters)
# Validate that inputsizes matches parameter count if both are present
if parameters and self._inputsizes:
if len(self._inputsizes) != len(parameters):
warnings.warn(
f"Number of input sizes ({len(self._inputsizes)}) does not match "
f"number of parameters ({len(parameters)}). This may lead to unexpected behavior.",
Warning
)
if parameters: