B
    \q                 @   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 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)schemaz-\s*(?:UPDATE|INSERT|CREATE|DELETE|DROP|ALTER)z	\s*SELECTc               @   sj  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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Z/i Z0dZ1dZ2dZ3dZ4dZ5dZ6dZ7dZ8dZ9e  Z:ej;ddd[ddZ<ej=dd Z>e?dd Z@e?dd  ZAeBd!d" ZCd#d$ ZDd%d& ZEd\d'd(ZFd)d* ZGd+d, ZHd-d. ZId]d/d0ZJd1d2 ZKd3d4 ZLd5d6 ZMd7d8 ZNd9d: ZOd;d< ZPd=d> ZQd?d@ ZRdAdB ZSdCdD ZTej=dEdF ZUdGdH ZVdIdJ ZWdKdL ZXdMdN ZYdOdP ZZd^dQdRZ[d_dSdTZ\d`dUdVZ]dWdX Z^dYdZ Z_dS )aDefaultDialectz!Default implementation of DialectTFr   convert_unicodeZforceZpool_timeoutZechodebugZ	echo_poolZpool_recycleZ	pool_sizeZmax_overflowZpool_threadlocalNuse_encodingdefaulti'  Z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   sv  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	|
r|
| jkrtd
|
| jf |
| _| jdkr<t|| _n| jd k	rVt| j| _t| j| _t| j| _ d S )NZported_sqla_06Tz2The %s dialect is not yet ported to the 0.6 formatF)ZqmarkformatZnumericr   )Zdynamicdynamic_warnr   z?empty_in_strategy may be 'static', 'dynamic', or 'dynamic_warn'zQLabel length of %d is greater than this dialect's maximum identifier length of %dr   )!getattrr   warnnamer   encoding
positionalZ_ischemadbapi
paramstyledefault_paramstyleimplicit_returningpreparerZidentifier_preparertype_compilersupports_right_nested_joinssupports_native_booleancase_sensitiveempty_in_strategyZ_use_static_inZ_warn_on_empty_inr   ZArgumentErrormax_identifier_lengthlabel_lengthdescription_encodingr
   Zto_unicode_processor_factory_description_decodercodecs
getencoder_encoderZ_decoder)selfr   r   r    r   r"   r%   r'   r&   r(   r*   kwargsr   r   8lib/python3.7/site-packages/sqlalchemy/engine/default.py__init__   sZ    



zDefaultDialect.__init__c             C   s   t  S )N)weakrefWeakKeyDictionary)r0   r   r   r2   _type_memos  s    zDefaultDialect._type_memosc             C   s   | j d | j S )N+)r   Zdriver)r0   r   r   r2   dialect_description  s    z"DefaultDialect.dialect_descriptionc             C   s   | j S )N)supports_sane_rowcount)r0   r   r   r2    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   r2   get_pool_class  s    zDefaultDialect.get_pool_classc             C   s   y|  || _W n tk
r*   d | _Y nX y| || _W n tk
rV   d | _Y nX y| |j| _W n tk
r   d | _Y nX | || _	| j
d k	r| |rd  | _| _
| |j d S )N)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_stringsr+   _check_unicode_descriptionr,   do_rollback)r0   r@   r   r   r2   
initialize  s(    

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   )r0   r   r   r2   
on_connect=  s    zDefaultDialect.on_connectc                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}y,j }|| | }|  W n2 tj	k
rv } zt
d|  dS d }~X Y nX t|d t
jS d S )N)dialectz2Exception attempting to detect unicode returns: %rFr   )r   selectcompiler@   cursor_cursor_executefetchonecloser   Z
DBAPIErrorr   r   
isinstance	text_type)test	statementrK   rowZde)cast_tor@   
parametersr0   r   r2   check_unicodeV  s    
z<DefaultDialect._check_unicode_returns.<locals>.check_unicodez'test plain returns'<   z'test unicode returns'c                s   h | ]} |qS r   r   ).0rQ   )rV   r   r2   	<setcomp>{  s    z8DefaultDialect._check_unicode_returns.<locals>.<setcomp>TFZconditional)r   py2ksupports_unicode_statementsbinary_typerP   r   execute_sequence_formatr   castliteral_columnsqltypesZVARCHARZUnicode
issuperset)r0   r@   Zadditional_testsZtestsZresultsr   )rT   rV   r@   rU   r0   r2   rB   K  s&    

