SQLAlchemy 2.0.36__cp313-cp313-win32.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.
Files changed (273) hide show
  1. SQLAlchemy-2.0.36.dist-info/LICENSE +19 -0
  2. SQLAlchemy-2.0.36.dist-info/METADATA +243 -0
  3. SQLAlchemy-2.0.36.dist-info/RECORD +273 -0
  4. SQLAlchemy-2.0.36.dist-info/WHEEL +5 -0
  5. SQLAlchemy-2.0.36.dist-info/top_level.txt +1 -0
  6. sqlalchemy/__init__.py +294 -0
  7. sqlalchemy/connectors/__init__.py +18 -0
  8. sqlalchemy/connectors/aioodbc.py +174 -0
  9. sqlalchemy/connectors/asyncio.py +213 -0
  10. sqlalchemy/connectors/pyodbc.py +249 -0
  11. sqlalchemy/cyextension/__init__.py +6 -0
  12. sqlalchemy/cyextension/collections.cp313-win32.pyd +0 -0
  13. sqlalchemy/cyextension/collections.pyx +409 -0
  14. sqlalchemy/cyextension/immutabledict.cp313-win32.pyd +0 -0
  15. sqlalchemy/cyextension/immutabledict.pxd +8 -0
  16. sqlalchemy/cyextension/immutabledict.pyx +133 -0
  17. sqlalchemy/cyextension/processors.cp313-win32.pyd +0 -0
  18. sqlalchemy/cyextension/processors.pyx +68 -0
  19. sqlalchemy/cyextension/resultproxy.cp313-win32.pyd +0 -0
  20. sqlalchemy/cyextension/resultproxy.pyx +102 -0
  21. sqlalchemy/cyextension/util.cp313-win32.pyd +0 -0
  22. sqlalchemy/cyextension/util.pyx +91 -0
  23. sqlalchemy/dialects/__init__.py +61 -0
  24. sqlalchemy/dialects/_typing.py +25 -0
  25. sqlalchemy/dialects/mssql/__init__.py +88 -0
  26. sqlalchemy/dialects/mssql/aioodbc.py +64 -0
  27. sqlalchemy/dialects/mssql/base.py +4010 -0
  28. sqlalchemy/dialects/mssql/information_schema.py +254 -0
  29. sqlalchemy/dialects/mssql/json.py +133 -0
  30. sqlalchemy/dialects/mssql/provision.py +162 -0
  31. sqlalchemy/dialects/mssql/pymssql.py +126 -0
  32. sqlalchemy/dialects/mssql/pyodbc.py +745 -0
  33. sqlalchemy/dialects/mysql/__init__.py +101 -0
  34. sqlalchemy/dialects/mysql/aiomysql.py +333 -0
  35. sqlalchemy/dialects/mysql/asyncmy.py +337 -0
  36. sqlalchemy/dialects/mysql/base.py +3494 -0
  37. sqlalchemy/dialects/mysql/cymysql.py +84 -0
  38. sqlalchemy/dialects/mysql/dml.py +219 -0
  39. sqlalchemy/dialects/mysql/enumerated.py +244 -0
  40. sqlalchemy/dialects/mysql/expression.py +141 -0
  41. sqlalchemy/dialects/mysql/json.py +81 -0
  42. sqlalchemy/dialects/mysql/mariadb.py +32 -0
  43. sqlalchemy/dialects/mysql/mariadbconnector.py +277 -0
  44. sqlalchemy/dialects/mysql/mysqlconnector.py +180 -0
  45. sqlalchemy/dialects/mysql/mysqldb.py +303 -0
  46. sqlalchemy/dialects/mysql/provision.py +110 -0
  47. sqlalchemy/dialects/mysql/pymysql.py +137 -0
  48. sqlalchemy/dialects/mysql/pyodbc.py +138 -0
  49. sqlalchemy/dialects/mysql/reflection.py +677 -0
  50. sqlalchemy/dialects/mysql/reserved_words.py +571 -0
  51. sqlalchemy/dialects/mysql/types.py +774 -0
  52. sqlalchemy/dialects/oracle/__init__.py +67 -0
  53. sqlalchemy/dialects/oracle/base.py +3271 -0
  54. sqlalchemy/dialects/oracle/cx_oracle.py +1483 -0
  55. sqlalchemy/dialects/oracle/dictionary.py +507 -0
  56. sqlalchemy/dialects/oracle/oracledb.py +431 -0
  57. sqlalchemy/dialects/oracle/provision.py +220 -0
  58. sqlalchemy/dialects/oracle/types.py +287 -0
  59. sqlalchemy/dialects/postgresql/__init__.py +167 -0
  60. sqlalchemy/dialects/postgresql/_psycopg_common.py +187 -0
  61. sqlalchemy/dialects/postgresql/array.py +425 -0
  62. sqlalchemy/dialects/postgresql/asyncpg.py +1274 -0
  63. sqlalchemy/dialects/postgresql/base.py +5008 -0
  64. sqlalchemy/dialects/postgresql/dml.py +310 -0
  65. sqlalchemy/dialects/postgresql/ext.py +496 -0
  66. sqlalchemy/dialects/postgresql/hstore.py +397 -0
  67. sqlalchemy/dialects/postgresql/json.py +333 -0
  68. sqlalchemy/dialects/postgresql/named_types.py +509 -0
  69. sqlalchemy/dialects/postgresql/operators.py +129 -0
  70. sqlalchemy/dialects/postgresql/pg8000.py +662 -0
  71. sqlalchemy/dialects/postgresql/pg_catalog.py +300 -0
  72. sqlalchemy/dialects/postgresql/provision.py +175 -0
  73. sqlalchemy/dialects/postgresql/psycopg.py +772 -0
  74. sqlalchemy/dialects/postgresql/psycopg2.py +886 -0
  75. sqlalchemy/dialects/postgresql/psycopg2cffi.py +61 -0
  76. sqlalchemy/dialects/postgresql/ranges.py +1029 -0
  77. sqlalchemy/dialects/postgresql/types.py +303 -0
  78. sqlalchemy/dialects/sqlite/__init__.py +57 -0
  79. sqlalchemy/dialects/sqlite/aiosqlite.py +396 -0
  80. sqlalchemy/dialects/sqlite/base.py +2805 -0
  81. sqlalchemy/dialects/sqlite/dml.py +240 -0
  82. sqlalchemy/dialects/sqlite/json.py +92 -0
  83. sqlalchemy/dialects/sqlite/provision.py +198 -0
  84. sqlalchemy/dialects/sqlite/pysqlcipher.py +155 -0
  85. sqlalchemy/dialects/sqlite/pysqlite.py +756 -0
  86. sqlalchemy/dialects/type_migration_guidelines.txt +145 -0
  87. sqlalchemy/engine/__init__.py +62 -0
  88. sqlalchemy/engine/_py_processors.py +136 -0
  89. sqlalchemy/engine/_py_row.py +128 -0
  90. sqlalchemy/engine/_py_util.py +74 -0
  91. sqlalchemy/engine/base.py +3375 -0
  92. sqlalchemy/engine/characteristics.py +155 -0
  93. sqlalchemy/engine/create.py +875 -0
  94. sqlalchemy/engine/cursor.py +2181 -0
  95. sqlalchemy/engine/default.py +2365 -0
  96. sqlalchemy/engine/events.py +951 -0
  97. sqlalchemy/engine/interfaces.py +3403 -0
  98. sqlalchemy/engine/mock.py +131 -0
  99. sqlalchemy/engine/processors.py +61 -0
  100. sqlalchemy/engine/reflection.py +2098 -0
  101. sqlalchemy/engine/result.py +2382 -0
  102. sqlalchemy/engine/row.py +401 -0
  103. sqlalchemy/engine/strategies.py +19 -0
  104. sqlalchemy/engine/url.py +910 -0
  105. sqlalchemy/engine/util.py +167 -0
  106. sqlalchemy/event/__init__.py +25 -0
  107. sqlalchemy/event/api.py +225 -0
  108. sqlalchemy/event/attr.py +655 -0
  109. sqlalchemy/event/base.py +470 -0
  110. sqlalchemy/event/legacy.py +246 -0
  111. sqlalchemy/event/registry.py +386 -0
  112. sqlalchemy/events.py +17 -0
  113. sqlalchemy/exc.py +830 -0
  114. sqlalchemy/ext/__init__.py +11 -0
  115. sqlalchemy/ext/associationproxy.py +2013 -0
  116. sqlalchemy/ext/asyncio/__init__.py +25 -0
  117. sqlalchemy/ext/asyncio/base.py +279 -0
  118. sqlalchemy/ext/asyncio/engine.py +1466 -0
  119. sqlalchemy/ext/asyncio/exc.py +21 -0
  120. sqlalchemy/ext/asyncio/result.py +961 -0
  121. sqlalchemy/ext/asyncio/scoping.py +1614 -0
  122. sqlalchemy/ext/asyncio/session.py +1936 -0
  123. sqlalchemy/ext/automap.py +1691 -0
  124. sqlalchemy/ext/baked.py +574 -0
  125. sqlalchemy/ext/compiler.py +570 -0
  126. sqlalchemy/ext/declarative/__init__.py +65 -0
  127. sqlalchemy/ext/declarative/extensions.py +548 -0
  128. sqlalchemy/ext/horizontal_shard.py +481 -0
  129. sqlalchemy/ext/hybrid.py +1514 -0
  130. sqlalchemy/ext/indexable.py +341 -0
  131. sqlalchemy/ext/instrumentation.py +450 -0
  132. sqlalchemy/ext/mutable.py +1073 -0
  133. sqlalchemy/ext/mypy/__init__.py +6 -0
  134. sqlalchemy/ext/mypy/apply.py +320 -0
  135. sqlalchemy/ext/mypy/decl_class.py +515 -0
  136. sqlalchemy/ext/mypy/infer.py +590 -0
  137. sqlalchemy/ext/mypy/names.py +335 -0
  138. sqlalchemy/ext/mypy/plugin.py +303 -0
  139. sqlalchemy/ext/mypy/util.py +357 -0
  140. sqlalchemy/ext/orderinglist.py +416 -0
  141. sqlalchemy/ext/serializer.py +181 -0
  142. sqlalchemy/future/__init__.py +16 -0
  143. sqlalchemy/future/engine.py +15 -0
  144. sqlalchemy/inspection.py +174 -0
  145. sqlalchemy/log.py +288 -0
  146. sqlalchemy/orm/__init__.py +170 -0
  147. sqlalchemy/orm/_orm_constructors.py +2571 -0
  148. sqlalchemy/orm/_typing.py +179 -0
  149. sqlalchemy/orm/attributes.py +2835 -0
  150. sqlalchemy/orm/base.py +973 -0
  151. sqlalchemy/orm/bulk_persistence.py +2123 -0
  152. sqlalchemy/orm/clsregistry.py +571 -0
  153. sqlalchemy/orm/collections.py +1620 -0
  154. sqlalchemy/orm/context.py +3268 -0
  155. sqlalchemy/orm/decl_api.py +1883 -0
  156. sqlalchemy/orm/decl_base.py +2190 -0
  157. sqlalchemy/orm/dependency.py +1304 -0
  158. sqlalchemy/orm/descriptor_props.py +1076 -0
  159. sqlalchemy/orm/dynamic.py +300 -0
  160. sqlalchemy/orm/evaluator.py +379 -0
  161. sqlalchemy/orm/events.py +3261 -0
  162. sqlalchemy/orm/exc.py +228 -0
  163. sqlalchemy/orm/identity.py +302 -0
  164. sqlalchemy/orm/instrumentation.py +754 -0
  165. sqlalchemy/orm/interfaces.py +1474 -0
  166. sqlalchemy/orm/loading.py +1682 -0
  167. sqlalchemy/orm/mapped_collection.py +557 -0
  168. sqlalchemy/orm/mapper.py +4432 -0
  169. sqlalchemy/orm/path_registry.py +811 -0
  170. sqlalchemy/orm/persistence.py +1782 -0
  171. sqlalchemy/orm/properties.py +886 -0
  172. sqlalchemy/orm/query.py +3396 -0
  173. sqlalchemy/orm/relationships.py +3500 -0
  174. sqlalchemy/orm/scoping.py +2165 -0
  175. sqlalchemy/orm/session.py +5301 -0
  176. sqlalchemy/orm/state.py +1143 -0
  177. sqlalchemy/orm/state_changes.py +198 -0
  178. sqlalchemy/orm/strategies.py +3473 -0
  179. sqlalchemy/orm/strategy_options.py +2569 -0
  180. sqlalchemy/orm/sync.py +164 -0
  181. sqlalchemy/orm/unitofwork.py +796 -0
  182. sqlalchemy/orm/util.py +2424 -0
  183. sqlalchemy/orm/writeonly.py +678 -0
  184. sqlalchemy/pool/__init__.py +44 -0
  185. sqlalchemy/pool/base.py +1515 -0
  186. sqlalchemy/pool/events.py +370 -0
  187. sqlalchemy/pool/impl.py +581 -0
  188. sqlalchemy/py.typed +0 -0
  189. sqlalchemy/schema.py +70 -0
  190. sqlalchemy/sql/__init__.py +145 -0
  191. sqlalchemy/sql/_dml_constructors.py +140 -0
  192. sqlalchemy/sql/_elements_constructors.py +1850 -0
  193. sqlalchemy/sql/_orm_types.py +20 -0
  194. sqlalchemy/sql/_py_util.py +75 -0
  195. sqlalchemy/sql/_selectable_constructors.py +635 -0
  196. sqlalchemy/sql/_typing.py +460 -0
  197. sqlalchemy/sql/annotation.py +585 -0
  198. sqlalchemy/sql/base.py +2185 -0
  199. sqlalchemy/sql/cache_key.py +1057 -0
  200. sqlalchemy/sql/coercions.py +1405 -0
  201. sqlalchemy/sql/compiler.py +7818 -0
  202. sqlalchemy/sql/crud.py +1669 -0
  203. sqlalchemy/sql/ddl.py +1378 -0
  204. sqlalchemy/sql/default_comparator.py +552 -0
  205. sqlalchemy/sql/dml.py +1817 -0
  206. sqlalchemy/sql/elements.py +5499 -0
  207. sqlalchemy/sql/events.py +455 -0
  208. sqlalchemy/sql/expression.py +162 -0
  209. sqlalchemy/sql/functions.py +2055 -0
  210. sqlalchemy/sql/lambdas.py +1449 -0
  211. sqlalchemy/sql/naming.py +212 -0
  212. sqlalchemy/sql/operators.py +2579 -0
  213. sqlalchemy/sql/roles.py +323 -0
  214. sqlalchemy/sql/schema.py +6158 -0
  215. sqlalchemy/sql/selectable.py +7004 -0
  216. sqlalchemy/sql/sqltypes.py +3827 -0
  217. sqlalchemy/sql/traversals.py +1024 -0
  218. sqlalchemy/sql/type_api.py +2339 -0
  219. sqlalchemy/sql/util.py +1486 -0
  220. sqlalchemy/sql/visitors.py +1165 -0
  221. sqlalchemy/testing/__init__.py +96 -0
  222. sqlalchemy/testing/assertions.py +989 -0
  223. sqlalchemy/testing/assertsql.py +516 -0
  224. sqlalchemy/testing/asyncio.py +135 -0
  225. sqlalchemy/testing/config.py +427 -0
  226. sqlalchemy/testing/engines.py +472 -0
  227. sqlalchemy/testing/entities.py +117 -0
  228. sqlalchemy/testing/exclusions.py +435 -0
  229. sqlalchemy/testing/fixtures/__init__.py +28 -0
  230. sqlalchemy/testing/fixtures/base.py +366 -0
  231. sqlalchemy/testing/fixtures/mypy.py +312 -0
  232. sqlalchemy/testing/fixtures/orm.py +227 -0
  233. sqlalchemy/testing/fixtures/sql.py +503 -0
  234. sqlalchemy/testing/pickleable.py +155 -0
  235. sqlalchemy/testing/plugin/__init__.py +6 -0
  236. sqlalchemy/testing/plugin/bootstrap.py +51 -0
  237. sqlalchemy/testing/plugin/plugin_base.py +779 -0
  238. sqlalchemy/testing/plugin/pytestplugin.py +868 -0
  239. sqlalchemy/testing/profiling.py +324 -0
  240. sqlalchemy/testing/provision.py +496 -0
  241. sqlalchemy/testing/requirements.py +1818 -0
  242. sqlalchemy/testing/schema.py +224 -0
  243. sqlalchemy/testing/suite/__init__.py +19 -0
  244. sqlalchemy/testing/suite/test_cte.py +211 -0
  245. sqlalchemy/testing/suite/test_ddl.py +389 -0
  246. sqlalchemy/testing/suite/test_deprecations.py +153 -0
  247. sqlalchemy/testing/suite/test_dialect.py +740 -0
  248. sqlalchemy/testing/suite/test_insert.py +630 -0
  249. sqlalchemy/testing/suite/test_reflection.py +3225 -0
  250. sqlalchemy/testing/suite/test_results.py +502 -0
  251. sqlalchemy/testing/suite/test_rowcount.py +258 -0
  252. sqlalchemy/testing/suite/test_select.py +1999 -0
  253. sqlalchemy/testing/suite/test_sequence.py +317 -0
  254. sqlalchemy/testing/suite/test_types.py +2141 -0
  255. sqlalchemy/testing/suite/test_unicode_ddl.py +189 -0
  256. sqlalchemy/testing/suite/test_update_delete.py +139 -0
  257. sqlalchemy/testing/util.py +537 -0
  258. sqlalchemy/testing/warnings.py +52 -0
  259. sqlalchemy/types.py +76 -0
  260. sqlalchemy/util/__init__.py +160 -0
  261. sqlalchemy/util/_collections.py +715 -0
  262. sqlalchemy/util/_concurrency_py3k.py +288 -0
  263. sqlalchemy/util/_has_cy.py +40 -0
  264. sqlalchemy/util/_py_collections.py +541 -0
  265. sqlalchemy/util/compat.py +301 -0
  266. sqlalchemy/util/concurrency.py +108 -0
  267. sqlalchemy/util/deprecations.py +401 -0
  268. sqlalchemy/util/langhelpers.py +2218 -0
  269. sqlalchemy/util/preloaded.py +150 -0
  270. sqlalchemy/util/queue.py +322 -0
  271. sqlalchemy/util/tool_support.py +201 -0
  272. sqlalchemy/util/topological.py +120 -0
  273. sqlalchemy/util/typing.py +629 -0
