o
    [h                     @   sr  d Z ddlZddlZddlZ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 edejejB ZedejejB ZG dd dejZG dd deZG dd deejZ G dd deej!Z"G dd deej#Z$G dd deZ%G d d! d!ej&Z'e'e_(dS )"a  Default implementations of per-dialect sqlalchemy.engine classes.

These are semi-private implementation classes which are only of importance
to database dialect authors; dialects will usually use the classes here
as the base class for their own corresponding classes.

    N   )
interfaces)
reflection)result   )event)exc)pool)
processors)types)util)compiler)
expression)schema)quoted_namez-\s*(?:UPDATE|INSERT|CREATE|DELETE|DROP|ALTER)z	\s*SELECTc                   @   s  e Zd ZdZejZejZej	Z
ej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dZdZdZdZdZe !de "dfde j#fde "d	fd
e "d	fde j#fde j#fde j#fde j$fgZ%dZ&e j'rodZ(dZ)dZ*dZ+ndZ(dZ)dZ*dZ+dZ,dZ-dZ.dZ/dZ0dZ1i Z2dZ3dZ4dZ5dZ6dZ7dZ8dZ9dZ:dZ;	 dZ<dZ=e ! Z>	 e j?dd											deddZ@e jAdd ZBeCdd ZDeCdd  ZEeFd!d" ZGeFd#d$ ZHd%d& ZId'd( ZJd)d* ZKd+d, ZLdfd-d.ZMd/d0 ZNd1d2 ZOd3d4 ZPdfd5d6ZQd7d8 ZRd9d: ZSd;d< ZTd=d> ZUd?d@ ZVdAdB ZWdCdD ZXdEdF ZYdGdH ZZdIdJ Z[e jAdKdL Z\dMdN Z]dOdP Z^dQdR Z_dSdT Z`dUdV ZadfdWdXZbdfdYdZZcdfd[d\Zdd]d^ Zed_d` Zfdadb Zgdcdd ZhdS )gDefaultDialectz!Default implementation of DialectTFr   convert_unicodeforcepool_timeoutZechodebugZ	echo_poolZpool_recycleZ	pool_sizeZmax_overflowZpool_threadlocalNuse_encodingdefaulti'  named )z1.3zThe :paramref:`_sa.create_engine.convert_unicode` parameter and corresponding dialect-level parameters are deprecated, and will be removed in a future release.  Modern DBAPIs support Python Unicode natively and this parameter is unnecessary.)r   utf-8staticc                 K   s`  t | ddstd| j  || _|| _d| _d | _|| _|d ur%|| _	n| jd ur0| jj	| _	n| j
| _	|d ur;|| _| j	dv | _| | | _| | | _|d urT|| _|d ur[|| _|| _|	| _|	dkrid| _n|	dv rvd| _|	dk| _ntd	|
| _| jr| j| _|| _| jd
krt|| _n| jd urt| j| _t| j| _ t| j| _!d S )NZported_sqla_06Tz2The %s dialect is not yet ported to the 0.6 formatF)Zqmarkformatnumericr   )Zdynamicdynamic_warnr   z?empty_in_strategy may be 'static', 'dynamic', or 'dynamic_warn'r   )"getattrr   warnnamer   encoding
positionalZ_ischemadbapi
paramstyledefault_paramstyleimplicit_returningprepareridentifier_preparertype_compilersupports_right_nested_joinssupports_native_booleancase_sensitiveempty_in_strategyZ_use_static_inZ_warn_on_empty_inr   ArgumentError#_user_defined_max_identifier_lengthmax_identifier_lengthlabel_lengthdescription_encodingr
   Zto_unicode_processor_factory_description_decodercodecs
getencoder_encoder_decoder)selfr   r"   r%   r$   r'   r+   r-   r,   r.   r1   r2   kwargsr   r   /home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/sqlalchemy/engine/default.py__init__   sh   


zDefaultDialect.__init__c                 C   s   t  S N)weakrefWeakKeyDictionaryr9   r   r   r;   _type_memos     zDefaultDialect._type_memosc                 C   s   | j d | j S )N+)r!   Zdriverr@   r   r   r;   dialect_description  s   z"DefaultDialect.dialect_descriptionc                 C   s   | j S )zTrue if this dialect supports sane rowcount even if RETURNING is
        in use.

        For dialects that don't support RETURNING, this is synonymous with
        ``supports_sane_rowcount``.

        )supports_sane_rowcountr@   r   r   r;    supports_sane_rowcount_returning  s   	z/DefaultDialect.supports_sane_rowcount_returningc                 C   s   t | dtjS )NZ	poolclass)r   r	   Z	QueuePool)clsurlr   r   r;   get_pool_class*     zDefaultDialect.get_pool_classc                 C   sB   d | jddd }z	t|d  W d S  ty    Y d S w )N.r   z
