
    g                       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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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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d"l*m+Z+ dd#l*m,Z, dd$l*m-Z- dd%lm.Z. erBdd&l/m0Z0 dd'lm1Z1 dd(l2m3Z3 dd)l4m5Z5 dd*l6m7Z7 dd+l8m9Z9 dd,l:m;Z; dd-l<m=Z= dd.l>m?Z? dd/l#m@Z@ dd0l'mAZA  ed1e2      ZB ed3eC2      ZD ej                         ZFeeGeeeGef      f   ZHdNd4ZI ej                  d5      dOd6       ZK ej                  d7d89       ej                  d5      	 dO	 	 	 	 	 	 	 dPd:              ZM	 	 	 	 	 	 	 	 	 	 dQd;ZNd<d<d<d<d=ej\                  ej\                  d=d=d>		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dRd?ZOd<d<d<d<d<d=ej\                  ej\                  d=d=d@
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dSdAZP	 	 	 	 	 	 dTdBZQ	 	 	 dUdCZRdD ZS	 	 	 	 dVdEZTdF ZUdG ZVdH ZWdI ZXdJ ZY G dK dL      ZZdM Z[y<)Wzprivate module containing functions used to convert database
rows into object instances and associated state.

the functions here are called primarily by Query, Mapper,
as well as some of the attribute loading strategies.

    )annotations)Any)Dict)Iterable)List)Mapping)Optional)Sequence)Tuple)TYPE_CHECKING)TypeVar)Union   )
attributes)exc)path_registry)_DEFER_FOR_STATE)_RAISE_FOR_STATE)_SET_DEFERRED_EXPIRED)PassiveFlag)FromStatement)ORMCompileState)QueryContext)	_none_set)	state_str   )util)result_tuple)ChunkedIteratorResult)FrozenResult)SimpleResultMetaData)select)ForUpdateArg)LABEL_STYLE_TABLENAME_PLUS_COL)SelectState)
EMPTY_DICT)_IdentityKeyType)LoaderCallableStatus)	ORMOption)Mapper)Query)Session)InstanceState)CursorResult)_ExecuteOptions)Result)Select_T)bound_Oc                &    t               _        j                  rdj                  j                  _        n	di _        j                  }|j
                  }j                  j                   xr3 t        |j                        dk(  xr |j                  d   j                  	 t        t        j                  j                  D cg c]  }|j                          c}       \  }}j                  r-j                  sj                   rt#        j$                  d      d }dddd	}j                  j                  D 	cg c]  }	j                  r|nt|	j.                  sU|	j0                  s|	j2                  r= ||	j4                  j6                  j                  j8                  |	j2                        n|	j.                  rt:        nd }
}	t=        |

      } fd}j>                  jA                  dd      rt         |d            fd}tC        ||  jD                  jF                        }|jH                  jK                  tM        |            |_$        j                  jN                  rd }d|f|_(        j                  r|j                  j                         |S c c}w # t&        $ r> t)        j*                         5   j-                          ddd       n# 1 sw Y   nxY wY w xY wc c}	w )a  Return a :class:`.Result` given an ORM query context.

    :param cursor: a :class:`.CursorResult`, generated by a statement
     which came from :class:`.ORMCompileState`

    :param context: a :class:`.QueryContext` object

    :return: a :class:`.Result` object representing ORM results

    .. versionchanged:: 1.4 The instances() function now uses
       :class:`.Result` objects and has an all new interface.

    FTr   r   zCan't use yield_per with eager loaders that require uniquing or row buffering, e.g. joinedload() against collections or subqueryload().  Consider the selectinload() strategy for better flexibility in loading objects.Nc                ,    t        j                  d      )Nz@Can't use the ORM yield_per feature in conjunction with unique()sa_excInvalidRequestError)entrys    V/var/www/api/v1/venv_getwork_v1/lib/python3.12/site-packages/sqlalchemy/orm/loading.py
_no_uniquezinstances.<locals>._no_unique   s    ((N
 	
    )legacy	uncertainc               >     |s fd}|S st         S dfd}|S )Nc                n    r	 t        |       S t        j                  ddrdnd d      #  Y $xY w)Nz=Can't apply uniqueness to row tuple containing value of type z; z the values returned appear to bezthis datatype producesz non-hashable values)hashr8   r9   )objdatatyper?   s    r;   goz,instances.<locals>._not_hashable.<locals>.go   sZ    #Cy( 00&\$ ;5, *- s   
