1
1
import contextlib
2
2
import logging
3
3
import re
4
- from collections .abc import Generator , Sequence
4
+ from collections .abc import Generator , Mapping , Sequence
5
5
from contextlib import contextmanager
6
6
from typing import TYPE_CHECKING , Any , ClassVar , Optional , Union , cast , overload
7
7
8
8
from adbc_driver_manager .dbapi import Connection , Cursor
9
9
from sqlglot import exp as sqlglot_exp
10
10
11
11
from sqlspec .base import SyncDriverAdapterProtocol
12
- from sqlspec .exceptions import ParameterStyleMismatchError , SQLParsingError
12
+ from sqlspec .exceptions import SQLParsingError
13
13
from sqlspec .filters import StatementFilter
14
14
from sqlspec .mixins import ResultConverter , SQLTranslatorMixin , SyncArrowBulkOperationsMixin
15
15
from sqlspec .statement import SQLStatement
@@ -91,7 +91,6 @@ def _process_sql_params( # noqa: C901, PLR0912, PLR0915
91
91
self ,
92
92
sql : str ,
93
93
parameters : "Optional[StatementParameterType]" = None ,
94
- / ,
95
94
* filters : "StatementFilter" ,
96
95
** kwargs : Any ,
97
96
) -> "tuple[str, Optional[tuple[Any, ...]]]" : # Always returns tuple or None for params
@@ -108,14 +107,24 @@ def _process_sql_params( # noqa: C901, PLR0912, PLR0915
108
107
**kwargs: Additional keyword arguments.
109
108
110
109
Raises:
111
- ParameterStyleMismatchError: If positional parameters are mixed with keyword arguments.
112
110
SQLParsingError: If the SQL statement cannot be parsed.
113
111
114
112
Returns:
115
113
A tuple of (sql, parameters) ready for execution.
116
114
"""
115
+ passed_parameters : Optional [Union [Mapping [str , Any ], Sequence [Any ]]] = None
116
+ combined_filters_list : list [StatementFilter ] = list (filters )
117
+
118
+ if parameters is not None :
119
+ if isinstance (parameters , StatementFilter ):
120
+ combined_filters_list .insert (0 , parameters )
121
+ # passed_parameters remains None
122
+ else :
123
+ # If parameters is not a StatementFilter, it's actual data parameters.
124
+ passed_parameters = parameters
125
+
117
126
# Special handling for SQLite with non-dict parameters and named placeholders
118
- if self .dialect == "sqlite" and parameters is not None and not is_dict (parameters ):
127
+ if self .dialect == "sqlite" and passed_parameters is not None and not is_dict (passed_parameters ):
119
128
# First mask out comments and strings to avoid detecting parameters in those
120
129
comments = list (SQL_COMMENT_PATTERN .finditer (sql ))
121
130
strings = list (SQL_STRING_PATTERN .finditer (sql ))
@@ -136,26 +145,15 @@ def _process_sql_params( # noqa: C901, PLR0912, PLR0915
136
145
param_positions .sort (reverse = True )
137
146
for start , end in param_positions :
138
147
sql = sql [:start ] + "?" + sql [end :]
139
- if not isinstance (parameters , (list , tuple )):
140
- return sql , ( parameters ,)
141
- return sql , tuple (parameters )
148
+ if not isinstance (passed_parameters , (list , tuple )):
149
+ passed_parameters = ( passed_parameters ,)
150
+ passed_parameters = tuple (passed_parameters )
142
151
143
152
# Standard processing for all other cases
144
- merged_params = parameters
145
- if kwargs :
146
- if is_dict (parameters ):
147
- merged_params = {** parameters , ** kwargs }
148
- elif parameters is not None :
149
- msg = "Cannot mix positional parameters with keyword arguments for adbc driver."
150
- raise ParameterStyleMismatchError (msg )
151
- else :
152
- merged_params = kwargs
153
+ statement = SQLStatement (sql , passed_parameters , kwargs = kwargs , dialect = self .dialect )
153
154
154
- # 2. Create SQLStatement with dialect and process
155
- statement = SQLStatement (sql , merged_params , dialect = self .dialect )
156
-
157
- # Apply any filters
158
- for filter_obj in filters :
155
+ # Apply any filters from combined_filters_list
156
+ for filter_obj in combined_filters_list :
159
157
statement = statement .apply_filter (filter_obj )
160
158
161
159
processed_sql , processed_params , parsed_expr = statement .process ()
@@ -284,7 +282,6 @@ def select(
284
282
self ,
285
283
sql : str ,
286
284
parameters : "Optional[StatementParameterType]" = None ,
287
- / ,
288
285
* filters : "StatementFilter" ,
289
286
connection : "Optional[AdbcConnection]" = None ,
290
287
schema_type : None = None ,
@@ -295,7 +292,6 @@ def select(
295
292
self ,
296
293
sql : str ,
297
294
parameters : "Optional[StatementParameterType]" = None ,
298
- / ,
299
295
* filters : "StatementFilter" ,
300
296
connection : "Optional[AdbcConnection]" = None ,
301
297
schema_type : "type[ModelDTOT]" ,
@@ -305,7 +301,6 @@ def select(
305
301
self ,
306
302
sql : str ,
307
303
parameters : Optional ["StatementParameterType" ] = None ,
308
- / ,
309
304
* filters : "StatementFilter" ,
310
305
connection : Optional ["AdbcConnection" ] = None ,
311
306
schema_type : "Optional[type[ModelDTOT]]" = None ,
@@ -341,7 +336,6 @@ def select_one(
341
336
self ,
342
337
sql : str ,
343
338
parameters : "Optional[StatementParameterType]" = None ,
344
- / ,
345
339
* filters : "StatementFilter" ,
346
340
connection : "Optional[AdbcConnection]" = None ,
347
341
schema_type : None = None ,
@@ -352,7 +346,6 @@ def select_one(
352
346
self ,
353
347
sql : str ,
354
348
parameters : "Optional[StatementParameterType]" = None ,
355
- / ,
356
349
* filters : "StatementFilter" ,
357
350
connection : "Optional[AdbcConnection]" = None ,
358
351
schema_type : "type[ModelDTOT]" ,
@@ -362,7 +355,6 @@ def select_one(
362
355
self ,
363
356
sql : str ,
364
357
parameters : "Optional[StatementParameterType]" = None ,
365
- / ,
366
358
* filters : "StatementFilter" ,
367
359
connection : "Optional[AdbcConnection]" = None ,
368
360
schema_type : "Optional[type[ModelDTOT]]" = None ,
@@ -396,7 +388,6 @@ def select_one_or_none(
396
388
self ,
397
389
sql : str ,
398
390
parameters : "Optional[StatementParameterType]" = None ,
399
- / ,
400
391
* filters : "StatementFilter" ,
401
392
connection : "Optional[AdbcConnection]" = None ,
402
393
schema_type : None = None ,
@@ -407,7 +398,6 @@ def select_one_or_none(
407
398
self ,
408
399
sql : str ,
409
400
parameters : "Optional[StatementParameterType]" = None ,
410
- / ,
411
401
* filters : "StatementFilter" ,
412
402
connection : "Optional[AdbcConnection]" = None ,
413
403
schema_type : "type[ModelDTOT]" ,
@@ -417,7 +407,6 @@ def select_one_or_none(
417
407
self ,
418
408
sql : str ,
419
409
parameters : Optional ["StatementParameterType" ] = None ,
420
- / ,
421
410
* filters : "StatementFilter" ,
422
411
connection : Optional ["AdbcConnection" ] = None ,
423
412
schema_type : "Optional[type[ModelDTOT]]" = None ,
@@ -452,8 +441,7 @@ def select_value(
452
441
self ,
453
442
sql : str ,
454
443
parameters : "Optional[StatementParameterType]" = None ,
455
- / ,
456
- * filters : StatementFilter ,
444
+ * filters : "StatementFilter" ,
457
445
connection : "Optional[AdbcConnection]" = None ,
458
446
schema_type : None = None ,
459
447
** kwargs : Any ,
@@ -463,8 +451,7 @@ def select_value(
463
451
self ,
464
452
sql : str ,
465
453
parameters : "Optional[StatementParameterType]" = None ,
466
- / ,
467
- * filters : StatementFilter ,
454
+ * filters : "StatementFilter" ,
468
455
connection : "Optional[AdbcConnection]" = None ,
469
456
schema_type : "type[T]" ,
470
457
** kwargs : Any ,
@@ -473,8 +460,7 @@ def select_value(
473
460
self ,
474
461
sql : str ,
475
462
parameters : "Optional[StatementParameterType]" = None ,
476
- / ,
477
- * filters : StatementFilter ,
463
+ * filters : "StatementFilter" ,
478
464
connection : "Optional[AdbcConnection]" = None ,
479
465
schema_type : "Optional[type[T]]" = None ,
480
466
** kwargs : Any ,
@@ -508,8 +494,7 @@ def select_value_or_none(
508
494
self ,
509
495
sql : str ,
510
496
parameters : "Optional[StatementParameterType]" = None ,
511
- / ,
512
- * filters : StatementFilter ,
497
+ * filters : "StatementFilter" ,
513
498
connection : "Optional[AdbcConnection]" = None ,
514
499
schema_type : None = None ,
515
500
** kwargs : Any ,
@@ -519,8 +504,7 @@ def select_value_or_none(
519
504
self ,
520
505
sql : str ,
521
506
parameters : "Optional[StatementParameterType]" = None ,
522
- / ,
523
- * filters : StatementFilter ,
507
+ * filters : "StatementFilter" ,
524
508
connection : "Optional[AdbcConnection]" = None ,
525
509
schema_type : "type[T]" ,
526
510
** kwargs : Any ,
@@ -529,8 +513,7 @@ def select_value_or_none(
529
513
self ,
530
514
sql : str ,
531
515
parameters : "Optional[StatementParameterType]" = None ,
532
- / ,
533
- * filters : StatementFilter ,
516
+ * filters : "StatementFilter" ,
534
517
connection : "Optional[AdbcConnection]" = None ,
535
518
schema_type : "Optional[type[T]]" = None ,
536
519
** kwargs : Any ,
@@ -564,22 +547,21 @@ def insert_update_delete(
564
547
self ,
565
548
sql : str ,
566
549
parameters : "Optional[StatementParameterType]" = None ,
567
- / ,
568
550
* filters : "StatementFilter" ,
569
551
connection : "Optional[AdbcConnection]" = None ,
570
552
** kwargs : Any ,
571
553
) -> int :
572
554
"""Execute an insert, update, or delete statement.
573
555
574
556
Args:
575
- sql: The SQL statement to execute .
557
+ sql: The SQL statement string .
576
558
parameters: The parameters for the statement (dict, tuple, list, or None).
577
559
*filters: Statement filters to apply.
578
560
connection: Optional connection override.
579
561
**kwargs: Additional keyword arguments to merge with parameters if parameters is a dict.
580
562
581
563
Returns:
582
- The number of rows affected by the statement .
564
+ Row count affected by the operation .
583
565
"""
584
566
connection = self ._connection (connection )
585
567
sql , parameters = self ._process_sql_params (sql , parameters , * filters , ** kwargs )
@@ -593,8 +575,7 @@ def insert_update_delete_returning(
593
575
self ,
594
576
sql : str ,
595
577
parameters : "Optional[StatementParameterType]" = None ,
596
- / ,
597
- * filters : StatementFilter ,
578
+ * filters : "StatementFilter" ,
598
579
connection : "Optional[AdbcConnection]" = None ,
599
580
schema_type : None = None ,
600
581
** kwargs : Any ,
@@ -604,8 +585,7 @@ def insert_update_delete_returning(
604
585
self ,
605
586
sql : str ,
606
587
parameters : "Optional[StatementParameterType]" = None ,
607
- / ,
608
- * filters : StatementFilter ,
588
+ * filters : "StatementFilter" ,
609
589
connection : "Optional[AdbcConnection]" = None ,
610
590
schema_type : "type[ModelDTOT]" ,
611
591
** kwargs : Any ,
@@ -614,24 +594,23 @@ def insert_update_delete_returning(
614
594
self ,
615
595
sql : str ,
616
596
parameters : "Optional[StatementParameterType]" = None ,
617
- / ,
618
- * filters : StatementFilter ,
597
+ * filters : "StatementFilter" ,
619
598
connection : "Optional[AdbcConnection]" = None ,
620
599
schema_type : "Optional[type[ModelDTOT]]" = None ,
621
600
** kwargs : Any ,
622
601
) -> "Optional[Union[dict[str, Any], ModelDTOT]]" :
623
- """Insert, update, or delete data from the database and return result .
602
+ """Insert, update, or delete data with RETURNING clause .
624
603
625
604
Args:
626
- sql: The SQL statement to execute .
605
+ sql: The SQL statement string .
627
606
parameters: The parameters for the statement (dict, tuple, list, or None).
628
607
*filters: Statement filters to apply.
629
608
connection: Optional connection override.
630
609
schema_type: Optional schema class for the result.
631
610
**kwargs: Additional keyword arguments to merge with parameters if parameters is a dict.
632
611
633
612
Returns:
634
- The first row of results .
613
+ The returned row data, or None if no row returned .
635
614
"""
636
615
connection = self ._connection (connection )
637
616
sql , parameters = self ._process_sql_params (sql , parameters , * filters , ** kwargs )
@@ -648,7 +627,6 @@ def execute_script(
648
627
self ,
649
628
sql : str ,
650
629
parameters : "Optional[StatementParameterType]" = None ,
651
- / ,
652
630
connection : "Optional[AdbcConnection]" = None ,
653
631
** kwargs : Any ,
654
632
) -> str :
@@ -673,12 +651,11 @@ def execute_script(
673
651
674
652
# --- Arrow Bulk Operations ---
675
653
676
- def select_arrow ( # pyright: ignore[reportUnknownParameterType]
654
+ def select_arrow (
677
655
self ,
678
656
sql : str ,
679
657
parameters : "Optional[StatementParameterType]" = None ,
680
- / ,
681
- * filters : StatementFilter ,
658
+ * filters : "StatementFilter" ,
682
659
connection : "Optional[AdbcConnection]" = None ,
683
660
** kwargs : Any ,
684
661
) -> "ArrowTable" : # pyright: ignore[reportUnknownVariableType]
@@ -692,7 +669,7 @@ def select_arrow( # pyright: ignore[reportUnknownParameterType]
692
669
**kwargs: Additional keyword arguments to merge with parameters if parameters is a dict.
693
670
694
671
Returns:
695
- An Apache Arrow Table containing the query results.
672
+ An Arrow Table containing the query results.
696
673
"""
697
674
connection = self ._connection (connection )
698
675
sql , parameters = self ._process_sql_params (sql , parameters , * filters , ** kwargs )
0 commit comments