.provision)join
__module__split
__import__ImportError)rG   packager   r   r;   load_provisioning.  s   z DefaultDialect.load_provisioningc                 C   s   z|  || _W n ty   d | _Y nw z| || _W n ty)   d | _Y nw z	| |j| _W n ty?   d | _Y nw | || _	| j
d urV| |rVd  | _| _
| jsc| |}|rc|| _| jrw| j| jkrytd| j| jf d S d S )NzQLabel length of %d is greater than this dialect's maximum identifier length of %d)Z_get_server_version_infoserver_version_infoNotImplementedErrorZ_get_default_schema_nameZdefault_schema_nameget_default_isolation_level
connectiondefault_isolation_level_check_unicode_returnsreturns_unicode_stringsr3   _check_unicode_descriptionr4   r0   _check_max_identifier_lengthr1   r2   r   r/   )r9   rW   Zmax_ident_lengthr   r   r;   
initialize6  sP   








zDefaultDialect.initializec                 C      d S r=   r   r@   r   r   r;   
on_connectb  s   zDefaultDialect.on_connectc                 C      dS )zPerform a connection / server version specific check to determine
        the max_identifier_length.

        If the dialect's class level max_identifier_length should be used,
        can return None.

        .. versionadded:: 1.3.9

        Nr   )r9   rW   r   r   r;   r\   f  s   
z+DefaultDialect._check_max_identifier_lengthc                 C   s
   |  |S )a  Given a DBAPI connection, return its isolation level, or
        a default isolation level if one cannot be retrieved.

        May be overridden by subclasses in order to provide a
        "fallback" isolation level for databases that cannot reliably
        retrieve the actual isolation level.

        By default, calls the :meth:`_engine.Interfaces.get_isolation_level`
        method, propagating any exceptions raised.

        .. versionadded:: 1.3.22

        )Zget_isolation_levelr9   Z
dbapi_connr   r   r;   rV   r  s   
z*DefaultDialect.get_default_isolation_levelc                    s   t jr
js
t j nt j jr ni  fddtt	dt
dtt	dt
dg}|r>||7 }fdd|D }|dd	grPd
S |dhkS )Nc              
      s    t | gjd}zj }|| | }|  W n tj	y= } zt
d|  W Y d }~dS d }~ww t|d t
jS )Ndialectz2Exception attempting to detect unicode returns: %rFr   )r   selectcompilerW   cursor_cursor_executefetchonecloser   Z
DBAPIErrorr   r    
isinstance	text_type)test	statementrf   rowde)cast_torW   
parametersr9   r   r;   check_unicode  s$   
	z<DefaultDialect._check_unicode_returns.<locals>.check_unicodez'test plain returns'<   z'test unicode returns'c                    s   h | ]} |qS r   r   ).0rl   )rr   r   r;   	<setcomp>  s    z8DefaultDialect._check_unicode_returns.<locals>.<setcomp>TFZconditional)r   py2ksupports_unicode_statementsbinary_typerk   r#   execute_sequence_formatr   castliteral_columnsqltypesZVARCHARZUnicode
issuperset)r9   rW   Zadditional_testsZtestsresultsr   )rp   rr   rW   rq   r9   r;   rY     s,   

z%DefaultDialect._check_unicode_returnsc                 C   sz   t jr
| js
t j}nt j}|j }z%||t	t
ddgj| d t|jd d t jW |  S |  w )Nz'x'Z
some_labelrb   r   )r   rv   rw   rx   rk   rW   rf   executer   rd   r{   labelre   rj   descriptionri   )r9   rW   rp   rf   r   r   r;   r[     s    
z)DefaultDialect._check_unicode_descriptionc                 C   s   t || jS )a)  Provide a database-specific :class:`.TypeEngine` object, given
        the generic object which comes from the types module.

        This method looks for a dictionary called
        ``colspecs`` as a class or instance-level variable,
        and passes on to :func:`_types.adapt_type`.

        )r|   Z
adapt_typecolspecs)r9   Ztypeobjr   r   r;   type_descriptor  s   	zDefaultDialect.type_descriptorc                 K   s$   t j|}|j||||fi |S r=   )r   Z	InspectorZfrom_enginereflecttable)r9   rW   tableZinclude_columnsZexclude_columnsZresolve_fksoptsZinspr   r   r;   r     s   	zDefaultDialect.reflecttablec                 K   s   d| j ||fd|i|iS )zCompatibility method, adapts the result of get_primary_keys()
        for those dialects which don't implement get_pk_constraint().

        Zconstrained_columnsr   )Zget_primary_keys)r9   connZ
