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

168 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 .. versionadded:: 1.3.8 

266 

267 .. seealso:: 

268 

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

270 logging. 

271 

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

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

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

275 configure this on a per-table basis using the 

276 :paramref:`.Table.implicit_returning` parameter. 

277 

278 

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

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

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

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

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

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

285 

286 .. versionadded:: 2.0 

287 

288 .. seealso:: 

289 

290 :ref:`engine_insertmanyvalues` 

291 

292 :ref:`engine_insertmanyvalues_page_size` 

293 

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

295 

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

297 which will be set on all new connections unconditionally. 

298 Isolation levels are typically some subset of the string names 

299 ``"SERIALIZABLE"``, ``"REPEATABLE READ"``, 

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

301 based on backend. 

302 

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

304 in contrast to the 

305 :paramref:`.Connection.execution_options.isolation_level` 

306 execution option, which may be set on an individual 

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

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

309 multiple engines with different isolation levels that share a common 

310 connection pool and dialect. 

311 

312 .. versionchanged:: 2.0 The 

313 :paramref:`_sa.create_engine.isolation_level` 

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

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

316 up front configuration switch in contrast to the execution option 

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

318 

319 .. seealso:: 

320 

321 :ref:`dbapi_autocommit` 

322 

323 :param json_deserializer: for dialects that support the 

324 :class:`_types.JSON` 

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

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

327 used. 

328 

329 .. versionchanged:: 1.3.7 The SQLite dialect renamed this from 

330 ``_json_deserializer``. 

331 

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

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

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

335 

336 .. versionchanged:: 1.3.7 The SQLite dialect renamed this from 

337 ``_json_serializer``. 

338 

339 

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

341 the size of dynamically generated column labels to that many 

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

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

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

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

346 is used instead. The value of 

347 :paramref:`_sa.create_engine.label_length` 

348 may not be larger than that of 

349 :paramref:`_sa.create_engine.max_identfier_length`. 

350 

351 .. seealso:: 

352 

353 :paramref:`_sa.create_engine.max_identifier_length` 

354 

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

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

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

358 object's id. 

359 

360 .. seealso:: 

361 

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

363 logging. 

364 

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

366 

367 :param max_identifier_length: integer; override the max_identifier_length 

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

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

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

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

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

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

374 here. 

375 

376 .. versionadded:: 1.3.9 

377 

378 .. seealso:: 

379 

380 :paramref:`_sa.create_engine.label_length` 

381 

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

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

384 opened above and beyond the pool_size setting, which defaults 

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

386 

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

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

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

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

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

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

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

394 

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

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

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

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

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

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

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

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

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

404 to be supported by the DBAPI in use. 

405 

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

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

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

409 pool will be used directly as the underlying connection pool 

410 for the engine, bypassing whatever connection parameters are 

411 present in the URL argument. For information on constructing 

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

413 

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

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

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

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

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

419 of pool to be used. 

420 

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

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

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

424 id. 

425 

426 .. seealso:: 

427 

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

429 logging. 

430 

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

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

433 each checkout. 

434 

435 .. versionadded:: 1.2 

436 

437 .. seealso:: 

438 

439 :ref:`pool_disconnects_pessimistic` 

440 

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

442 inside the connection pool. This used with 

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

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

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

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

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

448 

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

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

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

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

453 MySQL in particular will disconnect automatically if no 

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

455 this is configurable with the MySQLDB connection itself and the 

456 server configuration as well). 

457 

458 .. seealso:: 

459 

460 :ref:`pool_setting_recycle` 

461 

462 :param pool_reset_on_return='rollback': set the 

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

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

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

466 

467 .. seealso:: 

468 

469 :ref:`pool_reset_on_return` 

470 

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

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

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

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

475 reliable in the tens of milliseconds. 

476 

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

478 

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

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

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

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

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

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

485 

486 .. versionadded:: 1.3 

487 

488 .. seealso:: 

489 

490 :ref:`pool_use_lifo` 

491 

492 :ref:`pool_disconnects` 

493 

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

495 :class:`.CreateEnginePlugin` for background. 

496 

497 .. versionadded:: 1.2.3 

498 

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

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

501 

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

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

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

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

506 used items. 

507 

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

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

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

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

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

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

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

515 

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

517 as some attribute loading strategies will make use of individual 

518 per-mapper caches outside of the main cache. 

519 

520 

521 .. seealso:: 

522 

