
    +h                       d dl 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	jR                  Z* e
jV                         Z, e
jZ                  d$      Z. G d% d&e/      Z0 e
jV                  d'd'd(      Z1 G d) d*e      Z2ej
                  j>                  jg                  d+d,       G d- d.e2             Z4 G d/ d0e      Z5ej
                  j>                  jg                  d+d1       G d2 d3e2e%             Z6	 dFd4Z7d5 Z8d6 Z9d7 Z: G d8 d9e/      Z; G d: d;e;      Z< G d< d=e;      Z= G d> d?e;      Z> G d@ dAe>      Z? G dB dCe>      Z@ G dD dEe@      ZAy)G    N   )
attributes)
interfaces)loading)_is_aliased_class)ORMColumnsClauseRole)PathRegistry)_entity_corresponds_to)_ORMJoin)aliased)Bundle)
ORMAdapter   )exc)future)inspect)sql)util)ClauseElement)	coercions)
expression)roles)visitors)_entity_namespace_key)_select_iterables)CacheableOptions)CompileState)Options)LABEL_STYLE_DISAMBIGUATE_ONLY)LABEL_STYLE_NONE)LABEL_STYLE_TABLENAME_PLUS_COL)SelectState)ExtendedInternalTraversal)InternalTraversalLABEL_STYLE_LEGACY_ORMc                   2    e Zd ZdZ G d de      Z	 	 ddZy)QueryContext)compile_statequeryparamsload_optionsbind_argumentsexecution_optionssession	autoflushpopulate_existinginvoke_all_eagersversion_checkrefresh_statecreate_eager_joinspropagated_loader_optionsr   runidpartialspost_load_pathsidentity_token	yield_perloaders_require_bufferingloaders_require_uniquingc                   4    e Zd ZdZdZdZdZdZdZdZ	dZ
dZdZy)!QueryContext.default_load_optionsFTN)__name__
__module____qualname___only_return_tuples_populate_existing_version_check_invoke_all_eagers
_autoflush_refresh_identity_token
_yield_per_refresh_state_lazy_loaded_from_legacy_uniquing     I/var/www/html/venv/lib/python3.12/site-packages/sqlalchemy/orm/context.pydefault_load_optionsr>   M   s:    #"!
"&
  rM   rO   Nc                     | _         |xs t         _        |xs t         _        | _        | _        | _        d _        d _        | _	        |j                  j                  }|j                  }	t         fdt        ||	      D               _        t        |j                          _        |j"                   _        |j&                   _        |j*                   _        |j.                   _        |j2                   _        |j6                   _        |j:                   _         j8                  r>|j>                  r1tA        jB                  ddjE                  |j>                        z        y y )NFc              3   `   K   | ]%  \  }}|j                   r|j                  |       ' y wN)propagate_to_loaders'_adapt_cached_option_to_uncached_option).0optuncached_optselfs      rN   	<genexpr>z(QueryContext.__init__.<locals>.<genexpr>{   s3      /
!\'' 77lK/
s   +.zThe yield_per Query option is currently not compatible with %s eager loading.  Please specify lazyload('*') or query.enable_eagerloads(False) in order to proceed with query.yield_per().z, )#r+   _EMPTY_DICTr-   r,   r(   r)   r.   r;   r<   r*   select_statement_with_optionstuplezipr5   dictr   rF   r/   rC   r0   rE   r1   rD   r2   rI   r3   rH   r:   rG   r9   _no_yield_perssa_excInvalidRequestErrorjoin)
rX   r(   	statementr*   r.   r+   r-   r,   cached_optionsuncached_optionss
   `         rN   __init__zQueryContext.__init__Y   sP    )!2!Ak,;*
).&(-%&77EE$22 */ /
%(9I%J/
 *
& }778%00!-!@!@!-!@!@)88)88%00*BB>>m::,,2
 ))M889:  ;>rM   )NN)r?   r@   rA   	__slots__r   rO   rg   rL   rM   rN   r'   r'   3   s"    I2
!w 
!& :rM   r'   T) _result_disable_adapt_to_contextfuture_resultc                       e Zd ZeZ G d de      ZeZd Z	d Z
ed        Zed        Zed        Zed        Zed	        Zed
        Zd Zd Zed        Zy)ORMCompileStatec                      e Zd Zdej                  fdej                  fdej                  fdej                  fdej                  fdej                  fdej                  fdej                  fd	ej                  fd
ej                  fdej                  fdej                  fdej                  fgZ	dZ
dZdZdZeZdZdZdZdZdZdZdZdZy)'ORMCompileState.default_compile_options_use_legacy_query_style_for_statement_bake_ok_with_polymorphic_adapt_map_current_path_enable_single_crit_enable_eagerloads_orm_only_from_obj_alias_only_load_props_set_base_alias_for_refresh_state_render_for_subquery_is_starFTrL   N)r?   r@   rA   r$   
dp_booleanr#   dp_has_cache_key_tuplesdp_has_cache_keydp_plain_obj_cache_key_traversalro   rp   rq   rr   _path_registryrs   rt   ru   rv   rw   rx   ry   rz   r{   rL   rM   rN   default_compile_optionsrn      s   &(9(D(DE0;;<*556-)AA /@@A"$5$@$@A!#4#?#?@'):)E)EF!2!?!?@ 1 < <=!#4#?#?@#%6%A%AB*556! 
0 #(
 &(#&"!#' "$rM   r   c                     t               rR   NotImplementedError)rX   argkws      rN   rg   zORMCompileState.__init__       !##rM   c                 j    | j                   }||vr#|j                  |       |j                  |       y y rR   )dedupe_columnsaddappend)rX   objcol_collectiondedupes       rN   _append_dedupe_col_collectionz-ORMCompileState._append_dedupe_col_collection   s3    $$fJJsO!!#& rM   c                 <    |rdd}|S t        j                  |      S )Nc                 "    |r|S t        | d      S )Nkey)getattr)colcol_names     rN   namez7ORMCompileState._column_naming_convention.<locals>.name   s    #O"3..rM   rR   )r"   _column_naming_convention)clslabel_stylelegacyr   s       rN   r   z)ORMCompileState._column_naming_convention   s$     / K88EErM   c                     t               )a  Create a context for a statement given a :class:`.Compiler`.

        This method is always invoked in the context of SQLCompiler.process().

        For a Select object, this would be invoked from
        SQLCompiler.visit_select(). For the special FromStatement object used
        by Query to indicate "Query.from_statement()", this is called by
        FromStatement._compiler_dispatch() that would be called by
        SQLCompiler.process().

        r   )r   statement_containercompilerr   s       rN   create_for_statementz$ORMCompileState.create_for_statement   s     "##rM   c                     t        |      S rR   )_column_descriptionsr   rd   s     rN   get_column_descriptionsz'ORMCompileState.get_column_descriptions   s    #I..rM   c                    |r||fS t         j                  j                  dh d||j                        \  }}|st        }n|j                  t              }|j                  r|j                  d|j                  i      }||d<   	 |j                  d   }|r|j                  |d<   	 |j                  r|j                          ||fS # t        $ r J d       w xY w)N_sa_orm_load_options>   r/   r:   r0   r:   clauseplugin_subjectmapperz0statement had 'orm' plugin but no plugin_subject)r'   rO   from_execution_options_execution_options_orm_load_exec_optionsunionrH   _propagate_attrsr   KeyErrorrF   )	r   r.   rd   r*   r-   r,   is_reentrant_invoker+   r   s	            rN   orm_pre_session_execz$ORMCompileState.orm_pre_session_exec   s    ///
 --DD";((	
	
$ ! 6 1 7 78N O"" 1 7 7l556! $-x 	A&778HIN +9+@+@x("" +++  	MLLL5	Ms   C Cc           	         |j                   }|j                  j                  }|j                  dt        j
                        }	|j                  j                  r|S t	        |||||	||      }
t        j                  ||
      S )Nr   )
contextcompiledr(   getr'   rO   compile_optionsr{   r   	instances)r   r.   rd   r*   r-   r,   resultexecution_contextr(   r+   querycontexts              rN   orm_setup_cursor_resultz'ORMCompileState.orm_setup_cursor_result<  s     #NN)22@@ ),,"L$E$E
 ((11M#
   66rM   c                 b    | j                   D cg c]  }t        |t              s| c}S c c}w )zreturn all _MapperEntity objects in the lead entities collection.

        Does **not** include entities that have been replaced by
        with_entities(), with_only_columns()

        )	_entities
isinstance_MapperEntity)rX   ents     rN   _lead_mapper_entitiesz%ORMCompileState._lead_mapper_entities_  s.      >>
Z]-KC
 	
 
s   ,,c                    |j                   ss|j                  j                  | j                  vrP|j                  j	                         D ]2  }| j                  |t        j                  ||j                               4 yyy)zagiven MapperEntity or ORMColumnEntity, setup polymorphic loading
        if appropriate

        N)	is_aliased_classr   persist_selectable_polymorphic_adaptersiterate_to_root"_mapper_loads_polymorphically_withsql_utilColumnAdapter_equivalent_columns)rX   ext_info
selectablemps       rN    _create_with_polymorphic_adapterz0ORMCompileState._create_with_polymorphic_adapterk  sw     ))22--. oo557 77**:r7M7MN. *rM   c                     |j                   xs |gD ]?  }|| j                  |<   |j                         D ]  }|| j                  |j                  <    A y rR   )_with_polymorphic_mappersr   r   local_table)rX   r   adapterm2ms        rN   r   z2ORMCompileState._mapper_loads_polymorphically_with{  s^    22>vh 	DB-4D&&r*'') D<C**1==9D	DrM   c                     t        d      )Nz0this method only works for ORMSelectCompileStater   )r   r)   r   s      rN   _create_entities_collectionz+ORMCompileState._create_entities_collection  s    !>
 	
rM   N)r?   r@   rA   rZ   rr   r   r   r   current_pathrg   r   classmethodr   r   r   r   r   propertyr   r   r   r   rL   rM   rN   rl   rl      s     #.*"2 *X "L$' F F $ $ / / @, @,D  7  7D 	
 	
 D 
 
rM   rl   ormorm_from_statementc                   j    e Zd Z ej                         ZdZdZdZdZ	dZ
dZeZeZed        Zd Zd Zy)ORMFromStatementCompileStateNFc                 z   ||j                    }nd}| j                  |       }d |_        |j                  j                  |_        |x|_        |_        |j                  x|_	        }|j                  r|j                  |_        g |_        i |_        t               |_        |j                  |_        |j
                  r_t%        |t&        j(                        rE|j*                  s9|j                  s-|j,                  t.        u r|j1                  t2              |_        n||_        |j7                  |j*                  s|j                  s|j,                  nt.        |j
                        |_        t:        j=                  ||j>                  |j                  d       |j                  j@                  |_!        |r^|jD                  rRdt               i|_#        |jH                  |_%        |jD                  D ]   }|jL                  s|jO                  |       " ni |_#        |jH                  |_%        |jP                  r|jP                  D ]  \  }}	 ||        g |_)        g |_*        t               |_+        g |_,        g |_-        d |_.        t%        |j4                  t&        j^                  t&        j`                  f      rPi |_1        |j                  D ]  }