0 4Fc                J    s	 t        |       S t        |       S #  Y nxY wd)NT)rB   id)rC   _use_ids    r;   rE   z,instances.<locals>._not_hashable.<locals>.go   s4     #Cy( #w
 #Gs   
 )rG   )rD   r>   r?   rE   rH   s   ` ` @r;   _not_hashablez instances.<locals>._not_hashable   s+      IIG  Ir=   )_unique_filtersc              3    K   	 | }i 
_         |rj                  |      }|sy j                         }rd   }|D cg c]
  } ||       }}n.|D cg c]!  }t        D cg c]
  } ||       c}      # }}}r|r#t	        
j
                  j                               }
j
                  rft	        
j
                  j                               }
j
                  j                          |D ]  \  }}	|	j                  
|        
j
                  rf|r5
j
                  j                          
j
                  j                         | |sy Sc c}w c c}w c c}}w w)Nr   )
partials	fetchmany_raw_all_rowstuplelistpost_load_pathsitemsclearinvokeupdate)size	yield_perfetchprocrowrowstop_level_post_loads
post_loadspath	post_loadcontextcursoris_top_levelprocesssingle_entitys             r;   chunkszinstances.<locals>.chunks   sl    I!G((3,,.qz-23UcS	U3 GLFKsE9499:e    ,0//557,( --!%g&=&=&C&C&E!FJ++113+5i!(($7 ,6 -- ++113++223GHJ_  4 :s<   <E(EE(E"$E3	E"<BE(AE(E""E(prebuffer_rowsc                    t              S N)iter)rV   _prebuffereds    r;   re   zinstances.<locals>.chunks  s    %%r=   )source_supports_scalarsrawdynamic_yield_per)filteredis_single_entityc                ,    t        j                  d      )Nz~The unique() method must be invoked on this Result, as it contains results that include joined eager loads against collectionsr7   )rC   s    r;   require_uniquez!instances.<locals>.require_unique  s    ,,& r=   ))
_new_runidrunidtop_level_contextrQ   compile_state_has_mapper_entitiesload_options_only_return_tupleslen	_entitiessupports_single_entityrP   ziprow_processorrW   loaders_require_bufferingloaders_require_uniquingr8   r9   	Exceptionr   safe_reraisecloseuse_id_for_hash_non_hashable_value_null_column_typecolumntype_legacy_uniquingrG   r!   execution_optionsgetr   r`   _is_server_side_attributesuniondictmulti_row_eager_loaders_unique_filter_state)ra   r`   ru   rn   query_entitylabelsextrar<   rI   entunique_filtersrow_metadatare   resultrq   rj   rb   rc   rd   s   ``             @@@@r;   	instancesr   N   s    LGM  ")";";"K"K"$))M11H  444 	>''(A-	>##A&== #' )0(=(=(G(G(G !..w?(G$
 &% --//,,= 

 +05 )x ((22#" 3C     ++00C4I4I JJOO"//@@!33 ..RD 3#  ( (~L0 0d   $$%5u= F4L)	& # - ..88F  ++11h?F
 44	 (,^&<#**+Mo$   LLN !  hsD   ."K J?)AK B
L?K L!K;2	L;L	 L
Lzsqlalchemy.orm.contextc           	     @   t         j                  j                  }|r| j                          |j                  j                  |d      }| j                  }	 d| _        t        |j                        D cg c]  \  }}t        ||j                        r| }	}}|j                  D 
cg c]  }
|
j                   }}
t        ||j                  D 
cg c]  }
|
j                   c}
      }g }|j                         D ]k  }|	D ]M  }||   	| j                  t!        j"                  ||         t!        j$                  ||         |i i       ||<   O |j'                   ||             m |j)                  |      || _        S c c}}w c c}
w c c}
w # || _        w xY w)az  Merge a :class:`_engine.FrozenResult` back into a :class:`_orm.Session`,
    returning a new :class:`_engine.Result` object with :term:`persistent`
    objects.

    See the section :ref:`do_orm_execute_re_executing` for an example.

    .. seealso::

        :ref:`do_orm_execute_re_executing`

        :meth:`_engine.Result.freeze`

        :class:`_engine.FrozenResult`

    Fr>   load
_recursive_resolve_conflict_map)r   	preloadedorm_context
_autoflushORMSelectCompileState_create_entities_collection	autoflush	enumeraterz   
isinstance_MapperEntity_label_namer   _extra_entitiesrewrite_rows_merger   instance_stateinstance_dictappendwith_new_rows)session	statementfrozen_resultr   querycontextctxr   iemapped_entitiesr   keyskeyed_tupler   newrows                  r;   merge_frozen_resultr   +  s   " >>--L

