Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/sqlalchemy/engine/create.py: 68%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

166 statements  

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 

8from __future__ import annotations 

9 

10import inspect 

11import typing 

12from typing import Any 

13from typing import Callable 

14from typing import cast 

15from typing import Dict 

16from typing import List 

17from typing import Optional 

18from typing import overload 

19from typing import Type 

20from typing import Union 

21 

22from . import base 

23from . import url as _url 

24from .interfaces import DBAPIConnection 

25from .mock import create_mock_engine 

26from .. import event 

27from .. import exc 

28from .. import util 

29from ..pool import _AdhocProxiedConnection 

30from ..pool import ConnectionPoolEntry 

31from ..sql import compiler 

32from ..util import immutabledict 

33 

34if 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 

49def 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 

89def 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) 

116def 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( 

137 "mysql+mysqldb://scott:tiger@hostname/dbname", 

138 pool_recycle=3600, 

139 echo=True, 

140 ) 

141 

142 The string form of the URL is 

143 ``dialect[+driver]://user:password@host/dbname[?key=value..]``, where 

144 ``dialect`` is a database name such as ``mysql``, ``oracle``, 

145 ``postgresql``, etc., and ``driver`` the name of a DBAPI, such as 

146 ``psycopg2``, ``pyodbc``, ``cx_oracle``, etc. Alternatively, 

147 the URL can be an instance of :class:`~sqlalchemy.engine.url.URL`. 

148 

149 ``**kwargs`` takes a wide variety of options which are routed 

150 towards their appropriate components. Arguments may be specific to 

151 the :class:`_engine.Engine`, the underlying :class:`.Dialect`, 

152 as well as the 

153 :class:`_pool.Pool`. Specific dialects also accept keyword arguments that 

154 are unique to that dialect. Here, we describe the parameters 

155 that are common to most :func:`_sa.create_engine()` usage. 

156 

157 Once established, the newly resulting :class:`_engine.Engine` will 

158 request a connection from the underlying :class:`_pool.Pool` once 

159 :meth:`_engine.Engine.connect` is called, or a method which depends on it 

160 such as :meth:`_engine.Engine.execute` is invoked. The 

161 :class:`_pool.Pool` in turn 

162 will establish the first actual DBAPI connection when this request 

163 is received. The :func:`_sa.create_engine` call itself does **not** 

164 establish any actual DBAPI connections directly. 

165 

166 .. seealso:: 

167 

168 :doc:`/core/engines` 

169 

170 :doc:`/dialects/index` 

171 

172 :ref:`connections_toplevel` 

173 

174 :param connect_args: a dictionary of options which will be 

175 passed directly to the DBAPI's ``connect()`` method as 

176 additional keyword arguments. See the example 

177 at :ref:`custom_dbapi_args`. 

178 

179 :param creator: a callable which returns a DBAPI connection. 

180 This creation function will be passed to the underlying 

181 connection pool and will be used to create all new database 

182 connections. Usage of this function causes connection 

183 parameters specified in the URL argument to be bypassed. 

184 

185 This hook is not as flexible as the newer 

186 :meth:`_events.DialectEvents.do_connect` hook which allows complete 

187 control over how a connection is made to the database, given the full 

188 set of URL arguments and state beforehand. 

189 

190 .. seealso:: 

191 

192 :meth:`_events.DialectEvents.do_connect` - event hook that allows 

193 full control over DBAPI connection mechanics. 

194 

195 :ref:`custom_dbapi_args` 

196 

197 :param echo=False: if True, the Engine will log all statements 

198 as well as a ``repr()`` of their parameter lists to the default log 

199 handler, which defaults to ``sys.stdout`` for output. If set to the 

200 string ``"debug"``, result rows will be printed to the standard output 

201 as well. The ``echo`` attribute of ``Engine`` can be modified at any 

202 time to turn logging on and off; direct control of logging is also 

203 available using the standard Python ``logging`` module. 

204 

205 .. seealso:: 

206 

207 :ref:`dbengine_logging` - further detail on how to configure 

208 logging. 

209 

210 

211 :param echo_pool=False: if True, the connection pool will log 

212 informational output such as when connections are invalidated 

213 as well as when connections are recycled to the default log handler, 

214 which defaults to ``sys.stdout`` for output. If set to the string 