|
je                  |        tg        |j4                  |jR                        |_        |S th        jj                  jm                  |j4                  d      |_7        |S )NTis_current_entities_unbound_load_dedupes)adapt_on_names)8stack__new___primary_entity_compile_optionsro   use_legacy_query_styler   r[   elementrequested_statementis_dmltable	dml_tabler   r   setr`   r   r   r   
SelectBase_is_textual_label_styler    set_label_styler!   rd   r   _label_convention_QueryEntityto_compile_state_raw_columnsrs   r   r\   r   _global_attributesglobal_attributes_is_compile_stateprocess_compile_state_with_context_optionsprimary_columnssecondary_columnsr   r4   _fallback_from_clausesorder_by
TextClause
UpdateBaseextra_criteria_entitiessetup_compile_state_AdHocColumnsStatementr   r   r   _from_obj_alias)r   r   r   r   toplevelrX   rd   rV   fnr   entitys              rN   r   z1ORMFromStatementCompileState.create_for_statement  s    #>>)HH{{3#  00HH 	# <ON 4#8/B/J/JJ 9&__DN%'"!e2CC ''9j&;&;<))$$&&*::&66.DN 'DN!%!?!?((1A1A ""!''	"
 	%%,,NN $	 	& 	
 0@@NN+996>DO%-%@%@D"*88 4((--d34
 !DO%-%@%@D"44.DD C4  "!#!e"$&(#NNZ22J4I4IJ
 ,.D(
 .. 1**401 4 4 4DN&  $'88#9#9t $: $D  rM   c                     |S rR   rL   )rX   colscurrent_adapters      rN   _adapt_col_listz,ORMFromStatementCompileState._adapt_col_list  s    rM   c                      y rR   rL   rX   s    rN   _get_current_adapterz1ORMFromStatementCompileState._get_current_adapter
  s    rM   )r?   r@   rA   r   immutabledict_aliased_generationsr  _has_mapper_entities_has_orm_entitiesmulti_row_eager_loaderseager_adding_joinscompound_eager_adapterrZ   r   eager_joinsr   r   r
  r  rL   rM   rN   r   r     s`    -4--/O #!)Kn n`rM   r   c                   0    e Zd ZdZdZd Zd Zd Z	 ddZy)r  ztinternal object created to somewhat act like a SELECT when we
    are selecting columns from a DML RETURNING.


    Nc                     || _         |D cg c]&  }t        j                  t        j                  |      ( c}| _        y c c}w rR   )r   r   expectr   ColumnsClauseRolecolumn_args)rX   textcolumnscs       rN   rg   z_AdHocColumnsStatement.__init__  s9    BI
=>IU44a8
 
s   +A c                     t               rR   r   r  s    rN   _generate_cache_keyz*_AdHocColumnsStatement._generate_cache_key  r   rM   c                     t               rR   r   )rX   anon_map
bindparamss      rN   _gen_cache_keyz%_AdHocColumnsStatement._gen_cache_key   r   rM   c                 ~   |j                    }|r|j                  n|j                   d   }|xs- |dk(  xr |j                  dd      xs |j                  dd      }|rDdx|_        |_        d|_        | j                  D ]   }|j                  |d|j                         "  |j                  | j                  fi |S )z*provide a fixed _compiler_dispatch method.r   need_result_map_for_compoundFneed_result_map_for_nestedT)within_columns_clauseadd_to_result_map)
r   _default_stack_entryr   _ordered_columns_textual_ordered_columns_loose_column_name_matchingr  process_add_to_result_mapr   )	rX   r   compound_indexasfromr   r  entrypopulate_result_mapr  s	            rN   _compiler_dispatchz)_AdHocColumnsStatement._compiler_dispatch#  s    
  ~~%-5H))8>>";M 	
  > !# EII<eD	> yy5u= 	  H%1
 48H0%%   *.&.&A&A !   x333rM   NF)	r?   r@   rA   __doc____visit_name__rg   r  r#  r4  rL   rM   rN   r  r    s'     N
$$ 5:#4rM   r  selectc                      e Zd ZexZZeZdZdZdZ	dZ
dZdZdZdZdZdZed        Zd Zed        Zed        Zed        Zed	        Ze ej2                  d
      d               Zd Zd Zd Zd Zd Zd Z d(dZ!d Z"d Z#d Z$d Z%d Z&d Z'd Z(d Z)d Z*d Z+d Z,d Z-d Z.d  Z/d! Z0d" Z1d# Z2e3d$        Z4e3d%        Z5d& Z6d' Z7y))ORMSelectCompileStateNFrL   c                    | j                  |       }||j                   }|j                  |_        n	d}i |_        |}| j                  j                  |j                        |_        |j                  r9|j                         |_	        t        j                         |j                  _        n||_	        |j                  j                  |_        |j                  j                  |_        g |_        d|_        i |_        i |_        t)               |_        |j                  j,                  r4t/        |j                  j,                        |_        |j1                          |j                  |_        |s|xj2                  dddz  c_        |j                  r@|j                  j4                  t6        u r$|j                  st8        |_        n't<        |_        n|j                  j4                  |_        |j>                  rA|j>                  D ci c]&  }|t@        jC                  ||jD                  g d      ( c}|_#        |jI                  |j4                  |j                        |_%        t@        jC                  ||jD                  |j                   d       |j                  jL                  |_'        d|_(        |r|jR                  s|j>                  rdt)               i|_*        |j>                  D ]]  }|jR                  D ]L  }|jV                  s|jY                  ||jF                  |   D 	cg c]  }	t[        |	t\              r|	 c}	       N _ |j                  jR                  D ]   }|jV                  s|j_                  |       " ni |_*        |j`                  r|j`                  D ]  \  }
} |
|        g |_1        g |_2        t)               |_3        i |_4        i |_5        g |_6        g |_7        |jq                  d |jr                  D              |_:        |jw                          ty        jz                  ||j|                  |fi | |S c c}w c c}	w )	z@compiler hook, we arrive here from compiler.visit_select() only.NTF)ru   rz   r   rL   r   c              3   4   K   | ]  }|j                     y wrR   )r   )rU   infos     rN   rY   z=ORMSelectCompileState.create_for_statement.<locals>.<genexpr>  s      2
 $DOO2