,
,
H
H% I C !!I&! "#--0
01!\778 0 	 

 ,/==9=C=9"#--@-33&&-@
 #002F$!9( '"11&)<"00;!#%.0 !/ !F1I % MM+f-. 3 **62%7

 : A& &s=   F 6"FF )F
<F F
%&F A0F F 	Fz:func:`_orm.merge_result`zyThe function as well as the method on :class:`_orm.Query` is superseded by the :func:`_orm.merge_frozen_result` function.)alternativec           
        t         j                  j                  }| j                  }|r|j	                          t        |t              r|}t        |j                        }nd}|j                  j                  | d      }|j                  }	 d|_        | xr t        |j                        dk(  }|r{t        |j                  d   |j                        rK|D 	cg c]>  }	|j                  t!        j"                  |	      t!        j$                  |	      |i i       @ }
}	nt'        |      }
nt)        |j                        D cg c]  \  }}t        ||j                        r| }}}g }
|j                  D cg c]  }|j*                   }}t-        ||j                  D cg c]  }|j.                   c}      }|D ]v  }t'        |      }|D ]M  }||   	|j                  t!        j"                  ||         t!        j$                  ||         |i i       ||<   O |
j1                   ||             x |r|j3                  |
      ||_        S t        |
      ||_        S c c}	w c c}}w c c}w c c}w # ||_        w xY w)zMerge a result into the given :class:`.Query` object's Session.

    See :meth:`_orm.Query.merge_result` for top-level documentation on this
    function.

    NTr   Fr   r   r   )r   r   r   r   r   r   r    ri   datar   r   r   ry   rz   r   r   r   r   r   rP   r   r   r   r   r   r   )queryiteratorr   r   r   r   r   r   rd   instancer   r   r   r   r   r   r   rZ   r   s                      r;   merge_resultr   g  s   $ >>--LmmG (L) **+

,
,
H
Hd I C !!I1&!))Ec#--.@A.E#--*L,F,FG %-	 %- NN"11(;"00:!#%.0 #  %-  	 h &cmm44DAqa!;!;< 4  
 F/2}}=}COO}D=&cmmDmss**mDK  c(Aay,$+NN&55fQi@&44VAY?!%')24 %3 %q	 ) k&12    ..v6 & <%W	 > E* &sQ   AI: AI%)I: "I*(I: ;I0I: $I5
7!I: A2I: 
I: %I: :	Jc                   | j                   j                  |      }|t        j                  |      }|j                  r+|j
                  j                  |      st        j                  S |j                  rM|t        j                  z  st        j                  S |t        j                  z  s|S 	 |j                  ||       |S |S y# t        j                  $ r | j                  |g       Y yw xY w)zqLook up the given key in the given session's identity map,
    check the object for expired state if found.

    N)identity_mapr   r   r   inheritsmapperisaPASSIVE_CLASS_MISMATCHexpiredSQL_OKPASSIVE_NO_RESULTRELATED_OBJECT_OK_load_expiredorm_excObjectDeletedError_remove_newly_deleted)r   r   keypassiver   states         r;   get_from_identityr     s     ##'',H))(3??5<<#3#3F#;444 ==Z...!333z;;;
  ##E73 x -- --ug6s   /C %C.-C.NF)	rw   refresh_statewith_for_updateonly_load_propsno_autoflushbind_argumentsr   require_pk_colsis_user_refreshc       	        T    ||d   }|d   }ndx}}t        | ||||||||||	|
|      S )z.Load the given identity key from the database.Nr   r   
rw   r   r   r   identity_tokenr   r   r   r   r   )load_on_pk_identity)r   r   r   rw   r   r   r   r   r   r   r   r   identr   s                 r;   load_on_identr     sY      AQ!%%!#''%!%+'' r=   r   c       
   	        |}|j                         }|j                  rJ |t        j                  }|j                  t
        j                  u rt        j                  }n|j                  }||j                  d   }|j                  \  }}d|v ryt        |j                  |      D ch c]  \  }}|||   j                   }}}t        j                  ||      }t!        |      t!        |      k(  rt#        j$                  d       t        j&                  |ddi      f|_        t        ||j                        D ci c]  \  }}||   j                  | }}}nd}|
d}||_        n"|j*                  d}|j*                  |_        nd}|r|r|st-        j.                  d      |j0                  j2                  }|j5                  |      D ch c]+  }|j6                  |   j8                  j;                         r|- }}|r&t-        j<                  d| d	|j0                   d
      |j0                  j>                  }|D ]$  }||   j@                  s|jC                  |      } n |r@|jD                  r4|d|jF                  jH                  iz  } |jJ                  |jD                   }tM        |||||||      \  }}||_        d|_'        |r|ddiz  }t"        jP                  jS                  |
