url.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. # engine/url.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. """Provides the :class:`~sqlalchemy.engine.url.URL` class which encapsulates
  8. information about a database connection specification.
  9. The URL object is created automatically when
  10. :func:`~sqlalchemy.engine.create_engine` is called with a string
  11. argument; alternatively, the URL is a public-facing construct which can
  12. be used directly and is also accepted directly by ``create_engine()``.
  13. """
  14. from __future__ import annotations
  15. import collections.abc as collections_abc
  16. import re
  17. from typing import Any
  18. from typing import cast
  19. from typing import Dict
  20. from typing import Iterable
  21. from typing import List
  22. from typing import Mapping
  23. from typing import NamedTuple
  24. from typing import Optional
  25. from typing import overload
  26. from typing import Sequence
  27. from typing import Tuple
  28. from typing import Type
  29. from typing import Union
  30. from urllib.parse import parse_qsl
  31. from urllib.parse import quote
  32. from urllib.parse import quote_plus
  33. from urllib.parse import unquote
  34. from .interfaces import Dialect
  35. from .. import exc
  36. from .. import util
  37. from ..dialects import plugins
  38. from ..dialects import registry
  39. class URL(NamedTuple):
  40. """
  41. Represent the components of a URL used to connect to a database.
  42. URLs are typically constructed from a fully formatted URL string, where the
  43. :func:`.make_url` function is used internally by the
  44. :func:`_sa.create_engine` function in order to parse the URL string into
  45. its individual components, which are then used to construct a new
  46. :class:`.URL` object. When parsing from a formatted URL string, the parsing
  47. format generally follows
  48. `RFC-1738 <https://www.ietf.org/rfc/rfc1738.txt>`_, with some exceptions.
  49. A :class:`_engine.URL` object may also be produced directly, either by
  50. using the :func:`.make_url` function with a fully formed URL string, or
  51. by using the :meth:`_engine.URL.create` constructor in order
  52. to construct a :class:`_engine.URL` programmatically given individual
  53. fields. The resulting :class:`.URL` object may be passed directly to
  54. :func:`_sa.create_engine` in place of a string argument, which will bypass
  55. the usage of :func:`.make_url` within the engine's creation process.
  56. .. versionchanged:: 1.4
  57. The :class:`_engine.URL` object is now an immutable object. To
  58. create a URL, use the :func:`_engine.make_url` or
  59. :meth:`_engine.URL.create` function / method. To modify
  60. a :class:`_engine.URL`, use methods like
  61. :meth:`_engine.URL.set` and
  62. :meth:`_engine.URL.update_query_dict` to return a new
  63. :class:`_engine.URL` object with modifications. See notes for this
  64. change at :ref:`change_5526`.
  65. .. seealso::
  66. :ref:`database_urls`
  67. :class:`_engine.URL` contains the following attributes:
  68. * :attr:`_engine.URL.drivername`: database backend and driver name, such as
  69. ``postgresql+psycopg2``
  70. * :attr:`_engine.URL.username`: username string
  71. * :attr:`_engine.URL.password`: password string
  72. * :attr:`_engine.URL.host`: string hostname
  73. * :attr:`_engine.URL.port`: integer port number
  74. * :attr:`_engine.URL.database`: string database name
  75. * :attr:`_engine.URL.query`: an immutable mapping representing the query
  76. string. contains strings for keys and either strings or tuples of
  77. strings for values.
  78. """
  79. drivername: str
  80. """database backend and driver name, such as
  81. ``postgresql+psycopg2``
  82. """
  83. username: Optional[str]
  84. "username string"
  85. password: Optional[str]
  86. """password, which is normally a string but may also be any
  87. object that has a ``__str__()`` method."""
  88. host: Optional[str]
  89. """hostname or IP number. May also be a data source name for some
  90. drivers."""
  91. port: Optional[int]
  92. """integer port number"""
  93. database: Optional[str]
  94. """database name"""
  95. query: util.immutabledict[str, Union[Tuple[str, ...], str]]
  96. """an immutable mapping representing the query string. contains strings
  97. for keys and either strings or tuples of strings for values, e.g.::
  98. >>> from sqlalchemy.engine import make_url
  99. >>> url = make_url(
  100. ... "postgresql+psycopg2://user:pass@host/dbname?alt_host=host1&alt_host=host2&ssl_cipher=%2Fpath%2Fto%2Fcrt"
  101. ... )
  102. >>> url.query
  103. immutabledict({'alt_host': ('host1', 'host2'), 'ssl_cipher': '/path/to/crt'})
  104. To create a mutable copy of this mapping, use the ``dict`` constructor::
  105. mutable_query_opts = dict(url.query)
  106. .. seealso::
  107. :attr:`_engine.URL.normalized_query` - normalizes all values into sequences
  108. for consistent processing
  109. Methods for altering the contents of :attr:`_engine.URL.query`:
  110. :meth:`_engine.URL.update_query_dict`
  111. :meth:`_engine.URL.update_query_string`
  112. :meth:`_engine.URL.update_query_pairs`
  113. :meth:`_engine.URL.difference_update_query`
  114. """ # noqa: E501
  115. @classmethod
  116. def create(
  117. cls,
  118. drivername: str,
  119. username: Optional[str] = None,
  120. password: Optional[str] = None,
  121. host: Optional[str] = None,
  122. port: Optional[int] = None,
  123. database: Optional[str] = None,
  124. query: Mapping[str, Union[Sequence[str], str]] = util.EMPTY_DICT,
  125. ) -> URL:
  126. """Create a new :class:`_engine.URL` object.
  127. .. seealso::
  128. :ref:`database_urls`
  129. :param drivername: the name of the database backend. This name will
  130. correspond to a module in sqlalchemy/databases or a third party
  131. plug-in.
  132. :param username: The user name.
  133. :param password: database password. Is typically a string, but may
  134. also be an object that can be stringified with ``str()``.
  135. .. note:: The password string should **not** be URL encoded when
  136. passed as an argument to :meth:`_engine.URL.create`; the string
  137. should contain the password characters exactly as they would be
  138. typed.
  139. .. note:: A password-producing object will be stringified only
  140. **once** per :class:`_engine.Engine` object. For dynamic password
  141. generation per connect, see :ref:`engines_dynamic_tokens`.
  142. :param host: The name of the host.
  143. :param port: The port number.
  144. :param database: The database name.
  145. :param query: A dictionary of string keys to string values to be passed
  146. to the dialect and/or the DBAPI upon connect. To specify non-string
  147. parameters to a Python DBAPI directly, use the
  148. :paramref:`_sa.create_engine.connect_args` parameter to
  149. :func:`_sa.create_engine`. See also
  150. :attr:`_engine.URL.normalized_query` for a dictionary that is
  151. consistently string->list of string.
  152. :return: new :class:`_engine.URL` object.
  153. .. versionadded:: 1.4
  154. The :class:`_engine.URL` object is now an **immutable named
  155. tuple**. In addition, the ``query`` dictionary is also immutable.
  156. To create a URL, use the :func:`_engine.url.make_url` or
  157. :meth:`_engine.URL.create` function/ method. To modify a
  158. :class:`_engine.URL`, use the :meth:`_engine.URL.set` and
  159. :meth:`_engine.URL.update_query` methods.
  160. """
  161. return cls(
  162. cls._assert_str(drivername, "drivername"),
  163. cls._assert_none_str(username, "username"),
  164. password,
  165. cls._assert_none_str(host, "host"),
  166. cls._assert_port(port),
  167. cls._assert_none_str(database, "database"),
  168. cls._str_dict(query),
  169. )
  170. @classmethod
  171. def _assert_port(cls, port: Optional[int]) -> Optional[int]:
  172. if port is None:
  173. return None
  174. try:
  175. return int(port)
  176. except TypeError:
  177. raise TypeError("Port argument must be an integer or None")
  178. @classmethod
  179. def _assert_str(cls, v: str, paramname: str) -> str:
  180. if not isinstance(v, str):
  181. raise TypeError("%s must be a string" % paramname)
  182. return v
  183. @classmethod
  184. def _assert_none_str(
  185. cls, v: Optional[str], paramname: str
  186. ) -> Optional[str]:
  187. if v is None:
  188. return v
  189. return cls._assert_str(v, paramname)
  190. @classmethod
  191. def _str_dict(
  192. cls,
  193. dict_: Optional[
  194. Union[
  195. Sequence[Tuple[str, Union[Sequence[str], str]]],
  196. Mapping[str, Union[Sequence[str], str]],
  197. ]
  198. ],
  199. ) -> util.immutabledict[str, Union[Tuple[str, ...], str]]:
  200. if dict_ is None:
  201. return util.EMPTY_DICT
  202. @overload
  203. def _assert_value(
  204. val: str,
  205. ) -> str: ...
  206. @overload
  207. def _assert_value(
  208. val: Sequence[str],
  209. ) -> Union[str, Tuple[str, ...]]: ...
  210. def _assert_value(
  211. val: Union[str, Sequence[str]],
  212. ) -> Union[str, Tuple[str, ...]]:
  213. if isinstance(val, str):
  214. return val
  215. elif isinstance(val, collections_abc.Sequence):
  216. return tuple(_assert_value(elem) for elem in val)
  217. else:
  218. raise TypeError(
  219. "Query dictionary values must be strings or "
  220. "sequences of strings"
  221. )
  222. def _assert_str(v: str) -> str:
  223. if not isinstance(v, str):
  224. raise TypeError("Query dictionary keys must be strings")
  225. return v
  226. dict_items: Iterable[Tuple[str, Union[Sequence[str], str]]]
  227. if isinstance(dict_, collections_abc.Sequence):
  228. dict_items = dict_
  229. else:
  230. dict_items = dict_.items()
  231. return util.immutabledict(
  232. {
  233. _assert_str(key): _assert_value(
  234. value,
  235. )
  236. for key, value in dict_items
  237. }
  238. )
  239. def set(
  240. self,
  241. drivername: Optional[str] = None,
  242. username: Optional[str] = None,
  243. password: Optional[str] = None,
  244. host: Optional[str] = None,
  245. port: Optional[int] = None,
  246. database: Optional[str] = None,
  247. query: Optional[Mapping[str, Union[Sequence[str], str]]] = None,
  248. ) -> URL:
  249. """return a new :class:`_engine.URL` object with modifications.
  250. Values are used if they are non-None. To set a value to ``None``
  251. explicitly, use the :meth:`_engine.URL._replace` method adapted
  252. from ``namedtuple``.
  253. :param drivername: new drivername
  254. :param username: new username
  255. :param password: new password
  256. :param host: new hostname
  257. :param port: new port
  258. :param query: new query parameters, passed a dict of string keys
  259. referring to string or sequence of string values. Fully
  260. replaces the previous list of arguments.
  261. :return: new :class:`_engine.URL` object.
  262. .. versionadded:: 1.4
  263. .. seealso::
  264. :meth:`_engine.URL.update_query_dict`
  265. """
  266. kw: Dict[str, Any] = {}
  267. if drivername is not None:
  268. kw["drivername"] = drivername
  269. if username is not None:
  270. kw["username"] = username
  271. if password is not None:
  272. kw["password"] = password
  273. if host is not None:
  274. kw["host"] = host
  275. if port is not None:
  276. kw["port"] = port
  277. if database is not None:
  278. kw["database"] = database
  279. if query is not None:
  280. kw["query"] = query
  281. return self._assert_replace(**kw)
  282. def _assert_replace(self, **kw: Any) -> URL:
  283. """argument checks before calling _replace()"""
  284. if "drivername" in kw:
  285. self._assert_str(kw["drivername"], "drivername")
  286. for name in "username", "host", "database":
  287. if name in kw:
  288. self._assert_none_str(kw[name], name)
  289. if "port" in kw:
  290. self._assert_port(kw["port"])
  291. if "query" in kw:
  292. kw["query"] = self._str_dict(kw["query"])
  293. return self._replace(**kw)
  294. def update_query_string(
  295. self, query_string: str, append: bool = False
  296. ) -> URL:
  297. """Return a new :class:`_engine.URL` object with the :attr:`_engine.URL.query`
  298. parameter dictionary updated by the given query string.
  299. E.g.::
  300. >>> from sqlalchemy.engine import make_url
  301. >>> url = make_url("postgresql+psycopg2://user:pass@host/dbname")
  302. >>> url = url.update_query_string(
  303. ... "alt_host=host1&alt_host=host2&ssl_cipher=%2Fpath%2Fto%2Fcrt"
  304. ... )
  305. >>> str(url)
  306. 'postgresql+psycopg2://user:pass@host/dbname?alt_host=host1&alt_host=host2&ssl_cipher=%2Fpath%2Fto%2Fcrt'
  307. :param query_string: a URL escaped query string, not including the
  308. question mark.
  309. :param append: if True, parameters in the existing query string will
  310. not be removed; new parameters will be in addition to those present.
  311. If left at its default of False, keys present in the given query
  312. parameters will replace those of the existing query string.
  313. .. versionadded:: 1.4
  314. .. seealso::
  315. :attr:`_engine.URL.query`
  316. :meth:`_engine.URL.update_query_dict`
  317. """ # noqa: E501
  318. return self.update_query_pairs(parse_qsl(query_string), append=append)
  319. def update_query_pairs(
  320. self,
  321. key_value_pairs: Iterable[Tuple[str, Union[str, List[str]]]],
  322. append: bool = False,
  323. ) -> URL:
  324. """Return a new :class:`_engine.URL` object with the
  325. :attr:`_engine.URL.query`
  326. parameter dictionary updated by the given sequence of key/value pairs
  327. E.g.::
  328. >>> from sqlalchemy.engine import make_url
  329. >>> url = make_url("postgresql+psycopg2://user:pass@host/dbname")
  330. >>> url = url.update_query_pairs(
  331. ... [
  332. ... ("alt_host", "host1"),
  333. ... ("alt_host", "host2"),
  334. ... ("ssl_cipher", "/path/to/crt"),
  335. ... ]
  336. ... )
  337. >>> str(url)
  338. 'postgresql+psycopg2://user:pass@host/dbname?alt_host=host1&alt_host=host2&ssl_cipher=%2Fpath%2Fto%2Fcrt'
  339. :param key_value_pairs: A sequence of tuples containing two strings
  340. each.
  341. :param append: if True, parameters in the existing query string will
  342. not be removed; new parameters will be in addition to those present.
  343. If left at its default of False, keys present in the given query
  344. parameters will replace those of the existing query string.
  345. .. versionadded:: 1.4
  346. .. seealso::
  347. :attr:`_engine.URL.query`
  348. :meth:`_engine.URL.difference_update_query`
  349. :meth:`_engine.URL.set`
  350. """ # noqa: E501
  351. existing_query = self.query
  352. new_keys: Dict[str, Union[str, List[str]]] = {}
  353. for key, value in key_value_pairs:
  354. if key in new_keys:
  355. new_keys[key] = util.to_list(new_keys[key])
  356. cast("List[str]", new_keys[key]).append(cast(str, value))
  357. else:
  358. new_keys[key] = (
  359. list(value) if isinstance(value, (list, tuple)) else value
  360. )
  361. new_query: Mapping[str, Union[str, Sequence[str]]]
  362. if append:
  363. new_query = {}
  364. for k in new_keys:
  365. if k in existing_query:
  366. new_query[k] = tuple(
  367. util.to_list(existing_query[k])
  368. + util.to_list(new_keys[k])
  369. )
  370. else:
  371. new_query[k] = new_keys[k]
  372. new_query.update(
  373. {
  374. k: existing_query[k]
  375. for k in set(existing_query).difference(new_keys)
  376. }
  377. )
  378. else:
  379. new_query = self.query.union(
  380. {
  381. k: tuple(v) if isinstance(v, list) else v
  382. for k, v in new_keys.items()
  383. }
  384. )
  385. return self.set(query=new_query)
  386. def update_query_dict(
  387. self,
  388. query_parameters: Mapping[str, Union[str, List[str]]],
  389. append: bool = False,
  390. ) -> URL:
  391. """Return a new :class:`_engine.URL` object with the
  392. :attr:`_engine.URL.query` parameter dictionary updated by the given
  393. dictionary.
  394. The dictionary typically contains string keys and string values.
  395. In order to represent a query parameter that is expressed multiple
  396. times, pass a sequence of string values.
  397. E.g.::
  398. >>> from sqlalchemy.engine import make_url
  399. >>> url = make_url("postgresql+psycopg2://user:pass@host/dbname")
  400. >>> url = url.update_query_dict(
  401. ... {"alt_host": ["host1", "host2"], "ssl_cipher": "/path/to/crt"}
  402. ... )
  403. >>> str(url)
  404. 'postgresql+psycopg2://user:pass@host/dbname?alt_host=host1&alt_host=host2&ssl_cipher=%2Fpath%2Fto%2Fcrt'
  405. :param query_parameters: A dictionary with string keys and values
  406. that are either strings, or sequences of strings.
  407. :param append: if True, parameters in the existing query string will
  408. not be removed; new parameters will be in addition to those present.
  409. If left at its default of False, keys present in the given query
  410. parameters will replace those of the existing query string.
  411. .. versionadded:: 1.4
  412. .. seealso::
  413. :attr:`_engine.URL.query`
  414. :meth:`_engine.URL.update_query_string`
  415. :meth:`_engine.URL.update_query_pairs`
  416. :meth:`_engine.URL.difference_update_query`
  417. :meth:`_engine.URL.set`
  418. """ # noqa: E501
  419. return self.update_query_pairs(query_parameters.items(), append=append)
  420. def difference_update_query(self, names: Iterable[str]) -> URL:
  421. """
  422. Remove the given names from the :attr:`_engine.URL.query` dictionary,
  423. returning the new :class:`_engine.URL`.
  424. E.g.::
  425. url = url.difference_update_query(["foo", "bar"])
  426. Equivalent to using :meth:`_engine.URL.set` as follows::
  427. url = url.set(
  428. query={
  429. key: url.query[key]
  430. for key in set(url.query).difference(["foo", "bar"])
  431. }
  432. )
  433. .. versionadded:: 1.4
  434. .. seealso::
  435. :attr:`_engine.URL.query`
  436. :meth:`_engine.URL.update_query_dict`
  437. :meth:`_engine.URL.set`
  438. """
  439. if not set(names).intersection(self.query):
  440. return self
  441. return URL(
  442. self.drivername,
  443. self.username,
  444. self.password,
  445. self.host,
  446. self.port,
  447. self.database,
  448. util.immutabledict(
  449. {
  450. key: self.query[key]
  451. for key in set(self.query).difference(names)
  452. }
  453. ),
  454. )
  455. @property
  456. def normalized_query(self) -> Mapping[str, Sequence[str]]:
  457. """Return the :attr:`_engine.URL.query` dictionary with values normalized
  458. into sequences.
  459. As the :attr:`_engine.URL.query` dictionary may contain either
  460. string values or sequences of string values to differentiate between
  461. parameters that are specified multiple times in the query string,
  462. code that needs to handle multiple parameters generically will wish
  463. to use this attribute so that all parameters present are presented
  464. as sequences. Inspiration is from Python's ``urllib.parse.parse_qs``
  465. function. E.g.::
  466. >>> from sqlalchemy.engine import make_url
  467. >>> url = make_url(
  468. ... "postgresql+psycopg2://user:pass@host/dbname?alt_host=host1&alt_host=host2&ssl_cipher=%2Fpath%2Fto%2Fcrt"
  469. ... )
  470. >>> url.query
  471. immutabledict({'alt_host': ('host1', 'host2'), 'ssl_cipher': '/path/to/crt'})
  472. >>> url.normalized_query
  473. immutabledict({'alt_host': ('host1', 'host2'), 'ssl_cipher': ('/path/to/crt',)})
  474. """ # noqa: E501
  475. return util.immutabledict(
  476. {
  477. k: (v,) if not isinstance(v, tuple) else v
  478. for k, v in self.query.items()
  479. }
  480. )
  481. @util.deprecated(
  482. "1.4",
  483. "The :meth:`_engine.URL.__to_string__ method is deprecated and will "
  484. "be removed in a future release. Please use the "
  485. ":meth:`_engine.URL.render_as_string` method.",
  486. )
  487. def __to_string__(self, hide_password: bool = True) -> str:
  488. """Render this :class:`_engine.URL` object as a string.
  489. :param hide_password: Defaults to True. The password is not shown
  490. in the string unless this is set to False.
  491. """
  492. return self.render_as_string(hide_password=hide_password)
  493. def render_as_string(self, hide_password: bool = True) -> str:
  494. """Render this :class:`_engine.URL` object as a string.
  495. This method is used when the ``__str__()`` or ``__repr__()``
  496. methods are used. The method directly includes additional options.
  497. :param hide_password: Defaults to True. The password is not shown
  498. in the string unless this is set to False.
  499. """
  500. s = self.drivername + "://"
  501. if self.username is not None:
  502. s += quote(self.username, safe=" +")
  503. if self.password is not None:
  504. s += ":" + (
  505. "***"
  506. if hide_password
  507. else quote(str(self.password), safe=" +")
  508. )
  509. s += "@"
  510. if self.host is not None:
  511. if ":" in self.host:
  512. s += f"[{self.host}]"
  513. else:
  514. s += self.host
  515. if self.port is not None:
  516. s += ":" + str(self.port)
  517. if self.database is not None:
  518. s += "/" + self.database
  519. if self.query:
  520. keys = list(self.query)
  521. keys.sort()
  522. s += "?" + "&".join(
  523. f"{quote_plus(k)}={quote_plus(element)}"
  524. for k in keys
  525. for element in util.to_list(self.query[k])
  526. )
  527. return s
  528. def __repr__(self) -> str:
  529. return self.render_as_string()
  530. def __copy__(self) -> URL:
  531. return self.__class__.create(
  532. self.drivername,
  533. self.username,
  534. self.password,
  535. self.host,
  536. self.port,
  537. self.database,
  538. # note this is an immutabledict of str-> str / tuple of str,
  539. # also fully immutable. does not require deepcopy
  540. self.query,
  541. )
  542. def __deepcopy__(self, memo: Any) -> URL:
  543. return self.__copy__()
  544. def __hash__(self) -> int:
  545. return hash(str(self))
  546. def __eq__(self, other: Any) -> bool:
  547. return (
  548. isinstance(other, URL)
  549. and self.drivername == other.drivername
  550. and self.username == other.username
  551. and self.password == other.password
  552. and self.host == other.host
  553. and self.database == other.database
  554. and self.query == other.query
  555. and self.port == other.port
  556. )
  557. def __ne__(self, other: Any) -> bool:
  558. return not self == other
  559. def get_backend_name(self) -> str:
  560. """Return the backend name.
  561. This is the name that corresponds to the database backend in
  562. use, and is the portion of the :attr:`_engine.URL.drivername`
  563. that is to the left of the plus sign.
  564. """
  565. if "+" not in self.drivername:
  566. return self.drivername
  567. else:
  568. return self.drivername.split("+")[0]
  569. def get_driver_name(self) -> str:
  570. """Return the backend name.
  571. This is the name that corresponds to the DBAPI driver in
  572. use, and is the portion of the :attr:`_engine.URL.drivername`
  573. that is to the right of the plus sign.
  574. If the :attr:`_engine.URL.drivername` does not include a plus sign,
  575. then the default :class:`_engine.Dialect` for this :class:`_engine.URL`
  576. is imported in order to get the driver name.
  577. """
  578. if "+" not in self.drivername:
  579. return self.get_dialect().driver
  580. else:
  581. return self.drivername.split("+")[1]
  582. def _instantiate_plugins(
  583. self, kwargs: Mapping[str, Any]
  584. ) -> Tuple[URL, List[Any], Dict[str, Any]]:
  585. plugin_names = util.to_list(self.query.get("plugin", ()))
  586. plugin_names += kwargs.get("plugins", [])
  587. kwargs = dict(kwargs)
  588. loaded_plugins = [
  589. plugins.load(plugin_name)(self, kwargs)
  590. for plugin_name in plugin_names
  591. ]
  592. u = self.difference_update_query(["plugin", "plugins"])
  593. for plugin in loaded_plugins:
  594. new_u = plugin.update_url(u)
  595. if new_u is not None:
  596. u = new_u
  597. kwargs.pop("plugins", None)
  598. return u, loaded_plugins, kwargs
  599. def _get_entrypoint(self) -> Type[Dialect]:
  600. """Return the "entry point" dialect class.
  601. This is normally the dialect itself except in the case when the
  602. returned class implements the get_dialect_cls() method.
  603. """
  604. if "+" not in self.drivername:
  605. name = self.drivername
  606. else:
  607. name = self.drivername.replace("+", ".")
  608. cls = registry.load(name)
  609. # check for legacy dialects that
  610. # would return a module with 'dialect' as the
  611. # actual class
  612. if (
  613. hasattr(cls, "dialect")
  614. and isinstance(cls.dialect, type)
  615. and issubclass(cls.dialect, Dialect)
  616. ):
  617. return cls.dialect
  618. else:
  619. return cast("Type[Dialect]", cls)
  620. def get_dialect(self, _is_async: bool = False) -> Type[Dialect]:
  621. """Return the SQLAlchemy :class:`_engine.Dialect` class corresponding
  622. to this URL's driver name.
  623. """
  624. entrypoint = self._get_entrypoint()
  625. if _is_async:
  626. dialect_cls = entrypoint.get_async_dialect_cls(self)
  627. else:
  628. dialect_cls = entrypoint.get_dialect_cls(self)
  629. return dialect_cls
  630. def translate_connect_args(
  631. self, names: Optional[List[str]] = None, **kw: Any
  632. ) -> Dict[str, Any]:
  633. r"""Translate url attributes into a dictionary of connection arguments.
  634. Returns attributes of this url (`host`, `database`, `username`,
  635. `password`, `port`) as a plain dictionary. The attribute names are
  636. used as the keys by default. Unset or false attributes are omitted
  637. from the final dictionary.
  638. :param \**kw: Optional, alternate key names for url attributes.
  639. :param names: Deprecated. Same purpose as the keyword-based alternate
  640. names, but correlates the name to the original positionally.
  641. """
  642. if names is not None:
  643. util.warn_deprecated(
  644. "The `URL.translate_connect_args.name`s parameter is "
  645. "deprecated. Please pass the "
  646. "alternate names as kw arguments.",
  647. "1.4",
  648. )
  649. translated = {}
  650. attribute_names = ["host", "database", "username", "password", "port"]
  651. for sname in attribute_names:
  652. if names:
  653. name = names.pop(0)
  654. elif sname in kw:
  655. name = kw[sname]
  656. else:
  657. name = sname
  658. if name is not None and getattr(self, sname, False):
  659. if sname == "password":
  660. translated[name] = str(getattr(self, sname))
  661. else:
  662. translated[name] = getattr(self, sname)
  663. return translated
  664. def make_url(name_or_url: Union[str, URL]) -> URL:
  665. """Given a string, produce a new URL instance.
  666. The format of the URL generally follows `RFC-1738
  667. <https://www.ietf.org/rfc/rfc1738.txt>`_, with some exceptions, including
  668. that underscores, and not dashes or periods, are accepted within the
  669. "scheme" portion.
  670. If a :class:`.URL` object is passed, it is returned as is.
  671. .. seealso::
  672. :ref:`database_urls`
  673. """
  674. if isinstance(name_or_url, str):
  675. return _parse_url(name_or_url)
  676. elif not isinstance(name_or_url, URL) and not hasattr(
  677. name_or_url, "_sqla_is_testing_if_this_is_a_mock_object"
  678. ):
  679. raise exc.ArgumentError(
  680. f"Expected string or URL object, got {name_or_url!r}"
  681. )
  682. else:
  683. return name_or_url
  684. def _parse_url(name: str) -> URL:
  685. pattern = re.compile(
  686. r"""
  687. (?P<name>[\w\+]+)://
  688. (?:
  689. (?P<username>[^:/]*)
  690. (?::(?P<password>[^@]*))?
  691. @)?
  692. (?:
  693. (?:
  694. \[(?P<ipv6host>[^/\?]+)\] |
  695. (?P<ipv4host>[^/:\?]+)
  696. )?
  697. (?::(?P<port>[^/\?]*))?
  698. )?
  699. (?:/(?P<database>[^\?]*))?
  700. (?:\?(?P<query>.*))?
  701. """,
  702. re.X,
  703. )
  704. m = pattern.match(name)
  705. if m is not None:
  706. components = m.groupdict()
  707. query: Optional[Dict[str, Union[str, List[str]]]]
  708. if components["query"] is not None:
  709. query = {}
  710. for key, value in parse_qsl(components["query"]):
  711. if key in query:
  712. query[key] = util.to_list(query[key])
  713. cast("List[str]", query[key]).append(value)
  714. else:
  715. query[key] = value
  716. else:
  717. query = None
  718. components["query"] = query
  719. if components["username"] is not None:
  720. components["username"] = unquote(components["username"])
  721. if components["password"] is not None:
  722. components["password"] = unquote(components["password"])
  723. ipv4host = components.pop("ipv4host")
  724. ipv6host = components.pop("ipv6host")
  725. components["host"] = ipv4host or ipv6host
  726. name = components.pop("name")
  727. if components["port"]:
  728. components["port"] = int(components["port"])
  729. return URL.create(name, **components) # type: ignore
  730. else:
  731. raise exc.ArgumentError(
  732. "Could not parse SQLAlchemy URL from given URL string"
  733. )