z%DefaultDialect._check_unicode_returnsc             C   st   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S |  X d S )Nz'x'Z
some_label)rH   r   )r   rZ   r[   r\   rP   r@   rK   executer   rI   r_   ZlabelrJ   rO   descriptionrN   )r0   r@   rT   rK   r   r   r2   rD     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)r0   Ztypeobjr   r   r2   type_descriptor  s    	zDefaultDialect.type_descriptorc             K   s    t j|}|j||||f|S )N)r   Z	InspectorZfrom_enginereflecttable)r0   r@   tableZinclude_columnsZexclude_columnsZresolve_fksoptsZinspr   r   r2   rf     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)r0   connZ
table_namer   kwr   r   r2   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)lenr)   r   ZIdentifierError)r0   Zidentr   r   r2   validate_identifier  s    z"DefaultDialect.validate_identifierc             O   s   | j j||S )N)r   connect)r0   ZcargsZcparamsr   r   r2   rn     s    zDefaultDialect.connectc             C   s   |  }||j g |gS )N)Ztranslate_connect_argsupdateZquery)r0   r<   rh   r   r   r2   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 )N)_set_connection_isolation)r@   branch)rq   r0   r   r2   set_isolation  s    zBDefaultDialect.set_engine_execution_options.<locals>.set_isolationschema_translate_mapc                s
    | _ d S )N)schema_for_object)r@   rs   )getterr   r2   set_schema_translate_map  s    zMDefaultDialect.set_engine_execution_options.<locals>.set_schema_translate_map)r   Zlistens_forr   _schema_getterrv   )r0   enginerh   rt   rx   r   )rw   rq   r0   r2   set_engine_execution_options  s    z+DefaultDialect.set_engine_execution_optionsc             C   s8   d|kr|  ||d  d|kr4t|d }||_d S )Nrq   ru   )rr   r   ry   rv   )r0   r@   rh   rw   r   r   r2    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_levelr@   Z_connection_recordZfinalize_callbackappendreset_isolation_level)r0   r@   levelr   r   r2   rr     s    
z(DefaultDialect._set_connection_isolationc             C   s   d S )Nr   )r0   dbapi_connectionr   r   r2   do_begin  s    zDefaultDialect.do_beginc             C   s   |   d S )N)Zrollback)r0   r   r   r   r2   rE     s    zDefaultDialect.do_rollbackc             C   s   |   d S )N)Zcommit)r0   r   r   r   r2   	do_commit  s    zDefaultDialect.do_commitc             C   s   |   d S )N)rN   )r0   r   r   r   r2   do_close  s    zDefaultDialect.do_closec             C   s   t tdgj| dS )Nr   )rH   )strr   rI   rJ   )r0   r   r   r2   _dialect_specific_select_one  s    z+DefaultDialect._dialect_specific_select_onec          
   C   sp   d }y(|  }z|| j W d |  X W n: | jjk