d|i      }
| jU                  |||
|	      jW                         jY                         } 	 | j[                         S c c}}w c c}}w c c}w # t\        j^                  $ r Y yw xY w)z6Load the given primary key identity from the database.Nplugin_subjectzofully NULL primary key identity cannot load any object.  This condition may raise an error in a future release.
_orm_adaptTFz9refresh_state is required when require_pk_cols is presentz7Please flush pending primary key changes on attributes z for mapper z! before proceeding with a refresh_current_path)version_checkr   r   r   r   r   _sa_orm_load_options)paramsr   r   )0_clone_is_lambda_elementr   default_load_options_compile_optionsr%   default_select_compile_optionsr   default_compile_options_propagate_attrs_get_clauser|   primary_keyr   sql_utiladapt_criterion_to_nullry   r   warn_deep_annotate_where_criteria_for_update_argr8   ArgumentErrorr   _primary_key_propkeys
differenceattrshistoryhas_changesr9   _props_is_relationshipr   rw   	load_pathparentoptions_set_get_options	_order_byr&   
merge_withexecuteuniquescalarsoner   NoResultFound)!r   r   primary_key_identityrw   r   r   r   r   r   r   r   r   r   r   qcompile_optionsr   r   _get_paramscolvaluenonesid_valr   r   r   refresh_state_prokeysr   r   mppnew_compile_optionsr   s!                                    r;   r   r     s   $ EA#####88 	""55	6 *AA#44'''(89%+%7%7"k '' #&f&8&8:N"O"OJC= C $$"O   #::;NK5zS!566		 ##K,1EF
 (+$f&8&8(
(# $((&0( 	 
 "+				*!11?&&K  !. 4 4 J J -77H
H""3'//;;= H 	 

  ,,-|M,@,@+A B,, D !!(( A!u%%"7"="=o"N !
 33O]-D-D-K-KLLAII}112(8#'#%')% -AAKu--222LA 	/)	 	 	
 
	 zz|S&
2
|    s$   )L= M0M	-M M$#M$c                    i }i }	|r||	d<   |r||	d<   |r
||	d<   d|d<   |rt        |      |d<   |r||	d<   |r||	d<   |	r||	z  }|r| |z  } | |fS )	N_version_check_populate_existing_refresh_stateT_for_refresh_state_only_load_props_identity_token_is_user_refresh)	frozenset)
compile_optload_optpopulate_existingr   r   r   r   r   r  rw   s
             r;   r  r    s     OL)6%&->)*)6%&04,-.7.H*+*8&'+:'(L &  r=   c	           	        |r|j                  |      }
n|j                  }
i }|j                  | j                  d|       |xr t	        |      dk(  xr |d   j
                  }|
D ].  }|r|j                  |vr |j                  | |||f||||d|	 0 |5||j                  ur&|r|j                  |   }n|}|j                  |       y y y )Nmemoized_setupsr   )r   column_collectionmemoized_populatorscheck_for_adapt)_iterate_polymorphic_properties_polymorphic_propertiessetr   ry   is_aliased_classr   setuppolymorphic_oncolumnsr   )ru   r   r   r^   adapterr&  with_polymorphicr   polymorphic_discriminatorkwpoly_propertiesquick_populatorsr(  r  pds                  r;   _setup_entity_queryr7    s      @@
 !88HH]%%'8:JK N#d)q.NT"X5N5NO uyy?	
	

 ,/ 0+
	
 
	
 !  	"-%V-B-BB!:;B*B  $ C 	.r=   c                H    t        j                  dt        |       d       y )NzLoading context for aW   has changed within a load/refresh handler, suggesting a row refresh operation took place. If this event handler is expected to be emitting row refresh operations within an existing load or refresh operation, set restore_load_context=True when establishing the listener to ensure the context remains unchanged when the event handler completes.)r   r   r   )r   s    r;   _warn_for_runid_changedr9    s    II !*% 0	3r=   c
                  "#$%&'()*+,-./0123456 j                   "j                  }
df}|j                  |
j                  |d      }|j                  }|j                  fdD              }|j                  j                  dt              }g }g g g g g g d}<j                  }|r|D cg c]  }|j                  |    }}|j                  |      .nd.||.d}|D ]$  }||v r||   }|t        u r+|d   j                  |j                  |j                  f       A|t        u r!|d   j                  |j                  d	f       j|t        u r+|d   j                  |j                  |j                   f       d}|r#|j                  |   }||j#                  |d	      }|s|j#                  |d	      }|r!|d
   j                  |j                  |f       |j%                  | ||||       |j                  |       ' |j'                  |