s   )?r   r   r   r   r   
safe_merger   r   _cloner[   r   r  rp   for_statementro   r   r   r   r  r   r   r`   rr   r_   _setup_with_polymorphicsr   r   r%   r!   r   r   _memoized_select_entitiesr   r   r   _memoized_entitiesr   r   rs   r   eager_order_byr\   r   r   'process_compile_state_replaced_entitiesr   r   r   r   r   r   r   r  r   r4   r   _normalize_froms	_from_objfrom_clauses_setup_for_generater"   rg   rd   )r   rd   r   r   rX   r  r[   memoized_entitiesrV   r   r  r   s               rN   r   z*ORMSelectCompileState.create_for_statement\  s    {{3#>>)H%-%@%@D"H%'D"$ &)%@%@%K%K&&&
	" .. %5$;$;$=D!7;7I7I7KD!!4$4D! .>>MM --EE 	# #$&!%'"!e ,,HH/3 11MM0D, ))+/@@   &+(,%   ''%%226LL%%#A #@ #44AAD55 %>>
' & "<#@#@%22(-	 $A $ 
'D#  "&!?!?""D$?$?"
 	%%))NN $	 	& 	
 -==KK **996>DO ";;!,:: C,,CC  ,0+B+B$5,"$' $.c=#A	 !$	  ,,:: 4((--d34 !DO11+AA C4  "!#!e')$"$&(# !11 2
(8(B(B2
 
 	  "T4>>8BrBq
'Xs   +Q+Qc                 V
   | j                   }d | _        d| _        | j                  j                  r| j                          |j                  D ]m  }|j                  r)| j                  |j                  | j                  |          |j                  sE| j                  |j                  | j                  |          o |j                  r&| j                  |j                  | j                         |j                  r&| j                  |j                  | j                         | j                         |j                  r6|j                  | _        r#t        fd| j                  D              | _        r*|j                   dvr| j#                  |j                         n|j                   | _        |j&                  r#t        fd|j&                  D              | _        r=|j(                  dvr/| j#                  t+        j,                  |j(                              n|j(                  xs d | _        | j0                  r9| j2                  d   j4                  }|j7                  | j0                        | _        |j8                  r"| j#                  |j8                        | _        nd| _        |j<                  | _        |j@                  r5t        t+        j,                  d |j@                  D                    | _!        nT|jD                  5t        t+        j,                  d |jD                  D                    | _#        n|jH                  sd| _!        |jJ                  | _%        | j                  jL                  r-tO        | j                        d	k7  rtQ        jR                  d
      | j                  D ]  }|jU                  |         | jV                  D ]  }|d   } || g|d	d     | j                  jX                  r| j[                          | j\                  s@| j                  j^                  rtQ        j`                  d      tQ        j`                  d      | j2                  stc        | jd                        | _        | j$                  du rd | _        | jf                  r.| jh                  r"| jj                  r| jm                         | _        n| jo                         | _        | jp                  r6| js                         }|#| j                  ju                  d|i      | _        y y y )NrL   c              3   0   K   | ]  } |d         ywTNrL   rU   critr	  s     rN   rY   z<ORMSelectCompileState._setup_for_generate.<locals>.<genexpr>$  s       - $D$/-s   r5  c              3   8   K   | ]  }r	 |d       n|  ywrM  rL   rN  s     rN   rY   z<ORMSelectCompileState._setup_for_generate.<locals>.<genexpr>3  s'      * 0?d+DH*s   r   c              3   N   K   | ]  }|t        j                  |      nd   y wrR   r   surface_selectablesrU   ss     rN   rY   z<ORMSelectCompileState._setup_for_generate.<locals>.<genexpr>R  ,      & 89}H003$N&   #%c              3   N   K   | ]  }|t        j                  |      nd   y wrR   rR  rT  s     rN   rY   z<ORMSelectCompileState._setup_for_generate.<locals>.<genexpr>Y  rV  rW  rR   r   zsCan't generate ORM query that includes multiple expressions at the same time as '*'; query for '*' alone if presentzyNo column-based properties specified for refresh operation. Use session.expire() to reload collections and related items.z4Query contains no columns with which to SELECT from.F
deepentity);r[   rd   _join_entitiesr   rx   _set_select_from_aliasrB  _setup_joins_joinrC  _legacy_setup_joins_legacy_joinr   r  _where_criteriar]   _order_by_clausesr
  r   _having_criteria_group_by_clausesr   flatten_iteratorgroup_byrD  rH  _target_adaptercopy_and_process_distinct_ondistinct_on	_distinctdistinct
_correlate	correlate_correlate_exceptcorrelate_except_auto_correlate_for_update_argr{   lenra   CompileErrorr  r4   rt   _adjust_for_extra_criteriar   rw   rb   listr   r  r  _should_nest_selectable_compound_eager_statement_simple_statementr@  _mapper_zero	_annotate)	rX   r)   rJ  r   r  recstrategyezeror	  s	           @rN   rI  z)ORMSelectCompileState._setup_for_generate  s   %% //'')!&!@!@ 
	 --

%22++,=> !44!!%99++,=>
	 JJu))4>>:$$e77H335  #(#8#8D ', - $ 4 4- ($ 5#:#:-#O   !8!8/J(( 	 !!$) *!22* %D! 5#:#:-#O   %%e&=&=> ((0D 	 ''*::G")":":4;N;N"OD#33""O D  "D #%% &"--& DN $$0$)%% &"44& %D! &&$DN  %44((c$...AQ.F%%J  nn 	-F&&t,	- ** 	%C1vHT$CG$	% 33++-####4400?  00J     $T%@%@ AD==E! DM (('',,!;;=DN!335DN%%'E  "&!9!9!5)" ! rM   c                    | j                  |       }g |_        d|_        i |_        i |_        | j
                  j                  |j                        }|j                  r*t        |j                        |_        |j                          |j                  |j                  |      |_        t        j                  ||j                   |j                  d       |S )a  Creates a partial ORMSelectCompileState that includes
        the full collection of _MapperEntity and other _QueryEntity objects.

        Supports a few remaining use cases that are pre-compilation
        but still need to gather some of the column  / adaption information.

        NTr   )r   r   r   r  r   r   r>  r   rr   r_   rA  r   r   r   r   r   r   )r   r)   r   rX   r   s        rN   r   z1ORMSelectCompileState._create_entities_collection  s     {{3#$&!%'"55@@""
 66/3;;0D, ))+!%!?!?"
 	%%%$$dnn$ 	& 	
 rM   c                     |j                   }|sy |d   \  }}}}t        |t        j                        r|j                  S |S )Nr%  )r\  r   r   PropComparatorr  )r   rd   setup_joinstargetonclausefrom_flagss          rN   determine_last_joined_entityz2ORMSelectCompileState.determine_last_joined_entity  sD    ,,+6r?(5%fj778== MrM   c              #   D  K   |j                   D ]  }|j                  rjd|j                  v r\|j                  d   }|j                  s"|j                  st        |g      D ]  }|  Zt        |j                        D ]  }|  yt        |g      D ]  }|   y w)Nentity_namespace)r   is_selectable_annotations	is_mapperr   r   _all_column_expressions)r   rd   r   enselems        rN   all_selected_columnsz*ORMSelectCompileState.all_selected_columns  s      -- 	G%%&'*>*>>**+=>}}S-A-A 17) < #"
# !2#2M2M N #"
# .wi8 DJ	s   BB c                     | j                  t        j                  j                  d |j                  D                    S )Nc              3      K   | ]<  }d |j                   vr|j                  n|j                   d    j                         g > yw)parententityN)r  _from_objects__clause_element__)rU   r   s     rN   rY   zAORMSelectCompileState.get_columns_clause_froms.<locals>.<genexpr>  sS      * 	 ")=)== %% ((8KKM*s   AA)rF  	itertoolschainfrom_iterabler   r   s     rN   get_columns_clause_fromsz.ORMSelectCompileState.get_columns_clause_froms  s<    ##OO)) *  )55* 	
 		
rM   zsqlalchemy.orm.queryc                 L   t         j                  j                  }t        j                  t
        j                  ||      }|j                  |j                  |      }|j                  j                  |j                  |j                  |j                  |j                         |S )N)apply_propagate_attrs)r\   r   r   r   )r   	preloaded	orm_queryr   r  r   ReturnsRowsRoleFromStatementr   __dict__updater\   r   r   r   )r   rd   from_statementr)   stmts        rN   r  z$ORMSelectCompileState.from_statement  s     (("))!!"+
 ""9#9#9>J#11"+"A"A(;;&77	 	 	
 rM   c                     | j                   j                         D ]!  \  }}| j                  ||j                         # y rR   )rr   itemsr   _adapter)rX   r   wps      rN   rA  z.ORMSelectCompileState._setup_with_polymorphics  s:     <<BBD 	KLHb33HbkkJ	KrM   c                     | j                   }| j                  j                  sJ t        |j                        dk(  sJ | j                  |j                  d         }|r| xj                  ddiz  c_        || _        y y )Nr   r   rt   F)r[   r   rx   rr  rG  _get_select_from_alias_from_objr  )rX   r)   r   s      rN   r[  z,ORMSelectCompileState._set_select_from_alias  sy    %%##33335??#q(((66uq7IJ  %:E$BB #*D  rM   c                 \   |}d|j                   v r|j                   d   }t        |d      r-|j                  st        j                  d      |j
                  S t        |j                  t        j                  j                        r&| j                         }t        j                  ||      S y )Nr  r   zPA selectable (FromClause) instance is expected when the base alias is being set.)r  hasattrr   ra   ArgumentErrorr  r   r   r   AliasedReturnsRows_all_equivsr   r   )rX   from_objr=  equivss       rN   r  z5ORMSelectCompileState._get_select_from_alias_from_obj  s    T...$$^4D4"((**A 
 }}$)J)JK%%'F))$77rM   c                 4    | j                   d   j                  S )z8return the Mapper associated with the first QueryEntity.r   )r   r   r  s    rN   ry  z"ORMSelectCompileState._mapper_zero(  s    ~~a '''rM   c                     | j                   D ]!  }d|j                  v s|j                  d   c S  | j                  D ]  }|j                  s|j                  c S  y)zReturn the 'entity' (mapper or AliasedClass) associated
        with the first QueryEntity, or alternatively the 'select from'
        entity if specified.r  N)rH  r  r   entity_zero)rX   r   qents      rN   _entity_zeroz"ORMSelectCompileState._entity_zero,  sg    
 $$ 	8C!1!11''77	8 NN 	(D'''	( rM   c                     | j                   | j                  gk7  rt        j                  d|z        | j                  j                  S )Nz4%s() can only be used against a single mapped class.)r   r   ra   rb   r  )rX   methnames     rN   _only_full_mapper_zeroz,ORMSelectCompileState._only_full_mapper_zero:  sK    >>d2233,,)+34  ##///rM   c                     t        | j                        dkD  rt        j                  |xs d      | j	                         S )Nr   z8This operation requires a Query against a single mapper.)rr  r   ra   rb   r  )rX   	rationales     rN   _only_entity_zeroz'ORMSelectCompileState._only_entity_zeroB  sD    t~~",, ++ 
   ""rM   c                    i }| j                   j                         D ]L  }|D cg c]  }t        |t              r| c}D ]'  }|j	                  |j
                  j                         ) N | j                  D cg c]  }t        |t              s| c}D ]'  }|j	                  |j
                  j                         ) |S c c}w c c}w rR   )rC  valuesr   r   r  r   r   r   )rX   r  rJ  r   s       rN   r  z!ORMSelectCompileState._all_equivsK  s    !%!8!8!?!?!A 	> -c=1  >
 cjj<<=>	>  >>
Z]-KC
 	:C MM#**889	: 
s   C:CCc           	      Z   | j                   rj| j                   D cg c]4  }t        |t        j                  j                        r|j
                  n|6 }}t        j                  | j                  |      }ng }d } | j                  | j                  |D cg c]  }|| j                  vs| c}z   | j                  | j                  | j                  | j                  | j                   f| j                  | j                   j"                  | j                   j$                  | j&                  | j(                  d| j*                  }|j-                         }| j/                         }t        j0                  ||      | _        t5        j6                  |g| j8                  z    }| j                  |_        | j                  '| j                  j<                  | j                  |_        |}| j>                  jA                         D ]#  }	t        jB                  ||	|	jD                        }% |jF                  jI                  ||       |r6 |j                   jH                  |g| j2                  jK                  |         |j                   jH                  |g| jL                    |S c c}w c c}w )N
for_updatehintsstatement_hintsrm  ro  )'r   r   r   elements_label_referencer   r    expand_column_list_from_order_byr   _select_statementr   rH  r`  rb  r   rq  r[   _hints_statement_hintsrm  ro  _select_argsaliasr  r   r  r   r8  r   r   ofr  r  splice_joinsstop_onselect_fromnon_generativerg  rD  )
rX   r  unwrapped_order_byorder_by_col_exprr  innerr  rd   from_clause
eager_joins
             rN   rw  z/ORMSelectCompileState._compound_eager_statement\  s   
 == !MM	"  dCLL$A$AB " " !) I I$$&8! !#!% '&&  +LQq8K8K/KqLM  !!MM
 ++''.. 11BBnn!22
 
  !!#&.&<&<UF&K#MMg...
	 "&!1!1	   ,$$''/(,(<(<I%**113 	J #//Z););K		 	,,YD-I--,,==& 	*	)))Jd6I6IJS"$ Ms   9J#J(
*J(
c           	      T   | j                   j                  rz| j                  rn| j                  sb| j                  rVt        j                  | j                  | j                        }|rt        j                  d       | xj                  |z  c_         | j                  | j                  | j                  z   t        | j                        t        | j                  j                               z   | j                   | j"                  | j$                  | j                  f| j&                  | j(                  j*                  | j(                  j,                  | j.                  | j0                  d| j2                  }| j4                  r' |j                  j6                  |g| j4                    |S )NzORDER BY columns added implicitly due to DISTINCT is deprecated and will be removed in SQLAlchemy 2.0.  SELECT statements with DISTINCT should be written to explicitly include the appropriate columns in the columns clauser  )r   ro   rk  ri  r   r   r  r   r   warn_deprecated_20r  r   r]   rH  r  r  r`  rb  r   rq  r[   r  r  rm  ro  r  rD  r  )rX   to_addrd   s      rN   rx  z'ORMSelectCompileState._simple_statement  sb      88t'7'7>>$$dmmF ''4   F* *D**  4#9#99$##$uT-=-=-D-D-F'GG  !!MM
 ++''.. 11BBnn!22
 
	 -I--iN$:M:MNrM   c                 v   t         j                  }|j                  |||      }|r||_        |r||_        |r|xj
                  t        |      z  c_        |r |j                  j                  |g|  n|r|j                  j                  |       |r|xj                  t        |      z  c_	        ||_
        ||_        ||_        ||_        |r||_        |r||_        ||_        |r||_        |	r|	|_        |
