o
    [h1                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 dd	lm
Z
 dd
lmZ ddlmZ dgZe
jG dd deZdS )z|MapperProperty implementations.

This is a private module which defines the behavior of individual ORM-
mapped attributes.

    )absolute_import   )
attributes)PropComparator)StrategizedProperty)_orm_full_deannotate   )log)util
expressionColumnPropertyc                       s   e Zd ZdZdZdZejdd fddZe	dd	d
d Z
dd Zedd Zdd Z fddZdd ZejfddZdd ZG dd dejeZdd Z  ZS )r   zDescribes an object attribute that corresponds to a table column.

    Public constructor is the :func:`_orm.column_property` function.

    column)_orig_columnscolumnsgroupdeferred
instrumentcomparator_factory
descriptor	extensionactive_historyexpire_on_flushinfodocstrategy_keyZ_creation_orderZ_is_polymorphic_discriminatorZ_mapped_by_synonymZ_deferred_column_loader)z0.7z:class:`.AttributeExtension` is deprecated in favor of the :class:`.AttributeEvents` listener interface.  The :paramref:`.column_property.extension` parameter will be removed in a future release.)r   c                    sJ  t t|   dd |D | _dd |D | _|dd| _|dd| _|dd	| _|d
| j	j
| _|dd| _|dd| _|dd| _|dd	| _d|v r[|d| _d|v rf|d| _nt| jD ]}t|dd}|dur||| _ nqkd| _|rtd| j	jdt| f t|  d| jfd| jff| _dS )a  Provide a column-level property for use with a mapping.

        Column-based properties can normally be applied to the mapper's
        ``properties`` dictionary using the :class:`_schema.Column`
        element directly.
        Use this function when the given column is not directly present within
        the mapper's selectable; examples include SQL expressions, functions,
        and scalar SELECT queries.

        The :func:`_orm.column_property` function returns an instance of
        :class:`.ColumnProperty`.

        Columns that aren't present in the mapper's selectable won't be
        persisted by the mapper and are effectively "read-only" attributes.

        :param \*cols:
              list of Column objects to be mapped.

        :param active_history=False:
          When ``True``, indicates that the "previous" value for a
          scalar attribute should be loaded when replaced, if not
          already loaded. Normally, history tracking logic for
          simple non-primary-key scalar values only needs to be
          aware of the "new" value in order to perform a flush. This
          flag is available for applications that make use of
          :func:`.attributes.get_history` or :meth:`.Session.is_modified`
          which also need to know
          the "previous" value of the attribute.

        :param comparator_factory: a class which extends
           :class:`.ColumnProperty.Comparator` which provides custom SQL
           clause generation for comparison operations.

        :param group:
            a group name for this property when marked as deferred.

        :param deferred:
              when True, the column property is "deferred", meaning that
              it does not load immediately, and is instead loaded when the
              attribute is first accessed on an instance.  See also
              :func:`~sqlalchemy.orm.deferred`.

        :param doc:
              optional string that will be applied as the doc on the
              class-bound descriptor.

        :param expire_on_flush=True:
            Disable expiry on flush.   A column_property() which refers
            to a SQL expression (and not a single table-bound column)
            is considered to be a "read only" property; populating it
            has no effect on the state of data, and it can only return
            database state.   For this reason a column_property()'s value
            is expired whenever the parent object is involved in a
            flush, that is, has any kind of "dirty" state within a flush.
            Setting this parameter to ``False`` will have the effect of
            leaving any existing value present after the flush proceeds.
            Note however that the :class:`.Session` with default expiration
            settings still expires
            all attributes after a :meth:`.Session.commit` call, however.

        :param info: Optional data dictionary which will be populated into the
            :attr:`.MapperProperty.info` attribute of this object.

        :param extension:
            an :class:`.AttributeExtension` instance, or list of extensions,
            which will be prepended to the list of attribute listeners for the
            resulting descriptor placed on the class.

        .. seealso::

            :ref:`column_property_options` - to map columns while including
            mapping options

            :ref:`mapper_column_property_sql_expressions` - to map SQL
            expressions

        c                 S   s   g | ]}t |qS  )r   _labeled.0cr   r   /home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/sqlalchemy/orm/properties.py
<listcomp>       z+ColumnProperty.__init__.<locals>.<listcomp>c                 S   s   g | ]	}t t|qS r   )r   r   r   r   r   r   r!   r"      s    r   Nr   FZ_instrumentTr   r   r   r   r   r   r   z.%s received unexpected keyword argument(s): %sz, r   )superr   __init__r   r   popr   r   r   	__class__
Comparatorr   r   r   r   r   r   r   reversedgetattr	TypeError__name__joinsortedkeysr
   Zset_creation_orderr   )selfr   kwargscolr   r'   r   r!   r%   :   sH   W

zColumnProperty.__init__zsqlalchemy.orm.statezsqlalchemy.orm.strategiesc                 C   s   |j | jj|| j| jS N)ZInstanceStateZ"_instance_level_callable_processorparentZclass_managerZLoadDeferredColumnskey)r0   stateZ
strategiesr   r   r!   &_memoized_attr__deferred_column_loader   s
   