j                  ||       |d   }|j)                         D ci c]  \  }}|t+        |       c}},|d   D ]  }|j%                  | |||,        j,                  /j                  j.                  j0                  rj                  j.                  |z   n|(j2                  j4                  4j6                  xs j8                  +t;        j<                  j>                  j@                        't;        j<                  j>                  jB                        0t;        j2                  j>                  jD                        **r j2                  j>                  jD                  )t        jF                  %t        jH                  $j2                  jJ                  3jL                  2jN                  #jP                  55r3jR                  }|#|r|j                  |   }|j#                  |      6nd6s|	d|j0                  f}|j                  v r9j                  |   jT                  dk(  rj                  |   jV                  d   }nd}jY                  ||	      }|r||	urJ |jZ                  r/g }|}|r,||	ur(|j                  |       |j\                  }|r	||	ur$n|g}|D ]M  }t^        ja                  (|      rtc        |||	|      }t^        je                  (|jf                  |||       O t^        ji                  (      -r j                  11jk                        1nd1|d   .jl                  rtn        jp                  &ntn        j
                  &"#$%&'()*+,-./0123456fd} jr                  r|	ss"#&.fd}!tu        | | |||||!	      } | S c c}w c c}}w )z]Produce a mapper level row processor callable
    which processes rows into mapped instances.gettersNc              3  <   K   | ]  }j                   |     y wrh   )r   ).0kr   s     r;   	<genexpr>z&_instance_processor.<locals>.<genexpr>U  s      '*9Qa /s   r$  )newquickdeferredexpireexistingeager)cached_populatorstodoprimary_key_getterr@  rC  FrA  rF  rG  loader))selectinload_polymorphicTentitiesrH  c                   r.}|j                         } |      }|j                   k7  }d}d}n |       f}"j                  |      }|: |      } |      }|j                   k7  }| }d}#rw$ru|sst        ||| $       nc |d         ry d}d}d}j                  j                         } |      } |      }||_        |_        !|_        "j                  ||       }|u rd}|s|r9|rs|s|_
        |_        t        | |||||	       |r|j                  }	|rrr@|j                  j                  j                  |       |j                  |	k7  rt!        |       rq j"                  |       |j                  |	k7  rOt!        |       nCrA|j                  j                  j%                  |       |j                   k7  rt!        |       |s|j&                  r)rr|j)                  |       n|j+                  |"       rj-                  |d       |S |j.                  }
|j0                  v}|r|
sd   rut3        | ||||
      }|rarM|j                  }	|j                  j                  j%                  ||       |j                  |	k7  rt!        |       |j)                  ||       rj4                  rj-                  |d       |S )NTFr   rE  )rC   rs   r   _validate_version_idclass_managernew_instancer   r   
