U
    x^R                     @   s&  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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'rd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;e ! Z<e j=dddaddZ>e j?dd Z@eAdd ZBeAdd  ZCeDd!d" ZEd#d$ ZFd%d& ZGd'd( ZHdbd)d*ZId+d, ZJd-d. ZKd/d0 ZLdcd1d2ZMd3d4 ZNd5d6 ZOd7d8 ZPd9d: ZQd;d< ZRd=d> ZSd?d@ ZTdAdB ZUdCdD ZVdEdF ZWe j?dGdH ZXdIdJ ZYdKdL ZZdMdN Z[dOdP Z\dQdR Z]dddSdTZ^dedUdVZ_dfdWdXZ`dYdZ Zad[d\ Zbd]d^ Zcd_d` Zdd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:`.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   sf  t | ddstd| j  || _|| _d| _d | _|| _|d k	rJ|| _	n| jd k	r`| jj	| _	n| j
| _	|d k	rv|| _| j	dk| _| | | _| | | _|d k	r|| _|d k	r|| _|| _|	| _|	dkrd| _n$|	dkrd| _|	dk| _n
td	|
| _| jr| j| _|| _| jd
kr,t|| _n| jd k	rFt| 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   A/tmp/pip-install-dq5v43_d/SQLAlchemy/sqlalchemy/engine/default.py__init__   sh    
zDefaultDialect.__init__c                 C   s   t  S N)weakrefWeakKeyDictionaryr9   r   r   r;   _type_memos  s    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 r=   )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  s    zDefaultDialect.get_pool_classc                 C   s   z|  || _W n tk
r*   d | _Y nX z| || _W n tk
rV   d | _Y nX z| |j| _W n tk
r   d | _Y nX | || _	| j
d k	r| |rd  | _| _
| js| |}|r|| _| jr| j| jkrtd| j| jf 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_nameZget_isolation_level
connectiondefault_isolation_level_check_unicode_returnsreturns_unicode_stringsr3   _check_unicode_descriptionr4   r0   _check_max_identifier_lengthr1   r2   r   r/   )r9   rK   Zmax_ident_lengthr   r   r;   
initialize"  sJ    





zDefaultDialect.initializec                 C   s   dS )a  return a callable which sets up a newly created DBAPI connection.

        This is used to set dialect-wide per-connection options such as
        isolation modes, unicode modes, etc.

        If a callable is returned, it will be assembled into a pool listener
        that receives the direct DBAPI connection, with all wrappers removed.

        If None is returned, no listener will be generated.

        Nr   r@   r   r   r;   
on_connectN  s    zDefaultDialect.on_connectc                 C   s   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   rK   r   r   r;   rP   \  s    
z+DefaultDialect._check_max_identifier_lengthc                    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rd
S |dhkS d S )Nc              
      s    t | gjd}z,j }|| | }|  W n8 tj	k