215 ``"debug"``, the logging will include pool checkouts and checkins. 

216 Direct control of logging is also available using the standard Python 

217 ``logging`` module. 

218 

219 .. seealso:: 

220 

221 :ref:`dbengine_logging` - further detail on how to configure 

222 logging. 

223 

224 

225 :param empty_in_strategy: No longer used; SQLAlchemy now uses 

226 "empty set" behavior for IN in all cases. 

227 

228 :param enable_from_linting: defaults to True. Will emit a warning 

229 if a given SELECT statement is found to have un-linked FROM elements 

230 which would cause a cartesian product. 

231 

232 .. versionadded:: 1.4 

233 

234 .. seealso:: 

235 

236 :ref:`change_4737` 

237 

238 :param execution_options: Dictionary execution options which will 

239 be applied to all connections. See 

240 :meth:`~sqlalchemy.engine.Connection.execution_options` 

241 

242 :param future: Use the 2.0 style :class:`_engine.Engine` and 

243 :class:`_engine.Connection` API. 

244 

245 As of SQLAlchemy 2.0, this parameter is present for backwards 

246 compatibility only and must remain at its default value of ``True``. 

247 

248 The :paramref:`_sa.create_engine.future` parameter will be 

249 deprecated in a subsequent 2.x release and eventually removed. 

250 

251 .. versionadded:: 1.4 

252 

253 .. versionchanged:: 2.0 All :class:`_engine.Engine` objects are 

254 "future" style engines and there is no longer a ``future=False`` 

255 mode of operation. 

256 

257 .. seealso:: 

258 

259 :ref:`migration_20_toplevel` 

260 

261 :param hide_parameters: Boolean, when set to True, SQL statement parameters 

262 will not be displayed in INFO logging nor will they be formatted into 

263 the string representation of :class:`.StatementError` objects. 

264 

265 .. seealso:: 

266 

267 :ref:`dbengine_logging` - further detail on how to configure 

268 logging. 

269 

270 :param implicit_returning=True: Legacy parameter that may only be set 

271 to True. In SQLAlchemy 2.0, this parameter does nothing. In order to 

272 disable "implicit returning" for statements invoked by the ORM, 

273 configure this on a per-table basis using the 

274 :paramref:`.Table.implicit_returning` parameter. 

275 

276 

277 :param insertmanyvalues_page_size: number of rows to format into an 

278 INSERT statement when the statement uses "insertmanyvalues" mode, which is 

279 a paged form of bulk insert that is used for many backends when using 

280 :term:`executemany` execution typically in conjunction with RETURNING. 

281 Defaults to 1000, but may also be subject to dialect-specific limiting 

282 factors which may override this value on a per-statement basis. 

283 

284 .. versionadded:: 2.0 

285 

286 .. seealso:: 

287 

288 :ref:`engine_insertmanyvalues` 

289 

290 :ref:`engine_insertmanyvalues_page_size` 

291 

292 :paramref:`_engine.Connection.execution_options.insertmanyvalues_page_size` 

293 

294 :param isolation_level: optional string name of an isolation level 

295 which will be set on all new connections unconditionally. 

296 Isolation levels are typically some subset of the string names 

297 ``"SERIALIZABLE"``, ``"REPEATABLE READ"``, 

298 ``"READ COMMITTED"``, ``"READ UNCOMMITTED"`` and ``"AUTOCOMMIT"`` 

299 based on backend. 

300 

301 The :paramref:`_sa.create_engine.isolation_level` parameter is 

302 in contrast to the 

303 :paramref:`.Connection.execution_options.isolation_level` 

304 execution option, which may be set on an individual 

305 :class:`.Connection`, as well as the same parameter passed to 

306 :meth:`.Engine.execution_options`, where it may be used to create 

307 multiple engines with different isolation levels that share a common 

308 connection pool and dialect. 

309 

310 .. versionchanged:: 2.0 The 

311 :paramref:`_sa.create_engine.isolation_level` 

312 parameter has been generalized to work on all dialects which support 

313 the concept of isolation level, and is provided as a more succinct, 

314 up front configuration switch in contrast to the execution option 

315 which is more of an ad-hoc programmatic option. 

316 

317 .. seealso:: 

318 

319 :ref:`dbapi_autocommit` 

320 