rf } z| |||rTdS  W d d }~X Y nX dS d S )NFT)rK   rb   r   rN   r   Erroris_disconnect)r0   r   rK   errr   r   r2   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      )randomZrandint)r0   r   r   r2   
create_xid  s    zDefaultDialect.create_xidc             C   s   | t| d S )N)rb   r   ZSavepointClause)r0   r@   r   r   r   r2   do_savepoint  s    zDefaultDialect.do_savepointc             C   s   | t| d S )N)rb   r   ZRollbackToSavepointClause)r0   r@   r   r   r   r2   do_rollback_to_savepoint  s    z'DefaultDialect.do_rollback_to_savepointc             C   s   | t| d S )N)rb   r   ZReleaseSavepointClause)r0   r@   r   r   r   r2   do_release_savepoint!  s    z#DefaultDialect.do_release_savepointc             C   s   | || d S )N)executemany)r0   rK   rR   rU   contextr   r   r2   do_executemany$  s    zDefaultDialect.do_executemanyc             C   s   | || d S )N)rb   )r0   rK   rR   rU   r   r   r   r2   
do_execute'  s    zDefaultDialect.do_executec             C   s   | | d S )N)rb   )r0   rK   rR   r   r   r   r2   do_execute_no_params*  s    z#DefaultDialect.do_execute_no_paramsc             C   s   dS )NFr   )r0   er@   rK   r   r   r2   r   -  s    zDefaultDialect.is_disconnectc             C   s   |  || j d S )N)r}   rA   )r0   Z
dbapi_connr   r   r2   r   0  s    z$DefaultDialect.reset_isolation_level)
Fr   NNNNTNr   N)N)N)N)N)N)`__name__
__module____qualname____doc__r   ZSQLCompilerstatement_compilerDDLCompilerddl_compilerZGenericTypeCompilerr$   IdentifierPreparerr#   Zsupports_alterZsupports_commentsZinline_commentsZdefault_sequence_basetupler]   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_labelr   immutabledictZbool_or_strZasintZasboolZengine_config_typesZsupports_native_decimalZpy3kr[   supports_unicode_bindsrC   r+   r   r)   Zmax_index_name_lengthr9   supports_sane_multi_rowcountrd   r!   Zsupports_default_valuesZsupports_empty_insertZsupports_multivalues_insertsupports_server_side_cursorsr>   Zconstruct_argumentsZrequires_name_normalizeZreflection_optionsZdbapi_exception_translation_mapZdeprecated_paramsr3   memoized_propertyr6   propertyr8   r:   classmethodr=   rF   rG   rB   rD   re   rf   rk   rm   rn   rp   r{   r|   rr   r   rE   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r2   r   +   s   *		         ?
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   r2   r   7  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_connectionrH   compiledisddlexecution_options_execution_optionsdictro   r[   r   rP   unicode_statementr/   rR   create_cursorrK   compiled_parametersr   r]   rU   )r;   rH   r@   r   Zcompiled_ddlr0   r   r   r   r2   	_init_ddl]  s(    


z!DefaultExecutionContext._init_ddlc                s\  |  | }||_||_|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.rx|jD ]Zg }x>|D ]6}	|	kr|0|	 |	  n|0|	  qW |01| qW n^j }
xT|jD ]J|
r$t2fddD }nt2fddD }|0| qW 1||_3|S )z6Initialize execution context for a Compiled construct.c                s   g | ]\}} j ||d qS ))Z_group_number)construct_params)rX   Zgrpm)r   r   r2   
<listcomp>  s   z:DefaultExecutionContext._init_compiled.<locals>.<listcomp>r   Tc             3   s<   | ]4} |d  |kr*|  | n | fV  qdS )r   N)r/   )rX   key)compiled_paramsrH   r
   r   r2   	<genexpr>  s   z9DefaultExecutionContext._init_compiled.<locals>.<genexpr>c             3   s2   | ]*}||kr |  | n | fV  qd S )Nr   )rX   r   )r   r
   r   r2   r     s   )4r   r   r   rH   r   Zcan_executeAssertionErrorr   unionr   Z_result_columnsZ_ordered_columnsZ_textual_ordered_columnsresult_column_structr   rP   r   r[   encoder   rR   isinsertisupdateisdeleteZisplaintextis_textr   r   	enumeraterl   r   r   rK   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~   r]   r   rU   )r;   rH   r@   r   r   rU   r0   r   paramr   r   r   )r   r   rH   r
   r2   _init_compiled~  sz    









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 }x||jrbjj	njj
D ]` jj
  }|jr kr|  n| }|sg  | < j|jr|jn|jg < nt|d ttfr* fddt|d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 < qj|jrj|  qjW 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   )rX   ituple_elementjvalue)r   r   r2   r   -  s   zADefaultExecutionContext._expand_in_parameters.<locals>.<listcomp>r   z, c             3   s8   | ]0\ d d  fddtD  V  qdS )z(%s)z, c             3   s6   | ].\}}j jd  t |  d i V  qdS )r   r   N)r   bindtemplaterl   )rX   r   r   )r   r0   	to_updater   r   r2   r   4  s   zJDefaultExecutionContext._expand_in_parameters.<locals>.<genexpr>.<genexpr>N)joinr   )rX   )r0   r   )r   r   r2   r   2  s   