r| } zt
d|  W Y dS d }~X Y nX t|d t
jS d S )Ndialectz2Exception attempting to detect unicode returns: %rFr   )r   selectcompilerK   cursor_cursor_executefetchonecloser   Z
DBAPIErrorr   r    
isinstance	text_type)test	statementrW   rowde)cast_torK   
parametersr9   r   r;   check_unicodes  s     
z<DefaultDialect._check_unicode_returns.<locals>.check_unicodez'test plain returns'<   z'test unicode returns'c                    s   h | ]} |qS r   r   ).0r]   )rc   r   r;   	<setcomp>  s     z8DefaultDialect._check_unicode_returns.<locals>.<setcomp>TFZconditional)r   py2ksupports_unicode_statementsbinary_typer\   r#   execute_sequence_formatr   castliteral_columnsqltypesZVARCHARZUnicode
issuperset)r9   rK   Zadditional_teststestsresultsr   )ra   rc   rK   rb   r9   r;   rM   h  s,    
z%DefaultDialect._check_unicode_returnsc                 C   sx   t jr| jst j}nt j}|j }zD||t	
t	ddgj| d t|jd d t jW S |  X d S )Nz'x'Z
some_labelrS   r   )r   rg   rh   ri   r\   rK   rW   rZ   executer   rU   rl   labelrV   r[   description)r9   rK   ra   rW   r   r   r;   rO     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`.

        )rm   Z
adapt_typecolspecs)r9   Ztypeobjr   r   r;   type_descriptor  s    	zDefaultDialect.type_descriptorc                 K   s    t j|}|j||||f|S r=   )r   Z	InspectorZfrom_enginereflecttable)r9   rK   tableZinclude_columnsZexclude_columnsZresolve_fksoptsZinspr   r   r;   rv     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||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   rG   rx   r   r   r;   create_connect_args  s    z"DefaultDialect.create_connect_argsc                    sb   d|kr*|d t |dfdd}d|kr^t|d   |_t |d fdd}d S )Nisolation_levelZengine_connectc                    s   |s |   d S r=   )_set_connection_isolationrK   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  s    zMDefaultDialect.set_engine_execution_options.<locals>.set_schema_translate_map)r   Zlistens_forr   _schema_getterr   )r9   enginerx   r   r   r   )r   r   r9   r;   set_engine_execution_options  s    

z+DefaultDialect.set_engine_execution_optionsc                 C   s8   d|kr|  ||d  d|kr4t|d }||_d S )Nr   r   )r   r   r   r   )r9   rK   rx   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_levelrK   Z_connection_recordZfinalize_callbackappendreset_isolation_level)r9   rK   levelr   r   r;   r     s    
z(DefaultDialect._set_connection_isolationc                 C   s   d S r=   r   r9   dbapi_connectionr   r   r;   do_begin  s    zDefaultDialect.do_beginc                 C   s   |   d S r=   )rollbackr   r   r   r;   do_rollback  s    zDefaultDialect.do_rollbackc                 C   s   |   d S r=   )commitr   r   r   r;   	do_commit  s    zDefaultDialect.do_commitc                 C   s   |   d S r=   )rZ   r   r   r   r;   do_close  s    zDefaultDialect.do_closec                 C   s   t tdgj| dS )Nr   rS   )strr   rU   rV   r@   r   r   r;   _dialect_specific_select_one  s    z+DefaultDialect._dialect_specific_select_onec              
   C   sv   d }z(|  }z|| j W 5 |  X W n@ | jjk
rl } z| |||rZW Y 
dS  W 5 d }~X Y nX dS d S )NFT)rW   rZ   rq   r   r$   Erroris_disconnect)r9   r   rW   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_xid/  s    zDefaultDialect.create_xidc                 C   s   | t| d S r=   )rq   r   ZSavepointClauser9   rK   r!   r   r   r;   do_savepoint8  s    zDefaultDialect.do_savepointc                 C   s   | t| d S r=   )rq   r   ZRollbackToSavepointClauser   r   r   r;   do_rollback_to_savepoint;  s    z'DefaultDialect.do_rollback_to_savepointc                 C   s   | t| d S r=   )rq   r   ZReleaseSavepointClauser   r   r   r;   do_release_savepoint>  s    z#DefaultDialect.do_release_savepointc                 C   s   | || d S r=   )executemanyr9   rW   r^   rb   contextr   r   r;   do_executemanyA  s    zDefaultDialect.do_executemanyc                 C   s   | || d S r=   rq   r   r   r   r;   
do_executeD  s    zDefaultDialect.do_executec                 C   s   | | d S r=   r   )r9   rW   r^   r   r   r   r;   do_execute_no_paramsG  s    z#DefaultDialect.do_execute_no_paramsc                 C   s   dS )NFr   )r9   erK   rW   r   r   r;   r   J  s    zDefaultDialect.is_disconnectc                 C   s   |  || j d S r=   )r   rL   )r9   Z
dbapi_connr   r   r;   r   M  s    z$DefaultDialect.reset_isolation_levelc                 C   sx   |d krd S t jr(t|tr(|| j}| }| }||krD|S ||kr\| j	|s\|S ||krpt
|ddS |S d S )NT)quote)r   rg   r[   r   decoder"   lowerupperr)   _requires_quotesr   r9   r!   Z
name_lowerZ
name_upperr   r   r;   normalize_nameS  s$    
zDefaultDialect.normalize_namec                 C   sf   |d krd S |  }| }||kr(|S ||kr@| j|s@|}tjrb| jsZ|| j}nt	|}|S r=   )
r   r   r)   r   r   rg   supports_unicode_bindsencoder"   unicoder   r   r   r;   denormalize_namep  s"    zDefaultDialect.denormalize_name)Fr   NNNNTNr   NN)N)N)N)N)N)e__name__
__module____qualname____doc__r   ZSQLCompilerstatement_compilerDDLCompilerddl_compilerZGenericTypeCompilerr*   IdentifierPreparerr(   Zsupports_alterZsupports_commentsZinline_commentsZdefault_sequence_basetuplerj   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py3krh   r   rN   r3   r!   r1   r0   Zmax_index_name_lengthrD   supports_sane_multi_rowcountrt   r&   Zsupports_default_valuesZsupports_empty_insertZsupports_multivalues_insertsupports_server_side_cursorsrI   Zconstruct_argumentsZrequires_name_normalizeZreflection_optionsZdbapi_exception_translation_mapZdeprecated_paramsr<   memoized_propertyrA   propertyrC   rE   classmethodrH   rQ   rR   rP   rM   rO   ru   rv   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ejZejZejZ	ej
ZdZdZdZdZdZdZdS )StrCompileDialectTFN)r   r   r   r   ZStrSQLCompilerr   r   r   ZStrSQLTypeCompilerr*   r   r(   r   r   r   r'   r,   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@ Z5dPdAdBZ6dCdD Z7dZ8dQdFdGZ9dHdI Z:dJdK Z;dLdM Z<dNdO Z=dS )RDefaultExecutionContextFNc                 C   s   |  | }||_||_|j|_| |_}d|_|j|_|jrVt|j|_|j	|j |j
s|t||_||jd |_nt| |_|_| |_g |_|jr| g|_ni g|_|S )z8Initialize execution context for a DDLElement construct.Tr   )__new__root_connection_dbapi_connectionrT   compiledisddlexecution_options_execution_optionsdictr   rh   r   r\   unicode_statementr7   r^   create_cursorrW   compiled_parametersr#   rj   rb   )rF   rT   rK   r   Zcompiled_ddlr9   r   r   r   r;   	_init_ddl  s(    


z!DefaultExecutionContext._init_ddlc                    sP  |  | }||_||_|j|_ |_ js.t j|j	|_ j
 j jf|_t |_jsv|j|jj|_n|j|_ j|_ j|_ j|_ j|_|s  g|_n& fddt|D |_t|dk|_| |_ |js|js|jr d|_!t" jj#|_$t" j%o jj# |_&|jj's4|jj(rN|jrF|)  n|*   j+ j,rj|- }n j.rz|jj/}g } j.r|jD ]Vg }|D ]6}	|	kr|0|	 |	  n|0|	  q|01| qnZj }
|jD ]J|
rt2fddD }nt2fddD }|0| q1||_3|S )z6Initialize execution context for a Compiled construct.c                    s   g | ]\}} j ||d qS ))Z_group_number)construct_params)re   grpm)r   r   r;   
<listcomp>  s   z:DefaultExecutionContext._init_compiled.<locals>.<listcomp>r   Tc                 3   s<   | ]4} |d  |kr*|  | n | fV  qdS )r   Nr7   re   key)compiled_paramsrT   r
   r   r;   	<genexpr>(  s   z9DefaultExecutionContext._init_compiled.<locals>.<genexpr>c                 3   s2   | ]*}||kr |  | n | fV  qd S r=   r   r   )r   r
   r   r;   r   2  s   )4r   r   r   rT   r   Zcan_executeAssertionErrorr   unionr   Z_result_columnsZ_ordered_columnsZ_textual_ordered_columnsresult_column_structr   r\   r   rh   r   r"   r^   isinsertisupdateisdeleteZisplaintextis_textr   r   	enumerater|   r   r   rW   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_parameters_expand_in_parametersr#   positiontupr   rj   r   rb   )rF   rT   rK   r   r   rb   r9   r   paramr   r   r   )r   r   rT   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rDg }nd}i i }|jr^jj	njj
D ]r jj
  }|jrʈ kr|  n| }|sg  | < j|jr|jn|jg < nt|d ttfr8 fddt|dD  | < jjrd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 < qf|jrf|  qf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   )re   ituple_elementjvaluer!   r   r;   r   |  s    zADefaultExecutionContext._expand_in_parameters.<locals>.<listcomp>r   zVALUES  , c                 3   s8   | ]0\ d d  fddtD  V  qdS )z(%s)r  c                 3   s6   | ].\}}j jd  t |  d i V  qdS )r!   r   N)r   bindtemplater|   )re   r   r  )r   r9   	to_updater   r   r;   r     s    zJDefaultExecutionContext._expand_in_parameters.<locals>.<genexpr>.<genexpr>N)joinr   re   )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   )re   r   r  r  r   r;   r     s   c                 3   s"   | ]\}} j jd |i V  qdS )r!   N)r   r  re   r   r  r@   r   r;   r     s   c                 3   s&   | ]\}} kr|  fV  qd S r=   r   r	  )r!   r
   r   r;   r     s   c                 s   s   | ]\}}|V  qd S r=   r   )re   r!   r  r   r   r;   r     s     c                 S   s   g | ]\}}|qS r   r   )re   
expand_keyr  r   r   r;   r     s    c                    s    |  d S )Nr   )group)r   )replacement_expressionsr   r;   process_expanding  s    zHDefaultExecutionContext._expand_in_parameters.<locals>.process_expandingz\[EXPANDING_(\S+)\])r   r   InvalidRequestErrorr   r#   Z_numeric_bindsrJ   _expanded_parametersr   r   bindsZ	expandingpopZvisit_empty_set_exprZ_expanding_in_typestyper[   r   listr   rT   r   r  r   extendr   resubr^   )	r9   r   r
   r   r   Zto_update_setsZ	parametervaluesr  r   )r!   r
   r  r9   r  r;   r   B  s    







  z-DefaultExecutionContext._expand_in_parametersc                    s  |  | }||_||_|j|_d|_|j|_|sP|jjrF  g|_	qi g|_	ndt
|d  jrh||_	nLt
|d tr jr||_	q fdd|D pi g|_	n fdd|D |_	t|dk|_ jst
|tjr||_ |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   )re   k)drT   r   r;   
<dictcomp>  s      zFDefaultExecutionContext._init_statement.<locals>.<listcomp>.<dictcomp>r   r  rS   )r  r;   r     s   z;DefaultExecutionContext._init_statement.<locals>.<listcomp>c                    s   g | ]}  |qS r   )rj   )re   prS   r   r;   r     s    r   )r   r   r   rT   r   r   r   r#   rj   rb   r[   r   rh   r|   r   r   r\   r   r7   r^   r   rW   )rF   rT   rK   r   r^   rb   r9   r   rS   r;   _init_statement  sB    



 
z'DefaultExecutionContext._init_statementc                 C   s4   |  | }||_||_|j|_|j|_| |_|S )z;Initialize execution context for a ColumnDefault construct.)r   r   r   rT   r   r   r   rW   )rF   rT   rK   r   r9   r   r   r;   _init_default  s    

z%DefaultExecutionContext._init_defaultc                 C   s   | j jS r=   )r   r   r@   r   r   r;   r     s    zDefaultExecutionContext.enginec                 C   s   | j jS r=   )r   	postfetchr@   r   r   r;   postfetch_cols  s    z&DefaultExecutionContext.postfetch_colsc                 C   s$   | j r| jjS | jr| jjS d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_cols  s    z&DefaultExecutionContext.returning_colsc                 C   s   | j ddS )Nno_parametersF)r   getr@   r   r   r;   r"    s    z%DefaultExecutionContext.no_parametersc                 C   s@   | j d| j r| jrtjpd}|tjkr8| | jS |S d S )N
autocommitF)r   r#  r   r^   r   ZPARSE_AUTOCOMMITshould_autocommit_textr   )r9   r$  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k	r|| j| j
jd d }|r||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   r[   r   r\   rT   rh   r7   r#   rj   rX   rW   rY   _cached_result_processorrs   )r9   Zstmttype_ry   Zdefault_paramsrprocr   r   r;   _execute_scalar  s&    

 z'DefaultExecutionContext._execute_scalarc                 C   s
   | j  S r=   )r   Z_branchr@   r   r   r;   rK   5  s    z"DefaultExecutionContext.connectionc                 C   s
   t |S r=   )AUTOCOMMIT_REGEXPmatch)r9   r^   r   r   r;   r%  9  s    z.DefaultExecutionContext.should_autocommit_textc                 C   sv   | j jsdS | j jrd| jddo`| jr8t| jjtj	p`| j sPt| jjtj
o`| jo`t| j}n| jdd}|S )NFZstream_resultsT)rT   r   Zserver_side_cursorsr   r#  r   r[   r^   r   Z
SelectableZ
TextClauseSERVER_SIDE_CURSOR_REr-  )r9   Zuse_server_sider   r   r;   _use_server_side_cursor<  s8       
 z/DefaultExecutionContext._use_server_side_cursorc                 C   s*   |   rd| _|  S d| _| j S d S )NTF)r/  _is_server_sidecreate_server_side_cursorr   rW   r@   r   r   r;   r   \  s
    z%DefaultExecutionContext.create_cursorc                 C   s
   t  d S r=   )rJ   r@   r   r   r;   r1  d  s    z1DefaultExecutionContext.create_server_side_cursorc                 C   s   d S r=   r   r@   r   r   r;   pre_execg  s    z DefaultExecutionContext.pre_execc                 C   s   d S r=   r   r@   r   r   r;   	post_execj  s    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.

        )r'  rT   )r9   r(  ZcolnameZcoltyper   r   r;   get_result_processorm  s    z,DefaultExecutionContext.get_result_processorc                 C   s   | j jS )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.

        )rW   	lastrowidr@   r   r   r;   get_lastrowidw  s    z%DefaultExecutionContext.get_lastrowidc                 C   s   d S r=   r   )r9   r   r   r   r;   handle_dbapi_exception  s    z.DefaultExecutionContext.handle_dbapi_exceptionc                 C   s   | j rt| S t| S d S r=   )r0  r   ZBufferedRowResultProxyZResultProxyr@   r   r   r;   get_result_proxy  s    
z(DefaultExecutionContext.get_result_proxyc                 C   s   | j jS r=   )rW   rowcountr@   r   r   r;   r9    s    z DefaultExecutionContext.rowcountc                 C   s   | j jS r=   )rT   rD   r@   r   r   r;   rD     s    z.DefaultExecutionContext.supports_sane_rowcountc                 C   s   | j jS r=   )rT   r   r@   r   r   r;   r     s    z4DefaultExecutionContext.supports_sane_multi_rowcountc                 C   s   | j r:| js:| js,| jjs,| jjr,|   n| js:|   | 	 }| j r| jrv|
 }|| _| | |  d |_q| js|  d |_nB| jr| jr|
 }|| _|  d |_n|jd kr|j |  |S r=   )r   r   r   r   inlinerT   r   _setup_ins_pk_from_lastrowid_setup_ins_pk_from_emptyr8  rY   returned_defaults%_setup_ins_pk_from_implicit_returningZ_soft_close	_metadatar   r   r9  )r9   r   r_   r   r   r;   _setup_crud_result_proxy  s<    


z0DefaultExecutionContext._setup_crud_result_proxyc                    s   | j jd | j jj}| jd |  d k	r||j  d k	r^ j| j	d }|d k	r^| fdd|j
D | _nfdd|j
D | _d S )Nr   r   c                    s(   g | ] }| krn |d qS r=   r#  re   cZautoinc_colr   
key_getterr5  r   r;   r     s   zHDefaultExecutionContext._setup_ins_pk_from_lastrowid.<locals>.<listcomp>c                    s   g | ]}  |d qS r=   rA  rB  r   rE  r   r;   r     s   )r   _key_getters_for_crud_columnr^   rw   r   r6  Z_autoincrement_columnr  r'  rT   primary_keyinserted_primary_key)r9   rw   r*  r   rD  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                    s   g | ]}  |d qS r=   rA  rB  rF  r   r;   r     s    zDDefaultExecutionContext._setup_ins_pk_from_empty.<locals>.<listcomp>)r   rG  r^   rw   r   rH  rI  )r9   rw   r   rF  r;   r<    s    

z0DefaultExecutionContext._setup_ins_pk_from_emptyc                    s\   d k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 kr | n|qS r=   r   )re   colr  )r_   r   r;   r     s   zQDefaultExecutionContext._setup_ins_pk_from_implicit_returning.<locals>.<listcomp>c                    s    g | ]}|  |d fqS r=   rA  )re   rJ  rF  r   r;   r     s   )rI  r   rG  r^   rw   r   rH  )r9   r_   rw   r   )r   rE  r_   r;   r>    s    


z=DefaultExecutionContext._setup_ins_pk_from_implicit_returningc                 C   s   | j s| jot| jjS r=   )r   r   r   r   r  r@   r   r   r;   lastrow_has_defaults  s    z,DefaultExecutionContext.lastrow_has_defaultsc              
      sH  t | jdsdS i }| jjD ]j}|j| j}t|}|| jj  dk	r~|r` |kr~||kr~|rt |kst||kr~ ||< qd||< q| jjr| jj	