321 :param json_deserializer: for dialects that support the 

322 :class:`_types.JSON` 

323 datatype, this is a Python callable that will convert a JSON string 

324 to a Python object. By default, the Python ``json.loads`` function is 

325 used. 

326 

327 :param json_serializer: for dialects that support the :class:`_types.JSON` 

328 datatype, this is a Python callable that will render a given object 

329 as JSON. By default, the Python ``json.dumps`` function is used. 

330 

331 :param label_length=None: optional integer value which limits 

332 the size of dynamically generated column labels to that many 

333 characters. If less than 6, labels are generated as 

334 "_(counter)". If ``None``, the value of 

335 ``dialect.max_identifier_length``, which may be affected via the 

336 :paramref:`_sa.create_engine.max_identifier_length` parameter, 

337 is used instead. The value of 

338 :paramref:`_sa.create_engine.label_length` 

339 may not be larger than that of 

340 :paramref:`_sa.create_engine.max_identfier_length`. 

341 

342 .. seealso:: 

343 

344 :paramref:`_sa.create_engine.max_identifier_length` 

345 

346 :param logging_name: String identifier which will be used within 

347 the "name" field of logging records generated within the 

348 "sqlalchemy.engine" logger. Defaults to a hexstring of the 

349 object's id. 

350 

351 .. seealso:: 

352 

353 :ref:`dbengine_logging` - further detail on how to configure 

354 logging. 

355 

356 :paramref:`_engine.Connection.execution_options.logging_token` 

357 

358 :param max_identifier_length: integer; override the max_identifier_length 

359 determined by the dialect. if ``None`` or zero, has no effect. This 

360 is the database's configured maximum number of characters that may be 

361 used in a SQL identifier such as a table name, column name, or label 

362 name. All dialects determine this value automatically, however in the 

363 case of a new database version for which this value has changed but 

364 SQLAlchemy's dialect has not been adjusted, the value may be passed 

365 here. 

366 

367 .. seealso:: 

368 

369 :paramref:`_sa.create_engine.label_length` 

370 

371 :param max_overflow=10: the number of connections to allow in 

372 connection pool "overflow", that is connections that can be 

373 opened above and beyond the pool_size setting, which defaults 

374 to five. this is only used with :class:`~sqlalchemy.pool.QueuePool`. 

375 

376 :param module=None: reference to a Python module object (the module 

377 itself, not its string name). Specifies an alternate DBAPI module to 

378 be used by the engine's dialect. Each sub-dialect references a 

379 specific DBAPI which will be imported before first connect. This 

380 parameter causes the import to be bypassed, and the given module to 

381 be used instead. Can be used for testing of DBAPIs as well as to 

382 inject "mock" DBAPI implementations into the :class:`_engine.Engine`. 

383 

384 :param paramstyle=None: The `paramstyle <https://legacy.python.org/dev/peps/pep-0249/#paramstyle>`_ 

385 to use when rendering bound parameters. This style defaults to the 

386 one recommended by the DBAPI itself, which is retrieved from the 

387 ``.paramstyle`` attribute of the DBAPI. However, most DBAPIs accept 

388 more than one paramstyle, and in particular it may be desirable 

389 to change a "named" paramstyle into a "positional" one, or vice versa. 

390 When this attribute is passed, it should be one of the values 

391 ``"qmark"``, ``"numeric"``, ``"named"``, ``"format"`` or 

392 ``"pyformat"``, and should correspond to a parameter style known 

393 to be supported by the DBAPI in use. 

394 

395 :param pool=None: an already-constructed instance of 

396 :class:`~sqlalchemy.pool.Pool`, such as a 

397 :class:`~sqlalchemy.pool.QueuePool` instance. If non-None, this 

398 pool will be used directly as the underlying connection pool 

399 for the engine, bypassing whatever connection parameters are 

400 present in the URL argument. For information on constructing 

401 connection pools manually, see :ref:`pooling_toplevel`. 

402 

403 :param poolclass=None: a :class:`~sqlalchemy.pool.Pool` 

404 subclass, which will be used to create a connection pool 

405 instance using the connection parameters given in the URL. Note 

406 this differs from ``pool`` in that you don't actually 

407 instantiate the pool in this case, you just indicate what type 

408 of pool to be used. 

409 