table_namer   kwr   r   r;   get_pk_constraint  s   z DefaultDialect.get_pk_constraintc                 C   s&   t || jkrtd|| jf d S )Nz7Identifier '%s' exceeds maximum length of %d characters)lenr1   r   ZIdentifierError)r9   identr   r   r;   validate_identifier  s   z"DefaultDialect.validate_identifierc                 O   s   | j j|i |S r=   )r$   connect)r9   ZcargsZcparamsr   r   r;   r     s   zDefaultDialect.connectc                 C   s   |  }||j g |gS r=   )Ztranslate_connect_argsupdatequery)r9   rH   r   r   r   r;   create_connect_args  s   z"DefaultDialect.create_connect_argsc                    sf   d|v r|d t |dfdd}d|v r1t|d   |_t |d fdd}d S d S )Nisolation_levelZengine_connectc                    s   |s
 |   d S d S r=   )_set_connection_isolationrW   branch)r   r9   r   r;   set_isolation	  s   zBDefaultDialect.set_engine_execution_options.<locals>.set_isolationschema_translate_mapc                    s
    | _ d S r=   )schema_for_objectr   )getterr   r;   set_schema_translate_map     
zMDefaultDialect.set_engine_execution_options.<locals>.set_schema_translate_map)r   Zlistens_forr   _schema_getterr   )r9   enginer   r   r   r   )r   r   r9   r;   set_engine_execution_options  s   

z+DefaultDialect.set_engine_execution_optionsc                 C   s<   d|v r|  ||d  d|v rt|d }||_d S d S )Nr   r   )r   r   r   r   )r9   rW   r   r   r   r   r;    set_connection_execution_options  s   
z/DefaultDialect.set_connection_execution_optionsc                 C   s6   |  r	td | |j| |jjj| j d S )NzConnection is already established with a Transaction; setting isolation_level may implicitly rollback or commit the existing transaction, or have no effect until next transaction)	Zin_transactionr   r    set_isolation_levelrW   Z_connection_recordZfinalize_callbackappendreset_isolation_level)r9   rW   levelr   r   r;   r     s   
z(DefaultDialect._set_connection_isolationc                 C   r^   r=   r   r9   dbapi_connectionr   r   r;   do_begin+     zDefaultDialect.do_beginc                 C      |   d S r=   )rollbackr   r   r   r;   do_rollback.     zDefaultDialect.do_rollbackc                 C   r   r=   )commitr   r   r   r;   	do_commit1  r   zDefaultDialect.do_commitc                 C   r   r=   )ri   r   r   r   r;   do_close4  r   zDefaultDialect.do_closec                 C   s   t tdgj| dS )Nr   rb   )strr   rd   re   r@   r   r   r;   _dialect_specific_select_one7  s   z+DefaultDialect._dialect_specific_select_onec              
   C   sp   d }z|  }z|| j W |  W dS |  w  | jjy7 } z| |||r2W Y d }~dS  d }~ww )NFT)rf   r   r   ri   r$   Erroris_disconnect)r9   r   rf   errr   r   r;   do_ping;  s   

zDefaultDialect.do_pingc                 C   s   dt ddd  S )zCreate a random two-phase transaction ID.

        This id will be passed to do_begin_twophase(), do_rollback_twophase(),
        do_commit_twophase().  Its format is unspecified.
        z	_sa_%032xr   r      )randomrandintr@   r   r   r;   
create_xidK  s   zDefaultDialect.create_xidc                 C      | t| d S r=   )r   r   ZSavepointClauser9   rW   r!   r   r   r;   do_savepointT     zDefaultDialect.do_savepointc                 C   r   r=   )r   r   ZRollbackToSavepointClauser   r   r   r;   do_rollback_to_savepointW  r   z'DefaultDialect.do_rollback_to_savepointc                 C   r   r=   )r   r   ZReleaseSavepointClauser   r   r   r;   do_release_savepointZ  r   z#DefaultDialect.do_release_savepointc                 C      | || d S r=   )executemanyr9   rf   rm   rq   contextr   r   r;   do_executemany]     zDefaultDialect.do_executemanyc                 C   r   r=   r   r   r   r   r;   
do_execute`  r   zDefaultDialect.do_executec                 C   s   | | d S r=   r   )r9   rf   rm   r   r   r   r;   do_execute_no_paramsc     z#DefaultDialect.do_execute_no_paramsc                 C   r`   )NFr   )r9   erW   rf   r   r   r;   r   f  r   zDefaultDialect.is_disconnectc                 C   s   |  || j d S r=   )r   rX   ra   r   r   r;   r   i  s   z$DefaultDialect.reset_isolation_levelc                 C   st   |d u rd S t jrt|tr|| j}| }| }||kr"|S ||kr.| j	|s.|S ||kr8t
|ddS |S )NT)quote)r   rv   rj   r   decoder"   lowerupperr)   _requires_quotesr   r9   r!   Z
name_lowerZ
name_upperr   r   r;   normalize_nameo  s$   
zDefaultDialect.normalize_namec                 C   sh   |d u rd S |  }| }||kr|S ||kr | j|s |}tjr2| js.|| j}|S t	|}|S r=   )
r   r   r)   r   r   rv   supports_unicode_bindsencoder"   unicoder   r   r   r;   denormalize_name  s$   zDefaultDialect.denormalize_name)Fr   NNNNTNr   NNr=   )i__name__rN   __qualname____doc__r   ZSQLCompilerstatement_compilerDDLCompilerddl_compilerZGenericTypeCompilerr*   IdentifierPreparerr(   Zsupports_alterZsupports_commentsZinline_commentsZdefault_sequence_basetuplery   Zsupports_viewssupports_sequencessequences_optional"preexecute_autoincrement_sequencespostfetch_lastrowidr'   r+   Zcte_follows_insertZsupports_native_enumr,   Z#non_native_boolean_check_constraintsupports_simple_order_by_labeltuple_in_valuesr   immutabledictZbool_or_strZasintZasboolZengine_config_typesZsupports_native_decimalZpy3krw   r   rZ   r3   r!   r1   r0   Zmax_index_name_lengthrE   supports_sane_multi_rowcountr   r&   Zsupports_default_valuesZsupports_empty_insertZsupports_multivalues_insertZsupports_is_distinct_fromsupports_server_side_cursorsZsupports_for_update_ofrT   Zconstruct_argumentsZrequires_name_normalizeZreflection_optionsZdbapi_exception_translation_mapZdeprecated_paramsr<   memoized_propertyrA   propertyrD   rF   classmethodrI   rS   r]   r_   r\   rV   rY   r[   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r;   r   ,   s    )J