|| j| j| j|  | jjrfg }| jjD ]V}	| jj|	 }||d  dk	r|	| jkr| gt| j|	   q|  qz| jj|  W n8 tk
rb }
 z| j|
ddd|  W 5 d}
~
X Y nX ni }| jj D ]\}}	||d  dk	rv|r||	|	}	| jjs| j|	d }	|	| jkr| fdd| j|	 D  n ||	< qvz| jjf | W n8 tk
rB }
 z| j|
ddd|  W 5 d}
~
X Y nX dS )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   )re   r
  Zdbtyper   r;   r   R  s   z:DefaultExecutionContext.set_input_sizes.<locals>.<genexpr>)hasattrr   rL  r  Z_unwrapped_dialect_implrT   Zget_dbapi_typer$   Z_has_eventsdispatchZdo_setinputsizesrW   r^   rb   r#   r   r  r#  r  r  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   rM  r;   set_input_sizes  s    	


    
    


    z'DefaultExecutionContext.set_input_sizesc                 C   s   |j r| ||S |jr(|| _|| S |jrv| j}|jsJt	|j|}n|j}t
|gj|d}||di  S |jS d S )N)bindr   )is_sequenceZfire_sequenceZis_callablecurrent_columnargZis_clause_elementrK   Z_arg_is_typedr   Ztype_coercerU   rV   Z_execute_compiledZscalar)r9   columnr   r(  ry   Zdefault_argrC  r   r   r;   _exec_default_  s    
z%DefaultExecutionContext._exec_defaultTc                    s   z| j | j}W n tk
r.   tdY nX |r| jr| jjjr|j	rh|j
d  |jj|j i}n|j|j i}d | jjjd  }| fdd|D  |S S dS )aF  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:`.Column`, which is usually synonymous
        with the name.

        :param isolate_multiinsert_groups=True: indicates that multi-valued
         INSERT constructs created using :meth:`.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   indexrb   r   r;   r     s    zADefaultExecutionContext.get_current_parameters.<locals>.<genexpr>N)current_parametersrV  AttributeErrorr   r  r   r   r^   Z_has_multi_parametersZ_is_multiparam_columnr[  originalr   rb   keysr   )r9   Zisolate_multiinsert_groupsrX  r  r_  r   rZ  r;   get_current_parameters  s0    


z.DefaultExecutionContext.get_current_parametersc                 C   s$   |j d krd S | ||j |jS d S r=   )r   rY  r  r9   rX  r   r   r;   get_insert_default  s    
z*DefaultExecutionContext.get_insert_defaultc                 C   s$   |j d krd S | ||j |jS d S r=   )onupdaterY  r  ra  r   r   r;   get_update_default  s    
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rH|jjrH|jj||< qH| jD ]~}|| _	|D ]4}||kr|| }n
| 
|}|d k	r|||||< q||D ]4}||kr|| }n
| |}|d k	r||||< qqn| `	d S )Nr   )r   rG  r   r   r   	is_scalarrW  rc  r   r\  rb  rd  )r9   rE  Zscalar_defaultsr   r   rC  r   valr   r   r;   r     s2    