r |j&                  j                  |g|
  | |j(                  j                  |g|  |S )N)r   rG  r   )r   Select_create_raw_selectr`  rb  ra  r]   rk  r  rc  _limit_clause_offset_clause_fetch_clause_fetch_clause_options	_prefixes	_suffixesrq  r  r  rm  ro  )rX   raw_columnsr  where_criteriahaving_criteriar   r   r  r  r  rm  ro  limit_clauseoffset_clausefetch_clausefetch_clause_optionsrk  ri  prefixessuffixesre  r  rd   s                          rN   r  z'ORMSelectCompileState._select_statement  sI   0 --$$ . 
	 (6I%)8I&''5?:'-I--iF+F--i8''5?:'".	#0	 ".	*>	'"*I"*I$.	!$I)8I&.I..yE9E'5I&&55, rM   c                 h   d|j                   v r>|j                   d   }| j                  j                  |d       }|r|j                  |      S t	        |t
        j                        r|}nt        |d      r|j                  }ny | j                  j                  |d       }|r|j                  |      S y )Nr  r   )	r  r   r   adapt_clauser   r   
FromClauser  r   )rX   r   searchr  s       rN   _adapt_polymorphic_elementz0ORMSelectCompileState._adapt_polymorphic_element%  s    W111)).9F..2264@E))'22gz445FWg&]]F**..vt<%%g.. rM   c                     d|j                   v rE| j                  j                  |j                   d   d      D ]  }|j                  |      }||c S  y )Naliased_generationrL   )r  r  r   replace)rX   r   r   replaced_elems       rN   _adapt_aliased_generationz/ORMSelectCompileState._adapt_aliased_generation7  sd      7#7#774488$$%9:B ) !( 8 ,(() rM   c                 B    |r|D cg c]  } ||d       c}S |S c c}w )NTrL   )rX   r  r	  os       rN   r
  z%ORMSelectCompileState._adapt_col_listD  s'    6:;OAt,;;K <s   c                 V   g | j                   r?j                  | j                  j                  rdnd| j                   j                  f       | j
                  rj                  d| j                  f       | j                  rj                  d| j                  f       sy fd}|S )NFTc                 <    fd}t        j                  | i |      S )Nc                 |    d| j                   v xs d| j                   v }D ]  \  }}|s|s ||       }||c S  y )N
_orm_adaptr  )r  )r  is_orm_adaptalways_adaptr   eadapterss        rN   r  zRORMSelectCompileState._get_current_adapter.<locals>._adapt_clause.<locals>.replacet  sW     D$5$55 ;%):)::  .6 %)L'#|#DM=#$H	%rM   )r   replacement_traverse)r   	as_filterr  r  s      rN   _adapt_clausezAORMSelectCompileState._get_current_adapter.<locals>._adapt_clausep  s    	% 00WEErM   )	r  r   r   rv   r  r  r  r   r  )rX   r  r  s     @rN   r  z*ORMSelectCompileState._get_current_adapterJ  s     OO ++DD ((00	 $$OOUD$B$BCD %%OOUD$C$CDE	F" rM   c                    |D ]  \  }}}}|d   }|d   }| j                          t        |      }|t        |      }|t        |t        j                        r|}d }nd|j
                  v r|j
                  d   }|-|j                  s!t        |d      st        j                  d      d }	t        |t        j                        r#t        |dd       }	||	r|	}n|j                  }	 |j                  }|j                   }| j"                  j%                  |d       }t        |t&              r=|j(                  j+                  |      r"|j,                  }t        ||j.                        }|j                  }t        |t0        j2                        s|}|b||ur^|j
                  j%                  dd       |ur@t        j4                  d	|j
                  j%                  d|      d
|      |d }|}nd x}}| j7                  |||||dd||	        y # t        $ r8}
t        j                  t        j                  d|z        |
       Y d }
~
Zd }
~
ww xY w)Nisouterfullr  r   9Expected mapped entity or selectable/table as join target_of_type0Join target %s does not refer to a mapped entityreplace_contextzexplicit from clause z4 does not match left side of relationship attribute F)_reset_joinpointr   r   r   r  r  r  r  ra   r  r   r   r  AttributeErrorr   raise__parententityr   r   r   r   isaaliased_classr   r   QueryableAttributerb   _join_left_to_right)rX   argsentities_collectionrightr  r  r  r  r  of_typeerrleftr  props                 rN   r]  zORMSelectCompileState._join  s   /3 q	+UHeUI&G=D!!#ENE#"8,Jz00%  !5#5#55**>:**75(3K ..: 
 G(J$=$=> "(J== ' ( 1 1	$)LLE  --2266tTB eZ0U\\5E5Ed5K ..D&tX\\:H((!(J,I,IJ#H
 $T)!..22>4H#$ %88 !& 2 2 6 6~u M (	  " #"t $$#
Oq	l  .  KK & 4 4%46;%<!" 14 s   )H	I-IIc                 t   |D ]n  \  }}}}|d   }|d   }|d   }	|d   }
|d   }|%t        |t        j                        st        |      }|%t        |t        j                        st        |      }|	s| j	                          nL| j
                  j                  dd      }|s|}n+|r)| j                  j                  |d      | j                  |<   t        |t        j                  t        j                  f      r||}d}nd|j                  v r|j                  d   }|-|j                  s!t        |d	      st        j                  d
      t        |t        j                        rt        |dd      }nd}t        |t        j                        r$t!        t        | j#                               |      }nf|	rdt        |t        j                        rJ| j#                         }t        |      }t        |d	d      |j$                  u rt!        ||j&                        }t        |t        j                        r||r|}n|j(                  }	 |j*                  }|j$                  }| j0                  j                  |d      }t        |t2              r=|j4                  j7                  |      r"|j8                  }t        ||j&                        }|j(                  }t        |t:        j<                        s|}|sb|||j&                  f}|| j
                  v rE| j
                  |   j?                         }|| j
                  f|d<   | jA                  |       Rdx}}| jC                  |||||||||
	       q y# t,        $ r8}t        j.                  t        j                  d|z        |       Y d}~Zd}~ww xY w)zconsumes arguments from join() or outerjoin(), places them into a
        consistent format with which to form the actual JOIN constructs.

        r  r   from_joinpointr  r  NrL   r  r   r  r  r  r  prev)"r   r   string_typesr   r  
_joinpointr   r  r   r  r  r  r  ra   r  r   r   _joinpoint_zeror  r   r   r  r	  r
  r   r   r   r  r  r   r  copy_update_joinpointr  )rX   r  r  r  r  r  r  	outerjoincreate_aliasesr  r  r  prev_aliased_generationr  jp0r=  r  r  r  edgejps                        rN   r_  z"ORMSelectCompileState._legacy_join  s   
 /3 X	*UHdEi(I"9-N"#34N=D!&';!<  E4;L;L)M#J$++- #8, "%%'*.//*=*=($+' *)@&, 1155/ --* J55t7H7HI $ 5#5#55**>:**75(3K ..: 
 (J$=$=>!(J=(D$5$56 1D0023X  J*33% **,s|440H4J4JJ4T8<<HH (J$=$=> = ' ( 1 1	$)LLE  --2266tTB eZ0U\\5E5Ed5K ..D&tX\\:H((!(J,I,IJ#H% !%2Dt. "__T2779&*DOO%<6
..r2 
 #"t $$#"
]X	~  .  KK & 4 4%46;%<!" 14 s   M66	N7?-N22N7c                 V    | j                   j                  d| j                               S )N_joinpoint_entity)r  r   r  r  s    rN   r  z%ORMSelectCompileState._joinpoint_zero  s#    ""#68I8I8KLLrM   c
           	      V   ||J | j                  ||||      \  }}
}n| j                  ||      \  }
}||u r|st        j                  d|d|d      | j	                  ||||||      \  }}}|j
                  s| j                  |      }nd}|
J| j                  |
   }| j                  d|
 t        |||||	|      gz   | j                  |
dz   d z   | _        y|%t        ||   t              sJ ||   j                  }n|}| j                  t        |||||	|      gz   | _        y)zgiven raw "left", "right", "onclause" parameters consumed from
        a particular key within _join(), add a real ORMJoin object to
        our _from_obj list (or augment an existing one)

        NzCan't construct a join from z to z, they are the same entityrL   )r  r  _extra_criteriar   )"_join_determine_implicit_left_side_join_place_explicit_left_sidera   rb    _join_check_and_adapt_right_sider  _get_extra_criteriarH  r   r   r   r   )rX   r  r  r  r  r  r   r  r  r  replace_from_obj_indexuse_entity_indexr_infoextra_criterialeft_clauses                  rN   r  z)ORMSelectCompileState._join_left_to_right  s   $ < <<
 77#T5(	&  334GN&  5=,,)-u6  #'"G"G%49K#
x ##!55f=NN!- ++,BCK !!"9#9:#  )!(6	
 ##$:Q$>$@AB    + "'(89=   22BCNN" $ 1 1%$2	5 	!DrM   c                 z   t        |      }dx}}| j                  rt        j                  | j                  |j                  |      }t        |      dk(  r|d   }| j                  |   }nMt        |      dkD  rt        j                  d      t        j                  d|d      |ri }	t        |      D ]W  \  }
}|j                  }|t        |      }||u r%t        |t              r|
|f|	|j                  <   Gd|f|	|j                  <   Y t        |	j                               }t        j                  ||j                  |      }t        |      dk(  r|	||d         \  }}nQt        |      dkD  rt        j                  d      t        j                  d|d      t        j                  d      |||fS )zWhen join conditions don't express the left side explicitly,
        determine if an existing FROM or entity in this query
        can serve as the left hand side.

        Nr   r   a  Can't determine which FROM clause to join from, there are multiple FROMS which can join to this entity. Please use the .select_from() method to establish an explicit left side, as well as providing an explicit ON clause if not present already to help resolve the ambiguity.zDon't know how to join to z. Please use the .select_from() method to establish an explicit left side, as well as providing an explicit ON clause if not present already to help resolve the ambiguity.zgNo entities to join from; please use select_from() to establish the left entity/selectable of this join)r   rH  r   find_left_clause_to_join_fromr   rr  ra   rb   	enumerateentity_zero_or_selectabler   r   ru  keys)rX   r  r  r  r  r/  r-  r.  indexes	potentialentity_indexr   r  ent_infoall_clausess                  rN   r)  z8ORMSelectCompileState._join_determine_implicit_left_side  s   & 488!1 <<!!6#4#4hG 7|q )0&(()?@W!005  00
 9>	@  ! I%./B%C D!c66>"6?v%
 c=11=v0FIcnn-6:F^Ih112D  y~~/0K<<V..G 7|q )2;wqz3J)K& $W!005  00
 9>	@  ,,1  +-===rM   c                    dx}}t        |      }| j                  rTt        j                  | j                  |j                        }t        |      dkD  rt        j                  d      |r|d   }|J|rHt        |d      r<t        |      D ].  \  }}t        |t              s|j                  |      s)|} ||fS  ||fS )zWhen join conditions express a left side explicitly, determine
        where in our existing list of FROM clauses we should join towards,
        or if we need to make a new join, and if so is it from one of our
        existing entities.

        Nr   zrCan't identify which entity in which to assign the left side of this join.   Please use a more specific ON clause.r   r   )r   rH  r   #find_left_clause_that_matches_givenr   rr  ra   rb   r  r4  r   r   corresponds_to)	rX   r  r  r-  r.  l_infor7  idxr   s	            rN   r*  z4ORMSelectCompileState._join_place_explicit_left_sideh  s    2 598!1BB!!6#4#4G 7|a00!  )0& #*#)%&9: S c=1c6H6H6N'*$%'777 &'777rM   c                 n   t        |      }t        |      }d}	|st        |dd      }
|
r|
j                  s$t        |
j                  t
        j                        re| j                  xs |j                  gD ]G  }t        j                  |j                  |      s$t        j                  ||j                        sEd}	 n |	s|s:|j                  |j                  u r"t        j                  d|j                  z        t        |dd      |j                  t        |dd      }}}
|
r8|r6|
j                  |j                        st        j                  d|d|      t        |d      r| xj                   |fz  c_        d}|j"                  r|r|j                  }
|j$                  r| j'                         }| ||d      }n|r|j)                  |
j                        s:t        j                  d	|j*                  d
|
j                  j*                  d      t        |t
        j,                        r&t/        j0                  t2        j4                  |      }d}t7        |
|      }t9        j:                  d|
z  dd       n|rt        j                  d      |
xr | xr |	}|s/|s|r+t7        |d      }d}|st9        j<                  d|
z  d       |rg|
sJ t?        ||
j@                        }|s| jC                  |
|       n|rd|_"        |f| jF                  jI                  |d      z   | jF                  |<   nx|j"                  sl|sj|
j                  r^t        |
jJ                  t
        jL                        r:| jC                  |
t        jN                  |
j                  |
j@                               t        |t
        jP                        r| j'                         }|r	 ||d      }|s1|r/| jS                  ||||jT                  f| jV                  f|d       n
||d| _+        t        |      ||fS )ztransform the "right" side of the join as well as the onclause
        according to polymorphic mapping translations, aliasing on the query
        or on the join, special cases where the right and left side have
        overlapping tables.

        Fr   NTz*Can't join table/selectable '%s' to itselfr   zJoin target z9 does not correspond to the right side of join condition zSelectable 'z' is not derived from ''zAn alias is being generated automatically against joined entity %s for raw clauseelement, which is deprecated and will be removed in a later release. Use the aliased() construct explicitly, see the linked example.z1.4xaj1)codezpThe aliased=True parameter on query.join() only works with an ORM entity, not a plain selectable, as the target.)flatzAn alias is being generated automatically against joined entity %s due to overlapping tables.  This is a legacy pattern which may be deprecated in a later release.  Use the aliased(<entity>, flat=True) construct explicitly, see the linked example.xaj2)equivalentsrL   )r&  r  r  )r&  r  ),r   r   with_polymorphicr   r   r   JoinrH  r   r   selectables_overlapra   rb   common_parentr   r  rZ  is_clause_element_is_lateralr  is_derived_fromdescriptionr   r   r  r   FromClauseRoler   r   warn_deprecatedwarnr   r   r   _debugr  r   _with_polymorphic_selectabler  r   r   r  r   r  )rX   r  r  r  r  r   r  r?  r/  overlapright_mapperr  right_selectableright_is_aliasedneed_adapterr	  aliased_entityr   s                     rN   r+  z6ORMSelectCompileState._join_check_and_adapt_right_side  sL    "68T:L --l==zO $ 1 1 Hf6G6G5H H33))8"66 &"3"3 #' >6#4#44,,<##$  FHd+F.6 )9&  ..t{{;,,9>J  68$F9, ###{{++ #'";";"=". ,E48E (77 33 !44 -88(;;GG  .
0E0EF'0'7'7,,.>($ $(L  .>?$$D
 ## 	   00  &J.>*>J7> E-EL!		D ##  	 < <#C#CG "77gN#!%A--112DbIAJ))*<= (($--99-- 33&& ++ 44 h
 8 89"779O*8T: $"")."E4884dooF*< &+&8DO
 u~uh..rM   c                     || _         d|v r4|d   \  }}t        |      }|j                         ||<   ||f|d<   |}d|v r4|| _        y )Nr  )r  r_   r  	_joinpath)rX   r$  fr  s       rN   r  z'ORMSelectCompileState._update_joinpoint  s[     ljGAt:DggiDGTBvJB l rM   c                 &    | j                   | _        y rR   )r\  r  r  s    rN   r  z&ORMSelectCompileState._reset_joinpoint  s    ..rM   c           
      N   | j                   j                  | j                   j                  | j                  | j                  | j                   j
                  | j                   j                  | j                  xs d | j                   j                  | j                   j                  d	S )N)	r  r  rk  ri  r  r  re  r  r  )
r[   r  r  rk  ri  r  r  re  r  r  r  s    rN   r  z"ORMSelectCompileState._select_args  s     !11??!22AA++--77--77- 11??%%;;
 	
rM   c                     | j                   }|j                  d      d uxsO |j                  d      d uxs: |j                  dd      xs& |j                  dd      xs |j                  dd      S )Nr  r  rk  Fri  rL   re  )r  r   )rX   kwargss     rN   rv  z-ORMSelectCompileState._should_nest_selectable  sv    ""JJ~&d2 -zz/*$6-zz*e,- zz-,- zz*e,	
rM   c                      dj                   f j                  v r.t         fd j                  dj                   f   D              S y)Nadditional_entity_criteriac              3      K   | ]@  }|j                   s|j                  u r$|j                        r|j                         B y wrR   )include_aliasesr  _should_include_resolve_where_criteria)rU   aer   rX   s     rN   rY   z<ORMSelectCompileState._get_extra_criteria.<locals>.<genexpr>  sH       &&"))x*?&&t, **84s   AA	rL   )r   r   r]   )rX   r   s   ``rN   r,  z)ORMSelectCompileState._get_extra_criteria  sZ    (OO
 ##$  0018??C   rM   c                    | j                   D ]  }|j                  j                  dd      }|s"|j                  j                  d|j                  f| j
                  v sS|| j                  vsb||j                  r|j                  ndf| j                  |<    t        | j                  j                               }|D ]  \  }}|| j                  v r|j                  j                  }| j                  j                  rg }n| j                  |      }|||fz  }| j                         }|D ]N  }|r|j!                  |      }|r!t#        j$                  |ddi      } ||d      }| xj&                  |fz  c_        P  y)a  Apply extra criteria filtering.

        For all distinct single-table-inheritance mappers represented in
        the columns clause of this query, as well as the "select from entity",
        add criterion to the WHERE
        clause of the given QueryContext such that only the appropriate
        subtypes are selected from the total results.

        Additionally, add WHERE criteria originating from LoaderCriteriaOptions
        associated with the global context.

        r  Nrc  r  TF)rH  r  r   r   _single_table_criterionr   r   r   r  r   r  rZ  r   ry   r,  r  traverser   _deep_annotater`  )	rX   