@@ -0,0 +1,875 @@
1
+ # engine/create.py
2
+ # Copyright (C) 2005-2024 the SQLAlchemy authors and contributors
3
+ # <see AUTHORS file>
4
+ #
5
+ # This module is part of SQLAlchemy and is released under
6
+ # the MIT License: https://www.opensource.org/licenses/mit-license.php
7
+
8
+ from __future__ import annotations
9
+
10
+ import inspect
11
+ import typing
12
+ from typing import Any
13
+ from typing import Callable
14
+ from typing import cast
15
+ from typing import Dict
16
+ from typing import List
17
+ from typing import Optional
18
+ from typing import overload
19
+ from typing import Type
20
+ from typing import Union
21
+
22
+ from . import base
23
+ from . import url as _url
24
+ from .interfaces import DBAPIConnection
25
+ from .mock import create_mock_engine
26
+ from .. import event
27
+ from .. import exc
28
+ from .. import util
29
+ from ..pool import _AdhocProxiedConnection
30
+ from ..pool import ConnectionPoolEntry
31
+ from ..sql import compiler
32
+ from ..util import immutabledict
33
+
34
+ if typing.TYPE_CHECKING:
35
+ from .base import Engine
36
+ from .interfaces import _ExecuteOptions
37
+ from .interfaces import _ParamStyle
38
+ from .interfaces import IsolationLevel
39
+ from .url import URL
40
+ from ..log import _EchoFlagType
41
+ from ..pool import _CreatorFnType
42
+ from ..pool import _CreatorWRecFnType
43
+ from ..pool import _ResetStyleArgType
44
+ from ..pool import Pool
45
+ from ..util.typing import Literal
46
+
47
+
48
+ @overload
49
+ def create_engine(
50
+ url: Union[str, URL],
51
+ *,
52
+ connect_args: Dict[Any, Any] = ...,
53
+ convert_unicode: bool = ...,
54
+ creator: Union[_CreatorFnType, _CreatorWRecFnType] = ...,
55
+ echo: _EchoFlagType = ...,
56
+ echo_pool: _EchoFlagType = ...,
57
+ enable_from_linting: bool = ...,
58
+ execution_options: _ExecuteOptions = ...,
59
+ future: Literal[True],
60
+ hide_parameters: bool = ...,
61
+ implicit_returning: Literal[True] = ...,
62
+ insertmanyvalues_page_size: int = ...,
63
+ isolation_level: IsolationLevel = ...,
64
+ json_deserializer: Callable[..., Any] = ...,
65
+ json_serializer: Callable[..., Any] = ...,
66
+ label_length: Optional[int] = ...,
67
+ logging_name: str = ...,
68
+ max_identifier_length: Optional[int] = ...,
69
+ max_overflow: int = ...,
70
+ module: Optional[Any] = ...,
71
+ paramstyle: Optional[_ParamStyle] = ...,
72
+ pool: Optional[Pool] = ...,
73
+ poolclass: Optional[Type[Pool]] = ...,
74
+ pool_logging_name: str = ...,
75
+ pool_pre_ping: bool = ...,
76
+ pool_size: int = ...,
77
+ pool_recycle: int = ...,
78
+ pool_reset_on_return: Optional[_ResetStyleArgType] = ...,
79
+ pool_timeout: float = ...,
80
+ pool_use_lifo: bool = ...,
81
+ plugins: List[str] = ...,
82
+ query_cache_size: int = ...,
83
+ use_insertmanyvalues: bool = ...,
84
+ **kwargs: Any,
85
+ ) -> Engine: ...
86
+
87
+
88
+ @overload
89
+ def create_engine(url: Union[str, URL], **kwargs: Any) -> Engine: ...
90
+
91
+
92
+ @util.deprecated_params(
93
+ strategy=(
94
+ "1.4",
95
+ "The :paramref:`_sa.create_engine.strategy` keyword is deprecated, "
96
+ "and the only argument accepted is 'mock'; please use "
97
+ ":func:`.create_mock_engine` going forward. For general "
98
+ "customization of create_engine which may have been accomplished "
99
+ "using strategies, see :class:`.CreateEnginePlugin`.",
100
+ ),
101
+ empty_in_strategy=(
102
+ "1.4",
103
+ "The :paramref:`_sa.create_engine.empty_in_strategy` keyword is "
104
+ "deprecated, and no longer has any effect. All IN expressions "
105
+ "are now rendered using "
106
+ 'the "expanding parameter" strategy which renders a set of bound'
107
+ 'expressions, or an "empty set" SELECT, at statement execution'
108
+ "time.",
109
+ ),
110
+ implicit_returning=(
111
+ "2.0",
112
+ "The :paramref:`_sa.create_engine.implicit_returning` parameter "
113
+ "is deprecated and will be removed in a future release. ",
114
+ ),
115
+ )
116
+ def create_engine(url: Union[str, _url.URL], **kwargs: Any) -> Engine:
117
+ """Create a new :class:`_engine.Engine` instance.
118
+
119
+ The standard calling form is to send the :ref:`URL <database_urls>` as the
120
+ first positional argument, usually a string
121
+ that indicates database dialect and connection arguments::
122
+
123
+ engine = create_engine("postgresql+psycopg2://scott:tiger@localhost/test")
124
+
125
+ .. note::
126
+
127
+ Please review :ref:`database_urls` for general guidelines in composing
128
+ URL strings. In particular, special characters, such as those often
129
+ part of passwords, must be URL encoded to be properly parsed.
130
+
131
+ Additional keyword arguments may then follow it which
132
+ establish various options on the resulting :class:`_engine.Engine`
133
+ and its underlying :class:`.Dialect` and :class:`_pool.Pool`
134
+ constructs::
135
+
136
+ engine = create_engine("mysql+mysqldb://scott:tiger@hostname/dbname",
137
+ pool_recycle=3600, echo=True)
138
+
139
+ The string form of the URL is
140
+ ``dialect[+driver]://user:password@host/dbname[?key=value..]``, where
141
+ ``dialect`` is a database name such as ``mysql``, ``oracle``,
142
+ ``postgresql``, etc., and ``driver`` the name of a DBAPI, such as
143
+ ``psycopg2``, ``pyodbc``, ``cx_oracle``, etc. Alternatively,
144
+ the URL can be an instance of :class:`~sqlalchemy.engine.url.URL`.
145
+
146
+ ``**kwargs`` takes a wide variety of options which are routed
147
+ towards their appropriate components. Arguments may be specific to
148
+ the :class:`_engine.Engine`, the underlying :class:`.Dialect`,
149
+ as well as the
150
+ :class:`_pool.Pool`. Specific dialects also accept keyword arguments that
151
+ are unique to that dialect. Here, we describe the parameters
152
+ that are common to most :func:`_sa.create_engine()` usage.
153
+
154
+ Once established, the newly resulting :class:`_engine.Engine` will
155
+ request a connection from the underlying :class:`_pool.Pool` once
156
+ :meth:`_engine.Engine.connect` is called, or a method which depends on it
157
+ such as :meth:`_engine.Engine.execute` is invoked. The
158
+ :class:`_pool.Pool` in turn
159
+ will establish the first actual DBAPI connection when this request
160
+ is received. The :func:`_sa.create_engine` call itself does **not**
161
+ establish any actual DBAPI connections directly.
162
+
163
+ .. seealso::
164
+
165
+ :doc:`/core/engines`
166
+
167
+ :doc:`/dialects/index`
168
+
169
+ :ref:`connections_toplevel`
170
+
171
+ :param connect_args: a dictionary of options which will be
172
+ passed directly to the DBAPI's ``connect()`` method as
173
+ additional keyword arguments. See the example
174
+ at :ref:`custom_dbapi_args`.
175
+
176
+ :param creator: a callable which returns a DBAPI connection.
177
+ This creation function will be passed to the underlying
178
+ connection pool and will be used to create all new database
179
+ connections. Usage of this function causes connection
180
+ parameters specified in the URL argument to be bypassed.
181
+
182
+ This hook is not as flexible as the newer
183
+ :meth:`_events.DialectEvents.do_connect` hook which allows complete
184
+ control over how a connection is made to the database, given the full
185
+ set of URL arguments and state beforehand.
186
+
187
+ .. seealso::
188
+
189
+ :meth:`_events.DialectEvents.do_connect` - event hook that allows
190
+ full control over DBAPI connection mechanics.
191
+
192
+ :ref:`custom_dbapi_args`
193
+
194
+ :param echo=False: if True, the Engine will log all statements
195
+ as well as a ``repr()`` of their parameter lists to the default log
196
+ handler, which defaults to ``sys.stdout`` for output. If set to the
197
+ string ``"debug"``, result rows will be printed to the standard output
198
+ as well. The ``echo`` attribute of ``Engine`` can be modified at any
199
+ time to turn logging on and off; direct control of logging is also
200
+ available using the standard Python ``logging`` module.
201
+
202
+ .. seealso::
203
+
204
+ :ref:`dbengine_logging` - further detail on how to configure
205
+ logging.
206
+
207
+
208
+ :param echo_pool=False: if True, the connection pool will log
209
+ informational output such as when connections are invalidated
210
+ as well as when connections are recycled to the default log handler,
211
+ which defaults to ``sys.stdout`` for output. If set to the string
212
+ ``"debug"``, the logging will include pool checkouts and checkins.
213
+ Direct control of logging is also available using the standard Python
214
+ ``logging`` module.
215
+
216
+ .. seealso::
217
+
218
+ :ref:`dbengine_logging` - further detail on how to configure
219
+ logging.
220
+
221
+
222
+ :param empty_in_strategy: No longer used; SQLAlchemy now uses
223
+ "empty set" behavior for IN in all cases.
224
+
225
+ :param enable_from_linting: defaults to True. Will emit a warning
226
+ if a given SELECT statement is found to have un-linked FROM elements
227
+ which would cause a cartesian product.
228
+
229
+ .. versionadded:: 1.4
230
+
231
+ .. seealso::
232
+
233
+ :ref:`change_4737`
234
+
235
+ :param execution_options: Dictionary execution options which will
236
+ be applied to all connections. See
237
+ :meth:`~sqlalchemy.engine.Connection.execution_options`
238
+
239
+ :param future: Use the 2.0 style :class:`_engine.Engine` and
240
+ :class:`_engine.Connection` API.
241
+
242
+ As of SQLAlchemy 2.0, this parameter is present for backwards
243
+ compatibility only and must remain at its default value of ``True``.
244
+
245
+ The :paramref:`_sa.create_engine.future` parameter will be
246
+ deprecated in a subsequent 2.x release and eventually removed.
247
+
248
+ .. versionadded:: 1.4
249
+
250
+ .. versionchanged:: 2.0 All :class:`_engine.Engine` objects are
251
+ "future" style engines and there is no longer a ``future=False``
252
+ mode of operation.
253
+
254
+ .. seealso::
255
+
256
+ :ref:`migration_20_toplevel`
257
+
258
+ :param hide_parameters: Boolean, when set to True, SQL statement parameters
259
+ will not be displayed in INFO logging nor will they be formatted into
260
+ the string representation of :class:`.StatementError` objects.
261
+
262
+ .. versionadded:: 1.3.8
263
+
264
+ .. seealso::
265
+
266
+ :ref:`dbengine_logging` - further detail on how to configure
267
+ logging.
268
+
269
+ :param implicit_returning=True: Legacy parameter that may only be set
270
+ to True. In SQLAlchemy 2.0, this parameter does nothing. In order to
271
+ disable "implicit returning" for statements invoked by the ORM,
272
+ configure this on a per-table basis using the
273
+ :paramref:`.Table.implicit_returning` parameter.
274
+
275
+
276
+ :param insertmanyvalues_page_size: number of rows to format into an
277
+ INSERT statement when the statement uses "insertmanyvalues" mode, which is
278
+ a paged form of bulk insert that is used for many backends when using
279
+ :term:`executemany` execution typically in conjunction with RETURNING.
280
+ Defaults to 1000, but may also be subject to dialect-specific limiting
281
+ factors which may override this value on a per-statement basis.
282
+
283
+ .. versionadded:: 2.0
284
+
285
+ .. seealso::
286
+
287
+ :ref:`engine_insertmanyvalues`
288
+
289
+ :ref:`engine_insertmanyvalues_page_size`
290
+
291
+ :paramref:`_engine.Connection.execution_options.insertmanyvalues_page_size`
292
+
293
+ :param isolation_level: optional string name of an isolation level
294
+ which will be set on all new connections unconditionally.
295
+ Isolation levels are typically some subset of the string names
296
+ ``"SERIALIZABLE"``, ``"REPEATABLE READ"``,
297
+ ``"READ COMMITTED"``, ``"READ UNCOMMITTED"`` and ``"AUTOCOMMIT"``
298
+ based on backend.
299
+
300
+ The :paramref:`_sa.create_engine.isolation_level` parameter is
301
+ in contrast to the
302
+ :paramref:`.Connection.execution_options.isolation_level`
303
+ execution option, which may be set on an individual
304
+ :class:`.Connection`, as well as the same parameter passed to
305
+ :meth:`.Engine.execution_options`, where it may be used to create
306
+ multiple engines with different isolation levels that share a common
307
+ connection pool and dialect.
308
+
309
+ .. versionchanged:: 2.0 The
310
+ :paramref:`_sa.create_engine.isolation_level`
311
+ parameter has been generalized to work on all dialects which support
312
+ the concept of isolation level, and is provided as a more succinct,
313
+ up front configuration switch in contrast to the execution option
314
+ which is more of an ad-hoc programmatic option.
315
+
316
+ .. seealso::
317
+
318
+ :ref:`dbapi_autocommit`
319
+
320
+ :param json_deserializer: for dialects that support the
321
+ :class:`_types.JSON`
322
+ datatype, this is a Python callable that will convert a JSON string
323
+ to a Python object. By default, the Python ``json.loads`` function is
324
+ used.
325
+
326
+ .. versionchanged:: 1.3.7 The SQLite dialect renamed this from
327
+ ``_json_deserializer``.
328
+
329
+ :param json_serializer: for dialects that support the :class:`_types.JSON`
330
+ datatype, this is a Python callable that will render a given object
331
+ as JSON. By default, the Python ``json.dumps`` function is used.
332
+
333
+ .. versionchanged:: 1.3.7 The SQLite dialect renamed this from
334
+ ``_json_serializer``.
335
+
336
+
337
+ :param label_length=None: optional integer value which limits
338
+ the size of dynamically generated column labels to that many
339
+ characters. If less than 6, labels are generated as
340
+ "_(counter)". If ``None``, the value of
341
+ ``dialect.max_identifier_length``, which may be affected via the
342
+ :paramref:`_sa.create_engine.max_identifier_length` parameter,
343
+ is used instead. The value of
344
+ :paramref:`_sa.create_engine.label_length`
345
+ may not be larger than that of
346
+ :paramref:`_sa.create_engine.max_identfier_length`.
347
+
348
+ .. seealso::
349
+
350
+ :paramref:`_sa.create_engine.max_identifier_length`
351
+
352
+ :param logging_name: String identifier which will be used within
353
+ the "name" field of logging records generated within the
354
+ "sqlalchemy.engine" logger. Defaults to a hexstring of the
355
+ object's id.
356
+
357
+ .. seealso::
358
+
359
+ :ref:`dbengine_logging` - further detail on how to configure
360
+ logging.
361
+
362
+ :paramref:`_engine.Connection.execution_options.logging_token`
363
+
364
+ :param max_identifier_length: integer; override the max_identifier_length
365
+ determined by the dialect. if ``None`` or zero, has no effect. This
366
+ is the database's configured maximum number of characters that may be
367
+ used in a SQL identifier such as a table name, column name, or label
368
+ name. All dialects determine this value automatically, however in the
369
+ case of a new database version for which this value has changed but
370
+ SQLAlchemy's dialect has not been adjusted, the value may be passed
371
+ here.
372
+
373
+ .. versionadded:: 1.3.9
374
+
375
+ .. seealso::
376
+
377
+ :paramref:`_sa.create_engine.label_length`
378
+
379
+ :param max_overflow=10: the number of connections to allow in
380
+ connection pool "overflow", that is connections that can be
381
+ opened above and beyond the pool_size setting, which defaults
382
+ to five. this is only used with :class:`~sqlalchemy.pool.QueuePool`.
383
+
384
+ :param module=None: reference to a Python module object (the module
385
+ itself, not its string name). Specifies an alternate DBAPI module to
386
+ be used by the engine's dialect. Each sub-dialect references a
387
+ specific DBAPI which will be imported before first connect. This
388
+ parameter causes the import to be bypassed, and the given module to
389
+ be used instead. Can be used for testing of DBAPIs as well as to
390
+ inject "mock" DBAPI implementations into the :class:`_engine.Engine`.
391
+
392
+ :param paramstyle=None: The `paramstyle <https://legacy.python.org/dev/peps/pep-0249/#paramstyle>`_
393
+ to use when rendering bound parameters. This style defaults to the
394
+ one recommended by the DBAPI itself, which is retrieved from the
395
+ ``.paramstyle`` attribute of the DBAPI. However, most DBAPIs accept
396
+ more than one paramstyle, and in particular it may be desirable
397
+ to change a "named" paramstyle into a "positional" one, or vice versa.
398
+ When this attribute is passed, it should be one of the values
399
+ ``"qmark"``, ``"numeric"``, ``"named"``, ``"format"`` or
400
+ ``"pyformat"``, and should correspond to a parameter style known
401
+ to be supported by the DBAPI in use.
402
+
403
+ :param pool=None: an already-constructed instance of
404
+ :class:`~sqlalchemy.pool.Pool`, such as a
405
+ :class:`~sqlalchemy.pool.QueuePool` instance. If non-None, this
406
+ pool will be used directly as the underlying connection pool
407
+ for the engine, bypassing whatever connection parameters are
408
+ present in the URL argument. For information on constructing
409
+ connection pools manually, see :ref:`pooling_toplevel`.
410
+
411
+ :param poolclass=None: a :class:`~sqlalchemy.pool.Pool`
412
+ subclass, which will be used to create a connection pool
413
+ instance using the connection parameters given in the URL. Note
414
+ this differs from ``pool`` in that you don't actually
415
+ instantiate the pool in this case, you just indicate what type
416
+ of pool to be used.
417
+
418
+ :param pool_logging_name: String identifier which will be used within
419
+ the "name" field of logging records generated within the
420
+ "sqlalchemy.pool" logger. Defaults to a hexstring of the object's
421
+ id.
422
+
423
+ .. seealso::
424
+
425
+ :ref:`dbengine_logging` - further detail on how to configure
426
+ logging.
427
+
428
+ :param pool_pre_ping: boolean, if True will enable the connection pool
429
+ "pre-ping" feature that tests connections for liveness upon
430
+ each checkout.
431
+
432
+ .. versionadded:: 1.2
433
+
434
+ .. seealso::
435
+
436
+ :ref:`pool_disconnects_pessimistic`
437
+
438
+ :param pool_size=5: the number of connections to keep open
439
+ inside the connection pool. This used with
440
+ :class:`~sqlalchemy.pool.QueuePool` as
441
+ well as :class:`~sqlalchemy.pool.SingletonThreadPool`. With
442
+ :class:`~sqlalchemy.pool.QueuePool`, a ``pool_size`` setting
443
+ of 0 indicates no limit; to disable pooling, set ``poolclass`` to
444
+ :class:`~sqlalchemy.pool.NullPool` instead.
445
+
446
+ :param pool_recycle=-1: this setting causes the pool to recycle
447
+ connections after the given number of seconds has passed. It
448
+ defaults to -1, or no timeout. For example, setting to 3600
449
+ means connections will be recycled after one hour. Note that
450
+ MySQL in particular will disconnect automatically if no
451
+ activity is detected on a connection for eight hours (although
452
+ this is configurable with the MySQLDB connection itself and the
453
+ server configuration as well).
454
+
455
+ .. seealso::
456
+
457
+ :ref:`pool_setting_recycle`
458
+
459
+ :param pool_reset_on_return='rollback': set the
460
+ :paramref:`_pool.Pool.reset_on_return` parameter of the underlying
461
+ :class:`_pool.Pool` object, which can be set to the values
462
+ ``"rollback"``, ``"commit"``, or ``None``.
463
+
464
+ .. seealso::
465
+
466
+ :ref:`pool_reset_on_return`
467
+
468
+ :param pool_timeout=30: number of seconds to wait before giving
469
+ up on getting a connection from the pool. This is only used
470
+ with :class:`~sqlalchemy.pool.QueuePool`. This can be a float but is
471
+ subject to the limitations of Python time functions which may not be
472
+ reliable in the tens of milliseconds.
473
+
474
+ .. note: don't use 30.0 above, it seems to break with the :param tag
475
+
476
+ :param pool_use_lifo=False: use LIFO (last-in-first-out) when retrieving
477
+ connections from :class:`.QueuePool` instead of FIFO
478
+ (first-in-first-out). Using LIFO, a server-side timeout scheme can
479
+ reduce the number of connections used during non- peak periods of
480
+ use. When planning for server-side timeouts, ensure that a recycle or
481
+ pre-ping strategy is in use to gracefully handle stale connections.
482
+
483
+ .. versionadded:: 1.3
484
+
485
+ .. seealso::
486
+
487
+ :ref:`pool_use_lifo`
488
+
489
+ :ref:`pool_disconnects`
490
+
491
+ :param plugins: string list of plugin names to load. See
492
+ :class:`.CreateEnginePlugin` for background.
493
+
494
+ .. versionadded:: 1.2.3
495
+
496
+ :param query_cache_size: size of the cache used to cache the SQL string
497
+ form of queries. Set to zero to disable caching.
498
+
499
+ The cache is pruned of its least recently used items when its size reaches
500
+ N * 1.5. Defaults to 500, meaning the cache will always store at least
501
+ 500 SQL statements when filled, and will grow up to 750 items at which
502
+ point it is pruned back down to 500 by removing the 250 least recently
503
+ used items.
504
+
505
+ Caching is accomplished on a per-statement basis by generating a
506
+ cache key that represents the statement's structure, then generating
507
+ string SQL for the current dialect only if that key is not present
508
+ in the cache. All statements support caching, however some features
509
+ such as an INSERT with a large set of parameters will intentionally
510
+ bypass the cache. SQL logging will indicate statistics for each
511
+ statement whether or not it were pull from the cache.
512
+
513
+ .. note:: some ORM functions related to unit-of-work persistence as well
514
+ as some attribute loading strategies will make use of individual
515
+ per-mapper caches outside of the main cache.
516
+
517
+
518
+ .. seealso::
519
+
520
+ :ref:`sql_caching`
521
+
522
+ .. versionadded:: 1.4
523
+
524
+ :param use_insertmanyvalues: True by default, use the "insertmanyvalues"
525
+ execution style for INSERT..RETURNING statements by default.
526
+
527
+ .. versionadded:: 2.0
528
+
529
+ .. seealso::
530
+
531
+ :ref:`engine_insertmanyvalues`
532
+
533
+ """ # noqa
534
+
535
+ if "strategy" in kwargs:
536
+ strat = kwargs.pop("strategy")
537
+ if strat == "mock":
538
+ # this case is deprecated
539
+ return create_mock_engine(url, **kwargs) # type: ignore
540
+ else:
541
+ raise exc.ArgumentError("unknown strategy: %r" % strat)
542
+
543
+ kwargs.pop("empty_in_strategy", None)
544
+
545
+ # create url.URL object
546
+ u = _url.make_url(url)
547
+
548
+ u, plugins, kwargs = u._instantiate_plugins(kwargs)
549
+
550
+ entrypoint = u._get_entrypoint()
551
+ _is_async = kwargs.pop("_is_async", False)
552
+ if _is_async:
553
+ dialect_cls = entrypoint.get_async_dialect_cls(u)
554
+ else:
555
+ dialect_cls = entrypoint.get_dialect_cls(u)
556
+
557
+ if kwargs.pop("_coerce_config", False):
558
+
559
+ def pop_kwarg(key: str, default: Optional[Any] = None) -> Any:
560
+ value = kwargs.pop(key, default)
561
+ if key in dialect_cls.engine_config_types:
562
+ value = dialect_cls.engine_config_types[key](value)
563
+ return value
564
+
565
+ else:
566
+ pop_kwarg = kwargs.pop # type: ignore
567
+
568
+ dialect_args = {}
569
+ # consume dialect arguments from kwargs
570
+ for k in util.get_cls_kwargs(dialect_cls):
571
+ if k in kwargs:
572
+ dialect_args[k] = pop_kwarg(k)
573
+
574
+ dbapi = kwargs.pop("module", None)
575
+ if dbapi is None:
576
+ dbapi_args = {}
577
+
578
+ if "import_dbapi" in dialect_cls.__dict__:
579
+ dbapi_meth = dialect_cls.import_dbapi
580
+
581
+ elif hasattr(dialect_cls, "dbapi") and inspect.ismethod(
582
+ dialect_cls.dbapi
583
+ ):
584
+ util.warn_deprecated(
585
+ "The dbapi() classmethod on dialect classes has been "
586
+ "renamed to import_dbapi(). Implement an import_dbapi() "
587
+ f"classmethod directly on class {dialect_cls} to remove this "
588
+ "warning; the old .dbapi() classmethod may be maintained for "
589
+ "backwards compatibility.",
590
+ "2.0",
591
+ )
592
+ dbapi_meth = dialect_cls.dbapi
593
+ else:
594
+ dbapi_meth = dialect_cls.import_dbapi
595
+
596
+ for k in util.get_func_kwargs(dbapi_meth):
597
+ if k in kwargs:
598
+ dbapi_args[k] = pop_kwarg(k)
599
+ dbapi = dbapi_meth(**dbapi_args)
600
+
601
+ dialect_args["dbapi"] = dbapi
602
+
603
+ dialect_args.setdefault("compiler_linting", compiler.NO_LINTING)
604
+ enable_from_linting = kwargs.pop("enable_from_linting", True)
605
+ if enable_from_linting:
606
+ dialect_args["compiler_linting"] ^= compiler.COLLECT_CARTESIAN_PRODUCTS
607
+
608
+ for plugin in plugins:
609
+ plugin.handle_dialect_kwargs(dialect_cls, dialect_args)
610
+
611
+ # create dialect
612
+ dialect = dialect_cls(**dialect_args)
613
+
614
+ # assemble connection arguments
615
+ (cargs_tup, cparams) = dialect.create_connect_args(u)
616
+ cparams.update(pop_kwarg("connect_args", {}))
617
+
618
+ if "async_fallback" in cparams and util.asbool(cparams["async_fallback"]):
619
+ util.warn_deprecated(
620
+ "The async_fallback dialect argument is deprecated and will be "
621
+ "removed in SQLAlchemy 2.1.",
622
+ "2.0",
623
+ )
624
+
625
+ cargs = list(cargs_tup) # allow mutability
626
+
627
+ # look for existing pool or create
628
+ pool = pop_kwarg("pool", None)
629
+ if pool is None:
630
+
631
+ def connect(
632
+ connection_record: Optional[ConnectionPoolEntry] = None,
633
+ ) -> DBAPIConnection:
634
+ if dialect._has_events:
635
+ for fn in dialect.dispatch.do_connect:
636
+ connection = cast(
637
+ DBAPIConnection,
638
+ fn(dialect, connection_record, cargs, cparams),
639
+ )
640
+ if connection is not None:
641
+ return connection
642
+
643
+ return dialect.connect(*cargs, **cparams)
644
+
645
+ creator = pop_kwarg("creator", connect)
646
+
647
+ poolclass = pop_kwarg("poolclass", None)
648
+ if poolclass is None:
649
+ poolclass = dialect.get_dialect_pool_class(u)
650
+ pool_args = {"dialect": dialect}
651
+
652
+ # consume pool arguments from kwargs, translating a few of
653
+ # the arguments
654
+ for k in util.get_cls_kwargs(poolclass):
655
+ tk = _pool_translate_kwargs.get(k, k)
656
+ if tk in kwargs:
657
+ pool_args[k] = pop_kwarg(tk)
658
+
659
+ for plugin in plugins:
660
+ plugin.handle_pool_kwargs(poolclass, pool_args)
661
+
662
+ pool = poolclass(creator, **pool_args)
663
+ else:
664
+ pool._dialect = dialect
665
+
666
+ if (
667
+ hasattr(pool, "_is_asyncio")
668
+ and pool._is_asyncio is not dialect.is_async
669
+ ):
670
+ raise exc.ArgumentError(
671
+ f"Pool class {pool.__class__.__name__} cannot be "
672
+ f"used with {'non-' if not dialect.is_async else ''}"
673
+ "asyncio engine",
674
+ code="pcls",
675
+ )
676
+
677
+ # create engine.
678
+ if not pop_kwarg("future", True):
679
+ raise exc.ArgumentError(
680
+ "The 'future' parameter passed to "
681
+ "create_engine() may only be set to True."
682
+ )
683
+
684
+ engineclass = base.Engine
685
+
686
+ engine_args = {}
687
+ for k in util.get_cls_kwargs(engineclass):
688
+ if k in kwargs:
689
+ engine_args[k] = pop_kwarg(k)
690
+
691
+ # internal flags used by the test suite for instrumenting / proxying
692
+ # engines with mocks etc.
693
+ _initialize = kwargs.pop("_initialize", True)
694
+
695
+ # all kwargs should be consumed
696
+ if kwargs:
697
+ raise TypeError(
698
+ "Invalid argument(s) %s sent to create_engine(), "
699
+ "using configuration %s/%s/%s. Please check that the "
700
+ "keyword arguments are appropriate for this combination "
701
+ "of components."
702
+ % (
703
+ ",".join("'%s'" % k for k in kwargs),
704
+ dialect.__class__.__name__,
705
+ pool.__class__.__name__,
706
+ engineclass.__name__,
707
+ )
708
+ )
709
+
710
+ engine = engineclass(pool, dialect, u, **engine_args)
711
+
712
+ if _initialize:
713
+ do_on_connect = dialect.on_connect_url(u)
714
+ if do_on_connect:
715
+
716
+ def on_connect(
717
+ dbapi_connection: DBAPIConnection,
718
+ connection_record: ConnectionPoolEntry,
719
+ ) -> None:
720
+ assert do_on_connect is not None
721
+ do_on_connect(dbapi_connection)
722
+
723
+ event.listen(pool, "connect", on_connect)
724
+
725
+ builtin_on_connect = dialect._builtin_onconnect()
726
+ if builtin_on_connect:
727
+ event.listen(pool, "connect", builtin_on_connect)
728
+
729
+ def first_connect(
730
+ dbapi_connection: DBAPIConnection,
731
+ connection_record: ConnectionPoolEntry,
732
+ ) -> None:
733
+ c = base.Connection(
734
+ engine,
735
+ connection=_AdhocProxiedConnection(
736
+ dbapi_connection, connection_record
737
+ ),
738
+ _has_events=False,
739
+ # reconnecting will be a reentrant condition, so if the
740
+ # connection goes away, Connection is then closed
741
+ _allow_revalidate=False,
742
+ # dont trigger the autobegin sequence
743
+ # within the up front dialect checks
744
+ _allow_autobegin=False,
745
+ )
746
+ c._execution_options = util.EMPTY_DICT
747
+
748
+ try:
749
+ dialect.initialize(c)
750
+ finally:
751
+ # note that "invalidated" and "closed" are mutually
752
+ # exclusive in 1.4 Connection.
753
+ if not c.invalidated and not c.closed:
754
+ # transaction is rolled back otherwise, tested by
755
+ # test/dialect/postgresql/test_dialect.py
756
+ # ::MiscBackendTest::test_initial_transaction_state
757
+ dialect.do_rollback(c.connection)
758
+
759
+ # previously, the "first_connect" event was used here, which was then
760
+ # scaled back if the "on_connect" handler were present. now,
761
+ # since "on_connect" is virtually always present, just use
762
+ # "connect" event with once_unless_exception in all cases so that
763
+ # the connection event flow is consistent in all cases.
764
+ event.listen(
765
+ pool, "connect", first_connect, _once_unless_exception=True
766
+ )
767
+
768
+ dialect_cls.engine_created(engine)
769
+ if entrypoint is not dialect_cls:
770
+ entrypoint.engine_created(engine)
771
+
772
+ for plugin in plugins:
773
+ plugin.engine_created(engine)
774
+
775
+ return engine
776
+
777
+
778
+ def engine_from_config(
779
+ configuration: Dict[str, Any], prefix: str = "sqlalchemy.", **kwargs: Any
780
+ ) -> Engine:
781
+ """Create a new Engine instance using a configuration dictionary.
782
+
783
+ The dictionary is typically produced from a config file.
784
+
785
+ The keys of interest to ``engine_from_config()`` should be prefixed, e.g.
786
+ ``sqlalchemy.url``, ``sqlalchemy.echo``, etc. The 'prefix' argument
787
+ indicates the prefix to be searched for. Each matching key (after the
788
+ prefix is stripped) is treated as though it were the corresponding keyword
789
+ argument to a :func:`_sa.create_engine` call.
790
+
791
+ The only required key is (assuming the default prefix) ``sqlalchemy.url``,
792
+ which provides the :ref:`database URL <database_urls>`.
793
+
794
+ A select set of keyword arguments will be "coerced" to their
795
+ expected type based on string values. The set of arguments
796
+ is extensible per-dialect using the ``engine_config_types`` accessor.
797
+
798
+ :param configuration: A dictionary (typically produced from a config file,
799
+ but this is not a requirement). Items whose keys start with the value
800
+ of 'prefix' will have that prefix stripped, and will then be passed to
801
+ :func:`_sa.create_engine`.
802
+
803
+ :param prefix: Prefix to match and then strip from keys
804
+ in 'configuration'.
805
+
806
+ :param kwargs: Each keyword argument to ``engine_from_config()`` itself
807
+ overrides the corresponding item taken from the 'configuration'
808
+ dictionary. Keyword arguments should *not* be prefixed.
809
+
810
+ """
811
+
812
+ options = {
813
+ key[len(prefix) :]: configuration[key]
814
+ for key in configuration
815
+ if key.startswith(prefix)
816
+ }
817
+ options["_coerce_config"] = True
818
+ options.update(kwargs)
819
+ url = options.pop("url")
820
+ return create_engine(url, **options)
821
+
822
+
823
+ @overload
824
+ def create_pool_from_url(
825
+ url: Union[str, URL],
826
+ *,
827
+ poolclass: Optional[Type[Pool]] = ...,
828
+ logging_name: str = ...,
829
+ pre_ping: bool = ...,
830
+ size: int = ...,
831
+ recycle: int = ...,
832
+ reset_on_return: Optional[_ResetStyleArgType] = ...,
833
+ timeout: float = ...,
834
+ use_lifo: bool = ...,
835
+ **kwargs: Any,
836
+ ) -> Pool: ...
837
+
838
+
839
+ @overload
840
+ def create_pool_from_url(url: Union[str, URL], **kwargs: Any) -> Pool: ...
841
+
842
+
843
+ def create_pool_from_url(url: Union[str, URL], **kwargs: Any) -> Pool:
844
+ """Create a pool instance from the given url.
845
+
846
+ If ``poolclass`` is not provided the pool class used
847
+ is selected using the dialect specified in the URL.
848
+
849
+ The arguments passed to :func:`_sa.create_pool_from_url` are
850
+ identical to the pool argument passed to the :func:`_sa.create_engine`
851
+ function.
852
+
853
+ .. versionadded:: 2.0.10
854
+ """
855
+
856
+ for key in _pool_translate_kwargs:
857
+ if key in kwargs:
858
+ kwargs[_pool_translate_kwargs[key]] = kwargs.pop(key)
859
+
860
+ engine = create_engine(url, **kwargs, _initialize=False)
861
+ return engine.pool
862
+
863
+
864
+ _pool_translate_kwargs = immutabledict(
865
+ {
866
+ "logging_name": "pool_logging_name",
867
+ "echo": "echo_pool",
868
+ "timeout": "pool_timeout",
869
+ "recycle": "pool_recycle",
870
+ "events": "pool_events", # deprecated
871
+ "reset_on_return": "pool_reset_on_return",
872
+ "pre_ping": "pool_pre_ping",
873
+ "use_lifo": "pool_use_lifo",
874
+ }
875
+ )