class documentation

Represent the components of a URL used to connect to a database. URLs are typically constructed from a fully formatted URL string, where the :func:`.make_url` function is used internally by the :func:`_sa.create_engine` function in order to parse the URL string into its individual components, which are then used to construct a new :class:`.URL` object. When parsing from a formatted URL string, the parsing format generally follows `RFC-1738 <https://www.ietf.org/rfc/rfc1738.txt>`_, with some exceptions. A :class:`_engine.URL` object may also be produced directly, either by using the :func:`.make_url` function with a fully formed URL string, or by using the :meth:`_engine.URL.create` constructor in order to construct a :class:`_engine.URL` programmatically given individual fields. The resulting :class:`.URL` object may be passed directly to :func:`_sa.create_engine` in place of a string argument, which will bypass the usage of :func:`.make_url` within the engine's creation process. .. versionchanged:: 1.4 The :class:`_engine.URL` object is now an immutable object. To create a URL, use the :func:`_engine.make_url` or :meth:`_engine.URL.create` function / method. To modify a :class:`_engine.URL`, use methods like :meth:`_engine.URL.set` and :meth:`_engine.URL.update_query_dict` to return a new :class:`_engine.URL` object with modifications. See notes for this change at :ref:`change_5526`. .. seealso:: :ref:`database_urls` :class:`_engine.URL` contains the following attributes: * :attr:`_engine.URL.drivername`: database backend and driver name, such as ``postgresql+psycopg2`` * :attr:`_engine.URL.username`: username string * :attr:`_engine.URL.password`: password string * :attr:`_engine.URL.host`: string hostname * :attr:`_engine.URL.port`: integer port number * :attr:`_engine.URL.database`: string database name * :attr:`_engine.URL.query`: an immutable mapping representing the query string. contains strings for keys and either strings or tuples of strings for values.

Class Method create Create a new :class:`_engine.URL` object.
Method __copy__ Undocumented
Method __deepcopy__ Undocumented
Method __eq__ Undocumented
Method __hash__ Undocumented
Method __ne__ Undocumented
Method __repr__ Undocumented
Method __to_string__ Render this :class:`_engine.URL` object as a string.
Method difference_update_query Remove the given names from the :attr:`_engine.URL.query` dictionary, returning the new :class:`_engine.URL`.
Method get_backend_name Return the backend name.
Method get_dialect Return the SQLAlchemy :class:`_engine.Dialect` class corresponding to this URL's driver name.
Method get_driver_name Return the backend name.
Method render_as_string Render this :class:`_engine.URL` object as a string.
Method set return a new :class:`_engine.URL` object with modifications.
Method translate_connect_args Translate url attributes into a dictionary of connection arguments.
Method update_query_dict Return a new :class:`_engine.URL` object with the :attr:`_engine.URL.query` parameter dictionary updated by the given dictionary.
Method update_query_pairs Return a new :class:`_engine.URL` object with the :attr:`_engine.URL.query` parameter dictionary updated by the given sequence of key/value pairs
Method update_query_string Return a new :class:`_engine.URL` object with the :attr:`_engine.URL.query` parameter dictionary updated by the given query string.
Class Variable database database name
Class Variable drivername database backend and driver name, such as ``postgresql+psycopg2``
Class Variable host hostname or IP number. May also be a data source name for some drivers.
Class Variable password password, which is normally a string but may also be any object that has a ``__str__()`` method.
Class Variable port integer port number
Class Variable query an immutable mapping representing the query string. contains strings for keys and either strings or tuples of strings for values, e.g.::
Class Variable username username string
Property normalized_query Return the :attr:`_engine.URL.query` dictionary with values normalized into sequences.
Class Method _assert_none_str Undocumented
Class Method _assert_port Undocumented
Class Method _assert_str Undocumented
Class Method _str_dict Undocumented
Method _assert_replace argument checks before calling _replace()
Method _get_entrypoint Return the "entry point" dialect class.
Method _instantiate_plugins Undocumented
@classmethod
def create(cls, drivername: str, username: Optional[str] = None, password: Optional[str] = None, host: Optional[str] = None, port: Optional[int] = None, database: Optional[str] = None, query: Mapping[str, Union[Sequence[str], str]] = util.EMPTY_DICT) -> URL: (source)