fromclauser   r  r   single_critrc  r	  rO  s	            rN   rt  z0ORMSelectCompileState._adjust_for_extra_criteria  s    ++ 	J!..22>4HH OO;;G4hooF--. D$@$@@ )1)B)BH%%:,,X6	$ T1188:;#) 	0Xw4..."//AAK##66-/*-1-E-Eh-O*&*{n<*"779O2 0"++D1D"#224,9MND*47D$$/$0	0rM   rR   )8r?   r@   rA   rZ   r\  r  rC  r  r  r  r  r  r  rm  ro  r`  rb  r   r   rI  r   r  r  r  r   preload_moduler  rA  r[  r  ry  r  r  r  r  rw  rx  r  r  r  r
  r  r]  r_  r  r  r)  r*  r+  r  r  r   r  rv  r,  rt  rL   rM   rN   r:  r:  I  s   ((I
$O #!IOd dLVp " "H    " 

 

 T/0 1 &K

+*(0#"Un%NIV/$7rrh]~McJh>T@8DZ/x
) 
 
 
 
 80rM   r:  c                    |t         j                  | |      }|}|j                  D cg c]'  }||j                  t	        |j                        nd f) c}D cg c][  \  }}|j
                  |j                  t        |dd      |j                  |j                  |j                  st        |dd       nd d] c}}S c c}w c c}}w )N)r   r   Fr  )r   typer   exprr  )