session_id_add_unpresentrw   r   _populate_fullmanagerdispatchr   r9  r   refreshmodified_commit_commit_all	add_stateunloadedrL   _populate_partialinvoke_all_eagers)%rZ   r   r   dict_isnewcurrentloadloaded_instanceidentitykeyeffective_populate_existingexisting_runidrZ  to_loadr`   identity_classr   r   r   is_not_primary_keyload_evtr   loaded_as_persistentr   r   persistent_evtr"  
populatorsr_   rH  propagated_loader_optionsrefresh_evtrefresh_identity_keyr   rs   rP  session_identity_mapr   version_id_getters%               r;   	_instancez&_instance_processor.<locals>._instance+  s   !Eyy{H!(+EKK5(EK#O
 "3'K ,//<H#&x0%h/u,"'i"' %6{(uc3D &k!n5""&!//<<>%h/&x0'	'5$ $. $33E;G&7#E!*.'
 5 )1L%>""++
  "'"..33E7C ;;.83E:%,W__eD ;;.83E: MM**22w {{e+/6.%..$e_=))%1EF##E40J ? ~~H!1!11EH
7(;
 ,	 ")...66ugwO ;;.83E:MM%1W66##E51r=   c                6     |       f} |d         s|S y )Nr    )rZ   ra  re  r   rf  rH  s     r;   ensure_no_pkz)_instance_processor.<locals>.ensure_no_pk  s/    "3'K
 &k!n5""r=   );_identity_classru   r   r   	_prop_setintersectionr&   r   r/  _tuple_getterr   r   r   _deferred_column_loaderr   r   _raise_column_loader_gettercreate_row_processorr+  rR   rP   rk  current_pathr^   r   r   r"  always_refreshboolrN  rT  r   rU  rh  r   r   hash_keyrs   r   r   version_id_colstrategy
local_opts_should_selectin_load	is_mapperr   PostLoadpath_exists_load_subclass_via_incallable_for_pathr   for_context_identity_key_from_stateallow_partial_pksr   
issupersetpolymorphic_map_decorate_polymorphic_switch)7r   r   r`   r   r^   r0  r   r   r2  _polymorphic_fromru   
getter_keyr;  propsr5  rG  rF  pk_colscpropr  getteradapted_colr   r  r  option_entitiesselectin_load_via_load_supers_endmost_mapper_selectinload_entity	callable_rp  rs  re  r   r   r   rf  rg  r   rh  ri  r"  rj  r_   rH  rk  rl  rm  rs   rP  rn  r   ro  s7    ``   ``                          @@@@@@@@@@@@@@@@@@@@@r;   _instance_processorr  +  sN   * ++N))M V$Jhh}//TBG   &&& '*9' E  88 1:
 
  ((G7>?w!7??1-w?!'!5!5g!>!% "3"4

 D''&t,**%e,334#?#?@ 11 &h/66%7HI,,%e,334#<#<= "F* '.ooc&:&2%+^^K%GF!!'U!;)'299488V:LM
 11#( ""#- D!A D 	)):w? 345F5L5L5NO5NzsE#tE{"5NOJ!!\4*	
  
 !( A A   --22 	**T1  #??7711JV5J5JF((11667Hv++44<<=K'//22GGHN&77LL..N,,M))JMME++N))M..%!(!@ &~ > $.:#'$$$););C)@)I)I N
 *
 &005@@LO"O"88

 !2:K!K #*** **!"3#'/@@ ''8&5&>&>O	 $'/@@
 !22(4$''Y(< 1(%#	 **(//(( )5* $$WiII,00' $*#B#B$   $$%9:&11&33^ ^ ^ ^@ &7		 1%

	 s @` Ps   >W-W2c                   	
 |j                   
|j                   }|j                  d u}t        
j                  j                        dk(  |j
                  s|r
j                  ||      \  	n
j                  \  		
fd}|S )Nr   c                
   s4|D cg c]$  \  }}|j                   j                        r||f& }}}|sy | j                  }|j                  r#j	                  |      }j	                  |      }	n}}	|f|j
                  z   |	fz   }
 j                  |
 }| j                  j                  |_	        |xj                  d|j                  iz  c_	        | j                  r|j                  d      }| j                  j                  |t        |D cg c](  \  }}r|j                  d   d   n|j                  d   * c}}            j!                         j#                         j%                          y c c}}w c c}}w )Nr   T)r"  r   r   )primary_keys)r   +_would_selectin_load_only_from_given_mapperr   r  _prepend_path_with_optionsr  ru   r   r   r"  r   r   r  r   r   r  r  all)r`   r^   states	load_onlyeffective_entitysv
orig_queryenable_opt_lcldisable_opt_lclr  q2r   
load_attrsdisable_opt
enable_optr   r  r  zero_idxs                 r;   do_loadz&_load_subclass_via_in.<locals>.do_load  sy    #"DAq88GGO A"   ]]
;;'55d;N)77=O'N)O
 8 88O;MM 	 QYY %33KK
==$$%%%=B .4-3)z (0EIIaLOUYYq\A-3	
 &(779SSUK@s   )E9-E?)r   r   ry   base_mapperr   r,  _subclass_load_via_in_subclass_load_via_in_mapper)r`   r^   entitypolymorphic_fromr  polymorphic_from_mappernot_against_basemostr  r  r  r   r  r  s       `   @@@@@r;   r  r    s     ]]F
 /552;;4G6%%112a7H"6%+%A%A$&
":{ &,%H%H":{,# ,#\ Nr=   c	                $   |r| j                   |_         |d   D ]  \  }	}
 |
|      ||	<    |r>|d   D ]5  \  }	}|j                  |	d        |s|j                  j                  |	       7 n+|d   D ]#  \  }	}|s	|j                  j                  |	       % |d   D ]  \  }	} ||||        y ||j                  k7  r<||_        |d   D ]  \  }	}
|	|vs |
|      ||	<    |d   D ]  \  }	} ||||        y |d   D ]  \  }	} ||||        y )NrA  rC  r@  rD  )rs   popexpired_attributesaddr   )r`   rZ   r   r]  r^  r   r`  r"  rj  r   r  set_callable	populators                r;   rR  rR  -  s?    mm%g.KCE#J /%/%9!\		#t$,,005 &:
 &0%9!\,,005 &: )/NCeUC( 0 
eoo	% $ &g.KC%#C[c
 /
 )4NCeUC( 5 )4NCeUC( 5r=   c                   |sK|r|d   D ]  \  }}	||v s |	|      ||<    | j                   |   }