410 :param pool_logging_name: String identifier which will be used within 

411 the "name" field of logging records generated within the 

412 "sqlalchemy.pool" logger. Defaults to a hexstring of the object's 

413 id. 

414 

415 .. seealso:: 

416 

417 :ref:`dbengine_logging` - further detail on how to configure 

418 logging. 

419 

420 :param pool_pre_ping: boolean, if True will enable the connection pool 

421 "pre-ping" feature that tests connections for liveness upon 

422 each checkout. 

423 

424 .. seealso:: 

425 

426 :ref:`pool_disconnects_pessimistic` 

427 

428 :param pool_size=5: the number of connections to keep open 

429 inside the connection pool. This used with 

430 :class:`~sqlalchemy.pool.QueuePool` as 

431 well as :class:`~sqlalchemy.pool.SingletonThreadPool`. With 

432 :class:`~sqlalchemy.pool.QueuePool`, a ``pool_size`` setting 

433 of 0 indicates no limit; to disable pooling, set ``poolclass`` to 

434 :class:`~sqlalchemy.pool.NullPool` instead. 

435 

436 :param pool_recycle=-1: this setting causes the pool to recycle 

437 connections after the given number of seconds has passed. It 

438 defaults to -1, or no timeout. For example, setting to 3600 

439 means connections will be recycled after one hour. Note that 

440 MySQL in particular will disconnect automatically if no 

441 activity is detected on a connection for eight hours (although 

442 this is configurable with the MySQLDB connection itself and the 

443 server configuration as well). 

444 

445 .. seealso:: 

446 

447 :ref:`pool_setting_recycle` 

448 

449 :param pool_reset_on_return='rollback': set the 

450 :paramref:`_pool.Pool.reset_on_return` parameter of the underlying 

451 :class:`_pool.Pool` object, which can be set to the values 

452 ``"rollback"``, ``"commit"``, or ``None``. 

453 

454 .. seealso:: 

455 

456 :ref:`pool_reset_on_return` 

457 

458 :param pool_timeout=30: number of seconds to wait before giving 

459 up on getting a connection from the pool. This is only used 

460 with :class:`~sqlalchemy.pool.QueuePool`. This can be a float but is 

461 subject to the limitations of Python time functions which may not be 

462 reliable in the tens of milliseconds. 

463 

464 .. note: don't use 30.0 above, it seems to break with the :param tag 

465 

466 :param pool_use_lifo=False: use LIFO (last-in-first-out) when retrieving 

467 connections from :class:`.QueuePool` instead of FIFO 

468 (first-in-first-out). Using LIFO, a server-side timeout scheme can 

469 reduce the number of connections used during non- peak periods of 

470 use. When planning for server-side timeouts, ensure that a recycle or 

471 pre-ping strategy is in use to gracefully handle stale connections. 

472 

473 .. seealso:: 

474 

475 :ref:`pool_use_lifo` 

476 

477 :ref:`pool_disconnects` 

478 

479 :param plugins: string list of plugin names to load. See 

480 :class:`.CreateEnginePlugin` for background. 

481 

482 :param query_cache_size: size of the cache used to cache the SQL string 

483 form of queries. Set to zero to disable caching. 

484 

485 The cache is pruned of its least recently used items when its size reaches 

486 N * 1.5. Defaults to 500, meaning the cache will always store at least 

487 500 SQL statements when filled, and will grow up to 750 items at which 

488 point it is pruned back down to 500 by removing the 250 least recently 

489 used items. 

490 

491 Caching is accomplished on a per-statement basis by generating a 

492 cache key that represents the statement's structure, then generating 

493 string SQL for the current dialect only if that key is not present 

494 in the cache. All statements support caching, however some features 

495 such as an INSERT with a large set of parameters will intentionally 

496 bypass the cache. SQL logging will indicate statistics for each 

497 statement whether or not it were pull from the cache. 

498 

499 .. note:: some ORM functions related to unit-of-work persistence as well 

500 as some attribute loading strategies will make use of individual 

501 per-mapper caches outside of the main cache. 

502 

503 

504 .. seealso:: 

505 

506 :ref:`sql_caching` 

507 

508 .. versionadded:: 1.4 

509 

510 :param use_insertmanyvalues: True by default, use the "insertmanyvalues" 