r:  r   r   r  r   _label_namerq  r   rr  rL  )query_or_select_stmtr(   r   ctx_entr   insp_ents          rN   r   r     s     -II  J 
 C( 

   ''3 D,,-

 C OOHHx);UCHH*83M3M h$7	
 

s   ,C A Cc                     | }|j                   r|j                  }||S |j                  r7d|j                  d   j                  v r|j                  d   j                  d   S t	        |      S )Nr  r   )r^  _last_joined_entityrG  r  _entity_from_pre_ent_zero)query_or_augmented_selectrX   ry  s      rN   _legacy_filter_by_entity_zeror|  	  sj    $D"66*&&~~.DNN1,=,J,JJ~~a --n==$T**rM   c                     | }|j                   sy |j                   d   }d|j                  v r|j                  d   S t        |t              r|j                  S d|j                  v r|j                  d   S |S )Nr   r  bundle)r   r  r   r   r  )r{  rX   r   s      rN   rz  rz  (	  s{    $D


A
C)))//	C-	.zz	S%%	%))
rM   c                 D   | s|S | d   \  }}}}|d   }|$t        |t        t        j                  f      r|}d}|'d|j                  v r|j                  d   j
                  }|3|}t        |      }|j                  s|j                  s|j                  r|S |}t        |t        j                        r|j
                  S t        |t              rQ|rt        | dd |      }	n|}	|	yt        |	      }	t        |	j
                  |d      }