,
7

	


r   c                   @   s   e Zd Zdd ZdS )_RendersLiteralc                 C   s   dd }|S )Nc                 S   s   d|  S )Nz'%s'r   )valuer   r   r;   process     z2_RendersLiteral.literal_processor.<locals>.processr   )r9   rc   r   r   r   r;   literal_processor  s   z!_RendersLiteral.literal_processorN)r   rN   r   r   r   r   r   r;   r     s    r   c                   @      e Zd ZdS )_StrDateTimeNr   rN   r   r   r   r   r;   r         r   c                   @   r   )_StrDateNr   r   r   r   r;   r     r   r   c                   @   r   )_StrTimeNr   r   r   r   r;   r     r   r   c                   @   sR   e Zd ZejZejZejZ	ej
ZdZdZdZdZdZdZejeejeejeiZdS )StrCompileDialectTFN)r   rN   r   r   ZStrSQLCompilerr   r   r   ZStrSQLTypeCompilerr*   r   r(   r   r   r   r'   r,   r   r|   DateTimer   Dater   Timer   r   r   r   r   r;   r     s    r   c                   @   s  e 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Ze Zedd Zedd Zdd Zed	d
 Zedd Zejdd Zejdd Zejdd Zejdd Zejdd Zejdd Z dd Z!e"dd Z#dd Z$dd  Z%d!d" Z&d#d$ Z'd%d& Z(d'd( Z)d)d* Z*d+d, Z+d-d. Z,d/d0 Z-e"d1d2 Z.d3d4 Z/d5d6 Z0d7d8 Z1d9d: Z2d;d< Z3d=d> Z4d?d@ Z5	dPdAdBZ6dCdD Z7dZ8	 dQdFdGZ9dHdI Z:dJdK Z;dLdM Z<dNdO Z=dS )RDefaultExecutionContextFNc                 C   s   |  | }||_||_|j|_| |_}d|_|j|_|jr+t|j|_|j	|j |j
s>t||_||jd |_n	t| |_|_| |_g |_|jrZ| g|_|S i g|_|S )z8Initialize execution context for a DDLElement construct.Tr   )__new__root_connection_dbapi_connectionrc   compiledisddlexecution_options_execution_optionsdictr   rw   r   rk   unicode_statementr7   rm   create_cursorrf   compiled_parametersr#   ry   rq   )rG   rc   rW   r   Zcompiled_ddlr9   r   r   r   r;   	_init_ddl  s*   


z!DefaultExecutionContext._init_ddlc                    s@  |  | }||_||_|j|_ |_ jsJ  j|j|_ j	 j
 jf|_t |_js;|j|jj|_n|j|_ j|_ j|_ j|_ j|_|sX  g|_n fddt|D |_t|dk|_| |_|jsy|jsy|jrd|_ t! jj"|_#t! j$o jj" |_%|jj&s|jj'r|jr|(  n|)   j* j+rt,|- }n j.r|jj/}g } j.r|jD ](g }|D ]}	|	v r|0|	 |	  q|0|	  q|01| qn,j }