511 execution style for INSERT..RETURNING statements by default. 

512 

513 .. versionadded:: 2.0 

514 

515 .. seealso:: 

516 

517 :ref:`engine_insertmanyvalues` 

518 

519 """ # noqa 

520 

521 if "strategy" in kwargs: 

522 strat = kwargs.pop("strategy") 

523 if strat == "mock": 

524 # this case is deprecated 

525 return create_mock_engine(url, **kwargs) # type: ignore 

526 else: 

527 raise exc.ArgumentError("unknown strategy: %r" % strat) 

528 

529 kwargs.pop("empty_in_strategy", None) 

530 

531 # create url.URL object 

532 u = _url.make_url(url) 

533 

534 u, plugins, kwargs = u._instantiate_plugins(kwargs) 

535 

536 entrypoint = u._get_entrypoint() 

537 _is_async = kwargs.pop("_is_async", False) 

538 if _is_async: 

539 dialect_cls = entrypoint.get_async_dialect_cls(u) 

540 else: 

541 dialect_cls = entrypoint.get_dialect_cls(u) 

542 

543 if kwargs.pop("_coerce_config", False): 

544 

545 def pop_kwarg(key: str, default: Optional[Any] = None) -> Any: 

546 value = kwargs.pop(key, default) 

547 if key in dialect_cls.engine_config_types: 

548 value = dialect_cls.engine_config_types[key](value) 

549 return value 

550 

551 else: 

552 pop_kwarg = kwargs.pop # type: ignore 

553 

554 dialect_args = {} 

555 # consume dialect arguments from kwargs 

556 for k in util.get_cls_kwargs(dialect_cls): 

557 if k in kwargs: 

558 dialect_args[k] = pop_kwarg(k) 

559 

560 dbapi = kwargs.pop("module", None) 

561 if dbapi is None: 

562 dbapi_args = {} 

563 

564 if "import_dbapi" in dialect_cls.__dict__: 

565 dbapi_meth = dialect_cls.import_dbapi 

566 

567 elif hasattr(dialect_cls, "dbapi") and inspect.ismethod( 

568 dialect_cls.dbapi 

569 ): 

570 util.warn_deprecated( 

571 "The dbapi() classmethod on dialect classes has been " 

572 "renamed to import_dbapi(). Implement an import_dbapi() " 

573 f"classmethod directly on class {dialect_cls} to remove this " 

574 "warning; the old .dbapi() classmethod may be maintained for " 

575 "backwards compatibility.", 

576 "2.0", 

577 ) 

578 dbapi_meth = dialect_cls.dbapi 

579 else: 

580 dbapi_meth = dialect_cls.import_dbapi 

581 

582 for k in util.get_func_kwargs(dbapi_meth): 

583 if k in kwargs: 

584 dbapi_args[k] = pop_kwarg(k) 

585 dbapi = dbapi_meth(**dbapi_args) 

586 

587 dialect_args["dbapi"] = dbapi 

588 

589 dialect_args.setdefault("compiler_linting", compiler.NO_LINTING) 

590 enable_from_linting = kwargs.pop("enable_from_linting", True) 

591 if enable_from_linting: 

592 dialect_args["compiler_linting"] ^= compiler.COLLECT_CARTESIAN_PRODUCTS 

593 

594 for plugin in plugins: 

595 plugin.handle_dialect_kwargs(dialect_cls, dialect_args) 

596 

597 # create dialect 

598 dialect = dialect_cls(**dialect_args) 

599 

600 # assemble connection arguments 

601 (cargs_tup, cparams) = dialect.create_connect_args(u) 

602 cparams.update(pop_kwarg("connect_args", {})) 

603 cargs = list(cargs_tup) # allow mutability 

604 

605 # look for existing pool or create 

606 pool = pop_kwarg("pool", None) 

607 if pool is None: 

608 

609 def connect( 

610 connection_record: Optional[ConnectionPoolEntry] = None, 

611 ) -> DBAPIConnection: 

612 if dialect._has_events: 

613 for fn in dialect.dispatch.do_connect: 

614 connection = cast( 

615 DBAPIConnection, 

616 fn(dialect, connection_record, cargs, cparams), 

617 ) 

618 if connection is not None: 

619 return connection 

620 

621 return dialect.connect(*cargs, **cparams) 

622 

623 creator = pop_kwarg("creator", connect) 

624 

625 poolclass = pop_kwarg("poolclass", None) 

626 if poolclass is None: 

627 poolclass = dialect.get_dialect_pool_class(u) 

628 pool_args = {"dialect": dialect} 

629 

630 # consume pool arguments from kwargs, translating a few of 

631 # the arguments 

632 for k in util.get_cls_kwargs(poolclass): 

633 tk = _pool_translate_kwargs.get(k, k) 

634 if tk in kwargs: 

635 pool_args[k] = pop_kwarg(tk) 

636 

637 for plugin in plugins: 

638 plugin.handle_pool_kwargs(poolclass, pool_args) 

639 

640 pool = poolclass(creator, **pool_args) 

641 else: 

642 pool._dialect = dialect 

643 

644 if ( 

645 hasattr(pool, "_is_asyncio") 

646 and pool._is_asyncio is not dialect.is_async 

647 ): 

648 raise exc.ArgumentError( 

649 f"Pool class {pool.__class__.__name__} cannot be " 

650 f"used with {'non-' if not dialect.is_async else ''}" 

651 "asyncio engine", 

652 code="pcls", 

653 ) 

654 

655 # create engine. 

656 if not pop_kwarg("future", True): 

657 raise exc.ArgumentError( 

658 "The 'future' parameter passed to " 

659 "create_engine() may only be set to True." 

660 ) 

661 

662 engineclass = base.Engine 

663 

664 engine_args = {} 

665 for k in util.get_cls_kwargs(engineclass): 

666 if k in kwargs: 

667 engine_args[k] = pop_kwarg(k) 

668 

669 # internal flags used by the test suite for instrumenting / proxying 

670 # engines with mocks etc. 

671 _initialize = kwargs.pop("_initialize", True) 

672 

673 # all kwargs should be consumed 

674 if kwargs: 

675 raise TypeError( 

676 "Invalid argument(s) %s sent to create_engine(), " 

677 "using configuration %s/%s/%s. Please check that the " 

678 "keyword arguments are appropriate for this combination " 

679 "of components." 

680 % ( 

681 ",".join("'%s'" % k for k in kwargs), 

682 dialect.__class__.__name__, 

683 pool.__class__.__name__, 

684 engineclass.__name__, 

685 ) 

686 ) 

687 

688 engine = engineclass(pool, dialect, u, **engine_args) 

689 

690 if _initialize: 

691 do_on_connect = dialect.on_connect_url(u) 

692 if do_on_connect: 

693 

694 def on_connect( 

695 dbapi_connection: DBAPIConnection, 

696 connection_record: ConnectionPoolEntry, 

697 ) -> None: 

698 assert do_on_connect is not None 

699 do_on_connect(dbapi_connection) 

700 

701 event.listen(pool, "connect", on_connect) 

702 

703 builtin_on_connect = dialect._builtin_onconnect() 

704 if builtin_on_connect: 

705 event.listen(pool, "connect", builtin_on_connect) 

706 

707 def first_connect( 

708 dbapi_connection: DBAPIConnection, 

709 connection_record: ConnectionPoolEntry, 

710 ) -> None: 

711 c = base.Connection( 

712 engine, 

713 connection=_AdhocProxiedConnection( 

714 dbapi_connection, connection_record 

715 ), 

716 _has_events=False, 

717 # reconnecting will be a reentrant condition, so if the 

718 # connection goes away, Connection is then closed 

719 _allow_revalidate=False, 

720 # dont trigger the autobegin sequence 

721 # within the up front dialect checks 

722 _allow_autobegin=False, 

723 ) 

724 c._execution_options = util.EMPTY_DICT 

725 

726 try: 

727 dialect.initialize(c) 

728 finally: 

729 # note that "invalidated" and "closed" are mutually 

730 # exclusive in 1.4 Connection. 

731 if not c.invalidated and not c.closed: 

732 # transaction is rolled back otherwise, tested by 

733 # test/dialect/postgresql/test_dialect.py 

734 # ::MiscBackendTest::test_initial_transaction_state 

735 dialect.do_rollback(c.connection) 

736 

737 # previously, the "first_connect" event was used here, which was then 

738 # scaled back if the "on_connect" handler were present. now, 

739 # since "on_connect" is virtually always present, just use 

740 # "connect" event with once_unless_exception in all cases so that 

741 # the connection event flow is consistent in all cases. 

742 event.listen( 

743 pool, "connect", first_connect, _once_unless_exception=True 

744 ) 

745 

746 dialect_cls.engine_created(engine) 

747 if entrypoint is not dialect_cls: 

748 entrypoint.engine_created(engine) 

749 

750 for plugin in plugins: 

751 plugin.engine_created(engine) 

752 

753 return engine 

754 

755 

756def engine_from_config( 

757 configuration: Dict[str, Any], prefix: str = "sqlalchemy.", **kwargs: Any 

758) -> Engine: 

759 """Create a new Engine instance using a configuration dictionary. 