|
|
j                  j
                  S y)a  given the legacy_setup_joins collection at a point in time,
    figure out what the "filter by entity" would be in terms
    of those joins.

    in 2.0 this logic should hopefully be much simpler as there will
    be far fewer ways to specify joins with the ORM

    r%  r  Nr  r   )r   strr   r  r  r  r   rL  r   r  $_legacy_determine_last_joined_entityr   r   )r  r  r  r  left_r  r  last_entityinspr  attrs              rN   r  r  9	  s:     '2"o#UHeU+,NJZ../ ^u/A/AA"">299{#!!T%:%:dnnKK+z889!!! (C 7Ab!;D D<t}t{{Hd3==''' rM   c                   0    e Zd ZdZdZdZdZdZed        Z	y)r   z:represent an entity column returned within a Query result.rL   Fc                    t        |      D ]4  \  }}|j                  r7|j                  r| j                  ||j                  ||       >|j                  }|j
                  r|j                  rAd|j                  v rt        ||||       t        j                  ||j                  |||       |j                  j                  dd      rt        ||||       |j                  r$t        j                  ||j                  |||       t        j                  ||g|||       |j                  s(t        |||       7 |S )Nr  r~  F)r4  _is_lambda_element_is_sequencer   	_resolvedrL  r  r  r   _ColumnEntity_for_columns_select_iterabler   _BundleEntity_is_clause_list	is_bundle)r   r(   entitiesr  r   r@  r  s          rN   r   z_QueryEntity.to_compile_state	  sX   
 %X. 9	JKC((&&((%((++	 #--F''''%)<)<<%)"//	 &22)"33// **..x?%)"//	  // &22)"33// &22)#H// !!mV5HIs9	Jv #"rM   N)
r?   r@   rA   r6  rh   _non_hashable_value_null_column_typeuse_id_for_hashr   r   rL   rM   rN   r   r   |	  s-    DIO?# ?#rM   r   c                   ^    e Zd ZdZdZd ZdZdZdZe	d        Z
e	d        Zd Zd Zd	 Zd
 Zy)r   z mapper/class/AliasedClass entity)
rr  r   r  r   path_extra_entitiesrs  r   r   _polymorphic_discriminatorc                    |j                  |        |r!|j                  | |_        d|_        d|_        |j                  d   }|j
                   |x}| _        |j                  }|| _        |j                  x| _	        }| j                  f| _
        |j                  r|j                  | _        n|j                  j                  | _        |j                  | _        |j                   | _        ||j$                  v rxt'        |j$                  |         }| j                  rt)        d      |j+                  |j,                  |j.                        \  }}	|| _        |	| _        |j2                  | _        y |j.                  | _        |j,                  | _        |j2                  | _        |j6                  r|j9                  || j.                         y y )NTr  z6Can't use with_polymorphic() against an Aliased object)r   r   r  r  r  _post_inspectr  r  rr  r   r  r   r   rs  class_r?   r   r  rr   r   r   _with_polymorphic_argswith_polymorphic_mappersr   r   polymorphic_onr   _should_select_with_poly_adapterr   )
rX   r(   r  r  r   r   r   r  mappersr  s
             rN   rg   z_MapperEntity.__init__	  s    	""4(,,404-15M..2M+$$^4&,,4#	'.f $		|$$'}}D%}}55D ( 9 9++	}@@@ BB8LMB$$)L  !' = =++R]]!GX .5D*&DO.0.?.?D+ '11DO-5-N-ND*.6.E.ED+66>>doo 7rM   Tc                 .    | j                   j                  S rR   )r   r  r  s    rN   rq  z_MapperEntity.type
  s    {{!!!rM   c                     | j                   S rR   )r  r  s    rN   r5  z'_MapperEntity.entity_zero_or_selectable
  s    rM   c                 .    t        | j                  |      S rR   )r
   r  rX   r  s     rN   r>  z_MapperEntity.corresponds_to
  s    %d&6&6??rM   c                 *   d }| j                   s3|j                  r=|j                  j                  | j                  d       }n| j                  j
                  }|r-|j                  r|j                  |j                        }|S |}|S |j                  }|S rR   )r   r   r   r   r  r  r  wrap)rX   r(   r   rets       rN   _get_entity_clausesz!_MapperEntity._get_entity_clauses"
  s    $$22'==AAKK &&//G,,ll=#@#@A 
	  
  //C
rM   c                    |j                   }| j                  |      }|j                  r|r|j                  |j                        }n|s|j                  }|j                  | u r#|j
                  j                  }|j                  }nd x}}t        j                  | | j                  ||| j                  |||| j                  	      }|| j                  | j                  fS )N)only_load_propsr3   polymorphic_discriminator)r(   r  r  r  r   r   rw   r3   r   _instance_processorr   r  r  rs  r  )rX   r   r   r(   r   r  r3   	_instances           rN   row_processorz_MapperEntity.row_processor8
  s    --**=9//Gll=#G#GHG#::G((D0+;;LLO#11M.22Om//KKII+'&*&E&E

	 $**D,@,@@@rM   c                    | j                  |      }| j                  j                  }|d| j                  f|j                  v r5| j                  }||j
                  r|j                  nd f|j                  |<   t        j                  || j                  | | j                  ||j                  | j                  |j                  j                  | j                  	       |j                   j#                  | j$                         y )Nrc  )rH  r  r  )r  r   rj  r   r  r   r  r   r   _setup_entity_queryr  r   r   r   rw   r  r   r   r   )rX   r(   r   single_table_critr   s        rN   r  z!_MapperEntity.setup_compile_stateU
  s    **=9 KK??),dkk:../ ''H%-%>%>!!D?M11(;
 	##KKII))!;;)99JJ&*&E&E
	
 	,,33DOODrM   N)r?   r@   rA   r6  rh   rg   supports_single_entityr  r  r   rq  r5  r>  r  r  r  rL   rM   rN   r   r   	  sb    *I8t "O" "    @,A:ErM   r   c                   b    e Zd ZdZdZ	 	 ddZed        Zed        Zd Z	ed        Z
d	 Zd
 Zy)r  rL   )r~  rr  rq  rs  r   r  Nc           	         d|_         |j                  d   }|r|j                  j                  |        n|j                  |        t	        |t
        j                  t        j                  f      r|j                         }n|}|x| _
        | _        t        |      | _        |j                  | _        g | _        |rm|j                  D ]^  }d|j                  v rt!        |||||        "t	        |t"              rt!        |||||        Ct$        j'                  ||g|d ||        ` | j                  j(                  | _        | j*                  r-|j,                  j.                  st1        j2                  d       y y y )NTr~  parent_bundlezKThe Bundle.single_entity flag has no effect when using 2.0 style execution.)r  r  r   r   r   r   r  r   r  r  r~  rr  rq  r   rs  exprsr  r   _ORMColumnEntityr  single_entityr  r   ro   r   r  )rX   r(   rr  r  r   setup_entitiesr  r~  s           rN   rg   z_BundleEntity.__init__
  sq    +/'  *##**40&&t,:00*2K2KL
 ,,.FF"((diL	!;; t000!%++&*  f-!%++&* %11%++&* 2 %6 '+kk&?&?#''!11II##- J (rM   c                 8    | j                   }||j                  S y rR   )r  r   )rX   r}  s     rN   r   z_BundleEntity.mapper
  s       <<rM   c                 J    | j                   D ]  }|j                  }||c S  y rR   )r   r  rX   r   r}  s      rN   r  z_BundleEntity.entity_zero
  s.    >> 	COOE 	
 rM   c                      yr5  rL   r  s     rN   r>  z_BundleEntity.corresponds_to
  s     rM   c                 J    | j                   D ]  }|j                  }||c S  y rR   )r   r5  r  s      rN   r5  z'_BundleEntity.entity_zero_or_selectable
  s0    >> 	C11E 	
 rM   c                 H    | j                   D ]  }|j                  |        y rR   )r   r  )rX   r(   r   s      rN   r  z!_BundleEntity.setup_compile_state
  s#    >> 	3C##M2	3rM   c           	          t        | j                  D cg c]  }|j                  ||       c} \  }}}| j                  j	                  |j
                  ||      }|| j                  | j                  fS c c}w rR   )r^   r   r  r~  create_row_processorr)   rs  r  )rX   r   r   r   procslabelsextraprocs           rN   r  z_BundleEntity.row_processor
  so    "<@NNKSc0K 