z5ColumnProperty._memoized_attr__deferred_column_loaderc                 C   s   | j S )zsAllow the ColumnProperty to work in expression before it is turned
        into an instrumented attribute.
        r   r0   r   r   r!   __clause_element__   s   z!ColumnProperty.__clause_element__c                 C   s
   | j d S )a  Return the primary column or expression for this ColumnProperty.

        E.g.::


            class File(Base):
                # ...

                name = Column(String(64))
                extension = Column(String(8))
                filename = column_property(name + '.' + extension)
                path = column_property('C:/' + filename.expression)

        .. seealso::

            :ref:`mapper_column_property_sql_expressions_composed`

        r   )r   r9   r   r   r!   r      s   
zColumnProperty.expressionc                 C   s0   | j sd S tj|j| j| | ||| jd d S )N)Z
comparatorparententityr   )r   r   Zregister_descriptorclass_r6   r   r   )r0   Zmapperr   r   r!   instrument_class   s   

zColumnProperty.instrument_classc                    sb   t t|   t| jdkr-t| jj| jr/t	
d| j| jd | jd | jf  d S d S d S )Nr   zOn mapper %s, primary key column '%s' is being combined with distinct primary key column '%s' in attribute '%s'. Use explicit properties to give each column its own mapped attribute name.r   )r$   r   do_initlenr   setr5   Zprimary_key
issupersetr
   warnr6   r9   r3   r   r!   r>      s   zColumnProperty.do_initc                 C   s   t | j| j| j| jdS )N)r   r   r   )r   r   r   r   r   r9   r   r   r!   copy   s   zColumnProperty.copyc                 C   s   | | jj|||dS )N)passive)get_implr6   Zget_committed_value)r0   r7   Zdict_r   rD   r   r   r!   _getcommitted  s   zColumnProperty._getcommittedc	                 C   s   | j sd S | j|v r(|| j }	|s|	|| j< d S || j}
|
|||	d  d S |jr<| j|vr>|j|| jgdd d S d S d S )NT)Z	no_loader)r   r6   rE   r@   Zhas_identityZ_expire_attributes)r0   sessionZsource_stateZsource_dictZ
dest_stateZ	dest_dictloadZ
_recursiveZ_resolve_conflict_mapvalueimplr   r   r!   merge  s   



zColumnProperty.mergec                   @   sD   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dS )zColumnProperty.Comparatora  Produce boolean, comparison, and other operators for
        :class:`.ColumnProperty` attributes.

        See the documentation for :class:`.PropComparator` for a brief
        overview.

        .. seealso::

            :class:`.PropComparator`

            :class:`.ColumnOperators`

            :ref:`types_operators`

            :attr:`.TypeEngine.comparator_factory`

        )r:   r   Zexpressionsc                 C   s4   | j r|  | jjd S | jjd | j| jdS )Nr   )r;   parentmapper)adapterpropr   	_annotate_parententityr9   r   r   r!   #_memoized_method___clause_element__:  s   z=ColumnProperty.Comparator._memoized_method___clause_element__c                 C   s,   |   }z|jW S  ty   | jj Y S w )z(The .info dictionary for this attribute.)r:   r   AttributeErrorrN   )r0   Zcer   r   r!   _memoized_attr_infoG  s   z-ColumnProperty.Comparator._memoized_attr_infoc                    s2    j r fdd jjD S  fdd jjD S )zThe full sequence of columns referenced by this
            attribute, adjusted for any aliasing in progress.

            .. versionadded:: 1.3.17

            c                    s   g | ]}  |qS r   )rM   r   r2   r9   r   r!   r"   X  r#   zHColumnProperty.Comparator._memoized_attr_expressions.<locals>.<listcomp>c                    s&   g | ]}|  j j jjd qS ))r;   rL   Zorm_key)rO   rP   rN   r6   rT   r9   r   r!   r"   \  s    )rM   rN   r   r9   r   r9   r!   _memoized_attr_expressionsP  s
   
z4ColumnProperty.Comparator._memoized_attr_expressionsc                 C   s   t |  |S )zproxy attribute access down to the mapped column.

            this allows user-defined comparison methods to be accessed.
            )r*   r:   )r0   r6   r   r   r!   _fallback_getattrg  s   z+ColumnProperty.Comparator._fallback_getattrc                 O   s   ||   g|R i |S r4   )r:   )r0   opotherr1   r   r   r!   operaten  s   z!ColumnProperty.Comparator.operatec                 K   s"   |   }|||||fi |S r4   )r:   Z_bind_param)r0   rW   rX   r1   r2   r   r   r!   reverse_operateq  s   z)ColumnProperty.Comparator.reverse_operateN)r,   
__module____qualname____doc__	__slots__rQ   rS   rU   rV   rY   rZ   r   r   r   r!   r(   %  s    	r(   c                 C   s   t | jjjd | j S )N.)strr5   r<   r,   r6   r9   r   r   r!   __str__u  s   zColumnProperty.__str__)r,   r[   r\   r]   Zstrategy_wildcard_keyr^   r
   Zdeprecated_paramsr%   dependenciesr8   r:   propertyr   r=   r>   rC   r   ZPASSIVE_OFFrF   rK   ZMemoizedSlotsr   r(   ra   __classcell__r   r   r3   r!   r      s*    	
y

	
PN)r]   
__future__r    r   Z
interfacesr   r   r
   r   r	   sqlr   __all__Zclass_loggerr   r   r   r   r!   <module>   s   