|d   D ]  \  }}||
v s ||||        n|}
|
| j                   |<   |d   D ]  \  }}	||
v s |	|      ||<    |d   D ]:  \  }}||
v s|j                  |d        |s |j                  j                  |       < |d   D ]  \  }}||
v s ||||        |d   D ]  \  }}||vs ||||        |
S )NrA  rD  rC  r@  rE  )rL   r  r  r  )r`   rZ   r   r]  r^  r   rZ  rj  r   r  rd  r  r  s                r;   r[  r[  f  sA    )'2V(?!'E#J  3 ""5)(4NCg~%, 5 ")%g.KCg~#C[c
 / ",H!5Cg~		#t$,,005	 "6
 )/NCg~%, 0 %W-YheUC( . Nr=   c           
         | j                  ||| j                         ||      k7  rIt        j                  dt	        |      d| j                  ||| j                        d ||      d      y )Nz
Instance 'z' has version id 'z3' which does not match database-loaded version id 'z'.)_get_state_attr_by_columnr  r   StaleDataErrorr   )r   r   r]  rZ   r  s        r;   rM  rM    sv    ''uf++	 $$ % 005&"7"7 s

 
	
r=   c	                     ||nj                    S rj                     fd}	t        j                  |	      j	                         fd}
|
S )Nc           	         	 j                   |    }|u ry |j                        syt        |      S # t        $ r t	        d| z        w xY w)NF)r  z*No such polymorphic_identity %r is defined)r  r   r  KeyErrorAssertionError)discriminator
sub_mapperr0  r`   r   r^   r   r   s     r;   configure_subclass_mapperz?_decorate_polymorphic_switch.<locals>.configure_subclass_mapper  s|    	//>J V#^^F+&"(   	 <}L 	s	   ; Ac           
         |       }|T|   }|r ||       S |du r9 |       }|r.t        j                  d|d	dj                  |   d      y  |       S  |       }|rt        j                  d|d	d      y )NFzRow with identity key zG can't be loaded into an object; the polymorphic discriminator column 'z' refers to z-, which is not a sub-mapper of the requested z	' is NULL)r8   r9   r  )
rZ   r  rp  ra  rs  r  instance_fnr   polymorphic_instancesr.  s
       r;   polymorphic_instancez:_decorate_polymorphic_switch.<locals>.polymorphic_instance  s    s$-m<I ~%e#*3/ 44 (*"22=A"	   "3''&s+K00 *>;  r=   )r.  r/  r   PopulateDictrz  )r  r`   r   r   r   r^   r2  r0  rs  r  r  r  r  r.  s   `````` ``  @@@r;   r  r    sx     !,2.. 8 . !--.GH^^N+F$ $L  r=   c                  V    e Zd ZdZdZd Zd Zd Zed        Z	ed        Z
ed        Zy	)
r  z4Track loaders and states for "post load" operations.)loadersr  	load_keysc                R    i | _         t        j                         | _        d | _        y rh   )r  r   OrderedDictr  r  )selfs    r;   __init__zPostLoad.__init__  s     &&(r=   c                "    || j                   |<   y rh   )r  )r  r   	overwrites      r;   rY  zPostLoad.add_state  s    
 'Er=   c                   | j                   sy t        j                  j                  |      }| j                  j                         D ]x  \  }}}}}}| j                   j                         D 	
cg c].  \  }	}
|	j                  j                  j                  |      r|	|
f0 }}	}
|s` ||||| j                  g|i | z | j                   j                          y c c}
}	w rh   )r  r   PathRegistrycoercer  valuesrR   rS  r   r   r  rS   )r  r`   r^   effective_contexttokenlimit_to_mapperrI  argr3  r   r  r  s               r;   rT   zPostLoad.invoke  s    {{))006 \\  "
 )-(9(9(;(;$E9==''++O< 	"(;  
 %tVT^^FIMO # 	s   .3Cc                f    |j                   j                  |j                        }|	|r||_        |S rh   )rQ   r   r^   r  )clsr`   r^   r   pls        r;   r  zPostLoad.for_context  s/    $$((3>o*BL	r=   c                    |j                   |j                  v xr% ||j                  |j                      j                  v S rh   )r^   rQ   r  )r  r`   r^   r   s       r;   r  zPostLoad.path_exists$  s>     II000 Bw..tyy9AAA	
r=   c                    |j                   |j                  v r|j                  |j                      }n#t               x}|j                  |j                   <   ||||||f|j                  |<   y rh   )r^   rQ   r  r  )	r  r`   r^   r  r  loader_callabler  r3  r  s	            r;   r  zPostLoad.callable_for_path+  sg     99///((3B6>j@B((3


5r=   N)__name__
__module____qualname____doc__	__slots__r  rY  rT   classmethodr  r  r  rr  r=   r;   r  r    sU    >0I
'.   
 
 
 
r=   r  c                   |j                   }|s!t        j                  dt        |      z        t	        |t
        j                  z        }|r)|j                  | j                  j                               }| j                  r=| j                  s1| j                  ||      }|t        | |      }t        ||d|||      S t	        |j                        }|r|j                  }	n}| j                   D 