vu {{//ufMT%%t';';;; Ls   A5rM  )r?   r@   rA   r  rh   rg   r   r   r  r>  r5  r  r  rL   rM   rN   r  r  t
  sh    OI AF    
  3<rM   r  c                   Z    e Zd ZdZe	 dd       Zed        Zed        Zed        Z	d Z
y)	r  )_fetch_column_row_processorraw_column_indextranslate_raw_columnNc           
          |D ]u  }|j                   }d|v r|d   }	nt        j                  |d      }	|	r4d|j                   v rt        ||||	|||       Rt	        ||||	|||       et        ||||||       w y )Nr  r9   r  )r  r   extract_first_column_annotation_IdentityTokenEntityr  _RawColumnEntity)
r   r(   r  r  r  r   r  columnannotations_entitys
             rN   r  z_ColumnEntity._for_columns
  s      &	F --K,%n5"BBN #v':'::(%+(+&3 %%+(+&3 !!'$'"/?&	rM   c                 .    | j                   j                  S rR   )r  rq  r  s    rN   rq  z_ColumnEntity.type+  s    {{rM   c                 D    | j                   j                  j                   S rR   )r  rq  hashabler  s    rN   r  z!_ColumnEntity._non_hashable_value/  s    ;;##,,,,rM   c                 B    | j                   j                  j                  S rR   )r  rq  _isnullr  s    rN   r  z_ColumnEntity._null_column_type3  s    {{'''rM   c                    |j                   }| j                  R| j                  \  }}}| j                  r1||j                  j                  j
                  | j                     fz  }|||fS | j                  | j                  }nM| j                  }|j                  r|j                  j                  |   }|j                  r|j                         }|j                  r|j                  j                  |   }|j                  |      }|| j                  | j                   f}|| _        | j                  rJ| j                   |j                  j                  j
                  | j                     fz   }|| j                  |fS |S rR   )r(   r  r  r   invoked_statementr   r  r  r  r  r  r  _deannotater  _getterrs  r  )	rX   r   r   r(   getter
label_nameextra_entitiesr  r  s	            rN   r  z_ColumnEntity.row_processor7  sy   -- *151D1D.FJ((NN44AA--#  :~55 )''F [[F ,,&66>>vF""  ++-//"99AA&IF'd&&(<(<<!$$!1100==))5 N
 4++^;;JrM   rR   )r?   r@   rA   rh   r   r  r   rq  r  r  r  rL   rM   rN   r  r  
  sd    I  / /b     - - ( (5rM   r  c                   2    e Zd ZdZdZdZdZ	 ddZd Zd Z	y)r  NF)rr  r  rs  r5  r  c                    || _         || _        |d u| _        |j                  r|xj                  ddiz  c_        |r|j
                  rd | _        n|j                  |      | _        |r|j                  j                  |        n|j                  |        || _
        | j                  j                  r| j                  j                  d   nd | _        | j                   | j                  f| _        d x| _        | _        y )Nr{   Tr   )rr  r  r  r{   r   _is_text_clausers  r   r   r   r  r  r5  r  r  r  )rX   r(   r  r  r  r   r  s          rN   rg   z_RawColumnEntity.__init__|  s     	 0$4D$@!??))j$-??)"f&<&<#D,>>vFD##**40&&t,,0KK,E,EDKK%%a(4 	& !%		4;;7377T0rM   c                      yr5  rL   r  s     rN   r>  z_RawColumnEntity.corresponds_to  s    rM   c                    |j                         }|r || j                  d      }n| j                  }|j                  r|j                         }|j                  j                  |       |j                  j                  |       || _        y r5  )	r  r  r  r  r   r   r   r   r  )rX   r(   r	  r  s       rN   r  z$_RawColumnEntity.setup_compile_state  su    '<<>$T[[%8F[[F '')F$$((0%%,,V4#rM   rR   )
r?   r@   rA   r  r   r  rh   rg   r>  r  rL   rM   rN   r  r  o  s.    KF"I 8B$rM   r  c                   .    e Zd ZdZdZdZ	 ddZd Zd Zy)	r  zColumn/expression based entity.F)rr  r   r  rs  r5  r  r  Nc                    |j                   }|}	|j                  dd       }
|j                  d|	      }|
r#t        |j                  |
      | _        d| _        n|| _        |d u| _        || _        |r|j                  ||
      | _        nd | _        |	j                   |	x| _
        x| _        }|	j                  x| _        }|r|j                  j                  |        n|j                  |        d|_        || _        d x| _        | _        | j                  | j                   f| _        |j(                  r|j+                  ||j,                         y y )N	proxy_keyproxy_ownerF)r   T)r  r   r   r  rr  r  r  r   rs  r  r  r5  r   r   r   r  r  r  r  r  r  r   r   )rX   r(   r  r  r  r  r   r  r  r  orm_keyr  r}  r   s                 rN   rg   z_ORMColumnEntity.__init__  sK    )) //+t4!oomW= 2 2G<DI(-D% DI(8(DD% 0,>>  ?  D  $DDKKK49E&~~-f##**40&&t,*.'377T0 $		4;;722::u'' 3rM   c                     t        |      r|| j                  u S t        | j                         xr |j                  | j                        S rR   )r   r  rK  r  s     rN   r>  z_ORMColumnEntity.corresponds_to   sO    V$T----(    9&&t'7'789rM   c                    |j                         }|r || j                  d      }n| j                  }| j                  }| j                  j                  }|d| j                  f|j
                  v r)||j                  r|j                  nd f|j                  |<   |j                  r|j                  s|j                         }t        | j                  j                        j                  |j                  j                        r%|j                   j#                  |j                         |j$                  j'                  |       |j(                  j#                  |       || _        y )NFrc  )r  r  r  r   rj  r   r   r  r   r  _expression_labelr  r   r  intersectionr   r   r   r   r   r   r  )rX   r(   r	  r  r}  r  s         rN   r  z$_ORMColumnEntity.setup_compile_state	  s,   '<<>$T[[%8F[[F   KK??),dkk:../
 "'"8"8d<M11%8
 v'?'? '')F t{{(()66**
 00778H8HI$$((0%%,,V4#rM   rR   )	r?   r@   rA   r6  r  rh   rg   r>  r  rL   rM   rN   r  r    s'    )"I$ >@9&$rM   r  c                       e Zd ZdZd Zd Zy)r  Fc                      y rR   rL   )rX   r(   s     rN   r  z(_IdentityTokenEntity.setup_compile_state5  s    rM   c                 @    fd}|| j                   | j                  fS )Nc                 0    j                   j                  S rR   )r+   rG   )rowr   s    rN   r  z2_IdentityTokenEntity.row_processor.<locals>.getter9  s    ''???rM   )rs  r  )rX   r   r   r  s    `  rN   r  z"_IdentityTokenEntity.row_processor8  s$    	@ t'')=)===rM   N)r?   r@   rA   r  r  r  rL   rM   rN   r  r  2  s     >rM   r  r5  )Br   r   r   r   baser   r   path_registryr	   r   r
   r   r   r   r   r   ra   r   r   r   r   r   r   r   r   r   sql.baser   r   r   r   r   sql.selectabler   r    r!   r"   sql.visitorsr#   r$   rootr   r  rZ   symbolr%   objectr'   r   rl   
plugin_forr   r  r:  r   r|  rz  r  r   r   r  r  r  r  r  rL   rM   rN   <module>r     s       # , ' (              "  , ( ' #  : - ; ( 4 ,"" d  " %%=> `6 `F ,++)-E 
j
l j
Z !!%)=>B? B ?BJ84] 84v !!%2n0O[ n0 3n0d5 6;@
+"@FI#6 I#XiEL iEXz<L z<z{L {|?$} ?$D~$} ~$B
>+ 
>rM   