523 :ref:`sql_caching` 

524 

525 .. versionadded:: 1.4 

526 

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

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

529 

530 .. versionadded:: 2.0 

531 

532 .. seealso:: 

533 

534 :ref:`engine_insertmanyvalues` 

535 

536 """ # noqa 

537 

538 if "strategy" in kwargs: 

539 strat = kwargs.pop("strategy") 

540 if strat == "mock": 

541 # this case is deprecated 

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

543 else: 

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

545 

546 kwargs.pop("empty_in_strategy", None) 

547 

548 # create url.URL object 

549 u = _url.make_url(url) 

550 

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

552 

553 entrypoint = u._get_entrypoint() 

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

555 if _is_async: 

556 dialect_cls = entrypoint.get_async_dialect_cls(u) 

557 else: 

558 dialect_cls = entrypoint.get_dialect_cls(u) 

559 

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

561 

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

563 value = kwargs.pop(key, default) 

564 if key in dialect_cls.engine_config_types: 

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

566 return value 

567 

568 else: 

569 pop_kwarg = kwargs.pop # type: ignore 

570 

571 dialect_args = {} 

572 # consume dialect arguments from kwargs 

573 for k in util.get_cls_kwargs(dialect_cls): 

574 if k in kwargs: 

575 dialect_args[k] = pop_kwarg(k) 

576 

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

578 if dbapi is None: 

579 dbapi_args = {} 

580 

581 if "import_dbapi" in dialect_cls.__dict__: 

582 dbapi_meth = dialect_cls.import_dbapi 

583 

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

585 dialect_cls.dbapi 

586 ): 

587 util.warn_deprecated( 

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

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

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

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

592 "backwards compatibility.", 

593 "2.0", 

594 ) 

595 dbapi_meth = dialect_cls.dbapi 

596 else: 

597 dbapi_meth = dialect_cls.import_dbapi 

598 

599 for k in util.get_func_kwargs(dbapi_meth): 

600 if k in kwargs: 

601 dbapi_args[k] = pop_kwarg(k) 

602 dbapi = dbapi_meth(**dbapi_args) 

603 

604 dialect_args["dbapi"] = dbapi 

605 

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

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

608 if enable_from_linting: 

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

610 

611 for plugin in plugins: 

612 plugin.handle_dialect_kwargs(dialect_cls, dialect_args) 

613 

614 # create dialect 

615 dialect = dialect_cls(**dialect_args) 

616 

617 # assemble connection arguments 

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

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

620 

621 if "async_fallback" in cparams and util.asbool(cparams["async_fallback"]): 

622 util.warn_deprecated( 

623 "The async_fallback dialect argument is deprecated and will be " 

624 "removed in SQLAlchemy 2.1.", 

625 "2.0", 

626 ) 

627 

628 cargs = list(cargs_tup) # allow mutability 

629 

630 # look for existing pool or create 

631 pool = pop_kwarg("pool", None) 

632 if pool is None: 

633 

634 def connect( 

635 connection_record: Optional[ConnectionPoolEntry] = None, 

636 ) -> DBAPIConnection: 

637 if dialect._has_events: 

638 for fn in dialect.dispatch.do_connect: 

639 connection = cast( 

640 DBAPIConnection, 

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

642 ) 

643 if connection is not None: 

644 return connection 

645 

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

647 

648 creator = pop_kwarg("creator", connect) 

649 

650 poolclass = pop_kwarg("poolclass", None) 

651 if poolclass is None: 

652 poolclass = dialect.get_dialect_pool_class(u) 

653 pool_args = {"dialect": dialect} 

654 

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

656 # the arguments 

657 for k in util.get_cls_kwargs(poolclass): 

658 tk = _pool_translate_kwargs.get(k, k) 

659 if tk in kwargs: 

660 pool_args[k] = pop_kwarg(tk) 

661 

662 for plugin in plugins: 

663 plugin.handle_pool_kwargs(poolclass, pool_args) 

664 

665 pool = poolclass(creator, **pool_args) 

666 else: 

667 pool._dialect = dialect 

668 

669 if ( 

670 hasattr(pool, "_is_asyncio") 

671 and pool._is_asyncio is not dialect.is_async 

672 ): 

673 raise exc.ArgumentError( 

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

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

676 "asyncio engine", 

677 code="pcls", 

678 ) 

679 

680 # create engine. 

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

682 raise exc.ArgumentError( 

683 "The 'future' parameter passed to " 

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

685 ) 

686 

687 engineclass = base.Engine 

688 

689 engine_args = {} 

690 for k in util.get_cls_kwargs(engineclass): 

691 if k in kwargs: 

692 engine_args[k] = pop_kwarg(k) 

693 

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

695 # engines with mocks etc. 

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

697 

698 # all kwargs should be consumed 

699 if kwargs: 

700 raise TypeError( 

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

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

703 "keyword arguments are appropriate for this combination " 

704 "of components." 

705 % ( 

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

707 dialect.__class__.__name__, 

708 pool.__class__.__name__, 

709 engineclass.__name__, 

710 ) 

711 ) 

712 

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

714 

715 if _initialize: 

716 do_on_connect = dialect.on_connect_url(u) 

717 if do_on_connect: 

718 

719 def on_connect( 

720 dbapi_connection: DBAPIConnection, 

721 connection_record: ConnectionPoolEntry, 

722 ) -> None: 

723 assert do_on_connect is not None 

724 do_on_connect(dbapi_connection) 

725 

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

727 

728 builtin_on_connect = dialect._builtin_onconnect() 

729 if builtin_on_connect: 

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

731 

732 def first_connect( 

733 dbapi_connection: DBAPIConnection, 

734 connection_record: ConnectionPoolEntry, 

735 ) -> None: 

736 c = base.Connection( 

737 engine, 

738 connection=_AdhocProxiedConnection( 

739 dbapi_connection, connection_record 

740 ), 

741 _has_events=False, 

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

743 # connection goes away, Connection is then closed 

744 _allow_revalidate=False, 

745 # dont trigger the autobegin sequence 

746 # within the up front dialect checks 

747 _allow_autobegin=False, 

748 ) 

749 c._execution_options = util.EMPTY_DICT 

750 

751 try: 

752 dialect.initialize(c) 

753 finally: 

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

755 # exclusive in 1.4 Connection. 

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

757 # transaction is rolled back otherwise, tested by 

758 # test/dialect/postgresql/test_dialect.py 

759 # ::MiscBackendTest::test_initial_transaction_state 

760 dialect.do_rollback(c.connection) 

761 

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

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

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

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

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

767 event.listen( 

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

769 ) 

770 

771 dialect_cls.engine_created(engine) 

772 if entrypoint is not dialect_cls: 

773 entrypoint.engine_created(engine) 

774 

775 for plugin in plugins: 

776 plugin.engine_created(engine) 

777 

778 return engine 

779 

780 

781def engine_from_config( 

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

783) -> Engine: 

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

785 

786 The dictionary is typically produced from a config file. 

787 

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

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

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

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

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

793 

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

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

796 

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

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

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

800 

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

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

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

804 :func:`_sa.create_engine`. 

805 

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

807 in 'configuration'. 

808 

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

810 overrides the corresponding item taken from the 'configuration' 

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

812 

813 """ 