|jD ]$|
rt,fddD }nt,fddD }|0| q1||_2|S )z6Initialize execution context for a Compiled construct.c                    s   g | ]\}} j ||d qS ))Z_group_number)construct_params)rt   grpm)r   r   r;   
<listcomp>/  s    z:DefaultExecutionContext._init_compiled.<locals>.<listcomp>r   Tc                 3   s>    | ]} |d  |v r|  | n | fV  qdS )r   Nr7   rt   key)compiled_paramsrc   r
   r   r;   	<genexpr>`  s    
z9DefaultExecutionContext._init_compiled.<locals>.<genexpr>c                 3   s4    | ]}||v r|  | n | fV  qd S r=   r   r	  )r  r
   r   r;   r  j  s    
)3r   r   r   rc   r   Zcan_executer   unionr   Z_result_columnsZ_ordered_columnsZ_textual_ordered_columnsresult_column_structr   rk   r   rw   r   r"   rm   isinsertisupdateisdeleteZisplaintextis_textr  r  	enumerater   r   r  rf   is_crudboolZ
_returning_is_explicit_returning	returning_is_implicit_returninginsert_prefetchupdate_prefetch_process_executemany_defaults_process_executesingle_defaultsZ_bind_processorsZcontains_expanding_parametersr   _expand_in_parametersr#   positiontupr   ry   rq   )rG   rc   rW   r   r   rq   r9   r  paramr
  r   r   )r   r  rc   r
   r;   _init_compiled  s   





	



z&DefaultExecutionContext._init_compiledc           	         s  j rtdjjrjjrtdi _jd }|jr"g }nd}i i }|jr/jj	njj
D ] jj
  }|jr v rG|  no| }|seg  | < j|jr]|jn|jg < nQt|d ttfr fddt|dD  | < jjrdnd	d
fddt|D   < n fddt|dD  | < d
fddD  < |  fddD  |jr|dd D  dd D j < q3|jr|  q3fdd}td|j_|S )zhandle special 'expanding' parameters, IN tuples that are rendered
        on a per-parameter basis for an otherwise fixed SQL statement string.

        z7'expanding' parameters can't be used with executemany()zQ'expanding' bind parameters not supported with 'numeric' paramstyle at this time.r   Nc                    s6   g | ]\}}t |d D ]\}}d ||f |fqqS )r   z%s_%s_%s)r  )rt   ituple_elementjr   r!   r   r;   r    s    zADefaultExecutionContext._expand_in_parameters.<locals>.<listcomp>r   zVALUES  , c                 3   s:    | ]\ d d  fddtD  V  qdS )z(%s)r&  c                 3   s8    | ]\}}j jd  t |  d i V  qdS )r!   r   N)r   bindtemplater   )rt   r#  r   )r!  r9   	to_updater"  r   r;   r    s    
zJDefaultExecutionContext._expand_in_parameters.<locals>.<genexpr>.<genexpr>N)rM   r  rt   )r9   r(  )r!  r"  r;   r    s    
z@DefaultExecutionContext._expand_in_parameters.<locals>.<genexpr>c                    s    g | ]\}}d  |f |fqS )z%s_%sr   )rt   r!  r   r$  r   r;   r    s    c                 3   s$    | ]\}} j jd |i V  qdS )r!   N)r   r'  rt   r
  r   r@   r   r;   r    s
    
c                 3   s(    | ]\}} v r|  fV  qd S r=   r   r*  )r!   r
   r   r;   r    s    
c                 s   s    | ]\}}|V  qd S r=   r   )rt   r!   r   r   r   r;   r    s    c                 S   s   g | ]\}}|qS r   r   )rt   
expand_keyr   r   r   r;   r    s    c                    s    |  d S )Nr   )group)r  )replacement_expressionsr   r;   process_expanding  r   zHDefaultExecutionContext._expand_in_parameters.<locals>.process_expandingz\[EXPANDING_(\S+)\])r   r   InvalidRequestErrorr   r#   Z_numeric_bindsrU   _expanded_parametersr  r  bindsZ	expandingpopZvisit_empty_set_exprZ_expanding_in_typestyperj   r   listr  rc   r   rM   r   extendr   resubrm   )	r9   r   r
   r  r  Zto_update_setsZ	parametervaluesr.  r   )r!   r
   r-  r9   r(  r;   r  z  s   








z-DefaultExecutionContext._expand_in_parametersc                    s  |  | }||_||_|j|_d|_|j|_|s(|jjr#  g|_	n7i g|_	n2t
|d  jr4||_	n&t
|d trP jrB||_	n fdd|D pMi g|_	n
 fdd|D |_	t|dk|_ jsvt
|tjrv||_ |d |_n| |_|_| |_|S )z8Initialize execution context for a string SQL statement.Tr   c                    s    g | ]  fd d D qS )c                    s    i | ]} |d   | qS )r   r  )rt   k)drc   r   r;   
<dictcomp>  s     zFDefaultExecutionContext._init_statement.<locals>.<listcomp>.<dictcomp>r   r)  rb   )r:  r;   r        z;DefaultExecutionContext._init_statement.<locals>.<listcomp>c                    s   g | ]}  |qS r   )ry   )rt   prb   r   r;   r    s    
r   )r   r   r   rc   r  r   r   r#   ry   rq   rj   r   rw   r   r   r   rk   r   r7   rm   r  rf   )rG   rc   rW   r   rm   rq   r9   r   rb   r;   _init_statement  s@   




z'DefaultExecutionContext._init_statementc                 C   s4   |  | }||_||_|j|_|j|_| |_|S )z;Initialize execution context for a ColumnDefault construct.)r   r   r   rc   r   r   r  rf   )rG   rc   rW   r   r9   r   r   r;   _init_default  s   

z%DefaultExecutionContext._init_defaultc                 C      | j jS r=   )r   r   r@   r   r   r;   r   $  rB   zDefaultExecutionContext.enginec                 C   r@  r=   )r   	postfetchr@   r   r   r;   postfetch_cols(  rB   z&DefaultExecutionContext.postfetch_colsc                 C   s    | j r| jjS | jr| jjS dS )Nr   )r  r   r  r  r  r@   r   r   r;   prefetch_cols,  s
   z%DefaultExecutionContext.prefetch_colsc                 C   s   | j j d S r=   )r   r  r@   r   r   r;   returning_cols5  s   z&DefaultExecutionContext.returning_colsc                 C   s   | j ddS )Nno_parametersF)r   getr@   r   r   r;   rE  9  rJ   z%DefaultExecutionContext.no_parametersc                 C   s<   | j d| j r| jrtjpd}|tju r| | jS |S )N
autocommitF)r   rF  r   rm   r   ZPARSE_AUTOCOMMITshould_autocommit_textr   )r9   rG  r   r   r;   should_autocommit=  s   
z)DefaultExecutionContext.should_autocommitc                 C   s   | j }t|tjr| jjs| j|d }| jjr| j }ni }|j	| j
||| d | j
 d }|durI|| j| j
jd d }|rI||S |S )a  Execute a string statement on the current cursor, returning a
        scalar result.

        Used to fire off sequences, default phrases, and "select lastrowid"
        types of statements individually or in the context of a parent INSERT
        or UPDATE statement.

        r   )r   Nr   )r   rj   r   rk   rc   rw   r7   r#   ry   rg   rf   rh   _cached_result_processorr   )r9   stmttype_r   Zdefault_paramsrprocr   r   r;   _execute_scalarL  s$   

z'DefaultExecutionContext._execute_scalarc                 C   s
   | j  S r=   )r   Z_branchr@   r   r   r;   rW   m  r   z"DefaultExecutionContext.connectionc                 C   s
   t |S r=   )AUTOCOMMIT_REGEXPmatch)r9   rm   r   r   r;   rH  q  s   
z.DefaultExecutionContext.should_autocommit_textc                 C   sx   | j jsdS | j jr3| jddo0| jrt| jjtj	p0| j p't| jjtj
o0| jo0t| j}|S | jdd}|S )NFZstream_resultsT)rc   r   Zserver_side_cursorsr   rF  r   rj   rm   r   Z
SelectableZ
TextClauseSERVER_SIDE_CURSOR_RErQ  )r9   Zuse_server_sider   r   r;   _use_server_side_cursort  s2   


z/DefaultExecutionContext._use_server_side_cursorc                 C   s&   |   rd| _|  S d| _| j S )NTF)rS  _is_server_sidecreate_server_side_cursorr   rf   r@   r   r   r;   r    s
   
z%DefaultExecutionContext.create_cursorc                 C   s   t  r=   )rU   r@   r   r   r;   rU    s   z1DefaultExecutionContext.create_server_side_cursorc                 C   r^   r=   r   r@   r   r   r;   pre_exec  r   z DefaultExecutionContext.pre_execc                 C   r^   r=   r   r@   r   r   r;   	post_exec  r   z!DefaultExecutionContext.post_execc                 C   s   | | j|S )zReturn a 'result processor' for a given type as present in
        cursor.description.

        This has a default implementation that dialects can override
        for context-sensitive result type handling.

        )rJ  rc   )r9   rL  ZcolnameZcoltyper   r   r;   get_result_processor  s   z,DefaultExecutionContext.get_result_processorc                 C   r@  )a  return self.cursor.lastrowid, or equivalent, after an INSERT.

        This may involve calling special cursor functions,
        issuing a new SELECT on the cursor (or a new one),
        or returning a stored value that was
        calculated within post_exec().

        This function will only be called for dialects
        which support "implicit" primary key generation,
        keep preexecute_autoincrement_sequences set to False,
        and when no explicit id value was bound to the
        statement.

        The function is called once, directly after
        post_exec() and before the transaction is committed
        or ResultProxy is generated.   If the post_exec()
        method assigns a value to `self._lastrowid`, the
        value is used in place of calling get_lastrowid().

        Note that this method is *not* equivalent to the
        ``lastrowid`` method on ``ResultProxy``, which is a
        direct proxy to the DBAPI ``lastrowid`` accessor
        in all cases.

        )rf   	lastrowidr@   r   r   r;   get_lastrowid  s   z%DefaultExecutionContext.get_lastrowidc                 C   r^   r=   r   )r9   r   r   r   r;   handle_dbapi_exception  r   z.DefaultExecutionContext.handle_dbapi_exceptionc                 C   s   | j rt| S t| S r=   )rT  r   ZBufferedRowResultProxyZResultProxyr@   r   r   r;   get_result_proxy  s   

z(DefaultExecutionContext.get_result_proxyc                 C   r@  r=   )rf   rowcountr@   r   r   r;   r]    rB   z DefaultExecutionContext.rowcountc                 C   r@  r=   )rc   rE   r@   r   r   r;   rE     r   z.DefaultExecutionContext.supports_sane_rowcountc                 C   r@  r=   )rc   r   r@   r   r   r;   r     r   z4DefaultExecutionContext.supports_sane_multi_rowcountc                 C   s   | j r| js| js| jjs| jjr|   n| js|   | 	 }| j rH| jr<|
 }|| _| | |  d |_|S | jsF|  d |_|S | jr^| jr^|
 }|| _|  d |_|S |jd u rj|j |  |S r=   )r  r   r  r   inlinerc   r   _setup_ins_pk_from_lastrowid_setup_ins_pk_from_emptyr\  rh   returned_defaults%_setup_ins_pk_from_implicit_returningZ_soft_close	_metadatar  r  r]  )r9   r   rn   r   r   r;   _setup_crud_result_proxy  sB   


z0DefaultExecutionContext._setup_crud_result_proxyc                    s   | j jd | j jj}| jd |  d ur?|j  d ur/ j| j	d }|d ur/| fdd|j
D | _d S fdd|j
D | _d S )Nr   r   c                    s(   g | ]}| u r
n |d qS r=   rF  rt   cZautoinc_colr  
key_getterrY  r   r;   r    s    zHDefaultExecutionContext._setup_ins_pk_from_lastrowid.<locals>.<listcomp>c                       g | ]
}  |d qS r=   re  rf  r  ri  r   r;   r    s    )r   _key_getters_for_crud_columnrm   r   r  rZ  Z_autoincrement_columnr3  rJ  rc   primary_keyinserted_primary_key)r9   r   rN  r   rh  r;   r_    s$   

	z4DefaultExecutionContext._setup_ins_pk_from_lastrowidc                    s<   | j jd | j jj}| jd   fdd|jD | _d S )Nr   r   c                    rj  r=   re  rf  rk  r   r;   r  &  s    zDDefaultExecutionContext._setup_ins_pk_from_empty.<locals>.<listcomp>)r   rl  rm   r   r  rm  rn  )r9   r   r   rk  r;   r`  "  s   