760 

761 The dictionary is typically produced from a config file. 

762 

763 The keys of interest to ``engine_from_config()`` should be prefixed, e.g. 

764 ``sqlalchemy.url``, ``sqlalchemy.echo``, etc. The 'prefix' argument 

765 indicates the prefix to be searched for. Each matching key (after the 

766 prefix is stripped) is treated as though it were the corresponding keyword 

767 argument to a :func:`_sa.create_engine` call. 

768 

769 The only required key is (assuming the default prefix) ``sqlalchemy.url``, 

770 which provides the :ref:`database URL <database_urls>`. 

771 

772 A select set of keyword arguments will be "coerced" to their 

773 expected type based on string values. The set of arguments 

774 is extensible per-dialect using the ``engine_config_types`` accessor. 

775 

776 :param configuration: A dictionary (typically produced from a config file, 

777 but this is not a requirement). Items whose keys start with the value 

778 of 'prefix' will have that prefix stripped, and will then be passed to 

779 :func:`_sa.create_engine`. 

780 

781 :param prefix: Prefix to match and then strip from keys 

782 in 'configuration'. 

783 

784 :param kwargs: Each keyword argument to ``engine_from_config()`` itself 

785 overrides the corresponding item taken from the 'configuration' 

786 dictionary. Keyword arguments should *not* be prefixed. 