Create a new :class:`_engine.URL` object. .. seealso:: :ref:`database_urls` :param drivername: the name of the database backend. This name will correspond to a module in sqlalchemy/databases or a third party plug-in. :param username: The user name. :param password: database password. Is typically a string, but may also be an object that can be stringified with ``str()``. .. note:: A password-producing object will be stringified only **once** per :class:`_engine.Engine` object. For dynamic password generation per connect, see :ref:`engines_dynamic_tokens`. :param host: The name of the host. :param port: The port number. :param database: The database name. :param query: A dictionary of string keys to string values to be passed to the dialect and/or the DBAPI upon connect. To specify non-string parameters to a Python DBAPI directly, use the :paramref:`_sa.create_engine.connect_args` parameter to :func:`_sa.create_engine`. See also :attr:`_engine.URL.normalized_query` for a dictionary that is consistently string->list of string. :return: new :class:`_engine.URL` object. .. versionadded:: 1.4 The :class:`_engine.URL` object is now an **immutable named tuple**. In addition, the ``query`` dictionary is also immutable. To create a URL, use the :func:`_engine.url.make_url` or :meth:`_engine.URL.create` function/ method. To modify a :class:`_engine.URL`, use the :meth:`_engine.URL.set` and :meth:`_engine.URL.update_query` methods.

def __copy__(self) -> URL: (source)

Undocumented

def __deepcopy__(self, memo: Any) -> URL: (source)

Undocumented

def __eq__(self, other: Any) -> bool: (source)

Undocumented

def __hash__(self) -> int: (source)

Undocumented

def __ne__(self, other: Any) -> bool: (source)

Undocumented

def __repr__(self) -> str: (source)

Undocumented

@util.deprecated('1.4', 'The :meth:`_engine.URL.__to_string__ method is deprecated and will be removed in a future release. Please use the :meth:`_engine.URL.render_as_string` method.')
def __to_string__(self, hide_password: bool = True) -> str: (source)

Render this :class:`_engine.URL` object as a string. :param hide_password: Defaults to True. The password is not shown in the string unless this is set to False.

def difference_update_query(self, names: Iterable[str]) -> URL: (source)

Remove the given names from the :attr:`_engine.URL.query` dictionary, returning the new :class:`_engine.URL`. E.g.:: url = url.difference_update_query(['foo', 'bar']) Equivalent to using :meth:`_engine.URL.set` as follows:: url = url.set( query={ key: url.query[key] for key in set(url.query).difference(['foo', 'bar']) } ) .. versionadded:: 1.4 .. seealso:: :attr:`_engine.URL.query` :meth:`_engine.URL.update_query_dict` :meth:`_engine.URL.set`

def get_backend_name(self) -> str: (source)

Return the backend name. This is the name that corresponds to the database backend in use, and is the portion of the :attr:`_engine.URL.drivername` that is to the left of the plus sign.

def get_dialect(self, _is_async: bool = False) -> Type[Dialect]: (source)

Return the SQLAlchemy :class:`_engine.Dialect` class corresponding to this URL's driver name.

def get_driver_name(self) -> str: (source)

Return the backend name. This is the name that corresponds to the DBAPI driver in use, and is the portion of the :attr:`_engine.URL.drivername` that is to the right of the plus sign. If the :attr:`_engine.URL.drivername` does not include a plus sign, then the default :class:`_engine.Dialect` for this :class:`_engine.URL` is imported in order to get the driver name.

def render_as_string(self, hide_password: bool = True) -> str: (source)

Render this :class:`_engine.URL` object as a string. This method is used when the ``__str__()`` or ``__repr__()`` methods are used. The method directly includes additional options. :param hide_password: Defaults to True. The password is not shown in the string unless this is set to False.