z@DefaultExecutionContext._expand_in_parameters.<locals>.<genexpr>c                s    g | ]\}}d  |f |fqS )z%s_%sr   )rX   r   r   )r   r   r2   r   @  s   c             3   s"   | ]\}} j jd |i V  qdS )r   N)r   r   )rX   r   r   )r0   r   r2   r   D  s   c             3   s&   | ]\}} kr|  fV  qd S )Nr   )rX   r   r   )r   r
   r   r2   r   J  s   c             s   s   | ]\}}|V  qd S )Nr   )rX   r   r   r   r   r2   r   O  s    c             S   s   g | ]\}}|qS r   r   )rX   
expand_keyr   r   r   r2   r   Q  s    c                s    |  d S )Nr   )group)r   )replacement_expressionsr   r2   process_expandingV  s    zHDefaultExecutionContext._expand_in_parameters.<locals>.process_expandingz\[EXPANDING_(\S+)\])r   r   InvalidRequestErrorr   r   Z_numeric_bindsr?   _expanded_parametersr   r   bindsZ	expandingpopZvisit_empty_set_exprZ_expanding_in_typestyperO   r   listr   r   ro   extendr~   resubrR   )	r0   r   r
   r   r   Zto_update_setsZ	parametervaluesr   r   )r   r
   r   r0   r   r2   r     sd    








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/   )rX   k)drH   r   r2   
<dictcomp>y  s    zFDefaultExecutionContext._init_statement.<locals>.<listcomp>.<dictcomp>r   )rX   )rH   )r   r2   r   y  s   z;DefaultExecutionContext._init_statement.<locals>.<listcomp>c                s   g | ]}  |qS r   )r]   )rX   p)rH   r   r2   r   ~  s    r   )r   r   r   rH   r   r   r   r   r]   rU   rO   r   r[   rl   r   r   rP   r   r/   rR   r   rK   )r;   rH   r@   r   rR   rU   r0   r   )rH   r2   _init_statement^  s6    





z'DefaultExecutionContext._init_statementc             C   s4   |  | }||_||_|j|_|j|_| |_|S )z;Initialize execution context for a ColumnDefault construct.)r   r   r   rH   r   r   r   rK   )r;   rH   r@   r   r0   r   r   r2   _init_default  s    

z%DefaultExecutionContext._init_defaultc             C   s   | j jS )N)r   rz   )r0   r   r   r2   rz     s    zDefaultExecutionContext.enginec             C   s   | j jS )N)r   	postfetch)r0   r   r   r2   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   )r0   r   r   r2   prefetch_cols  s
    z%DefaultExecutionContext.prefetch_colsc             C   s   | j j d S )N)r   r   )r0   r   r   r2   returning_cols  s    z&DefaultExecutionContext.returning_colsc             C   s   | j ddS )Nno_parametersF)r   get)r0   r   r   r2   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   rR   r   ZPARSE_AUTOCOMMITshould_autocommit_textr   )r0   r   r   r   r2   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   rO   r   rP   rH   r[   r/   r   r]   rL   rK   rM   _cached_result_processorrc   )r0   Zstmttype_ri   Zdefault_paramsrprocr   r   r2   _execute_scalar  s    
z'DefaultExecutionContext._execute_scalarc             C   s
   | j  S )N)r   Z_branch)r0   r   r   r2   r@     s    z"DefaultExecutionContext.connectionc             C   s
   t |S )N)AUTOCOMMIT_REGEXPmatch)r0   rR   r   r   r2   r     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)rH   r   Zserver_side_cursorsr   r   r   rO   rR   r   Z