787 

788 """ 

789 

790 options = { 

791 key[len(prefix) :]: configuration[key] 

792 for key in configuration 

793 if key.startswith(prefix) 

794 } 

795 options["_coerce_config"] = True 

796 options.update(kwargs) 

797 url = options.pop("url") 

798 return create_engine(url, **options) 

799 

800 

801@overload 

802def create_pool_from_url( 

803 url: Union[str, URL], 

804 *, 

805 poolclass: Optional[Type[Pool]] = ..., 

806 logging_name: str = ..., 

807 pre_ping: bool = ..., 

808 size: int = ..., 

809 recycle: int = ..., 

810 reset_on_return: Optional[_ResetStyleArgType] = ..., 

811 timeout: float = ..., 

812 use_lifo: bool = ..., 

813 **kwargs: Any, 

814) -> Pool: ... 

815 

816 

817@overload 

818def create_pool_from_url(url: Union[str, URL], **kwargs: Any) -> Pool: ... 

819 

820 

821def create_pool_from_url(url: Union[str, URL], **kwargs: Any) -> Pool: 

822 """Create a pool instance from the given url. 

823 

824 If ``poolclass`` is not provided the pool class used 

825 is selected using the dialect specified in the URL. 

826 

827 The arguments passed to :func:`_sa.create_pool_from_url` are 

828 identical to the pool argument passed to the :func:`_sa.create_engine` 

829 function. 

830 

831 .. versionadded:: 2.0.10 

832 """ 

833 

834 for key in _pool_translate_kwargs: 

835 if key in kwargs: 

836 kwargs[_pool_translate_kwargs[key]] = kwargs.pop(key) 

837 

838 engine = create_engine(url, **kwargs, _initialize=False) 

839 return engine.pool 

840 

841 

842_pool_translate_kwargs = immutabledict( 

843 { 

844 "logging_name": "pool_logging_name", 

845 "echo": "echo_pool", 

846 "timeout": "pool_timeout", 

847 "recycle": "pool_recycle", 

848 "events": "pool_events", # deprecated 

849 "reset_on_return": "pool_reset_on_return", 

850 "pre_ping": "pool_pre_ping", 

851 "use_lifo": "pool_use_lifo", 

852 } 

853)