create.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. # engine/create.py
  2. # Copyright (C) 2005-2025 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. from __future__ import annotations
  8. import inspect
  9. import typing
  10. from typing import Any
  11. from typing import Callable
  12. from typing import cast
  13. from typing import Dict
  14. from typing import List
  15. from typing import Optional
  16. from typing import overload
  17. from typing import Type
  18. from typing import Union
  19. from . import base
  20. from . import url as _url
  21. from .interfaces import DBAPIConnection
  22. from .mock import create_mock_engine
  23. from .. import event
  24. from .. import exc
  25. from .. import util
  26. from ..pool import _AdhocProxiedConnection
  27. from ..pool import ConnectionPoolEntry
  28. from ..sql import compiler
  29. from ..util import immutabledict
  30. if typing.TYPE_CHECKING:
  31. from .base import Engine
  32. from .interfaces import _ExecuteOptions
  33. from .interfaces import _ParamStyle
  34. from .interfaces import IsolationLevel
  35. from .url import URL
  36. from ..log import _EchoFlagType
  37. from ..pool import _CreatorFnType
  38. from ..pool import _CreatorWRecFnType
  39. from ..pool import _ResetStyleArgType
  40. from ..pool import Pool
  41. from ..util.typing import Literal
  42. @overload
  43. def create_engine(
  44. url: Union[str, URL],
  45. *,
  46. connect_args: Dict[Any, Any] = ...,
  47. convert_unicode: bool = ...,
  48. creator: Union[_CreatorFnType, _CreatorWRecFnType] = ...,
  49. echo: _EchoFlagType = ...,
  50. echo_pool: _EchoFlagType = ...,
  51. enable_from_linting: bool = ...,
  52. execution_options: _ExecuteOptions = ...,
  53. future: Literal[True],
  54. hide_parameters: bool = ...,
  55. implicit_returning: Literal[True] = ...,
  56. insertmanyvalues_page_size: int = ...,
  57. isolation_level: IsolationLevel = ...,
  58. json_deserializer: Callable[..., Any] = ...,
  59. json_serializer: Callable[..., Any] = ...,
  60. label_length: Optional[int] = ...,
  61. logging_name: str = ...,
  62. max_identifier_length: Optional[int] = ...,
  63. max_overflow: int = ...,
  64. module: Optional[Any] = ...,
  65. paramstyle: Optional[_ParamStyle] = ...,
  66. pool: Optional[Pool] = ...,
  67. poolclass: Optional[Type[Pool]] = ...,
  68. pool_logging_name: str = ...,
  69. pool_pre_ping: bool = ...,
  70. pool_size: int = ...,
  71. pool_recycle: int = ...,
  72. pool_reset_on_return: Optional[_ResetStyleArgType] = ...,
  73. pool_timeout: float = ...,
  74. pool_use_lifo: bool = ...,
  75. plugins: List[str] = ...,
  76. query_cache_size: int = ...,
  77. use_insertmanyvalues: bool = ...,
  78. **kwargs: Any,
  79. ) -> Engine: ...
  80. @overload
  81. def create_engine(url: Union[str, URL], **kwargs: Any) -> Engine: ...
  82. @util.deprecated_params(
  83. strategy=(
  84. "1.4",
  85. "The :paramref:`_sa.create_engine.strategy` keyword is deprecated, "
  86. "and the only argument accepted is 'mock'; please use "
  87. ":func:`.create_mock_engine` going forward. For general "
  88. "customization of create_engine which may have been accomplished "
  89. "using strategies, see :class:`.CreateEnginePlugin`.",
  90. ),
  91. empty_in_strategy=(
  92. "1.4",
  93. "The :paramref:`_sa.create_engine.empty_in_strategy` keyword is "
  94. "deprecated, and no longer has any effect. All IN expressions "
  95. "are now rendered using "
  96. 'the "expanding parameter" strategy which renders a set of bound'
  97. 'expressions, or an "empty set" SELECT, at statement execution'
  98. "time.",
  99. ),
  100. implicit_returning=(
  101. "2.0",
  102. "The :paramref:`_sa.create_engine.implicit_returning` parameter "
  103. "is deprecated and will be removed in a future release. ",
  104. ),
  105. )
  106. def create_engine(url: Union[str, _url.URL], **kwargs: Any) -> Engine:
  107. """Create a new :class:`_engine.Engine` instance.
  108. The standard calling form is to send the :ref:`URL <database_urls>` as the
  109. first positional argument, usually a string
  110. that indicates database dialect and connection arguments::
  111. engine = create_engine("postgresql+psycopg2://scott:tiger@localhost/test")
  112. .. note::
  113. Please review :ref:`database_urls` for general guidelines in composing
  114. URL strings. In particular, special characters, such as those often
  115. part of passwords, must be URL encoded to be properly parsed.
  116. Additional keyword arguments may then follow it which
  117. establish various options on the resulting :class:`_engine.Engine`
  118. and its underlying :class:`.Dialect` and :class:`_pool.Pool`
  119. constructs::
  120. engine = create_engine(
  121. "mysql+mysqldb://scott:tiger@hostname/dbname",
  122. pool_recycle=3600,
  123. echo=True,
  124. )
  125. The string form of the URL is
  126. ``dialect[+driver]://user:password@host/dbname[?key=value..]``, where
  127. ``dialect`` is a database name such as ``mysql``, ``oracle``,
  128. ``postgresql``, etc., and ``driver`` the name of a DBAPI, such as
  129. ``psycopg2``, ``pyodbc``, ``cx_oracle``, etc. Alternatively,
  130. the URL can be an instance of :class:`~sqlalchemy.engine.url.URL`.
  131. ``**kwargs`` takes a wide variety of options which are routed
  132. towards their appropriate components. Arguments may be specific to
  133. the :class:`_engine.Engine`, the underlying :class:`.Dialect`,
  134. as well as the
  135. :class:`_pool.Pool`. Specific dialects also accept keyword arguments that
  136. are unique to that dialect. Here, we describe the parameters
  137. that are common to most :func:`_sa.create_engine()` usage.
  138. Once established, the newly resulting :class:`_engine.Engine` will
  139. request a connection from the underlying :class:`_pool.Pool` once
  140. :meth:`_engine.Engine.connect` is called, or a method which depends on it
  141. such as :meth:`_engine.Engine.execute` is invoked. The
  142. :class:`_pool.Pool` in turn
  143. will establish the first actual DBAPI connection when this request
  144. is received. The :func:`_sa.create_engine` call itself does **not**
  145. establish any actual DBAPI connections directly.
  146. .. seealso::
  147. :doc:`/core/engines`
  148. :doc:`/dialects/index`
  149. :ref:`connections_toplevel`
  150. :param connect_args: a dictionary of options which will be
  151. passed directly to the DBAPI's ``connect()`` method as
  152. additional keyword arguments. See the example
  153. at :ref:`custom_dbapi_args`.
  154. :param creator: a callable which returns a DBAPI connection.
  155. This creation function will be passed to the underlying
  156. connection pool and will be used to create all new database
  157. connections. Usage of this function causes connection
  158. parameters specified in the URL argument to be bypassed.
  159. This hook is not as flexible as the newer
  160. :meth:`_events.DialectEvents.do_connect` hook which allows complete
  161. control over how a connection is made to the database, given the full
  162. set of URL arguments and state beforehand.
  163. .. seealso::
  164. :meth:`_events.DialectEvents.do_connect` - event hook that allows
  165. full control over DBAPI connection mechanics.
  166. :ref:`custom_dbapi_args`
  167. :param echo=False: if True, the Engine will log all statements
  168. as well as a ``repr()`` of their parameter lists to the default log
  169. handler, which defaults to ``sys.stdout`` for output. If set to the
  170. string ``"debug"``, result rows will be printed to the standard output
  171. as well. The ``echo`` attribute of ``Engine`` can be modified at any
  172. time to turn logging on and off; direct control of logging is also
  173. available using the standard Python ``logging`` module.
  174. .. seealso::
  175. :ref:`dbengine_logging` - further detail on how to configure
  176. logging.
  177. :param echo_pool=False: if True, the connection pool will log
  178. informational output such as when connections are invalidated
  179. as well as when connections are recycled to the default log handler,
  180. which defaults to ``sys.stdout`` for output. If set to the string
  181. ``"debug"``, the logging will include pool checkouts and checkins.
  182. Direct control of logging is also available using the standard Python
  183. ``logging`` module.
  184. .. seealso::
  185. :ref:`dbengine_logging` - further detail on how to configure
  186. logging.
  187. :param empty_in_strategy: No longer used; SQLAlchemy now uses
  188. "empty set" behavior for IN in all cases.
  189. :param enable_from_linting: defaults to True. Will emit a warning
  190. if a given SELECT statement is found to have un-linked FROM elements
  191. which would cause a cartesian product.
  192. .. versionadded:: 1.4
  193. .. seealso::
  194. :ref:`change_4737`
  195. :param execution_options: Dictionary execution options which will
  196. be applied to all connections. See
  197. :meth:`~sqlalchemy.engine.Connection.execution_options`
  198. :param future: Use the 2.0 style :class:`_engine.Engine` and
  199. :class:`_engine.Connection` API.
  200. As of SQLAlchemy 2.0, this parameter is present for backwards
  201. compatibility only and must remain at its default value of ``True``.
  202. The :paramref:`_sa.create_engine.future` parameter will be
  203. deprecated in a subsequent 2.x release and eventually removed.
  204. .. versionadded:: 1.4
  205. .. versionchanged:: 2.0 All :class:`_engine.Engine` objects are
  206. "future" style engines and there is no longer a ``future=False``
  207. mode of operation.
  208. .. seealso::
  209. :ref:`migration_20_toplevel`
  210. :param hide_parameters: Boolean, when set to True, SQL statement parameters
  211. will not be displayed in INFO logging nor will they be formatted into
  212. the string representation of :class:`.StatementError` objects.
  213. .. versionadded:: 1.3.8
  214. .. seealso::
  215. :ref:`dbengine_logging` - further detail on how to configure
  216. logging.
  217. :param implicit_returning=True: Legacy parameter that may only be set
  218. to True. In SQLAlchemy 2.0, this parameter does nothing. In order to
  219. disable "implicit returning" for statements invoked by the ORM,
  220. configure this on a per-table basis using the
  221. :paramref:`.Table.implicit_returning` parameter.
  222. :param insertmanyvalues_page_size: number of rows to format into an
  223. INSERT statement when the statement uses "insertmanyvalues" mode, which is
  224. a paged form of bulk insert that is used for many backends when using
  225. :term:`executemany` execution typically in conjunction with RETURNING.
  226. Defaults to 1000, but may also be subject to dialect-specific limiting
  227. factors which may override this value on a per-statement basis.
  228. .. versionadded:: 2.0
  229. .. seealso::
  230. :ref:`engine_insertmanyvalues`
  231. :ref:`engine_insertmanyvalues_page_size`
  232. :paramref:`_engine.Connection.execution_options.insertmanyvalues_page_size`
  233. :param isolation_level: optional string name of an isolation level
  234. which will be set on all new connections unconditionally.
  235. Isolation levels are typically some subset of the string names
  236. ``"SERIALIZABLE"``, ``"REPEATABLE READ"``,
  237. ``"READ COMMITTED"``, ``"READ UNCOMMITTED"`` and ``"AUTOCOMMIT"``
  238. based on backend.
  239. The :paramref:`_sa.create_engine.isolation_level` parameter is
  240. in contrast to the
  241. :paramref:`.Connection.execution_options.isolation_level`
  242. execution option, which may be set on an individual
  243. :class:`.Connection`, as well as the same parameter passed to
  244. :meth:`.Engine.execution_options`, where it may be used to create
  245. multiple engines with different isolation levels that share a common
  246. connection pool and dialect.
  247. .. versionchanged:: 2.0 The
  248. :paramref:`_sa.create_engine.isolation_level`
  249. parameter has been generalized to work on all dialects which support
  250. the concept of isolation level, and is provided as a more succinct,
  251. up front configuration switch in contrast to the execution option
  252. which is more of an ad-hoc programmatic option.
  253. .. seealso::
  254. :ref:`dbapi_autocommit`
  255. :param json_deserializer: for dialects that support the
  256. :class:`_types.JSON`
  257. datatype, this is a Python callable that will convert a JSON string
  258. to a Python object. By default, the Python ``json.loads`` function is
  259. used.
  260. .. versionchanged:: 1.3.7 The SQLite dialect renamed this from
  261. ``_json_deserializer``.
  262. :param json_serializer: for dialects that support the :class:`_types.JSON`
  263. datatype, this is a Python callable that will render a given object
  264. as JSON. By default, the Python ``json.dumps`` function is used.
  265. .. versionchanged:: 1.3.7 The SQLite dialect renamed this from
  266. ``_json_serializer``.
  267. :param label_length=None: optional integer value which limits
  268. the size of dynamically generated column labels to that many
  269. characters. If less than 6, labels are generated as
  270. "_(counter)". If ``None``, the value of
  271. ``dialect.max_identifier_length``, which may be affected via the
  272. :paramref:`_sa.create_engine.max_identifier_length` parameter,
  273. is used instead. The value of
  274. :paramref:`_sa.create_engine.label_length`
  275. may not be larger than that of
  276. :paramref:`_sa.create_engine.max_identfier_length`.
  277. .. seealso::
  278. :paramref:`_sa.create_engine.max_identifier_length`
  279. :param logging_name: String identifier which will be used within
  280. the "name" field of logging records generated within the
  281. "sqlalchemy.engine" logger. Defaults to a hexstring of the
  282. object's id.
  283. .. seealso::
  284. :ref:`dbengine_logging` - further detail on how to configure
  285. logging.
  286. :paramref:`_engine.Connection.execution_options.logging_token`
  287. :param max_identifier_length: integer; override the max_identifier_length
  288. determined by the dialect. if ``None`` or zero, has no effect. This
  289. is the database's configured maximum number of characters that may be
  290. used in a SQL identifier such as a table name, column name, or label
  291. name. All dialects determine this value automatically, however in the
  292. case of a new database version for which this value has changed but
  293. SQLAlchemy's dialect has not been adjusted, the value may be passed
  294. here.
  295. .. versionadded:: 1.3.9
  296. .. seealso::
  297. :paramref:`_sa.create_engine.label_length`
  298. :param max_overflow=10: the number of connections to allow in
  299. connection pool "overflow", that is connections that can be
  300. opened above and beyond the pool_size setting, which defaults
  301. to five. this is only used with :class:`~sqlalchemy.pool.QueuePool`.
  302. :param module=None: reference to a Python module object (the module
  303. itself, not its string name). Specifies an alternate DBAPI module to
  304. be used by the engine's dialect. Each sub-dialect references a
  305. specific DBAPI which will be imported before first connect. This
  306. parameter causes the import to be bypassed, and the given module to
  307. be used instead. Can be used for testing of DBAPIs as well as to
  308. inject "mock" DBAPI implementations into the :class:`_engine.Engine`.
  309. :param paramstyle=None: The `paramstyle <https://legacy.python.org/dev/peps/pep-0249/#paramstyle>`_
  310. to use when rendering bound parameters. This style defaults to the
  311. one recommended by the DBAPI itself, which is retrieved from the
  312. ``.paramstyle`` attribute of the DBAPI. However, most DBAPIs accept
  313. more than one paramstyle, and in particular it may be desirable
  314. to change a "named" paramstyle into a "positional" one, or vice versa.
  315. When this attribute is passed, it should be one of the values
  316. ``"qmark"``, ``"numeric"``, ``"named"``, ``"format"`` or
  317. ``"pyformat"``, and should correspond to a parameter style known
  318. to be supported by the DBAPI in use.
  319. :param pool=None: an already-constructed instance of
  320. :class:`~sqlalchemy.pool.Pool`, such as a
  321. :class:`~sqlalchemy.pool.QueuePool` instance. If non-None, this
  322. pool will be used directly as the underlying connection pool
  323. for the engine, bypassing whatever connection parameters are
  324. present in the URL argument. For information on constructing
  325. connection pools manually, see :ref:`pooling_toplevel`.
  326. :param poolclass=None: a :class:`~sqlalchemy.pool.Pool`
  327. subclass, which will be used to create a connection pool
  328. instance using the connection parameters given in the URL. Note
  329. this differs from ``pool`` in that you don't actually
  330. instantiate the pool in this case, you just indicate what type
  331. of pool to be used.
  332. :param pool_logging_name: String identifier which will be used within
  333. the "name" field of logging records generated within the
  334. "sqlalchemy.pool" logger. Defaults to a hexstring of the object's
  335. id.
  336. .. seealso::
  337. :ref:`dbengine_logging` - further detail on how to configure
  338. logging.
  339. :param pool_pre_ping: boolean, if True will enable the connection pool
  340. "pre-ping" feature that tests connections for liveness upon
  341. each checkout.
  342. .. versionadded:: 1.2
  343. .. seealso::
  344. :ref:`pool_disconnects_pessimistic`
  345. :param pool_size=5: the number of connections to keep open
  346. inside the connection pool. This used with
  347. :class:`~sqlalchemy.pool.QueuePool` as
  348. well as :class:`~sqlalchemy.pool.SingletonThreadPool`. With
  349. :class:`~sqlalchemy.pool.QueuePool`, a ``pool_size`` setting
  350. of 0 indicates no limit; to disable pooling, set ``poolclass`` to
  351. :class:`~sqlalchemy.pool.NullPool` instead.
  352. :param pool_recycle=-1: this setting causes the pool to recycle
  353. connections after the given number of seconds has passed. It
  354. defaults to -1, or no timeout. For example, setting to 3600
  355. means connections will be recycled after one hour. Note that
  356. MySQL in particular will disconnect automatically if no
  357. activity is detected on a connection for eight hours (although
  358. this is configurable with the MySQLDB connection itself and the
  359. server configuration as well).
  360. .. seealso::
  361. :ref:`pool_setting_recycle`
  362. :param pool_reset_on_return='rollback': set the
  363. :paramref:`_pool.Pool.reset_on_return` parameter of the underlying
  364. :class:`_pool.Pool` object, which can be set to the values
  365. ``"rollback"``, ``"commit"``, or ``None``.
  366. .. seealso::
  367. :ref:`pool_reset_on_return`
  368. :ref:`dbapi_autocommit_skip_rollback` - a more modern approach
  369. to using connections with no transactional instructions
  370. :param pool_timeout=30: number of seconds to wait before giving
  371. up on getting a connection from the pool. This is only used
  372. with :class:`~sqlalchemy.pool.QueuePool`. This can be a float but is
  373. subject to the limitations of Python time functions which may not be
  374. reliable in the tens of milliseconds.
  375. .. note: don't use 30.0 above, it seems to break with the :param tag
  376. :param pool_use_lifo=False: use LIFO (last-in-first-out) when retrieving
  377. connections from :class:`.QueuePool` instead of FIFO
  378. (first-in-first-out). Using LIFO, a server-side timeout scheme can
  379. reduce the number of connections used during non- peak periods of
  380. use. When planning for server-side timeouts, ensure that a recycle or
  381. pre-ping strategy is in use to gracefully handle stale connections.
  382. .. versionadded:: 1.3
  383. .. seealso::
  384. :ref:`pool_use_lifo`
  385. :ref:`pool_disconnects`
  386. :param plugins: string list of plugin names to load. See
  387. :class:`.CreateEnginePlugin` for background.
  388. .. versionadded:: 1.2.3
  389. :param query_cache_size: size of the cache used to cache the SQL string
  390. form of queries. Set to zero to disable caching.
  391. The cache is pruned of its least recently used items when its size reaches
  392. N * 1.5. Defaults to 500, meaning the cache will always store at least
  393. 500 SQL statements when filled, and will grow up to 750 items at which
  394. point it is pruned back down to 500 by removing the 250 least recently
  395. used items.
  396. Caching is accomplished on a per-statement basis by generating a
  397. cache key that represents the statement's structure, then generating
  398. string SQL for the current dialect only if that key is not present
  399. in the cache. All statements support caching, however some features
  400. such as an INSERT with a large set of parameters will intentionally
  401. bypass the cache. SQL logging will indicate statistics for each
  402. statement whether or not it were pull from the cache.
  403. .. note:: some ORM functions related to unit-of-work persistence as well
  404. as some attribute loading strategies will make use of individual
  405. per-mapper caches outside of the main cache.
  406. .. seealso::
  407. :ref:`sql_caching`
  408. .. versionadded:: 1.4
  409. :param skip_autocommit_rollback: When True, the dialect will
  410. unconditionally skip all calls to the DBAPI ``connection.rollback()``
  411. method if the DBAPI connection is confirmed to be in "autocommit" mode.
  412. The availability of this feature is dialect specific; if not available,
  413. a ``NotImplementedError`` is raised by the dialect when rollback occurs.
  414. .. seealso::
  415. :ref:`dbapi_autocommit_skip_rollback`
  416. .. versionadded:: 2.0.43
  417. :param use_insertmanyvalues: True by default, use the "insertmanyvalues"
  418. execution style for INSERT..RETURNING statements by default.
  419. .. versionadded:: 2.0
  420. .. seealso::
  421. :ref:`engine_insertmanyvalues`
  422. """ # noqa
  423. if "strategy" in kwargs:
  424. strat = kwargs.pop("strategy")
  425. if strat == "mock":
  426. # this case is deprecated
  427. return create_mock_engine(url, **kwargs) # type: ignore
  428. else:
  429. raise exc.ArgumentError("unknown strategy: %r" % strat)
  430. kwargs.pop("empty_in_strategy", None)
  431. # create url.URL object
  432. u = _url.make_url(url)
  433. u, plugins, kwargs = u._instantiate_plugins(kwargs)
  434. entrypoint = u._get_entrypoint()
  435. _is_async = kwargs.pop("_is_async", False)
  436. if _is_async:
  437. dialect_cls = entrypoint.get_async_dialect_cls(u)
  438. else:
  439. dialect_cls = entrypoint.get_dialect_cls(u)
  440. if kwargs.pop("_coerce_config", False):
  441. def pop_kwarg(key: str, default: Optional[Any] = None) -> Any:
  442. value = kwargs.pop(key, default)
  443. if key in dialect_cls.engine_config_types:
  444. value = dialect_cls.engine_config_types[key](value)
  445. return value
  446. else:
  447. pop_kwarg = kwargs.pop # type: ignore
  448. dialect_args = {}
  449. # consume dialect arguments from kwargs
  450. for k in util.get_cls_kwargs(dialect_cls):
  451. if k in kwargs:
  452. dialect_args[k] = pop_kwarg(k)
  453. dbapi = kwargs.pop("module", None)
  454. if dbapi is None:
  455. dbapi_args = {}
  456. if "import_dbapi" in dialect_cls.__dict__:
  457. dbapi_meth = dialect_cls.import_dbapi
  458. elif hasattr(dialect_cls, "dbapi") and inspect.ismethod(
  459. dialect_cls.dbapi
  460. ):
  461. util.warn_deprecated(
  462. "The dbapi() classmethod on dialect classes has been "
  463. "renamed to import_dbapi(). Implement an import_dbapi() "
  464. f"classmethod directly on class {dialect_cls} to remove this "
  465. "warning; the old .dbapi() classmethod may be maintained for "
  466. "backwards compatibility.",
  467. "2.0",
  468. )
  469. dbapi_meth = dialect_cls.dbapi
  470. else:
  471. dbapi_meth = dialect_cls.import_dbapi
  472. for k in util.get_func_kwargs(dbapi_meth):
  473. if k in kwargs:
  474. dbapi_args[k] = pop_kwarg(k)
  475. dbapi = dbapi_meth(**dbapi_args)
  476. dialect_args["dbapi"] = dbapi
  477. dialect_args.setdefault("compiler_linting", compiler.NO_LINTING)
  478. enable_from_linting = kwargs.pop("enable_from_linting", True)
  479. if enable_from_linting:
  480. dialect_args["compiler_linting"] ^= compiler.COLLECT_CARTESIAN_PRODUCTS
  481. for plugin in plugins:
  482. plugin.handle_dialect_kwargs(dialect_cls, dialect_args)
  483. # create dialect
  484. dialect = dialect_cls(**dialect_args)
  485. # assemble connection arguments
  486. (cargs_tup, cparams) = dialect.create_connect_args(u)
  487. cparams.update(pop_kwarg("connect_args", {}))
  488. if "async_fallback" in cparams and util.asbool(cparams["async_fallback"]):
  489. util.warn_deprecated(
  490. "The async_fallback dialect argument is deprecated and will be "
  491. "removed in SQLAlchemy 2.1.",
  492. "2.0",
  493. )
  494. cargs = list(cargs_tup) # allow mutability
  495. # look for existing pool or create
  496. pool = pop_kwarg("pool", None)
  497. if pool is None:
  498. def connect(
  499. connection_record: Optional[ConnectionPoolEntry] = None,
  500. ) -> DBAPIConnection:
  501. if dialect._has_events:
  502. for fn in dialect.dispatch.do_connect:
  503. connection = cast(
  504. DBAPIConnection,
  505. fn(dialect, connection_record, cargs, cparams),
  506. )
  507. if connection is not None:
  508. return connection
  509. return dialect.connect(*cargs, **cparams)
  510. creator = pop_kwarg("creator", connect)
  511. poolclass = pop_kwarg("poolclass", None)
  512. if poolclass is None:
  513. poolclass = dialect.get_dialect_pool_class(u)
  514. pool_args = {"dialect": dialect}
  515. # consume pool arguments from kwargs, translating a few of
  516. # the arguments
  517. for k in util.get_cls_kwargs(poolclass):
  518. tk = _pool_translate_kwargs.get(k, k)
  519. if tk in kwargs:
  520. pool_args[k] = pop_kwarg(tk)
  521. for plugin in plugins:
  522. plugin.handle_pool_kwargs(poolclass, pool_args)
  523. pool = poolclass(creator, **pool_args)
  524. else:
  525. pool._dialect = dialect
  526. if (
  527. hasattr(pool, "_is_asyncio")
  528. and pool._is_asyncio is not dialect.is_async
  529. ):
  530. raise exc.ArgumentError(
  531. f"Pool class {pool.__class__.__name__} cannot be "
  532. f"used with {'non-' if not dialect.is_async else ''}"
  533. "asyncio engine",
  534. code="pcls",
  535. )
  536. # create engine.
  537. if not pop_kwarg("future", True):
  538. raise exc.ArgumentError(
  539. "The 'future' parameter passed to "
  540. "create_engine() may only be set to True."
  541. )
  542. engineclass = base.Engine
  543. engine_args = {}
  544. for k in util.get_cls_kwargs(engineclass):
  545. if k in kwargs:
  546. engine_args[k] = pop_kwarg(k)
  547. # internal flags used by the test suite for instrumenting / proxying
  548. # engines with mocks etc.
  549. _initialize = kwargs.pop("_initialize", True)
  550. # all kwargs should be consumed
  551. if kwargs:
  552. raise TypeError(
  553. "Invalid argument(s) %s sent to create_engine(), "
  554. "using configuration %s/%s/%s. Please check that the "
  555. "keyword arguments are appropriate for this combination "
  556. "of components."
  557. % (
  558. ",".join("'%s'" % k for k in kwargs),
  559. dialect.__class__.__name__,
  560. pool.__class__.__name__,
  561. engineclass.__name__,
  562. )
  563. )
  564. engine = engineclass(pool, dialect, u, **engine_args)
  565. if _initialize:
  566. do_on_connect = dialect.on_connect_url(u)
  567. if do_on_connect:
  568. def on_connect(
  569. dbapi_connection: DBAPIConnection,
  570. connection_record: ConnectionPoolEntry,
  571. ) -> None:
  572. assert do_on_connect is not None
  573. do_on_connect(dbapi_connection)
  574. event.listen(pool, "connect", on_connect)
  575. builtin_on_connect = dialect._builtin_onconnect()
  576. if builtin_on_connect:
  577. event.listen(pool, "connect", builtin_on_connect)
  578. def first_connect(
  579. dbapi_connection: DBAPIConnection,
  580. connection_record: ConnectionPoolEntry,
  581. ) -> None:
  582. c = base.Connection(
  583. engine,
  584. connection=_AdhocProxiedConnection(
  585. dbapi_connection, connection_record
  586. ),
  587. _has_events=False,
  588. # reconnecting will be a reentrant condition, so if the
  589. # connection goes away, Connection is then closed
  590. _allow_revalidate=False,
  591. # dont trigger the autobegin sequence
  592. # within the up front dialect checks
  593. _allow_autobegin=False,
  594. )
  595. c._execution_options = util.EMPTY_DICT
  596. try:
  597. dialect.initialize(c)
  598. finally:
  599. # note that "invalidated" and "closed" are mutually
  600. # exclusive in 1.4 Connection.
  601. if not c.invalidated and not c.closed:
  602. # transaction is rolled back otherwise, tested by
  603. # test/dialect/postgresql/test_dialect.py
  604. # ::MiscBackendTest::test_initial_transaction_state
  605. dialect.do_rollback(c.connection)
  606. # previously, the "first_connect" event was used here, which was then
  607. # scaled back if the "on_connect" handler were present. now,
  608. # since "on_connect" is virtually always present, just use
  609. # "connect" event with once_unless_exception in all cases so that
  610. # the connection event flow is consistent in all cases.
  611. event.listen(
  612. pool, "connect", first_connect, _once_unless_exception=True
  613. )
  614. dialect_cls.engine_created(engine)
  615. if entrypoint is not dialect_cls:
  616. entrypoint.engine_created(engine)
  617. for plugin in plugins:
  618. plugin.engine_created(engine)
  619. return engine
  620. def engine_from_config(
  621. configuration: Dict[str, Any], prefix: str = "sqlalchemy.", **kwargs: Any
  622. ) -> Engine:
  623. """Create a new Engine instance using a configuration dictionary.
  624. The dictionary is typically produced from a config file.
  625. The keys of interest to ``engine_from_config()`` should be prefixed, e.g.
  626. ``sqlalchemy.url``, ``sqlalchemy.echo``, etc. The 'prefix' argument
  627. indicates the prefix to be searched for. Each matching key (after the
  628. prefix is stripped) is treated as though it were the corresponding keyword
  629. argument to a :func:`_sa.create_engine` call.
  630. The only required key is (assuming the default prefix) ``sqlalchemy.url``,
  631. which provides the :ref:`database URL <database_urls>`.
  632. A select set of keyword arguments will be "coerced" to their
  633. expected type based on string values. The set of arguments
  634. is extensible per-dialect using the ``engine_config_types`` accessor.
  635. :param configuration: A dictionary (typically produced from a config file,
  636. but this is not a requirement). Items whose keys start with the value
  637. of 'prefix' will have that prefix stripped, and will then be passed to
  638. :func:`_sa.create_engine`.
  639. :param prefix: Prefix to match and then strip from keys
  640. in 'configuration'.
  641. :param kwargs: Each keyword argument to ``engine_from_config()`` itself
  642. overrides the corresponding item taken from the 'configuration'
  643. dictionary. Keyword arguments should *not* be prefixed.
  644. """
  645. options = {
  646. key[len(prefix) :]: configuration[key]
  647. for key in configuration
  648. if key.startswith(prefix)
  649. }
  650. options["_coerce_config"] = True
  651. options.update(kwargs)
  652. url = options.pop("url")
  653. return create_engine(url, **options)
  654. @overload
  655. def create_pool_from_url(
  656. url: Union[str, URL],
  657. *,
  658. poolclass: Optional[Type[Pool]] = ...,
  659. logging_name: str = ...,
  660. pre_ping: bool = ...,
  661. size: int = ...,
  662. recycle: int = ...,
  663. reset_on_return: Optional[_ResetStyleArgType] = ...,
  664. timeout: float = ...,
  665. use_lifo: bool = ...,
  666. **kwargs: Any,
  667. ) -> Pool: ...
  668. @overload
  669. def create_pool_from_url(url: Union[str, URL], **kwargs: Any) -> Pool: ...
  670. def create_pool_from_url(url: Union[str, URL], **kwargs: Any) -> Pool:
  671. """Create a pool instance from the given url.
  672. If ``poolclass`` is not provided the pool class used
  673. is selected using the dialect specified in the URL.
  674. The arguments passed to :func:`_sa.create_pool_from_url` are
  675. identical to the pool argument passed to the :func:`_sa.create_engine`
  676. function.
  677. .. versionadded:: 2.0.10
  678. """
  679. for key in _pool_translate_kwargs:
  680. if key in kwargs:
  681. kwargs[_pool_translate_kwargs[key]] = kwargs.pop(key)
  682. engine = create_engine(url, **kwargs, _initialize=False)
  683. return engine.pool
  684. _pool_translate_kwargs = immutabledict(
  685. {
  686. "logging_name": "pool_logging_name",
  687. "echo": "echo_pool",
  688. "timeout": "pool_timeout",
  689. "recycle": "pool_recycle",
  690. "events": "pool_events", # deprecated
  691. "reset_on_return": "pool_reset_on_return",
  692. "pre_ping": "pool_pre_ping",
  693. "use_lifo": "pool_use_lifo",
  694. }
  695. )