SelectableZ
TextClauseSERVER_SIDE_CURSOR_REr   )r0   Zuse_server_sider   r   r2   _use_server_side_cursor  s     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   rK   )r0   r   r   r2   r   
  s
    z%DefaultExecutionContext.create_cursorc             C   s
   t  d S )N)r?   )r0   r   r   r2   r    s    z1DefaultExecutionContext.create_server_side_cursorc             C   s   d S )Nr   )r0   r   r   r2   pre_exec  s    z DefaultExecutionContext.pre_execc             C   s   d S )Nr   )r0   r   r   r2   	post_exec  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   rH   )r0   r   ZcolnameZcoltyper   r   r2   get_result_processor  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.

        )rK   	lastrowid)r0   r   r   r2   get_lastrowid%  s    z%DefaultExecutionContext.get_lastrowidc             C   s   d S )Nr   )r0   r   r   r   r2   handle_dbapi_exceptionA  s    z.DefaultExecutionContext.handle_dbapi_exceptionc             C   s   | j rt| S t| S d S )N)r  r   ZBufferedRowResultProxyZResultProxy)r0   r   r   r2   get_result_proxyD  s    
z(DefaultExecutionContext.get_result_proxyc             C   s   | j jS )N)rK   rowcount)r0   r   r   r2   r  J  s    z DefaultExecutionContext.rowcountc             C   s   | j jS )N)rH   r9   )r0   r   r   r2   r9   N  s    z.DefaultExecutionContext.supports_sane_rowcountc             C   s   | j jS )N)rH   r   )r0   r   r   r2   r   Q  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 )N)r   r   r   r   ZinlinerH   r   _setup_ins_pk_from_lastrowid_setup_ins_pk_from_emptyr  rM   returned_defaults%_setup_ins_pk_from_implicit_returningZ_soft_closeZ	_metadatar   r   r  )r0   r   rS   r   r   r2   _setup_crud_result_proxyT  s6    


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 )N)r   )rX   c)autoinc_colr   
key_getterr  r   r2   r     s   zHDefaultExecutionContext._setup_ins_pk_from_lastrowid.<locals>.<listcomp>c                s   g | ]}  |d qS )N)r   )rX   r  )r   r  r   r2   r     s   )r   _key_getters_for_crud_columnrR   rg   r   r	  Z_autoincrement_columnr   r   rH   primary_keyinserted_primary_key)r0   rg   r   r   )r  r   r  r  r2   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 )N)r   )rX   r  )r   r  r   r2   r     s    zDDefaultExecutionContext._setup_ins_pk_from_empty.<locals>.<listcomp>)r   r  rR   rg   r   r  r  )r0   rg   r   )r   r  r2   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 )Nr   )rX   colr   )rS   r   r2   r     s   zQDefaultExecutionContext._setup_ins_pk_from_implicit_returning.<locals>.<listcomp>c                s    g | ]}|  |d fqS )N)r   )rX   r  )r   r  r   r2   r     s   )r  r   r  rR   rg   r   r  )r0   rS   rg   r   )r   r  rS   r2   r    s    


z=DefaultExecutionContext._setup_ins_pk_from_implicit_returningc             C   s   | j s| jot| jjS )N)r   r   r   r   r   )r0   r   r   r2   lastrow_has_defaults  s    z,DefaultExecutionContext.lastrow_has_defaultsc          
      sT  t | jdsdS i }xv| jjD ]j}|j| j}t|}|| jj  dk	r|rb |kr||kr|rv |ksv||kr ||< qd||< qW | jjr| jj	
|| j| j| j|  | jjrng }xb| jjD ]V}	| jj|	 }||d  dk	r|	| jkr| gt| j|	   q|  qW y| jj|  W n8 tk
rj }
 z| j|