cg c]  }
| j"                  |
   j                   }}
|j$                  j                  |      r!t'        j(                  dt        |      z        | j+                  |      }	t-        j.                  |	      r| j0                  rt-        j2                  |	      r t5        j6                  dt        |             yt        |t9        |       j;                  t<              |	|||      }|r|t        j>                  |      yyc c}
w )z4initiate a column-based attribute refresh operation.zQInstance %s is not bound to a Session; attribute refresh operation cannot proceedN)r   r   r   z_Instance %s cannot be refreshed - it's not  persistent and does not contain a full primary key.zwInstance %s to be refreshed doesn't contain a full primary key - can't be refreshed (and shouldn't be expired, either).)r   r   r   ) r   r   DetachedInstanceErrorr   r~  r   NO_AUTOFLUSHrv  r   r   r   concrete_optimized_get_statementr   r   r   r   _columntopropertyr  r8   r9   r  r   issubsetr  r  r   warn_limitedr"   set_label_styler$   r   )r   r   attribute_namesr   r   r   r   stmthas_keyidentity_keyr  pk_attrsr   s                r;   load_scalar_attributesr  =  s    mmG++9<Ee<LN
 	

 *"9"99:L )66v||7H7H7JKv 33E?K	  !3D  /#)  599oGyy :@9K9K
9K#F$$S)--9K 	 
 ##00:,,.09%0@A 
 66u= 	<(1I1I			l	+2 e		
 	v&&'EF'!F 6>((// "wC
s   2 G?)ra   zCursorResult[Any]r`   r   returnzResult[Any])T)r   z
Query[Any]r   z>Union[FrozenResult, Iterable[Sequence[Any]], Iterable[object]]r   r~  r  z"Union[FrozenResult, Iterable[Any]])
r   r,   r   z
Mapper[_O]r   z_IdentityKeyType[_O]r   r   r  z)Union[LoaderCallableStatus, Optional[_O]])r   r,   r   Union[Select, FromStatement]r   zOptional[_IdentityKeyType]rw   Optional[Sequence[ORMOption]]r   Optional[InstanceState[Any]]r   Optional[ForUpdateArg]r   Optional[Iterable[str]]r   r~  r   Mapping[str, Any]r   r/   r   r~  r   r~  )r   r,   r   r  r  zOptional[Tuple[Any, ...]]rw   r  r   r  r   r   r   r  r   zOptional[Any]r   r~  r   r  r   r/   r   r~  r   r~  )NNNNNN)NNN)NNNN)\r  
__future__r   typingr   r   r   r   r   r	   r
   r   r   r   r    r   r   r   r   baser   r   r   r   r`   r   r   r   r   r   r   r8   enginer   engine.resultr   r    r!   sqlr"   r   sql.selectabler#   r$   r%   r&   _typingr'   r(   
interfacesr)   r   r*   r   r+   r   r,   r   r-   engine.cursorr.   engine.interfacesr/   r0   r1   r2   objectr4   counterrr   str_PopulatorDictr   preload_moduler   became_legacy_20r   r   r   r   r  r7  r9  r  r  rR  r[  rM  r  r  r  rr  r=   r;   <module>r     s   #                " " '  " $ !     ! 1 ( 0  " ) ; ( )*% $,3&TT T\\^
 c4c3h001Zz -.8& /8&v F
 -. Q&Q&LQ& Q& (	Q& /Q&h### 
# 	#
 /#V 3726.2/3(,)-!!$$+$ 
$$
 0$ 0$ ,$ -$ $ &$ '$ $ $X 3726.2/3$((,)-!!uu+u 4u
 0u 0u ,u -u "u u &u 'u u uv !R "4%n	& "zzBJ3)r"J
"V rE
 E
PU0r=   