z0DefaultExecutionContext._setup_ins_pk_from_emptyc                    s\   d u r	d | _ d S | jjd | jjj}| jd  fdd fdd|jD D | _ d S )Nr   r   c                    s$   g | ]\}}|d u r | n|qS r=   r   )rt   colr   )rn   r   r;   r  2  s    zQDefaultExecutionContext._setup_ins_pk_from_implicit_returning.<locals>.<listcomp>c                    s    g | ]}|  |d fqS r=   re  )rt   ro  rk  r   r;   r  4  r<  )rn  r   rl  rm   r   r  rm  )r9   rn   r   r   )r  ri  rn   r;   rb  *  s   


z=DefaultExecutionContext._setup_ins_pk_from_implicit_returningc                 C   s   | j s| jot| jjS r=   )r  r  r  r   rA  r@   r   r   r;   lastrow_has_defaults:  s   z,DefaultExecutionContext.lastrow_has_defaultsc              
      sB  t | jdsdS i }| jjD ]5}|j| j}t|}|| jj  dur?|r0 |vr?||vr?|r: |v s:||v r? ||< qd||< q| jjrV| jj	
|| j| j| j|  | jjrg }| jjD ]*}	| jj|	 }||d  dur|	| jv r| gt| j|	   q`|  q`z	| jj|  W dS  ty }
 z| j|
