관리-도구
편집 파일: extensions.cpython-38.pyc
U -?�f[L � @ s d Z ddlmZ ddlZddlZddlmZ ddlmZ ddlmZ ddlm Z dd l mZ dd l mZ ddl mZ dd lmZ ddlmZ dd lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ er�ddlm Z G dd� d�Z!G dd� de!�Z"G dd� d�Z#dS )z1Public API functions and helpers for declarative.� )�annotationsN)�Any)�Callable)� TYPE_CHECKING)�Union� )�exc)� Connection)�Engine)� relationships)�_mapper_or_none)� _resolver)�_DeferredMapperConfig)�polymorphic_union��Table)�OrderedDict)�MetaDatac @ s( e Zd ZdZedd� �Zedd� �ZdS )�ConcreteBasea_ A helper class for 'concrete' declarative mappings. :class:`.ConcreteBase` will use the :func:`.polymorphic_union` function automatically, against all tables mapped as a subclass to this class. The function is called via the ``__declare_last__()`` function, which is essentially a hook for the :meth:`.after_configured` event. :class:`.ConcreteBase` produces a mapped table for the class itself. Compare to :class:`.AbstractConcreteBase`, which does not. Example:: from sqlalchemy.ext.declarative import ConcreteBase class Employee(ConcreteBase, Base): __tablename__ = 'employee' employee_id = Column(Integer, primary_key=True) name = Column(String(50)) __mapper_args__ = { 'polymorphic_identity':'employee', 'concrete':True} class Manager(Employee): __tablename__ = 'manager' employee_id = Column(Integer, primary_key=True) name = Column(String(50)) manager_data = Column(String(40)) __mapper_args__ = { 'polymorphic_identity':'manager', 'concrete':True} The name of the discriminator column used by :func:`.polymorphic_union` defaults to the name ``type``. To suit the use case of a mapping where an actual column in a mapped table is already named ``type``, the discriminator name can be configured by setting the ``_concrete_discriminator_name`` attribute:: class Employee(ConcreteBase, Base): _concrete_discriminator_name = '_concrete_discriminator' .. versionadded:: 1.3.19 Added the ``_concrete_discriminator_name`` attribute to :class:`_declarative.ConcreteBase` so that the virtual discriminator column name can be customized. .. versionchanged:: 1.4.2 The ``_concrete_discriminator_name`` attribute need only be placed on the basemost class to take correct effect for all subclasses. An explicit error message is now raised if the mapped column names conflict with the discriminator name, whereas in the 1.3.x series there would be some warnings and then a non-useful query would be generated. .. seealso:: :class:`.AbstractConcreteBase` :ref:`concrete_inheritance` c C s t tdd� |D ��|d�S )Nc s s | ]}|j |jfV qd S �N)Zpolymorphic_identity�local_table)�.0�mp� r �U/opt/hc_python/lib64/python3.8/site-packages/sqlalchemy/ext/declarative/extensions.py� <genexpr>g s z9ConcreteBase._create_polymorphic_union.<locals>.<genexpr>�pjoin)r r )�cls�mappers�discriminator_namer r r �_create_polymorphic_uniond s ��z&ConcreteBase._create_polymorphic_unionc C sX | j }|jrd S t| dd �pd}t|j�}| �||�}|�d|f� |�|j| � d S )N�_concrete_discriminator_name�type�*) � __mapper__�with_polymorphic�getattr�listZself_and_descendantsr Z_set_with_polymorphicZ_set_polymorphic_on�c)r �mr r r r r r �__declare_first__n s � zConcreteBase.__declare_first__N)�__name__� __module__�__qualname__�__doc__�classmethodr r* r r r r r $ s ? r c @ s8 e Zd ZdZdZedd� �Zedd� �Zedd� �Zd S ) �AbstractConcreteBasea� A helper class for 'concrete' declarative mappings. :class:`.AbstractConcreteBase` will use the :func:`.polymorphic_union` function automatically, against all tables mapped as a subclass to this class. The function is called via the ``__declare_first__()`` function, which is essentially a hook for the :meth:`.before_configured` event. :class:`.AbstractConcreteBase` applies :class:`_orm.Mapper` for its immediately inheriting class, as would occur for any other declarative mapped class. However, the :class:`_orm.Mapper` is not mapped to any particular :class:`.Table` object. Instead, it's mapped directly to the "polymorphic" selectable produced by :func:`.polymorphic_union`, and performs no persistence operations on its own. Compare to :class:`.ConcreteBase`, which maps its immediately inheriting class to an actual :class:`.Table` that stores rows directly. .. note:: The :class:`.AbstractConcreteBase` delays the mapper creation of the base class until all the subclasses have been defined, as it needs to create a mapping against a selectable that will include all subclass tables. In order to achieve this, it waits for the **mapper configuration event** to occur, at which point it scans through all the configured subclasses and sets up a mapping that will query against all subclasses at once. While this event is normally invoked automatically, in the case of :class:`.AbstractConcreteBase`, it may be necessary to invoke it explicitly after **all** subclass mappings are defined, if the first operation is to be a query against this base class. To do so, once all the desired classes have been configured, the :meth:`_orm.registry.configure` method on the :class:`_orm.registry` in use can be invoked, which is available in relation to a particular declarative base class:: Base.registry.configure() Example:: from sqlalchemy.orm import DeclarativeBase from sqlalchemy.ext.declarative import AbstractConcreteBase class Base(DeclarativeBase): pass class Employee(AbstractConcreteBase, Base): pass class Manager(Employee): __tablename__ = 'manager' employee_id = Column(Integer, primary_key=True) name = Column(String(50)) manager_data = Column(String(40)) __mapper_args__ = { 'polymorphic_identity':'manager', 'concrete':True } Base.registry.configure() The abstract base class is handled by declarative in a special way; at class configuration time, it behaves like a declarative mixin or an ``__abstract__`` base class. Once classes are configured and mappings are produced, it then gets mapped itself, but after all of its descendants. This is a very unique system of mapping not found in any other SQLAlchemy API feature. Using this approach, we can specify columns and properties that will take place on mapped subclasses, in the way that we normally do as in :ref:`declarative_mixins`:: from sqlalchemy.ext.declarative import AbstractConcreteBase class Company(Base): __tablename__ = 'company' id = Column(Integer, primary_key=True) class Employee(AbstractConcreteBase, Base): strict_attrs = True employee_id = Column(Integer, primary_key=True) @declared_attr def company_id(cls): return Column(ForeignKey('company.id')) @declared_attr def company(cls): return relationship("Company") class Manager(Employee): __tablename__ = 'manager' name = Column(String(50)) manager_data = Column(String(40)) __mapper_args__ = { 'polymorphic_identity':'manager', 'concrete':True } Base.registry.configure() When we make use of our mappings however, both ``Manager`` and ``Employee`` will have an independently usable ``.company`` attribute:: session.execute( select(Employee).filter(Employee.company.has(id=5)) ) :param strict_attrs: when specified on the base class, "strict" attribute mode is enabled which attempts to limit ORM mapped attributes on the base class to only those that are immediately present, while still preserving "polymorphic" loading behavior. .. versionadded:: 2.0 .. seealso:: :class:`.ConcreteBase` :ref:`concrete_inheritance` :ref:`abstract_concrete_base` Tc C s | � � d S r )�_sa_decl_prepare_nocascade�r r r r r* s z&AbstractConcreteBase.__declare_first__c s� t | dd �rd S t�| �}g }t| �� �}|r`|�� }|�|�� � t|�}|d k r*|�|� q*t | dd �pnd�| � |���t |j�}dd� |D �� t|j� � �D ].\}}||kr��j|j |j|<