z5DefaultExecutionContext._process_executemany_defaultsc                 C   s   | j jd }| jd  | _}| j jD ]B}|jrH|jjsH|jjrH|jj}n
| 	|}|d k	r$||||< q$| j j
D ]"}| |}|d k	rp||||< qp| `d S )Nr   r   )r   rG  r   r\  r   r   rU  re  rW  rb  r   rd  )r9   rE  r   rC  rf  r   r   r;   r     s    


z7DefaultExecutionContext._process_executesingle_defaults)NNN)T)>r   r   r   r   r   r   r   r   r   r   r   r^   r   r=  r   r   Z_translate_colnamer   r   r  r   r   r   r   r  r  r   r   r  r   r!  r"  r&  r+  r   rK   r%  r/  r   r1  r2  r3  r4  r6  r7  r8  r9  rD   r   r@  r;  r<  r>  rK  rS  rY  r\  r`  rb  rd  r   r   r   r   r   r;   r     s   
 
sn
/






!
 

'     
X
:%r   )!r   r5   r   r  r>   r  r   r   r   r   r   r	   r
   r   rm   r   Zsqlr   r   r   Zsql.elementsr   rV   IUNICODEr,  r.  Dialectr   r   ZExecutionContextr   Zexecution_ctx_clsr   r   r   r;   <module>   sH    
    _      z