def set(self, drivername: Optional[str] = None, username: Optional[str] = None, password: Optional[str] = None, host: Optional[str] = None, port: Optional[int] = None, database: Optional[str] = None, query: Optional[Mapping[str, Union[Sequence[str], str]]] = None) -> URL: (source)

return a new :class:`_engine.URL` object with modifications. Values are used if they are non-None. To set a value to ``None`` explicitly, use the :meth:`_engine.URL._replace` method adapted from ``namedtuple``. :param drivername: new drivername :param username: new username :param password: new password :param host: new hostname :param port: new port :param query: new query parameters, passed a dict of string keys referring to string or sequence of string values. Fully replaces the previous list of arguments. :return: new :class:`_engine.URL` object. .. versionadded:: 1.4 .. seealso:: :meth:`_engine.URL.update_query_dict`

def translate_connect_args(self, names: Optional[List[str]] = None, **kw: Any) -> Dict[str, Any]: (source)

Translate url attributes into a dictionary of connection arguments. Returns attributes of this url (`host`, `database`, `username`, `password`, `port`) as a plain dictionary. The attribute names are used as the keys by default. Unset or false attributes are omitted from the final dictionary. :param \**kw: Optional, alternate key names for url attributes. :param names: Deprecated. Same purpose as the keyword-based alternate names, but correlates the name to the original positionally.

def update_query_dict(self, query_parameters: Mapping[str, Union[str, List[str]]], append: bool = False) -> URL: (source)

Return a new :class:`_engine.URL` object with the :attr:`_engine.URL.query` parameter dictionary updated by the given dictionary. The dictionary typically contains string keys and string values. In order to represent a query parameter that is expressed multiple times, pass a sequence of string values. E.g.:: >>> from sqlalchemy.engine import make_url >>> url = make_url("postgresql+psycopg2://user:pass@host/dbname") >>> url = url.update_query_dict({"alt_host": ["host1", "host2"], "ssl_cipher": "/path/to/crt"}) >>> str(url) 'postgresql+psycopg2://user:pass@host/dbname?alt_host=host1&alt_host=host2&ssl_cipher=%2Fpath%2Fto%2Fcrt' :param query_parameters: A dictionary with string keys and values that are either strings, or sequences of strings. :param append: if True, parameters in the existing query string will not be removed; new parameters will be in addition to those present. If left at its default of False, keys present in the given query parameters will replace those of the existing query string. .. versionadded:: 1.4 .. seealso:: :attr:`_engine.URL.query` :meth:`_engine.URL.update_query_string` :meth:`_engine.URL.update_query_pairs` :meth:`_engine.URL.difference_update_query` :meth:`_engine.URL.set`

def update_query_pairs(self, key_value_pairs: Iterable[Tuple[str, Union[str, List[str]]]], append: bool = False) -> URL: (source)

Return a new :class:`_engine.URL` object with the :attr:`_engine.URL.query` parameter dictionary updated by the given sequence of key/value pairs E.g.:: >>> from sqlalchemy.engine import make_url >>> url = make_url("postgresql+psycopg2://user:pass@host/dbname") >>> url = url.update_query_pairs([("alt_host", "host1"), ("alt_host", "host2"), ("ssl_cipher", "/path/to/crt")]) >>> str(url) 'postgresql+psycopg2://user:pass@host/dbname?alt_host=host1&alt_host=host2&ssl_cipher=%2Fpath%2Fto%2Fcrt' :param key_value_pairs: A sequence of tuples containing two strings each. :param append: if True, parameters in the existing query string will not be removed; new parameters will be in addition to those present. If left at its default of False, keys present in the given query parameters will replace those of the existing query string. .. versionadded:: 1.4 .. seealso:: :attr:`_engine.URL.query` :meth:`_engine.URL.difference_update_query` :meth:`_engine.URL.set`

def update_query_string(self, query_string: str, append: bool = False) -> URL: (source)