814 

815 options = { 

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

817 for key in configuration 

818 if key.startswith(prefix) 

819 } 

820 options["_coerce_config"] = True 

821 options.update(kwargs) 

822 url = options.pop("url") 

823 return create_engine(url, **options) 

824 

825 

826@overload 

827def create_pool_from_url( 

828 url: Union[str, URL], 

829 *, 

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

831 logging_name: str = ..., 

832 pre_ping: bool = ..., 

833 size: int = ..., 

834 recycle: int = ..., 

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

836 timeout: float = ..., 

837 use_lifo: bool = ..., 

838 **kwargs: Any, 

839) -> Pool: ... 

840 

841 

842@overload 

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

844 

845 

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

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

848 

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

850 is selected using the dialect specified in the URL. 

851 

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

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

854 function. 

855 

856 .. versionadded:: 2.0.10 

857 """ 

858 

859 for key in _pool_translate_kwargs: 

860 if key in kwargs: 

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

862 

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

864 return engine.pool 

865 

866 

867_pool_translate_kwargs = immutabledict( 

868 { 

869 "logging_name": "pool_logging_name", 

870 "echo": "echo_pool", 

871 "timeout": "pool_timeout", 

872 "recycle": "pool_recycle", 

873 "events": "pool_events", # deprecated 

874 "reset_on_return": "pool_reset_on_return", 

875 "pre_ping": "pool_pre_ping", 

876 "use_lifo": "pool_use_lifo", 

877 } 

878)