sqlspec 0.9.0__py3-none-any.whl → 0.9.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of sqlspec might be problematic. Click here for more details.

sqlspec/base.py CHANGED
@@ -1,7 +1,9 @@
1
1
  # ruff: noqa: PLR6301
2
+ import atexit
3
+ import contextlib
2
4
  import re
3
5
  from abc import ABC, abstractmethod
4
- from collections.abc import Awaitable
6
+ from collections.abc import Awaitable, Sequence
5
7
  from dataclasses import dataclass, field
6
8
  from typing import (
7
9
  TYPE_CHECKING,
@@ -19,6 +21,7 @@ from typing import (
19
21
  from sqlspec.exceptions import NotFoundError
20
22
  from sqlspec.statement import SQLStatement
21
23
  from sqlspec.typing import ModelDTOT, StatementParameterType
24
+ from sqlspec.utils.sync_tools import maybe_async_
22
25
 
23
26
  if TYPE_CHECKING:
24
27
  from contextlib import AbstractAsyncContextManager, AbstractContextManager
@@ -202,6 +205,15 @@ class SQLSpec:
202
205
 
203
206
  def __init__(self) -> None:
204
207
  self._configs: dict[Any, DatabaseConfigProtocol[Any, Any, Any]] = {}
208
+ # Register the cleanup handler to run at program exit
209
+ atexit.register(self._cleanup_pools)
210
+
211
+ def _cleanup_pools(self) -> None:
212
+ """Clean up all open database pools at program exit."""
213
+ for config in self._configs.values():
214
+ if config.support_connection_pooling and config.pool_instance is not None:
215
+ with contextlib.suppress(Exception):
216
+ maybe_async_(config.close_pool)()
205
217
 
206
218
  @overload
207
219
  def add_config(self, config: "SyncConfigT") -> "type[SyncConfigT]": ...
@@ -284,6 +296,24 @@ class SQLSpec:
284
296
  config = self.get_config(name)
285
297
  return config.create_connection()
286
298
 
299
+ @overload
300
+ def get_session(
301
+ self,
302
+ name: Union[
303
+ "type[NoPoolSyncConfig[ConnectionT, DriverT]]",
304
+ "type[SyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
305
+ ],
306
+ ) -> "DriverT": ...
307
+
308
+ @overload
309
+ def get_session(
310
+ self,
311
+ name: Union[
312
+ "type[NoPoolAsyncConfig[ConnectionT, DriverT]]",
313
+ "type[AsyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
314
+ ],
315
+ ) -> "Awaitable[DriverT]": ...
316
+
287
317
  def get_session(
288
318
  self,
289
319
  name: Union[
@@ -311,6 +341,28 @@ class SQLSpec:
311
341
  return _create_session()
312
342
  return cast("DriverT", config.driver_type(connection)) # pyright: ignore
313
343
 
344
+ @overload
345
+ def provide_connection(
346
+ self,
347
+ name: Union[
348
+ "type[NoPoolSyncConfig[ConnectionT, DriverT]]",
349
+ "type[SyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
350
+ ],
351
+ *args: Any,
352
+ **kwargs: Any,
353
+ ) -> "AbstractContextManager[ConnectionT]": ...
354
+
355
+ @overload
356
+ def provide_connection(
357
+ self,
358
+ name: Union[
359
+ "type[NoPoolAsyncConfig[ConnectionT, DriverT]]",
360
+ "type[AsyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
361
+ ],
362
+ *args: Any,
363
+ **kwargs: Any,
364
+ ) -> "AbstractAsyncContextManager[ConnectionT]": ...
365
+
314
366
  def provide_connection(
315
367
  self,
316
368
  name: Union[
@@ -335,6 +387,28 @@ class SQLSpec:
335
387
  config = self.get_config(name)
336
388
  return config.provide_connection(*args, **kwargs)
337
389
 
390
+ @overload
391
+ def provide_session(
392
+ self,
393
+ name: Union[
394
+ "type[NoPoolSyncConfig[ConnectionT, DriverT]]",
395
+ "type[SyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
396
+ ],
397
+ *args: Any,
398
+ **kwargs: Any,
399
+ ) -> "AbstractContextManager[DriverT]": ...
400
+
401
+ @overload
402
+ def provide_session(
403
+ self,
404
+ name: Union[
405
+ "type[NoPoolAsyncConfig[ConnectionT, DriverT]]",
406
+ "type[AsyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
407
+ ],
408
+ *args: Any,
409
+ **kwargs: Any,
410
+ ) -> "AbstractAsyncContextManager[DriverT]": ...
411
+
338
412
  def provide_session(
339
413
  self,
340
414
  name: Union[
@@ -393,6 +467,24 @@ class SQLSpec:
393
467
  return cast("Union[type[PoolT], Awaitable[type[PoolT]]]", config.create_pool())
394
468
  return None
395
469
 
470
+ @overload
471
+ def close_pool(
472
+ self,
473
+ name: Union[
474
+ "type[NoPoolSyncConfig[ConnectionT, DriverT]]",
475
+ "type[SyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
476
+ ],
477
+ ) -> "None": ...
478
+
479
+ @overload
480
+ def close_pool(
481
+ self,
482
+ name: Union[
483
+ "type[NoPoolAsyncConfig[ConnectionT, DriverT]]",
484
+ "type[AsyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
485
+ ],
486
+ ) -> "Awaitable[None]": ...
487
+
396
488
  def close_pool(
397
489
  self,
398
490
  name: Union[
@@ -501,6 +593,32 @@ class SyncDriverAdapterProtocol(CommonDriverAttributes[ConnectionT], ABC, Generi
501
593
  def __init__(self, connection: "ConnectionT", **kwargs: Any) -> None:
502
594
  self.connection = connection
503
595
 
596
+ @overload
597
+ @abstractmethod
598
+ def select(
599
+ self,
600
+ sql: str,
601
+ parameters: "Optional[StatementParameterType]" = None,
602
+ /,
603
+ *,
604
+ connection: "Optional[ConnectionT]" = None,
605
+ schema_type: None = None,
606
+ **kwargs: Any,
607
+ ) -> "Sequence[dict[str, Any]]": ...
608
+
609
+ @overload
610
+ @abstractmethod
611
+ def select(
612
+ self,
613
+ sql: str,
614
+ parameters: "Optional[StatementParameterType]" = None,
615
+ /,
616
+ *,
617
+ connection: "Optional[ConnectionT]" = None,
618
+ schema_type: "type[ModelDTOT]",
619
+ **kwargs: Any,
620
+ ) -> "Sequence[ModelDTOT]": ...
621
+
504
622
  @abstractmethod
505
623
  def select(
506
624
  self,
@@ -511,7 +629,33 @@ class SyncDriverAdapterProtocol(CommonDriverAttributes[ConnectionT], ABC, Generi
511
629
  connection: "Optional[ConnectionT]" = None,
512
630
  schema_type: Optional[type[ModelDTOT]] = None,
513
631
  **kwargs: Any,
514
- ) -> "list[Union[ModelDTOT, dict[str, Any]]]": ...
632
+ ) -> "Sequence[Union[ModelDTOT, dict[str, Any]]]": ...
633
+
634
+ @overload
635
+ @abstractmethod
636
+ def select_one(
637
+ self,
638
+ sql: str,
639
+ parameters: "Optional[StatementParameterType]" = None,
640
+ /,
641
+ *,
642
+ connection: "Optional[ConnectionT]" = None,
643
+ schema_type: None = None,
644
+ **kwargs: Any,
645
+ ) -> "dict[str, Any]": ...
646
+
647
+ @overload
648
+ @abstractmethod
649
+ def select_one(
650
+ self,
651
+ sql: str,
652
+ parameters: "Optional[StatementParameterType]" = None,
653
+ /,
654
+ *,
655
+ connection: "Optional[ConnectionT]" = None,
656
+ schema_type: "type[ModelDTOT]",
657
+ **kwargs: Any,
658
+ ) -> "ModelDTOT": ...
515
659
 
516
660
  @abstractmethod
517
661
  def select_one(
@@ -525,6 +669,32 @@ class SyncDriverAdapterProtocol(CommonDriverAttributes[ConnectionT], ABC, Generi
525
669
  **kwargs: Any,
526
670
  ) -> "Union[ModelDTOT, dict[str, Any]]": ...
527
671
 
672
+ @overload
673
+ @abstractmethod
674
+ def select_one_or_none(
675
+ self,
676
+ sql: str,
677
+ parameters: "Optional[StatementParameterType]" = None,
678
+ /,
679
+ *,
680
+ connection: "Optional[ConnectionT]" = None,
681
+ schema_type: None = None,
682
+ **kwargs: Any,
683
+ ) -> "Optional[dict[str, Any]]": ...
684
+
685
+ @overload
686
+ @abstractmethod
687
+ def select_one_or_none(
688
+ self,
689
+ sql: str,
690
+ parameters: "Optional[StatementParameterType]" = None,
691
+ /,
692
+ *,
693
+ connection: "Optional[ConnectionT]" = None,
694
+ schema_type: "type[ModelDTOT]",
695
+ **kwargs: Any,
696
+ ) -> "Optional[ModelDTOT]": ...
697
+
528
698
  @abstractmethod
529
699
  def select_one_or_none(
530
700
  self,
@@ -537,6 +707,32 @@ class SyncDriverAdapterProtocol(CommonDriverAttributes[ConnectionT], ABC, Generi
537
707
  **kwargs: Any,
538
708
  ) -> "Optional[Union[ModelDTOT, dict[str, Any]]]": ...
539
709
 
710
+ @overload
711
+ @abstractmethod
712
+ def select_value(
713
+ self,
714
+ sql: str,
715
+ parameters: "Optional[StatementParameterType]" = None,
716
+ /,
717
+ *,
718
+ connection: "Optional[ConnectionT]" = None,
719
+ schema_type: None = None,
720
+ **kwargs: Any,
721
+ ) -> "Any": ...
722
+
723
+ @overload
724
+ @abstractmethod
725
+ def select_value(
726
+ self,
727
+ sql: str,
728
+ parameters: "Optional[StatementParameterType]" = None,
729
+ /,
730
+ *,
731
+ connection: "Optional[ConnectionT]" = None,
732
+ schema_type: "type[T]",
733
+ **kwargs: Any,
734
+ ) -> "T": ...
735
+
540
736
  @abstractmethod
541
737
  def select_value(
542
738
  self,
@@ -547,7 +743,33 @@ class SyncDriverAdapterProtocol(CommonDriverAttributes[ConnectionT], ABC, Generi
547
743
  connection: Optional[ConnectionT] = None,
548
744
  schema_type: Optional[type[T]] = None,
549
745
  **kwargs: Any,
550
- ) -> "Union[Any, T]": ...
746
+ ) -> "Union[T, Any]": ...
747
+
748
+ @overload
749
+ @abstractmethod
750
+ def select_value_or_none(
751
+ self,
752
+ sql: str,
753
+ parameters: "Optional[StatementParameterType]" = None,
754
+ /,
755
+ *,
756
+ connection: "Optional[ConnectionT]" = None,
757
+ schema_type: None = None,
758
+ **kwargs: Any,
759
+ ) -> "Optional[Any]": ...
760
+
761
+ @overload
762
+ @abstractmethod
763
+ def select_value_or_none(
764
+ self,
765
+ sql: str,
766
+ parameters: "Optional[StatementParameterType]" = None,
767
+ /,
768
+ *,
769
+ connection: "Optional[ConnectionT]" = None,
770
+ schema_type: "type[T]",
771
+ **kwargs: Any,
772
+ ) -> "Optional[T]": ...
551
773
 
552
774
  @abstractmethod
553
775
  def select_value_or_none(
@@ -559,7 +781,7 @@ class SyncDriverAdapterProtocol(CommonDriverAttributes[ConnectionT], ABC, Generi
559
781
  connection: Optional[ConnectionT] = None,
560
782
  schema_type: Optional[type[T]] = None,
561
783
  **kwargs: Any,
562
- ) -> "Optional[Union[Any, T]]": ...
784
+ ) -> "Optional[Union[T, Any]]": ...
563
785
 
564
786
  @abstractmethod
565
787
  def insert_update_delete(
@@ -572,6 +794,32 @@ class SyncDriverAdapterProtocol(CommonDriverAttributes[ConnectionT], ABC, Generi
572
794
  **kwargs: Any,
573
795
  ) -> int: ...
574
796
 
797
+ @overload
798
+ @abstractmethod
799
+ def insert_update_delete_returning(
800
+ self,
801
+ sql: str,
802
+ parameters: "Optional[StatementParameterType]" = None,
803
+ /,
804
+ *,
805
+ connection: "Optional[ConnectionT]" = None,
806
+ schema_type: None = None,
807
+ **kwargs: Any,
808
+ ) -> "dict[str, Any]": ...
809
+
810
+ @overload
811
+ @abstractmethod
812
+ def insert_update_delete_returning(
813
+ self,
814
+ sql: str,
815
+ parameters: "Optional[StatementParameterType]" = None,
816
+ /,
817
+ *,
818
+ connection: "Optional[ConnectionT]" = None,
819
+ schema_type: "type[ModelDTOT]",
820
+ **kwargs: Any,
821
+ ) -> "ModelDTOT": ...
822
+
575
823
  @abstractmethod
576
824
  def insert_update_delete_returning(
577
825
  self,
@@ -582,7 +830,7 @@ class SyncDriverAdapterProtocol(CommonDriverAttributes[ConnectionT], ABC, Generi
582
830
  connection: Optional[ConnectionT] = None,
583
831
  schema_type: Optional[type[ModelDTOT]] = None,
584
832
  **kwargs: Any,
585
- ) -> "Optional[Union[dict[str, Any], ModelDTOT]]": ...
833
+ ) -> "Union[ModelDTOT, dict[str, Any]]": ...
586
834
 
587
835
  @abstractmethod
588
836
  def execute_script(
@@ -631,6 +879,32 @@ class AsyncDriverAdapterProtocol(CommonDriverAttributes[ConnectionT], ABC, Gener
631
879
  def __init__(self, connection: "ConnectionT") -> None:
632
880
  self.connection = connection
633
881
 
882
+ @overload
883
+ @abstractmethod
884
+ async def select(
885
+ self,
886
+ sql: str,
887
+ parameters: "Optional[StatementParameterType]" = None,
888
+ /,
889
+ *,
890
+ connection: "Optional[ConnectionT]" = None,
891
+ schema_type: None = None,
892
+ **kwargs: Any,
893
+ ) -> "Sequence[dict[str, Any]]": ...
894
+
895
+ @overload
896
+ @abstractmethod
897
+ async def select(
898
+ self,
899
+ sql: str,
900
+ parameters: "Optional[StatementParameterType]" = None,
901
+ /,
902
+ *,
903
+ connection: "Optional[ConnectionT]" = None,
904
+ schema_type: "type[ModelDTOT]",
905
+ **kwargs: Any,
906
+ ) -> "Sequence[ModelDTOT]": ...
907
+
634
908
  @abstractmethod
635
909
  async def select(
636
910
  self,
@@ -641,7 +915,33 @@ class AsyncDriverAdapterProtocol(CommonDriverAttributes[ConnectionT], ABC, Gener
641
915
  connection: "Optional[ConnectionT]" = None,
642
916
  schema_type: "Optional[type[ModelDTOT]]" = None,
643
917
  **kwargs: Any,
644
- ) -> "list[Union[ModelDTOT, dict[str, Any]]]": ...
918
+ ) -> "Sequence[Union[ModelDTOT, dict[str, Any]]]": ...
919
+
920
+ @overload
921
+ @abstractmethod
922
+ async def select_one(
923
+ self,
924
+ sql: str,
925
+ parameters: "Optional[StatementParameterType]" = None,
926
+ /,
927
+ *,
928
+ connection: "Optional[ConnectionT]" = None,
929
+ schema_type: None = None,
930
+ **kwargs: Any,
931
+ ) -> "dict[str, Any]": ...
932
+
933
+ @overload
934
+ @abstractmethod
935
+ async def select_one(
936
+ self,
937
+ sql: str,
938
+ parameters: "Optional[StatementParameterType]" = None,
939
+ /,
940
+ *,
941
+ connection: "Optional[ConnectionT]" = None,
942
+ schema_type: "type[ModelDTOT]",
943
+ **kwargs: Any,
944
+ ) -> "ModelDTOT": ...
645
945
 
646
946
  @abstractmethod
647
947
  async def select_one(
@@ -655,6 +955,32 @@ class AsyncDriverAdapterProtocol(CommonDriverAttributes[ConnectionT], ABC, Gener
655
955
  **kwargs: Any,
656
956
  ) -> "Union[ModelDTOT, dict[str, Any]]": ...
657
957
 
958
+ @overload
959
+ @abstractmethod
960
+ async def select_one_or_none(
961
+ self,
962
+ sql: str,
963
+ parameters: "Optional[StatementParameterType]" = None,
964
+ /,
965
+ *,
966
+ connection: "Optional[ConnectionT]" = None,
967
+ schema_type: None = None,
968
+ **kwargs: Any,
969
+ ) -> "Optional[dict[str, Any]]": ...
970
+
971
+ @overload
972
+ @abstractmethod
973
+ async def select_one_or_none(
974
+ self,
975
+ sql: str,
976
+ parameters: "Optional[StatementParameterType]" = None,
977
+ /,
978
+ *,
979
+ connection: "Optional[ConnectionT]" = None,
980
+ schema_type: "type[ModelDTOT]",
981
+ **kwargs: Any,
982
+ ) -> "Optional[ModelDTOT]": ...
983
+
658
984
  @abstractmethod
659
985
  async def select_one_or_none(
660
986
  self,
@@ -667,6 +993,32 @@ class AsyncDriverAdapterProtocol(CommonDriverAttributes[ConnectionT], ABC, Gener
667
993
  **kwargs: Any,
668
994
  ) -> "Optional[Union[ModelDTOT, dict[str, Any]]]": ...
669
995
 
996
+ @overload
997
+ @abstractmethod
998
+ async def select_value(
999
+ self,
1000
+ sql: str,
1001
+ parameters: "Optional[StatementParameterType]" = None,
1002
+ /,
1003
+ *,
1004
+ connection: "Optional[ConnectionT]" = None,
1005
+ schema_type: None = None,
1006
+ **kwargs: Any,
1007
+ ) -> "Any": ...
1008
+
1009
+ @overload
1010
+ @abstractmethod
1011
+ async def select_value(
1012
+ self,
1013
+ sql: str,
1014
+ parameters: "Optional[StatementParameterType]" = None,
1015
+ /,
1016
+ *,
1017
+ connection: "Optional[ConnectionT]" = None,
1018
+ schema_type: "type[T]",
1019
+ **kwargs: Any,
1020
+ ) -> "T": ...
1021
+
670
1022
  @abstractmethod
671
1023
  async def select_value(
672
1024
  self,
@@ -677,7 +1029,33 @@ class AsyncDriverAdapterProtocol(CommonDriverAttributes[ConnectionT], ABC, Gener
677
1029
  connection: "Optional[ConnectionT]" = None,
678
1030
  schema_type: "Optional[type[T]]" = None,
679
1031
  **kwargs: Any,
680
- ) -> "Union[Any, T]": ...
1032
+ ) -> "Union[T, Any]": ...
1033
+
1034
+ @overload
1035
+ @abstractmethod
1036
+ async def select_value_or_none(
1037
+ self,
1038
+ sql: str,
1039
+ parameters: "Optional[StatementParameterType]" = None,
1040
+ /,
1041
+ *,
1042
+ connection: "Optional[ConnectionT]" = None,
1043
+ schema_type: None = None,
1044
+ **kwargs: Any,
1045
+ ) -> "Optional[Any]": ...
1046
+
1047
+ @overload
1048
+ @abstractmethod
1049
+ async def select_value_or_none(
1050
+ self,
1051
+ sql: str,
1052
+ parameters: "Optional[StatementParameterType]" = None,
1053
+ /,
1054
+ *,
1055
+ connection: "Optional[ConnectionT]" = None,
1056
+ schema_type: "type[T]",
1057
+ **kwargs: Any,
1058
+ ) -> "Optional[T]": ...
681
1059
 
682
1060
  @abstractmethod
683
1061
  async def select_value_or_none(
@@ -689,7 +1067,7 @@ class AsyncDriverAdapterProtocol(CommonDriverAttributes[ConnectionT], ABC, Gener
689
1067
  connection: "Optional[ConnectionT]" = None,
690
1068
  schema_type: "Optional[type[T]]" = None,
691
1069
  **kwargs: Any,
692
- ) -> "Optional[Union[Any, T]]": ...
1070
+ ) -> "Optional[Union[T, Any]]": ...
693
1071
 
694
1072
  @abstractmethod
695
1073
  async def insert_update_delete(
@@ -702,6 +1080,32 @@ class AsyncDriverAdapterProtocol(CommonDriverAttributes[ConnectionT], ABC, Gener
702
1080
  **kwargs: Any,
703
1081
  ) -> int: ...
704
1082
 
1083
+ @overload
1084
+ @abstractmethod
1085
+ async def insert_update_delete_returning(
1086
+ self,
1087
+ sql: str,
1088
+ parameters: "Optional[StatementParameterType]" = None,
1089
+ /,
1090
+ *,
1091
+ connection: "Optional[ConnectionT]" = None,
1092
+ schema_type: None = None,
1093
+ **kwargs: Any,
1094
+ ) -> "dict[str, Any]": ...
1095
+
1096
+ @overload
1097
+ @abstractmethod
1098
+ async def insert_update_delete_returning(
1099
+ self,
1100
+ sql: str,
1101
+ parameters: "Optional[StatementParameterType]" = None,
1102
+ /,
1103
+ *,
1104
+ connection: "Optional[ConnectionT]" = None,
1105
+ schema_type: "type[ModelDTOT]",
1106
+ **kwargs: Any,
1107
+ ) -> "ModelDTOT": ...
1108
+
705
1109
  @abstractmethod
706
1110
  async def insert_update_delete_returning(
707
1111
  self,
@@ -712,7 +1116,7 @@ class AsyncDriverAdapterProtocol(CommonDriverAttributes[ConnectionT], ABC, Gener
712
1116
  connection: "Optional[ConnectionT]" = None,
713
1117
  schema_type: "Optional[type[ModelDTOT]]" = None,
714
1118
  **kwargs: Any,
715
- ) -> "Optional[Union[dict[str, Any], ModelDTOT]]": ...
1119
+ ) -> "Union[ModelDTOT, dict[str, Any]]": ...
716
1120
 
717
1121
  @abstractmethod
718
1122
  async def execute_script(
@@ -7,6 +7,7 @@ from sqlspec.base import (
7
7
  AsyncConfigT,
8
8
  ConnectionT,
9
9
  DatabaseConfigProtocol,
10
+ DriverT,
10
11
  PoolT,
11
12
  SyncConfigT,
12
13
  )
@@ -75,6 +76,7 @@ class SQLSpec(InitPluginProtocol, SQLSpecBase):
75
76
  SQLSpec,
76
77
  ConnectionT,
77
78
  PoolT,
79
+ DriverT,
78
80
  DatabaseConfig,
79
81
  DatabaseConfigProtocol,
80
82
  SyncConfigT,