Return a new :class:`_engine.URL` object with the :attr:`_engine.URL.query` parameter dictionary updated by the given query string. E.g.:: >>> from sqlalchemy.engine import make_url >>> url = make_url("postgresql+psycopg2://user:pass@host/dbname") >>> url = url.update_query_string("alt_host=host1&alt_host=host2&ssl_cipher=%2Fpath%2Fto%2Fcrt") >>> str(url) 'postgresql+psycopg2://user:pass@host/dbname?alt_host=host1&alt_host=host2&ssl_cipher=%2Fpath%2Fto%2Fcrt' :param query_string: a URL escaped query string, not including the question mark. :param append: if True, parameters in the existing query string will not be removed; new parameters will be in addition to those present. If left at its default of False, keys present in the given query parameters will replace those of the existing query string. .. versionadded:: 1.4 .. seealso:: :attr:`_engine.URL.query` :meth:`_engine.URL.update_query_dict`

database name

drivername: str = (source)

database backend and driver name, such as ``postgresql+psycopg2``

hostname or IP number. May also be a data source name for some drivers.

password, which is normally a string but may also be any object that has a ``__str__()`` method.

integer port number

query: util.immutabledict[str, Union[Tuple[str, ...], str]] = (source)

an immutable mapping representing the query string. contains strings for keys and either strings or tuples of strings for values, e.g.:: >>> from sqlalchemy.engine import make_url >>> url = make_url("postgresql+psycopg2://user:pass@host/dbname?alt_host=host1&alt_host=host2&ssl_cipher=%2Fpath%2Fto%2Fcrt") >>> url.query immutabledict({'alt_host': ('host1', 'host2'), 'ssl_cipher': '/path/to/crt'}) To create a mutable copy of this mapping, use the ``dict`` constructor:: mutable_query_opts = dict(url.query) .. seealso:: :attr:`_engine.URL.normalized_query` - normalizes all values into sequences for consistent processing Methods for altering the contents of :attr:`_engine.URL.query`: :meth:`_engine.URL.update_query_dict` :meth:`_engine.URL.update_query_string` :meth:`_engine.URL.update_query_pairs` :meth:`_engine.URL.difference_update_query`

username string

@util.memoized_property
normalized_query: Mapping[str, Sequence[str]] = (source)

Return the :attr:`_engine.URL.query` dictionary with values normalized into sequences. As the :attr:`_engine.URL.query` dictionary may contain either string values or sequences of string values to differentiate between parameters that are specified multiple times in the query string, code that needs to handle multiple parameters generically will wish to use this attribute so that all parameters present are presented as sequences. Inspiration is from Python's ``urllib.parse.parse_qs`` function. E.g.:: >>> from sqlalchemy.engine import make_url >>> url = make_url("postgresql+psycopg2://user:pass@host/dbname?alt_host=host1&alt_host=host2&ssl_cipher=%2Fpath%2Fto%2Fcrt") >>> url.query immutabledict({'alt_host': ('host1', 'host2'), 'ssl_cipher': '/path/to/crt'}) >>> url.normalized_query immutabledict({'alt_host': ('host1', 'host2'), 'ssl_cipher': ('/path/to/crt',)})

@classmethod
def _assert_none_str(cls, v: Optional[str], paramname: str) -> Optional[str]: (source)

Undocumented

@classmethod
def _assert_port(cls, port: Optional[int]) -> Optional[int]: (source)

Undocumented

@classmethod
def _assert_str(cls, v: str, paramname: str) -> str: (source)

Undocumented

@classmethod
def _str_dict(cls, dict_: Optional[Union[Sequence[Tuple[str, Union[Sequence[str], str]]], Mapping[str, Union[Sequence[str], str]]]]) -> util.immutabledict[str, Union[Tuple[str, ...], str]]: (source)

Undocumented

def _assert_replace(self, **kw: Any) -> URL: (source)

argument checks before calling _replace()

def _get_entrypoint(self) -> Type[Dialect]: (source)

Return the "entry point" dialect class. This is normally the dialect itself except in the case when the returned class implements the get_dialect_cls() method.

def _instantiate_plugins(self, kwargs: Mapping[str, Any]) -> Tuple[URL, List[Any], Dict[str, Any]]: (source)

Undocumented