a
    xˆcÀ  ã                   @   sp   d Z ddlmZ ddlmZ ddlmZ ddlmZ dd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )zgprivate module containing functions used for copying data
between instances based on join conditions.

é   )Ú
attributes)Úexc)Úutilé   c                 C   s  | j }|j }|D ]ö\}	}
z(|j|	 }| j|j j | |tj¡}W n8 tj	yx } zt
d||	||
|ƒ W Y d }~n
d }~0 0 z(|j|
 }|j|j j |||d ¡ W n8 tj	yÚ } zt
d||	||
|ƒ W Y d }~n
d }~0 0 |r|	jr|
jr|
 |	¡rd|jd||
f< qd S )NFTZpk_cascaded)ÚdictÚ_columntopropertyÚmanagerÚkeyÚimplÚgetr   ÚPASSIVE_OFFr   ÚUnmappedColumnErrorÚ_raise_col_to_propÚsetÚprimary_keyZ
references)ÚsourceÚsource_mapperÚdestÚdest_mapperÚsynchronize_pairsÚ	uowcommitZflag_cascaded_pksÚsource_dictZ	dest_dictÚlÚrÚpropÚvalueÚerr© r   úV/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/sqlalchemy/orm/sync.pyÚpopulate   s0    	
ÿ(
(ÿþýür   c                 C   s¢   |D ]˜\}}z|j | }| |j }W n8 tjy\ } ztd|||||ƒ W Y d }~n
d }~0 0 z|j | }|| |j< W q tjyš   td||||ƒ Y q0 qd S )NFT)r   r	   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   Úbulk_populate_inherit_keys>   s    
(
r    c                 C   s”   |D ]Š\}}|j r>| | | j|¡tjvr>td|t | ¡f ƒ‚z| | | j|d ¡ W q tj	yŒ } zt
dd ||||ƒ W Y d }~qd }~0 0 qd S )NzKDependency rule tried to blank-out primary key column '%s' on instance '%s'T)r   Ú_get_state_attr_by_columnr   Úorm_utilZ	_none_setÚAssertionErrorZ	state_strZ_set_state_attr_by_columnr   r   r   )r   r   r   r   r   r   r   r   r   ÚclearN   s     ÿÿþÿÿr$   c           
      C   sŽ   |D ]„\}}z*|  |  ¡ |¡}|j| | j|tjd�}W n8 tjyn }	 ztd||d ||	ƒ W Y d }	~	n
d }	~	0 0 |||j	< ||||j	 < qd S ©N)ZpassiveF)
Z_get_committed_attr_by_columnÚobjr!   r   r   r   r   r   r   r	   )
r   r   r   Z
old_prefixr   r   r   Zoldvaluer   r   r   r   r   Úupdate`   s    ÿÿ
(
r'   c                 C   sp   |D ]f\}}z|j | | j|tjd�}W n8 tjy^ } ztd||d ||ƒ W Y d }~n
d }~0 0 |||j< qd S r%   )r!   r   r   r   r   r   r   r	   )r   r   Zdict_r   r   r   r   r   r   r   r   Úpopulate_dicto   s    ÿ
(r(   c           	      C   s|   |D ]r\}}z|j | }W n8 tjyR } ztd||d||ƒ W Y d}~n
d}~0 0 |  ||jtj¡}t|j	ƒr dS qdS )zpreturn true if the source object has changes from an old to a
    new value on the given synchronize pairs

    FNT)
r   r   r   r   Zget_attribute_historyr	   r   ZPASSIVE_NO_INITIALIZEÚboolZdeleted)	r   r   r   r   r   r   r   r   Úhistoryr   r   r   Úsource_modified{   s    (
ÿ
r+   c                 C   sD   | r"t jt d||f ¡|d� nt jt d|||f ¡|d� d S )NzÑCan't execute sync rule for destination column '%s'; mapper '%s' does not map this column.  Try using an explicit `foreign_keys` collection which does not include this column (or use a viewonly=True relation).)Zreplace_contextzØCan't execute sync rule for source column '%s'; mapper '%s' does not map this column.  Try using an explicit `foreign_keys` collection which does not include destination column '%s' (or use a viewonly=True relation).)r   Zraise_r   r   )Zisdestr   Zsource_columnr   Zdest_columnr   r   r   r   r   Ž   s"    üÿøûÿ÷r   N)Ú__doc__Ú r   r   r   r"   r   r    r$   r'   r(   r+   r   r   r   r   r   Ú<module>   s   +