ddd|  W Y d}
~
dS d}
~
ww i }| jj D ];\}}	||d  dur|r||	|	}	| jjs| j|	d }	|	| jv r| fdd| j|	 D  q ||	< qz| jjdi | W dS  ty  }
 z| j|
ddd|  W Y d}
~
dS d}
~
ww )a&  Given a cursor and ClauseParameters, call the appropriate
        style of ``setinputsizes()`` on the cursor, using DB-API types
        from the bind parameter's ``TypeEngine`` objects.

        This method only called by those dialects which require it,
        currently cx_oracle.

        
bind_namesNr   c                 3   s    | ]}| fV  qd S r=   r   )rt   r+  Zdbtyper   r;   r    s
    
z:DefaultExecutionContext.set_input_sizes.<locals>.<genexpr>r   )hasattrr   rq  r3  Z_unwrapped_dialect_implrc   Zget_dbapi_typer$   Z_has_eventsdispatchZdo_setinputsizesrf   rm   rq   r#   r  r1  rF  r0  r5  r   r   ZsetinputsizesBaseExceptionr   Z_handle_dbapi_exceptionitemsr   r7   r   )r9   	translateZinclude_typesZexclude_typesZ
inputsizesZ	bindparamZdialect_implZdialect_impl_clsZpositional_inputsizesr
  r   Zkeyword_inputsizesr   rr  r;   set_input_sizes?  s   