ddd|  W dd}
~
X Y nX ni }x| jj D ]\}}	||d  dk	r|r||	|	}	| jjs| j|	d }	|	| jkr| fdd| j|	 D  n ||	< qW y| jjf | W n8 tk
rN }
 z| j|
ddd|  W d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 )Nr   )rX   r   )dbtyper   r2   r     s   z:DefaultExecutionContext.set_input_sizes.<locals>.<genexpr>)hasattrr   r  r   Z_unwrapped_dialect_implrH   Zget_dbapi_typer   Z_has_eventsdispatchZdo_setinputsizesrK   rR   rU   r   r   r   r   r   r   rl   r~   ZsetinputsizesBaseExceptionr   Z_handle_dbapi_exceptionitemsr   r/   ro   )r0   	translateZinclude_typesZexclude_typesZ
inputsizesZ	bindparamZdialect_implZdialect_impl_clsZpositional_inputsizesr   r   Zkeyword_inputsizesr   )r  r2   set_input_sizes  sh    

"


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)Zbindr   )is_sequenceZfire_sequenceZis_callablecurrent_columnargZis_clause_elementr@   Z_arg_is_typedr   Ztype_coercerI   rJ   Z_execute_compiledZscalar)r0   columnr   r   ri   Zdefault_argr  r   r   r2   _exec_default  s    
z%DefaultExecutionContext._exec_defaultTc                s   y| 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   )rX   r   )indexrU   r   r2   r   l  s    zADefaultExecutionContext.get_current_parameters.<locals>.<genexpr>N)current_parametersr#  AttributeErrorr   r   r   r   rR   Z_has_multi_parametersZ_is_multiparam_columnr'  Zoriginalr   rU   keysro   )r0   Zisolate_multiinsert_groupsr%  r   r*  r   )r'  rU   r2   get_current_parameters8  s&    


z.DefaultExecutionContext.get_current_parametersc             C   s$   |j d krd S | ||j |jS d S )N)r   r&  r   )r0   r%  r   r   r2   get_insert_defaultr  s    
z*DefaultExecutionContext.get_insert_defaultc             C   s$   |j d krd S | ||j |jS d S )N)onupdater&  r   )r0   r%  r   r   r2   get_update_defaultx  s    
z*DefaultExecutionContext.get_update_defaultc             C   s
  | j jd }i }| j j}| j j}x&|D ]}|jr&|jjr&|jj||< q&W x&|D ]}|jrN|jjrN|jj||< qNW x| jD ]}|| _	x<|D ]4}||kr|| }n
| 
|}|d k	r||||< qW x<|D ]4}||kr|| }n
| |}|d k	r||||< qW qxW | `	d S )Nr   )r   r  r   r   r   	is_scalarr$  r-  r   r(  r,  r.  )r0   r  Zscalar_defaultsr   r   r  r   valr   r   r2   r   ~  s2    







z5DefaultExecutionContext._process_executemany_defaultsc             C   s   | j jd }| jd  | _}xN| j jD ]B}|jrJ|jjsJ|jjrJ|jj}n
| 	|}|d k	r&||||< q&W x.| j j
D ]"}| |}|d k	rv||||< qvW | `d S )Nr   r   )r   r  r   r(  r   r   r"  r/  r$  r,  r   r.  )r0   r  r   r  r0  r   r   r2   r     s    


z7DefaultExecutionContext._process_executesingle_defaults)NNN)T)>r   r   r   r   r   r   r   r   r   r   r   rR   r   r  r   r   Z_translate_colnamer   r   r   r   r   r   r   r   r   r   rz   r   r   r   r   r   r   r   r@   r   r  r   r  r  r  r  r	  r
  r  r  r9   r   r  r  r  r  r  r!  r&  r(  r+  r,  r.  r   r   r   r   r   r2   r   H  sl   !tl0	! 
'
W
:%r   )r   r-   r   r   r4    r   r   r   r   r   r	   r
   r   r`   r   Zsqlr   r   r   rJ   IUNICODEr   r  ZDialectr   r   ZExecutionContextr   Zexecution_ctx_clsr   r   r   r2   <module>   sB             x