z'DefaultExecutionContext.set_input_sizesc                 C   s|   |j r	| ||S |jr|| _|| S |jr;| j}|js%t	|j|}n|j}t
|gj|d}||di  S |jS )N)bindr   )is_sequenceZfire_sequenceis_callablecurrent_columnargZis_clause_elementrW   Z_arg_is_typedr   Ztype_coercerd   re   Z_execute_compiledZscalar)r9   columnr   rL  r   Zdefault_argrg  r   r   r;   _exec_default  s   
z%DefaultExecutionContext._exec_defaultTc                    s   z| j | j}W n ty   tdw |rS| jrS| jjjrS|j	r1|j
d  |jj|j i}n
|j|j i}d | jjjd  }| fdd|D  |S S )ai  Return a dictionary of parameters applied to the current row.

        This method can only be used in the context of a user-defined default
        generation function, e.g. as described at
        :ref:`context_default_functions`. When invoked, a dictionary is
        returned which includes entries for each column/value pair that is part
        of the INSERT or UPDATE statement. The keys of the dictionary will be
        the key value of each :class:`_schema.Column`,
        which is usually synonymous
        with the name.

        :param isolate_multiinsert_groups=True: indicates that multi-valued
         INSERT constructs created using :meth:`_expression.Insert.values`
         should be
         handled by returning only the subset of parameters that are local
         to the current column default invocation.   When ``False``, the
         raw parameters of the statement are returned including the
         naming convention used in the case of multi-valued INSERT.

        .. versionadded:: 1.2  added
           :meth:`.DefaultExecutionContext.get_current_parameters`
           which provides more functionality over the existing
           :attr:`.DefaultExecutionContext.current_parameters`
           attribute.

        .. seealso::

            :attr:`.DefaultExecutionContext.current_parameters`

            :ref:`context_default_functions`

        zdget_current_parameters() can only be invoked in the context of a Python side column default functionr   r   c                 3   s$    | ]}|d | f  fV  qdS )z%s_m%dNr   r	  indexrq   r   r;   r    s    
zADefaultExecutionContext.get_current_parameters.<locals>.<genexpr>)current_parametersr|  AttributeErrorr   r/  r  r   rm   Z_has_multi_parametersZ_is_multiparam_columnr  originalr
  rq   keysr   )r9   Zisolate_multiinsert_groupsr~  r:  r  r   r  r;   get_current_parameters  s2   !

z.DefaultExecutionContext.get_current_parametersc                 C       |j d u rd S | ||j |jS r=   )r   r  r3  r9   r~  r   r   r;   get_insert_default     
z*DefaultExecutionContext.get_insert_defaultc                 C   r  r=   )onupdater  r3  r  r   r   r;   get_update_default  r  z*DefaultExecutionContext.get_update_defaultc                 C   s   | j jd }i }| j j}| j j}|D ]}|jr!|jjr!|jj||< q|D ]}|jr3|jjr3|jj||< q$| jD ]?}|| _	|D ]}||v rI|| }n| 
|}|d urX||||< q>|D ]}||v rf|| }n| |}|d uru||||< q[q7| `	d S )Nr   )r   rl  r  r  r   	is_scalarr}  r  r  r  r  r  )r9   ri  Zscalar_defaultsr  r  rg  r  valr   r   r;   r    s<   




z5DefaultExecutionContext._process_executemany_defaultsc                 C   s   | j jd }| jd  | _}| j jD ]!}|jr$|jjs$|jjr$|jj}n| 	|}|d ur3||||< q| j j
D ]}| |}|d urI||||< q8| `d S )Nr   r   )r   rl  r  r  r  r   rz  r  r}  r  r  r  )r9   ri  r  rg  r  r   r   r;   r  0  s"   


z7DefaultExecutionContext._process_executesingle_defaults)NNN)T)>r   rN   r   r  r  r  r  r  r   r   r   rm   r  ra  r  r  Z_translate_colnamer   r   r0  r   r  r   r  r>  r?  r   r   rB  rC  rD  rE  rI  rO  r   rW   rH  rS  r  rU  rV  rW  rX  rZ  r[  r\  r]  rE   r   rd  r_  r`  rb  rp  rx  r  r  r  r  r  r  r  r   r   r   r;   r     s    
 
un
/






!
 

'
X
<%r   ))r   r5   r   r6  r>   r%  r   r   r   r   r   r	   r
   r   r|   r   sqlr   r   r   Zsql.elementsr   re   IUNICODErP  rR  Dialectr   objectr   r   r   r   r   r   r   r   ZExecutionContextr   Zexecution_ctx_clsr   r   r   r;   <module>   sP       {      
