U
    x^                 _   @   s  d 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 edddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZd[d\d]d^d_d`dadbdcdddedfdgdhdidjdkdlg^ZedmejZednejZdodp eddqD drgZedsejZedsejZedtejZeduejZedvejZdwdxdydzd{d|Zejd}ej d~ej!dej"dej#dej$dej%dej&dej'dej(dej)dej*dej+dej,dej-dej.dej/dej0dej1dej2dej3dej4dej5dej6dej7dej8dej9dej:dej;dej<dej=dej>dej?dej@dejAdejBdejCdi%ZDejEdejFdejGdejHdejIdejJdejKdejLdejMdejNdejOdejPdejQdejRdiZSddddddddddddddddZTe
jUjVde
jUjWde
jUjXde
jUjYde
jUjZde
jUj[diZ\G ddƄ de]Z^G ddȄ de_ej`e]ZaG ddʄ dejbZcG dd̄ de^ZdG dd΄ dedZeG ddЄ de^ZfG dd҄ deaZgG ddԄ degZhG ddք de]ZidS )a]  Base SQL and DDL compiler implementations.

Classes provided include:

:class:`.compiler.SQLCompiler` - renders SQL
strings

:class:`.compiler.DDLCompiler` - renders DDL
(data definition language) strings

:class:`.compiler.GenericTypeCompiler` - renders
type specification strings.

To generate user-defined SQL strings, see
:doc:`/ext/compiler`.

    N   )crud)elements)	functions)	operators)schema)
selectable)sqltypes)visitors   )exc)utilallZanalyseZanalyzeandanyarrayasZascZ
asymmetricauthorizationZbetweenbinaryZbothZcasecastcheckcollatecolumn
constraintcreateZcrosscurrent_dateZcurrent_rolecurrent_timecurrent_timestampcurrent_userdefault
deferrabledescZdistinctZdoelseendexceptfalseforZforeignfreezefromfullZgrantgroupZhavingZilikein	initiallyinnerZ	intersectZintoisZisnulljoinleadingleftlikelimit	localtimelocaltimestampZnaturalnewnotZnotnullnulloffoffsetoldononlyororderouteroverlapsZplacingZprimaryZ
referencesrightselectsession_usersetZsimilarZsome	symmetrictableZthentoZtrailingtrueunionuniqueuserZusingverbosewhenwherez^[A-Z0-9_$]+$z^[A-Z0-9_ $]+$c                 C   s   h | ]}t |qS  )str).0xrP   rP   ?/tmp/pip-install-dq5v43_d/SQLAlchemy/sqlalchemy/sql/compiler.py	<setcomp>   s     rU   
   $z5^(?:RESTRICT|CASCADE|SET NULL|NO ACTION|SET DEFAULT)$z^(?:DEFERRED|IMMEDIATE)$z%(?<![:\w\$\x5c]):([\w\$]+)(?![:\w\$])z\x5c(:[\w\$]*)(?![:\w\$])z%%(%(name)s)s?z%%sz:[_POSITION]z	:%(name)s)pyformatZqmarkformatnumericnamedz AND z OR z + z * z - z /  % -z < z <= z != z > z >= z = z IS DISTINCT FROM z IS NOT DISTINCT FROM z || z MATCH z NOT MATCH z IN z NOT IN , z FROM  AS z IS z IS NOT z	 COLLATE zEXISTS 	DISTINCT zNOT zANY zALL z DESCz ASCz NULLS FIRSTz NULLS LASTcoalesceZCURRENT_DATEZCURRENT_TIMEZCURRENT_TIMESTAMPZCURRENT_USERZ	LOCALTIMEZLOCALTIMESTAMPrandomsysdateZSESSION_USERUSERZCUBEZROLLUPzGROUPING SETSmonthdayyearsecondhourdoyminutequarterdowweekepochmillisecondsmicrosecondstimezone_hourtimezone_minute)rf   rg   rh   ri   rj   rk   rl   rm   rn   ro   rp   rq   rr   rs   rt   UNIONz	UNION ALLEXCEPTz
EXCEPT ALL	INTERSECTzINTERSECT ALLc                   @   s   e Zd ZdZdZe Zdde fddZe	dddd Z
d	d
 Zedd Zdd Zdd ZdddZedd Zdd Zdd ZdS )Compileda  Represent a compiled SQL or DDL expression.

    The ``__str__`` method of the ``Compiled`` object should produce
    the actual text of the statement.  ``Compiled`` objects are
    specific to their underlying database dialect, and also may
    or may not be specific to the columns referenced within a
    particular set of bind parameters.  In no case should the
    ``Compiled`` object be dependent on the actual values of those
    bind parameters, even though it may reference those values as
    defaults.
    Nc                 C   sb   || _ || _| j j| _|r(| j|| _|dk	r^|| _|j| _| jrL|j| _	| j
| jf|| _dS )aq  Construct a new :class:`.Compiled` object.

        :param dialect: :class:`.Dialect` to compile against.

        :param statement: :class:`.ClauseElement` to be compiled.

        :param bind: Optional Engine or Connection to compile this
          statement against.

        :param schema_translate_map: dictionary of schema names to be
         translated when forming the resultant SQL

         .. versionadded:: 1.1

         .. seealso::

            :ref:`schema_translating`

        :param compile_kwargs: additional kwargs that will be
         passed to the initial call to :meth:`.Compiled.process`.


        N)dialectbindZidentifier_preparerpreparer_with_schema_translate	statementZsupports_executioncan_execute_execution_optionsexecution_optionsprocessstring)selfry   r}   rz   schema_translate_mapcompile_kwargsrP   rP   rT   __init__  s     
zCompiled.__init__z0.7zThe :meth:`.Compiled.compile` method is deprecated and will be removed in a future release.   The :class:`.Compiled` object now runs its compilation within the constructor, and this method does nothing.c                 C   s   dS )zDProduce the internal string representation of this element.
        NrP   r   rP   rP   rT   compileA  s    
zCompiled.compilec                 C   s$   | j r|| ||S t| jd S N)r~   _execute_compiledr   ZObjectNotExecutableErrorr}   )r   
connectionmultiparamsparamsrP   rP   rT   _execute_on_connectionM  s    zCompiled._execute_on_connectionc                 C   s
   t  dS )zReturn a Compiled that is capable of processing SQL expressions.

        If this compiler is one, it would likely just return 'self'.

        NNotImplementedErrorr   rP   rP   rT   sql_compilerS  s    zCompiled.sql_compilerc                 K   s   |j | f|S r   _compiler_dispatch)r   objkwargsrP   rP   rT   r   ]  s    zCompiled.processc                 C   s
   | j pdS )z3Return the string text of the generated SQL or DDL. )r   r   rP   rP   rT   __str__`  s    zCompiled.__str__c                 C   s
   t  dS )zReturn the bind params for this compiled object.

        :param params: a dict of string/object pairs whose values will
                       override bind values compiled in to the
                       statement.
        Nr   r   r   rP   rP   rT   construct_paramse  s    zCompiled.construct_paramsc                 C   s   |   S )z0Return the bind params for this compiled object.r   r   rP   rP   rT   r   o  s    zCompiled.paramsc                 O   s*   | j }|dkrtjddd|| ||S )zExecute this compiled object.Nz>This Compiled object is not bound to any Engine or Connection.Z2aficode)rz   r   ZUnboundExecutionErrorr   )r   r   r   erP   rP   rT   executet  s    zCompiled.executec                 O   s   | j || S )zJExecute this compiled object and return the result's
        scalar value.)r   scalar)r   r   r   rP   rP   rT   r     s    zCompiled.scalar)N)__name__
__module____qualname____doc__Z_cached_metadatar   immutabledictr   r   
deprecatedr   r   propertyr   r   r   r   r   r   r   rP   rP   rP   rT   rx      s,   

/

	


rx   c                   @   s$   e Zd ZdZdZdd Zdd ZdS )TypeCompilerz2Produces DDL specification for TypeEngine objects.z	visit_\w+c                 C   s
   || _ d S r   )ry   )r   ry   rP   rP   rT   r     s    zTypeCompiler.__init__c                 K   s   |j | f|S r   r   r   type_kwrP   rP   rT   r     s    zTypeCompiler.processN)r   r   r   r   Zensure_kwargr   r   rP   rP   rP   rT   r     s   r   c                   @   sB   e Zd ZdZdZdZdddZedd Zed	d
 Z	dd Z
dS )_CompileLabelz;lightweight label object which acts as an expression.Label.label)elementnamerP   c                 C   s   || _ || _|f| | _d S r   )r   r   
_alt_names)r   colr   	alt_namesrP   rP   rT   r     s    z_CompileLabel.__init__c                 C   s   | j jS r   )r   	proxy_setr   rP   rP   rT   r     s    z_CompileLabel.proxy_setc                 C   s   | j jS r   )r   typer   rP   rP   rT   r     s    z_CompileLabel.typec                 K   s   | S r   rP   )r   r   rP   rP   rT   
self_group  s    z_CompileLabel.self_groupN)rP   )r   r   r   r   __visit_name__	__slots__r   r   r   r   r   rP   rP   rP   rT   r     s   


r   c                   @   s  e Zd ZdZeZeZd Z Z	Z
dZdZdZdZdZdZdZdZdZdZd ZZdddZedd	 Zejd
d Zejdd Zdd Z ej!dd Z"dd Z#edd Z$dddZ%edd Z&e'ddd Z(dd Z)ddd Z*dd!d"Z+dd#d$Z,d	d%d&Z-d'd( Z.d
d)d*Z/d+d, Z0d-d. Z1d/d0 Z2d1d2 Z3d3d4 Z4d5d6 Z5d7d8 Z6dd9d:Z7d;d< Z8d=d> Z9d?d@ Z:dAdB Z;dCdD Z<dEdF Z=dGdH Z>dIdJ Z?dKdL Z@dMdN ZAdOdP ZBdQdR ZCddSdTZDdUdV ZEdWdX ZFdYdZ ZGdd\d]ZHd^d_ ZId`da ZJdbdc ZKddde ZLdfdg ZMdhdi ZNdjdk ZOdldm ZPdndo ZQddpdqZRdrds ZSdtdu ZTdvdw ZUdxdy ZVdzd{ ZWdd|d}ZXd~d ZYdd ZZej!dd Z[dd Z\dd Z]dd Z^dd Z_dd Z`dd Zadd Zbdd Zcdd Zddd Zedd Zfdd ZgdddZhdd Zidd Zjdd Zkdd Zldd Zmdd ZndddZodddZpdddZqdd ZrdddZsdd Ztdd ZudddZvdd Zwdd Zxdd Zydd ZzddÄ Z{ddń Z|ddǄ Z}e~de fde fgZddd˄Zddd̈́Zddτ Zddф Zddӄ ZddՄ Zddׄ Zddل Zddۄ Zdd݄ Zdd߄ Zdd Zdd Zdd ZdddZdddZdd ZdddZdd Zdd Zdd ZdddZej!dd Zdd Zdd ZdddZdd Zd d Zdd ZdS (  SQLCompilerzsDefault implementation of :class:`.Compiled`.

    Compiles :class:`.ClauseElement` objects into SQL strings.

    FNTrP   c                 K   s   || _ |pt|dd| _i | _t | _g | _g | _|j	| _	| j	rTg | _
|jdk| _t|j | _d| _|jpp|j| _t| j| _i | _tj| ||f| | js| js| jr|jr|j| _| j	r| jr|   dS )a  Construct a new :class:`.SQLCompiler` object.

        :param dialect: :class:`.Dialect` to be used

        :param statement: :class:`.ClauseElement` to be compiled

        :param column_keys:  a list of column names to be compiled into an
         INSERT or UPDATE statement.

        :param inline: whether to generate INSERT statements as "inline", e.g.
         not formatted to return any generated defaults

        :param kwargs: additional keyword arguments to be consumed by the
         superclass.

        inlineFr[   N)column_keysgetattrr   bindsr   Zcolumn_dict
bind_namesstack_result_columns
positionalpositiontup
paramstyle_numeric_bindsBIND_TEMPLATESbindtemplatecteslabel_lengthmax_identifier_lengthZPopulateDict_process_anonanon_maptruncated_namesrx   r   isinsertisupdateisdelete
_returning	returning_apply_numbered_params)r   ry   r}   r   r   r   rP   rP   rT   r     s8    

zSQLCompiler.__init__c                 C   s   t | j| j S r   )listinsert_prefetchupdate_prefetchr   rP   rP   rT   prefetchE  s    zSQLCompiler.prefetchc                 C   s&   t  | _i | _d| _| jr"i | _dS )zInitialize collections related to CTEs only if
        a CTE is located, to save on the overhead of
        these collections otherwise.

        FN)r   OrderedDictr   ctes_by_namectes_recursiver   cte_positionalr   rP   rP   rT   _init_cte_stateI  s
    
zSQLCompiler._init_cte_statec              	   c   sp   | j | j }}g d | _ | _z0| jr8| jd }d|d< nd}| j | jfV  W 5 |r\|d || | _ | _X dS )z;special API to support the use case of 'nested result sets'Fneed_result_map_for_nestedTN)r   _ordered_columnspopr   )r   Zresult_columnsZordered_columnsentryrP   rP   rT   _nested_resultW  s    


zSQLCompiler._nested_resultc                    s(   t d td fdd| j| _d S )Nr   z\[_POSITION\]c                    s   t t S r   )rQ   r   nextmZposcountrP   rT   <lambda>r      z4SQLCompiler._apply_numbered_params.<locals>.<lambda>)	itertoolscountresubr   r   rP   r   rT   r   o  s    
 
 z"SQLCompiler._apply_numbered_paramsc                    s"   t dd  fdd jD D S )Nc                 s   s"   | ]\}}|d k	r||fV  qd S r   rP   )rR   keyvaluerP   rP   rT   	<genexpr>w  s   z/SQLCompiler._bind_processors.<locals>.<genexpr>c                 3   s&   | ]} j | |j jfV  qd S r   )r   r   Z_cached_bind_processorry   )rR   	bindparamr   rP   rT   r   y  s   )dictr   r   rP   r   rT   _bind_processorsu  s
    
zSQLCompiler._bind_processorsc                 C   s   t | jdkS Nr   )lenr   r   rP   rP   rT   is_subquery  s    zSQLCompiler.is_subqueryc                 C   s   | S r   rP   r   rP   rP   rT   r     s    zSQLCompiler.sql_compilerc                 C   s*  |ri }| j D ]}| j | }|j|kr6||j ||< q||krL|| ||< q|r|jr|rttjd|j|f ddqtjd|j ddq|jr|j||< q|j||< q|S i }| j D ]j}|r|jr|rtjd|j|f ddntjd|j dd|jr|j|| j | < q|j|| j | < q|S dS )z5return a dictionary of bind parameter keys and valuesz@A value is required for bind parameter %r, in parameter group %dZcd3xr   z)A value is required for bind parameter %rN)r   r   requiredr   ZInvalidRequestErrorcallableeffective_valuer   )r   r   Z_group_number_checkpdr   r   rP   rP   rT   r     s\    





zSQLCompiler.construct_paramsc                 C   s   | j ddS )zoReturn the bind param dictionary embedded into this
        compiled object, for those values that are present.F)r   r   r   rP   rP   rT   r     s    zSQLCompiler.paramszsqlalchemy.engine.resultc                 C   s   |j | jS )z(utility method used for unit tests only.)ZResultMetaData_create_result_mapr   )r   resultrP   rP   rT   r     s    zSQLCompiler._create_result_mapc                 C   s   dS )zCalled when a SELECT statement has no froms, and no FROM clause is
        to be appended.

        Gives Oracle a chance to tack on a ``FROM DUAL`` to the string output.

        r   rP   r   rP   rP   rT   default_from  s    zSQLCompiler.default_fromc                 K   s   d|j j| f| d S )N()r   r   )r   groupingasfromr   rP   rP   rT   visit_grouping  s    zSQLCompiler.visit_groupingc           
      K   s   | j rl| jjrl| j d d }|j\}}}|r2|}n|}|jj}	|	d k	rl|	j|krl|	||	j rl|jj|d< | j|jfd|i|S )Nr   r   render_label_as_labelwithin_columns_clause)	r   ry   Zsupports_simple_order_by_label_label_resolve_dictr   Z_order_by_label_elementr   Zshares_lineager   )
r   r   r  r   r   	with_cols
only_froms	only_colsZresolve_dictZorder_by_elemrP   rP   rT   visit_label_reference  s4    	z!SQLCompiler.visit_label_referencec           	      K   s   | j s| |jS | j d d }|j\}}}z|r>||j }n
||j }W n& tk
rp   t|jtj	d Y n X ||d< | j|fd|i|S d S )Nr   r   zECan't resolve label reference for ORDER BY / GROUP BY / DISTINCT etc.r  r  )
r   r   Z_text_clauser  r   KeyErrorr   Z_no_text_coercionr   CompileError)	r   r   r  r   r   r  r  r  r   rP   rP   rT   visit_textual_label_reference  s,    
z)SQLCompiler.visit_textual_label_referencec           
      K   s   |o| }||k}|s|r>t |jtjr8| d|j}	n|j}	|r|d k	rf||	|j||	f|j |j |jj| fddd|t	t
j  | j||	 S |r| j||	S |jj| fddi|S d S )NcolidentT)r  within_label_clauser  F)
isinstancer   r   _truncated_label_truncated_identifierr   r   r   r   	OPERATORSr   as_r{   format_label)
r   r   add_to_result_mapr  r  r  r   Zrender_label_with_asZrender_label_onlyZ	labelnamerP   rP   rT   visit_label  sJ    
zSQLCompiler.visit_labelc                 C   s   t dd S )Nz:Cannot compile Column object until its 'name' is assigned.r   r	  r   r   rP   rP   rT   _fallback_column_nameG  s    z!SQLCompiler._fallback_column_namec                 K   s   |j  }}|d kr| |}|j}|s>t|tjr>| d|}|d k	r^||||||jf|j |rn| 	|}n| j
|}|j}|d ks|r|js|S | j
|}	|	r| j
|	d }
nd}
|j }t|tjr| d|}|
| j
| d | S d S )Nr  .r   alias)r   r  
is_literalr  r   r  r  r   r   escape_literal_columnr{   quoterG   Znamed_with_columnschema_for_objectquote_schema)r   r   r  include_tabler   r   Z	orig_namer  rG   effective_schemaZschema_prefixZ	tablenamerP   rP   rT   visit_columnL  s8    

  
 zSQLCompiler.visit_columnc                 K   s   | j |jS r   )r{   format_collation	collation)r   r   r   rP   rP   rT   visit_collationt  s    zSQLCompiler.visit_collationc                 K   s   |j S r   r   )r   Z
fromclauser   rP   rP   rT   visit_fromclausew  s    zSQLCompiler.visit_fromclausec                 K   s   |j S r   r%  )r   indexr   rP   rP   rT   visit_indexz  s    zSQLCompiler.visit_indexc                 K   s   ||d< | j jj|jf|S )Ntype_expression)ry   type_compilerr   r   )r   
typeclauser   rP   rP   rT   visit_typeclause}  s    zSQLCompiler.visit_typeclausec                 C   s   | j jr|dd}|S N%%%r{   _double_percentsreplacer   textrP   rP   rT   post_process_text  s    zSQLCompiler.post_process_textc                 C   s   | j jr|dd}|S r-  r0  r3  rP   rP   rT   r    s    z!SQLCompiler.escape_literal_columnc              	      s<    fdd}j sd_tdd t|jS )Nc                    s:   |  d}|jkr(jj| f S j|f S d S r   )r*   Z_bindparamsr   bindparam_string)r   r   r   r   
textclauserP   rT   do_bindparam  s    

z2SQLCompiler.visit_textclause.<locals>.do_bindparamTc                 S   s
   |  dS r   )r*   r   rP   rP   rT   r     r   z.SQLCompiler.visit_textclause.<locals>.<lambda>)r   isplaintextBIND_PARAMS_ESCr   BIND_PARAMSr5  r4  )r   r8  r   r9  rP   r7  rT   visit_textclause  s     
zSQLCompiler.visit_textclausec                 K   s   | j  }|r| jn| j d }|p>|dkr4|ddp>|dd}|rp|j | _| _|jD ]}	| j|	d| jd qX| j|j	f|}
|r|rd|
 }
|
S )	Nr   r   need_result_map_for_compoundFr   Tr  r  (%s))
r   _default_stack_entrygetr   r   _textual_ordered_columnsZcolumn_argsr   _add_to_result_mapr   )r   Ztafcompound_indexr   parensr   toplevelr   populate_result_mapcr4  rP   rP   rT   visit_text_as_from  s,    

	
zSQLCompiler.visit_text_as_fromc                 K   s   dS )NZNULLrP   r   exprr   rP   rP   rT   
visit_null  s    zSQLCompiler.visit_nullc                 K   s   | j jrdS dS d S )NrI   1ry   supports_native_booleanrK  rP   rP   rT   
visit_true  s    zSQLCompiler.visit_truec                 K   s   | j jrdS dS d S )Nr%   0rO  rK  rP   rP   rT   visit_false  s    zSQLCompiler.visit_falsec                    s^   |j }|d krd}n
t|j  }|dd  fdd|jD D }|jrZjjrZd| }|S )N c                 s   s   | ]}|r|V  qd S r   rP   rR   srP   rP   rT   r     s   z/SQLCompiler.visit_clauselist.<locals>.<genexpr>c                 3   s   | ]}|j f V  qd S r   r   rR   rI  r   r   rP   rT   r     s    zVALUES )operatorr  r/   clausesZ_tuple_valuesry   Ztuple_in_values)r   Z
clauselistr   sepr4  rP   rX  rT   visit_clauselist  s    

zSQLCompiler.visit_clauselistc                 K   s   d}|j d k	r&||j j| f|d 7 }|jD ]4\}}|d|j| f| d |j| f| d 7 }q,|jd k	r|d|jj| f| d 7 }|d7 }|S )NzCASE rT  zWHEN z THEN zELSE ZEND)r   r   ZwhensZelse_)r   clauser   rS   Zcondr   rP   rP   rT   
visit_case  s*    

zSQLCompiler.visit_casec                 K   s   |j j| f|S r   )Ztyped_expressionr   )r   Ztype_coercer   rP   rP   rT   visit_type_coerce  s    zSQLCompiler.visit_type_coercec                 K   s$   d|j j| f||jj| f|f S )NzCAST(%s AS %s))r]  r   r+  )r   r   r   rP   rP   rT   
visit_cast  s    zSQLCompiler.visit_castc              	   K   s   d|d t jkrdn\|d t jkr&dnJ|d dk rTd| jt t|d f|f nd| jt |d f|f |d t jkrdn\|d t jkrdnJ|d dk rd| jt t|d f|f nd| jt |d f|f f S )	Nz	%s AND %sr   zUNBOUNDED PRECEDINGzCURRENT ROWz%s PRECEDINGz%s FOLLOWINGr   zUNBOUNDED FOLLOWING)r   ZRANGE_UNBOUNDEDZRANGE_CURRENTr   literalabs)r   range_r   rP   rP   rT   _format_frame_clause  s0    

z SQLCompiler._format_frame_clausec                    s   |j rdj|j f  }n |jr8dj|jf  }nd }d|jjf d fddd|jfd|jffD |r||gng  f S )	NzRANGE BETWEEN %szROWS BETWEEN %sz%s OVER (%s)rT  c                    s6   g | ].\}}|d k	rt |rd||jf f qS )Nz%s BY %s)r   r   )rR   wordr]  r   r   rP   rT   
<listcomp>!  s    z*SQLCompiler.visit_over.<locals>.<listcomp>Z	PARTITIONZORDER)rc  rd  Zrowsr   r   r/   Zpartition_byorder_by)r   Zoverr   rc  rP   rf  rT   
visit_over  s2    	zSQLCompiler.visit_overc                 K   s$   d|j j| f||jj| f|f S )Nz%s WITHIN GROUP (ORDER BY %s))r   r   rh  )r   Zwithingroupr   rP   rP   rT   visit_withingroup.  s    zSQLCompiler.visit_withingroupc                 K   s$   d|j j| f||jj| f|f S )Nz%s FILTER (WHERE %s))funcr   Z	criterion)r   Z
funcfilterr   rP   rP   rT   visit_funcfilter4  s    zSQLCompiler.visit_funcfilterc                 K   s*   | j |j|j}d||jj| f|f S )NzEXTRACT(%s FROM %s))extract_maprB  fieldrL  r   )r   extractr   rn  rP   rP   rT   visit_extract:  s
    zSQLCompiler.visit_extractc                    s   |d k	r||j |j d|j t d|j   d }|rB||f|S t|jd }|rd|jr|d7 }n*|j } j	|r j
|n|}|d }d fdd|jD |g d j|f|i S d S )NrP   zvisit_%s_funcz%(expr)sr  c                    s(   g | ] } j |r  j |n|qS rP   )r{   _requires_quotes_illegal_charsr  )rR   tokr   rP   rT   rg  V  s   
z.SQLCompiler.visit_function.<locals>.<listcomp>rL  )r   r   r   lower	FUNCTIONSrB  	__class__Z	_has_argsr{   rq  r  r/   Zpackagenamesfunction_argspec)r   rk  r  r   dispr   rP   r   rT   visit_functionA  s0    



zSQLCompiler.visit_functionc                 K   s   |  |jS r   )visit_sequencesequence)r   Z
next_valuer   rP   rP   rT   visit_next_value_funca  s    z!SQLCompiler.visit_next_value_funcc                 K   s   t d| jj d S )Nz2Dialect '%s' does not support sequence increments.r   ry   r   )r   rz  r   rP   rP   rT   ry  d  s
    zSQLCompiler.visit_sequencec                 K   s   |j j| f|S r   )Zclause_exprr   )r   rk  r   rP   rP   rT   rv  j  s    zSQLCompiler.function_argspecr   c                    s(  j  }|rjnj d }|p2|dko2|dd}j |d |d ||d j|j}	d|	 d  fd	d
t|jD }
|
j	|ft
f d i7 }
|
j|f7 }
|
|jd k	s|jd k	rj|fpd7 }
jr|r |
 }
j d  r |r d|
 d S |
S d S )Nr   r   r>  Fcorrelate_fromsasfrom_froms)r}  r~  r   r>  rT  c                 3   s,   | ]$\}}|j f d |dV  qdS )F)r   rF  rE  Nr   )rR   irI  r   r   r   rP   rT   r     s   z4SQLCompiler.visit_compound_select.<locals>.<genexpr>r   r   r   r   )r   rA  rB  appendcompound_keywordskeywordr/   	enumerateselectsgroup_by_clauser   order_by_clause_limit_clause_offset_clauselimit_clauser   _render_cte_clauser   )r   csr   rF  rE  r   rG  r   Zneed_result_mapr  r4  rP   r  rT   visit_compound_selectm  sB    
	 
z!SQLCompiler.visit_compound_selectc                 C   s(   d|j ||rd| ndf }t| |d S )Nzvisit_%s_%s%s_r   )r   r   )r   	operator_Z
qualifier1Z
qualifier2attrnamerP   rP   rT   _get_operator_dispatch  s    z"SQLCompiler._get_operator_dispatchc                 K   s   |j rR|jrtd| |j dd}|r:|||j f|S | j|t|j  f|S nL|jr| |jdd}|r||||jf|S | j|t|j f|S n
tdd S )NzFUnary expression does not support operator and modifier simultaneouslyunaryrY  modifierz,Unary expression has no operator or modifier)rY  r  r   r	  r   _generate_generic_unary_operatorr   _generate_generic_unary_modifier)r   r  r   rw  rP   rP   rT   visit_unary  sF          zSQLCompiler.visit_unaryc                 K   s6   |j s| jjr| j|jf|S d| j|jf| S d S )Nz%s = 1Z_is_implicitly_booleanry   rP  r   r   r   r   rY  r   rP   rP   rT   visit_istrue_unary_operator  s    z'SQLCompiler.visit_istrue_unary_operatorc                 K   s:   |j s| jjr"d| j|jf| S d| j|jf| S d S )NNOT %sz%s = 0r  r  rP   rP   rT   visit_isfalse_unary_operator  s    z(SQLCompiler.visit_isfalse_unary_operatorc                 K   s   d| j |tjd S )Nr  )override_operator)visit_binaryr   match_opr   r   rY  r   rP   rP   rT   visit_notmatch_op_binary  s     z$SQLCompiler.visit_notmatch_op_binaryc                 C   s   t d d S )NzThe IN-predicate was invoked with an empty sequence. This results in a contradiction, which nonetheless can be expensive to evaluate.  Consider alternative strategies for improved performance.)r   warnr   rP   rP   rT   _emit_empty_in_warning  s    z"SQLCompiler._emit_empty_in_warningc                 K   s2   | j jrdS | j jr|   | |j|jkS d S )Nz1 != 1ry   Z_use_static_inZ_warn_on_empty_inr  r   r1   r  rP   rP   rT   visit_empty_in_op_binary  s
    z$SQLCompiler.visit_empty_in_op_binaryc                 K   s2   | j jrdS | j jr|   | |j|jkS d S )Nz1 = 1r  r  rP   rP   rT   visit_empty_notin_op_binary  s
    z'SQLCompiler.visit_empty_notin_op_binaryc                 C   s   t d| jj d S )Nz3Dialect '%s' does not support empty set expression.r|  )r   Zelement_typesrP   rP   rT   visit_empty_set_expr  s
    z SQLCompiler.visit_empty_set_exprc                 K   s   | j r*t|jtjr*t|jtjr*d|d< |p2|j}| |dd }|rT|||f|S zt| }W n  t	k
r   t
| |Y nX | j||f|S d S )NTliteral_bindsr   )ansi_bind_rulesr  r1   r   ZBindParameterrB   rY  r  r  r  r   ZUnsupportedCompilationError_generate_generic_binary)r   r   r  eager_groupingr   r  rw  opstringrP   rP   rT   r    s     
zSQLCompiler.visit_binaryc                 K   s   | j |jf|S r   )r   Zsql_functionr  rP   rP   rT   &visit_function_as_comparison_op_binary  s    z2SQLCompiler.visit_function_as_comparison_op_binaryc                 K   sT   | j jr,| j|jf|d | j|jf| S | j|jf|d | j|jf| S d S )Nz %% r]   )r{   r1  r   r1   rB   r  rP   rP   rT   visit_mod_binary  s    zSQLCompiler.visit_mod_binaryc                 K   s$   |j |d< | j|d|j d f|S )Nr  rT  )r  r  r  r  rP   rP   rT   visit_custom_op_binary&  s    
 z"SQLCompiler.visit_custom_op_binaryc                 K   s   | j ||jd f|S NrT  )r  r  r  rP   rP   rT   visit_custom_op_unary_operator,  s     z*SQLCompiler.visit_custom_op_unary_operatorc                 K   s   | j |d|j f|S r  )r  r  r  rP   rP   rT   visit_custom_op_unary_modifier1  s     z*SQLCompiler.visit_custom_op_unary_modifierc                 K   s\   | dd}d|d< |jj| fd|i|| |jj| fd|i| }|rX|rXd| }|S )N
_in_binaryFTr  r@  )rB  r1   r   rB   )r   r   r  r  r   r  r4  rP   rP   rT   r  6  s.    
z$SQLCompiler._generate_generic_binaryc                 K   s   ||j j| f| S r   r   r   r  r  r   rP   rP   rT   r  K  s    z,SQLCompiler._generate_generic_unary_operatorc                 K   s   |j j| f|| S r   r   r  rP   rP   rT   r  N  s    z,SQLCompiler._generate_generic_unary_modifierc                 C   s   t jdtjdS )Nz'%')r   )r   Zliteral_columnr	   
STRINGTYPEr   rP   rP   rT   _like_percent_literalQ  s    z!SQLCompiler._like_percent_literalc                 K   s2   |  }| j}||j||_| j||f|S r   _cloner  __add__rB   visit_like_op_binaryr   r   rY  r   percentrP   rP   rT   visit_contains_op_binaryU  s    z$SQLCompiler.visit_contains_op_binaryc                 K   s2   |  }| j}||j||_| j||f|S r   r  r  r  rB   visit_notlike_op_binaryr  rP   rP   rT   visit_notcontains_op_binary[  s    z'SQLCompiler.visit_notcontains_op_binaryc                 K   s,   |  }| j}||j|_| j||f|S r   )r  r  __radd__rB   r  r  rP   rP   rT   visit_startswith_op_binarya  s    z&SQLCompiler.visit_startswith_op_binaryc                 K   s,   |  }| j}||j|_| j||f|S r   )r  r  r  rB   r  r  rP   rP   rT   visit_notstartswith_op_binaryg  s    z)SQLCompiler.visit_notstartswith_op_binaryc                 K   s,   |  }| j}||j|_| j||f|S r   r  r  rP   rP   rT   visit_endswith_op_binarym  s    z$SQLCompiler.visit_endswith_op_binaryc                 K   s,   |  }| j}||j|_| j||f|S r   r  r  rP   rP   rT   visit_notendswith_op_binarys  s    z'SQLCompiler.visit_notendswith_op_binaryc                 K   sL   |j dd }d|jj| f||jj| f|f |rFd| |tj nd S )Nescapez
%s LIKE %s ESCAPE r   	modifiersrB  r1   r   rB   render_literal_valuer	   r  r   r   rY  r   r  rP   rP   rT   r  y  s    z SQLCompiler.visit_like_op_binaryc                 K   sL   |j dd }d|jj| f||jj| f|f |rFd| |tj nd S )Nr  z%s NOT LIKE %sr  r   r  r  rP   rP   rT   r    s    z#SQLCompiler.visit_notlike_op_binaryc                 K   sL   |j dd }d|jj| f||jj| f|f |rFd| |tj nd S )Nr  zlower(%s) LIKE lower(%s)r  r   r  r  rP   rP   rT   visit_ilike_op_binary  s    z!SQLCompiler.visit_ilike_op_binaryc                 K   sL   |j dd }d|jj| f||jj| f|f |rFd| |tj nd S )Nr  zlower(%s) NOT LIKE lower(%s)r  r   r  r  rP   rP   rT   visit_notilike_op_binary  s    z$SQLCompiler.visit_notilike_op_binaryc                 K   s&   |j dd}| j||rdndf|S )NrF   Fz BETWEEN SYMMETRIC z	 BETWEEN r  rB  r  r   r   rY  r   rF   rP   rP   rT   visit_between_op_binary  s     
z#SQLCompiler.visit_between_op_binaryc                 K   s&   |j dd}| j||rdndf|S )NrF   Fz NOT BETWEEN SYMMETRIC z NOT BETWEEN r  r  rP   rP   rT   visit_notbetween_op_binary  s    
z&SQLCompiler.visit_notbetween_op_binaryc           
      K   s&  |s<|j | j}|jr<||}| j|fd||d|S |sJ|r| jr|jd krn|jd krnt	
d|j | j|fddi|S | |}|| jkr| j| }	|	|k	r|	js|jr|	j|jst	
d|j n"|	js|jrt	
d|j|jf | | j|j< | j|< | j|fd|ji|S )NT)skip_bind_expressionr  r  z@Bind parameter '%s' without a renderable value not allowed here.r  zIBind parameter '%s' conflicts with unique bind parameter of the same namezbindparam() name '%s' is reserved for automatic usage in the VALUES or SET clause of this insert/update statement.   Please use a name other than column name when using bindparam() with insert() or update() (for example, 'b_%s').	expanding)r   dialect_implry   Z_has_bind_expressionbind_expressionr   r  r   r   r   r	  r   render_literal_bindparam_truncate_bindparamr   rK   r   intersectionZ_is_crudr6  r  )
r   r   r  r  r  r   implr  r   existingrP   rP   rT   visit_bindparam  sp    	





zSQLCompiler.visit_bindparamc                 K   s   |j }| ||jS r   )r   r  r   )r   r   r   r   rP   rP   rT   r    s    z$SQLCompiler.render_literal_bindparamc                 C   s(   | | j}|r||S td| dS )a  Render the value of a bind parameter as a quoted literal.

        This is used for statement sections that do not accept bind parameters
        on the target driver/database.

        This should be implemented by subclasses using the quoting services
        of the DBAPI.

        z(Don't know how to literal-quote value %rN)Z_cached_literal_processorry   r   )r   r   r   	processorrP   rP   rT   r    s    z SQLCompiler.render_literal_valuec                 C   s@   || j kr| j | S |j}t|tjr2| d|}|| j |< |S )Nr   )r   r   r  r   r  r  )r   r   Z	bind_namerP   rP   rT   r  	  s    


zSQLCompiler._truncate_bindparamc                 C   s   ||f| j kr| j ||f S || j}t|| jd kr| j |d}|dt| jd d d t|dd   }|d | j |< n|}|| j ||f< |S )N   r   r   r  r   )r   Z	apply_mapr   r   r   rB  maxhex)r   Zident_classr   ZanonnamecounterZ	truncnamerP   rP   rT   r    s    z!SQLCompiler._truncated_identifierc                 C   s
   || j  S r   )r   r   r   rP   rP   rT   
_anonymize)  s    zSQLCompiler._anonymizec                 C   s<   | dd\}}| j|d}|d | j|< |d t| S )NrT  r   r  )splitr   rB  rQ   )r   r   identZderivedZanonymous_counterrP   rP   rT   r   ,  s    zSQLCompiler._process_anonc                 K   sJ   | j r&|d k	r|| n| j| |r8d| _d| S | jd|i S d S )NTz([EXPANDING_%s])r   )r   r  r   contains_expanding_parametersr   )r   r   positional_namesr  r   rP   rP   rT   r6  2  s    zSQLCompiler.bindparam_stringc                    s      ||d< t|jtjr. d|j}n|j}d}d}	| jkr j| }
||
k}	||
jksj||
krpd}n"|
|jkr j|
= nt	
d| |s|r|jd k	r|j}|jj}t|tjrԈ d|}n|}d }|rD| j|< d|jjkrd jkr jd|jjd i _| jkr2 j|f| |sD| jkrD|jrRd _ j||}|jrt|jtjr|j}n(t|jtjr|jjd }n
dstdd	 t|jD }|d
d fdd|D  7 } jrg  |d<  j |< |d|jj! fddi| 7 }|j"r:|d j#||j"f| 7 }| j|< |r|sd|	rd j||S |r j||} j$|r j%|}| &|7 }|S  j||S d S )Nvisiting_cter  TFz5Multiple, unrelated CTEs found with the same name: %rZ
autocommitr   c                 S   s   g | ]}|d k	r|qS r   rP   rW  rP   rP   rT   rg    s   z)SQLCompiler.visit_cte.<locals>.<listcomp>r@  r_   c                 3   s   | ]} j |V  qd S r   )r{   format_column)rR   r  r   rP   rT   r     s   z(SQLCompiler.visit_cte.<locals>.<genexpr>r  z AS 
r   rT  )'r   r  r   r   r  r  r   Z	_restatesr   r   r	  Z
_cte_aliasr   r   r   rJ   	visit_cte	recursiver   r{   format_aliasoriginalr   SelectCompoundSelectr  AssertionErrorr   Zunique_listinner_columnsr/   r   r   r   	_suffixes_generate_prefixes_requires_quotesr  get_render_as_alias_suffix)r   cter   ashint	fromhintsr  r   Zcte_nameZ
is_new_cteZembedded_in_current_named_cteZexisting_cteZpre_alias_cteZcte_pre_alias_namer4  Z
col_sourceZ
recur_colsrP   r   rT   r  @  s    	




 

 



 
zSQLCompiler.visit_ctec           	      K   s   |s|r,t |jtjr&| d|j}n|j}|r>| j||S |r|jj| fddi|| 	| j|| }|r||kr| 
|||| |}|S |jj| f|S d S )Nr  r   T)r  r   r   r  r  r{   r  r  r   r  format_from_hint_text)	r   r  r   r  iscrudr  r   Z
alias_nameretrP   rP   rT   visit_alias  s6    	   zSQLCompiler.visit_aliasc                 K   s   d|d< d| j |f| S )NTlateralz
LATERAL %s)r  )r   r  r   rP   rP   rT   visit_lateral  s    zSQLCompiler.visit_lateralc                 K   sR   d| j |fddi|| j| f|f }|jd k	rN|d|jj| f| 7 }|S )Nz%s TABLESAMPLE %sr   Tz REPEATABLE (%s))r  Z_get_methodr   seed)r   Ztablesampler   r   r4  rP   rP   rT   visit_tablesample  s    
zSQLCompiler.visit_tablesamplec                 C   s   d| S )Nr`   rP   )r   Zalias_name_textrP   rP   rT   r    s    z&SQLCompiler.get_render_as_alias_suffixc                 C   s   | j ||||f d S r   )r   r  )r   keynamer   objectsr   rP   rP   rT   rD    s    zSQLCompiler._add_to_result_mapc	                    s   j j}	|	jr@|s|r@|	 }
|r: fdd}qTd}n }
|rPj}nd}|s`|
}nt tjr|
 k	rt	|
 j
 jfd}n|
}n|dk	r|rt	|
| jfd}n|rt tjr js jdk	rt jtjst	|
t j
 jfd}nvt tjsFt tjr jrFt dr8t tjrFt	|
 j}n*|
 k	rlt	|
t j
 jfd}n|
}|j||d |jf|S )z.produce labeled columns present in a select().c                    s    | | f| | d S r   )rD  )r  r   r   r   r   r   rP   rT   r    s       z;SQLCompiler._label_select_column.<locals>.add_to_result_mapN)r   r   r?  )r   r  ry   Z_has_column_expressionZcolumn_expressionrD  r  r   ZLabelr   r   r   
_key_labelZColumnClauser  rG   r   r  Z_as_truncatedr   Z
TextClauseZUnaryExpressionZwraps_column_expressionhasattrr   ZFunctionZ
anon_labelupdater   )r   rC   r   rH  r   column_clause_argsr   r  need_column_expressionsr  Zcol_exprr  Zresult_exprrP   r  rT   _label_select_column  s    
    






z SQLCompiler._label_select_columnc                 C   s    |  ||}|r|d| 7 }|S r  )get_from_hint_text)r   sqltextrG   Zhintr  hinttextrP   rP   rT   r  M  s    z!SQLCompiler.format_from_hint_textc                 C   s   d S r   rP   )r   ZbyfromsrP   rP   rT   get_select_hint_textS  s    z SQLCompiler.get_select_hint_textc                 C   s   d S r   rP   r   rG   r4  rP   rP   rT   r  V  s    zSQLCompiler.get_from_hint_textc                 C   s   d S r   rP   r  rP   rP   rT   get_crud_hint_textY  s    zSQLCompiler.get_crud_hint_textc                 C   s
   d |S r  r/   )r   Z
hint_textsrP   rP   rT   get_statement_hint_text\  s    z#SQLCompiler.get_statement_hint_textc                    s"   i  i g fdd|S )zRewrite any "a JOIN (b JOIN c)" expression as
        "a JOIN (select * from b JOIN c) AS anon", to support
        databases that can't parse a parenthesized join correctly
        (i.e. sqlite < 3.7.16).

        c           
         s  | d krd |  S |  kr( |  S |    } | < |jrV|jrVt|jtjrV|  |jf||_|jf|}tj	|j
gdd }|jD ]}|j|_|j|_qtt|jj
j|j}|||j
j< |||j
j< |||jj
j< |||jj
j< td  D ]$\}}||kr
|| d |< q
d | ||_|jf||_n|jr|d|d< |jf di| nn|jr|jr|dd dk}	|	ri  d|d< |jf di| |	rd= n|jf di| |S )Nr   T)
use_labelsZselect_containerZtransform_cluecloneZinside_select)r  Zis_selectableZ_is_joinr  rB   r   ZFromGroupingZ_reset_exportedr1   r  r   r  rI  r   r  r   Z_labelr   zipr   itemsr  onclauseZ_is_from_containerZ_copy_internalsZ
_is_selectrB  r  )
r   r   ZnewelemrB   Zselectable_rI  Ztranslate_dictkvZbarrier_selectZclonedZcolumn_translatevisitrP   rT   r  i  s`     




z=SQLCompiler._transform_select_for_nested_joins.<locals>.visitrP   )r   rC   rP   r  rT   "_transform_select_for_nested_joins_  s    Qz.SQLCompiler._transform_select_for_nested_joinsc                    sF   t dd |jD t fdd|jD   fdd| jD | _d S )Nc                 s   s   | ]}|j |fV  qd S r   r  rW  rP   rP   rT   r     s    zESQLCompiler._transform_result_map_for_nested_joins.<locals>.<genexpr>c                 3   s   | ]} |j  |fV  qd S r   r  rW  )	inner_colrP   rT   r     s     c                    s2   g | ]*\}}}}||t  fd d|D |fqS )c                    s   g | ]}  ||qS rP   rB  )rR   r   drP   rT   rg    s     zQSQLCompiler._transform_result_map_for_nested_joins.<locals>.<listcomp>.<listcomp>tuple)rR   r   r   objstypr  rP   rT   rg    s   
zFSQLCompiler._transform_result_map_for_nested_joins.<locals>.<listcomp>)r   r  r   )r   rC   transformed_selectrP   )r  r  rT   &_transform_result_map_for_nested_joins  s    
z2SQLCompiler._transform_result_map_for_nested_joinsr}  r~  c           	      C   s\   | j  }|r| jn| j d }|d }|d }|rJ|sJ|j||dd}n|j||d}|S )Nr   r}  r~  rP   Zexplicit_correlate_fromsZimplicit_correlate_froms)r   rA  _get_display_froms
difference)	r   rC   r   r  rG  r   r}  r~  fromsrP   rP   rT   _display_froms_for_select  s     z%SQLCompiler._display_froms_for_selectc	                    sp  j o| oj ojj }
|
rJ}j|f |||dd|	}j }|r\jnjd }|p|ddp|dd |dkrdsd|	kr|	d= |
rr| |S 	| |}|	
 ddd	 d
}jr\}}|r||d 7 }nd }jr4|jjf|	7 }|jf|	7 }dd  fddjD D }r|d k	rttdd jD dd |jD fddjD _|||||	}jrfddjD }|r|d| 7 }jr|r | }jr>|djjf|	 7 }jd  sV|rh|rhd| d S |S d S )NT)r   rF  r  rE  nested_join_translationr   r>  Fr   r   r  )r  r  zSELECT rT  c                 S   s   g | ]}|d k	r|qS r   rP   rW  rP   rP   rT   rg  7  s   z,SQLCompiler.visit_select.<locals>.<listcomp>c                    s(   g | ] \}}j | |d qS ))r   r  r  )rR   r   r   )r   r  r  rH  rC   r   rP   rT   rg  9  s   
c                 S   s   g | ]\}}|qS rP   rP   rR   r   r   rP   rP   rT   rg  N  s     c                 S   s   g | ]\}}|qS rP   rP   r,  rP   rP   rT   rg  O  s   c                    s2   g | ]*\}}}}||t  fd d|D |fqS )c                 3   s   | ]}  ||V  qd S r   r  )rR   o	translaterP   rT   r   W  s     z6SQLCompiler.visit_select.<locals>.<listcomp>.<genexpr>r  )rR   r   r   r   r   r.  rP   rT   rg  V  s   
c                    s$   g | ]\}}|d  j jfkr|qS *ry   r   )rR   dialect_nameZhtr   rP   rT   rg  `  s   r   r   )r  r   ry   Zsupports_right_nested_joinsr  visit_selectrA  rB  r$  _setup_select_stackcopyr  _hints_setup_select_hints	_prefixesr  get_select_precolumnsZ_columns_plus_namesr   r  r   _compose_select_bodyZ_statement_hintsr  r   r  r  r   )r   rC   r   rF  r  rE  r*  Zselect_wraps_forr  r   Zneeds_nested_translationr#  r4  rG  r   r(  	hint_textbyfromr  Zper_dialectrP   )r   r  r  rH  rC   r   r/  rT   r4    s    


 


     
 zSQLCompiler.visit_selectc                    s.   t  fdd|j D } |}||fS )Nc              	      s>   g | ]6\\}}}|d  j jfkr||d|j ddi fqS )r1  r   T)r  ry   r   r   )rR   from_ry   r
  r   rP   rT   rg  y  s   
z3SQLCompiler._setup_select_hints.<locals>.<listcomp>)r   r7  r  r  )r   rC   r=  r<  rP   r   rT   r8  w  s    

zSQLCompiler._setup_select_hintsc                 C   sp   |d }|d }|r.|s.|j ||dd}n|j ||d}ttj| }||}	||	|d}
| j|
 |S )Nr}  r~  rP   r%  )r~  r}  r   )r&  r'  rE   r   _from_objectsrJ   r   r  )r   rC   r   r   r  r}  r~  r(  Znew_correlate_fromsZall_correlate_fromsZ	new_entryrP   rP   rT   r5    s*    
zSQLCompiler._setup_select_stackc                    sB  |d |7 }|rb|d7 }|jrB|d  fdd|D 7 }qn|d fdd|D 7 }n| 7 }|jd k	r|jjf}|r|d| 7 }|jjr|j|f7 }|jd k	r|jjf}|r|d| 7 }|j	jr|j
|f7 }|jd k	s|jd k	r |j|f7 }|jd k	r>|j|f7 }|S )Nr_   z 
FROM c                    s$   g | ]}|j fd  dqS )Tr   r  r   rR   fr=  r   r   rP   rT   rg    s    z4SQLCompiler._compose_select_body.<locals>.<listcomp>c                    s"   g | ]}|j fd di qS )r   Tr   rB  rf  rP   rT   rg    s   z 
WHERE z	 
HAVING )r/   r7  r   _whereclauser   _group_by_clauserZ  r  Z_having_order_by_clauser  r  r  r  Z_for_update_argfor_update_clause)r   r4  rC   r  r(  r=  r   trP   rD  rT   r;    sF    	

z SQLCompiler._compose_select_bodyc                    s*   d  fdd|D }|r&|d7 }|S )NrT  c                 3   s4   | ],\}}|d ks|j jkr|jf V  qd S r   r>  )rR   prefixr3  rX  rP   rT   r     s    z1SQLCompiler._generate_prefixes.<locals>.<genexpr>r  )r   stmtprefixesr   r]  rP   rX  rT   r    s    zSQLCompiler._generate_prefixesc                    sb    j r(t fdd jD g  j  _  jd }|ddd  j D 7 }|d7 }|S )Nc                    s   g | ]} j | qS rP   )r   )rR   r  r   rP   rT   rg    s     z2SQLCompiler._render_cte_clause.<locals>.<listcomp>rT  , 
c                 S   s   g | ]}|qS rP   rP   )rR   txtrP   rP   rT   rg    s     z
 )r   sumr   r   get_cte_preambler   r/   values)r   Zcte_textrP   r   rT   r    s    zSQLCompiler._render_cte_clausec                 C   s   |rdS dS d S )NzWITH RECURSIVEZWITHrP   )r   r  rP   rP   rT   rP    s    zSQLCompiler.get_cte_preamblec                 K   s   |j r
dpdS )zcCalled when building a ``SELECT`` statement, position is just
        before column list.

        ra   r   )Z	_distinctr   rC   r   rP   rP   rT   r:    s    z!SQLCompiler.get_select_precolumnsc                 K   s$   |j j| f|}|rd| S dS dS )z5allow dialects to customize how GROUP BY is rendered.z
 GROUP BY r   N)rF  r   )r   rC   r   Zgroup_byrP   rP   rT   r    s    zSQLCompiler.group_by_clausec                 K   s$   |j j| f|}|rd| S dS dS )z5allow dialects to customize how ORDER BY is rendered.z
 ORDER BY r   N)rG  r   )r   rC   r   rh  rP   rP   rT   r  	  s    zSQLCompiler.order_by_clausec                 K   s   dS )Nz FOR UPDATErP   rR  rP   rP   rT   rH  	  s    zSQLCompiler.for_update_clausec                 C   s   t dd S )Nz@RETURNING is not supported by this dialect's statement compiler.r  )r   rK  returning_colsrP   rP   rT   returning_clause	  s    zSQLCompiler.returning_clausec                 K   s^   d}|j d k	r&|d| j|j f| 7 }|jd k	rZ|j d krB|d7 }|d| j|jf| 7 }|S )Nr   z
 LIMIT z

 LIMIT -1z OFFSET )r  r   r  )r   rC   r   r4  rP   rP   rT   r  	  s    


zSQLCompiler.limit_clausec           
      K   sv   |s|rn| j |}|r<|r<| j |d | j |j }	n| j |j}	|rj||krj| |	||| |}	|	S dS d S )Nr  r   )r{   r  r  r  r   r  )
r   rG   r   r  r  r  
use_schemar   r   r  rP   rP   rT   visit_table 	  s&    

   zSQLCompiler.visit_tablec                 K   sd   |j rd}n|jrd}nd}|jj| fddi|| |jj| fddi| d |jj| f| S )Nz FULL OUTER JOIN z LEFT OUTER JOIN z JOIN r   Tz ON )r)   Zisouterr1   r   rB   r  )r   r/   r   r   Z	join_typerP   rP   rT   
visit_join=	  s    zSQLCompiler.visit_joinc                    sF   t  fdd|j D }|j|kr> ||j||j d}||fS )Nc                    s,   g | ]$\\}}}|d  j jfkr||fqS r0  r2  )rR   rG   ry   r<  r   rP   rT   rg  N	  s   
z1SQLCompiler._setup_crud_hints.<locals>.<listcomp>T)r   r7  r  rG   r  )r   rK  
table_textdialect_hintsrP   r   rT   _setup_crud_hintsL	  s    

   zSQLCompiler._setup_crud_hintsc                    s  | j  }| j t t |d tj| |tjf|}|sZ| jjsZ| jjsZt	
d| jj |jr| jjszt	
d| jj |d }n|}| j | jj}d}|jr|| j||jf|7 }|d7 } |j}	|jr| ||	\}
}	||	7 }|s|s|dd fd	d
|D  7 }| js"|jrN| || jp4|j}| jrR|d| 7 }nd }|jd k	r| j| jf|}| jr|r| jjr|d|  |f 7 }n|d| 7 }nh|s|r|d7 }nR|jr|dddd |D  7 }n,ddd
 |D }|d| 7 }|r|| _ |j!d k	r@| j|j!f|}|r@|d| 7 }|rZ| jsZ|d| 7 }| jr~|r~| jjs~|  | }| j "d |rd| d S |S d S )Nr}  r~  r   zWThe '%s' dialect with current database version settings does not support empty inserts.zcThe '%s' dialect with current database version settings does not support in-place multirow inserts.r   zINSERT zINTO z (%s)r_   c                    s   g | ]}  |d  qS )r   )r  rW  r{   rP   rT   rg  	  s     z,SQLCompiler.visit_insert.<locals>.<listcomp>rT  z %s%sz %sz DEFAULT VALUESz
 VALUES %sc                 s   s&   | ]}d d dd |D  V  qdS )r@  r_   c                 s   s   | ]}|d  V  qdS )r   NrP   rW  rP   rP   rT   r   	  s     z5SQLCompiler.visit_insert.<locals>.<genexpr>.<genexpr>Nr  )rR   Zcrud_param_setrP   rP   rT   r   	  s   z+SQLCompiler.visit_insert.<locals>.<genexpr>c                 S   s   g | ]}|d  qS )r   rP   rW  rP   rP   rT   rg  	  s     z VALUES (%s)r   r   r   )#r   r  rE   r   _setup_crud_paramsZISINSERTry   supports_default_valuesZsupports_empty_insertr   r	  r   Z_has_multi_parametersZsupports_multivalues_insertr{   r9  r  format_tablerG   r7  rZ  r/   r   r   rT  returning_precedes_valuesrC   r   Z_insert_from_selectr   Zcte_follows_insertr  insert_single_values_exprZ_post_values_clauser   )r   Zinsert_stmtr   r   rG  crud_paramsZcrud_params_singler^  r4  rX  r  rT  Zselect_textra  Zpost_values_clauserP   r\  rT   visit_insertZ	  s      
 
 

zSQLCompiler.visit_insertc                 C   s   dS )z3Provide a hook for MySQL to add LIMIT to the UPDATENrP   )r   update_stmtrP   rP   rT   update_limit_clause	  s    zSQLCompiler.update_limit_clausec                 K   s   d|d< |j | fddi|S )z|Provide a hook to override the initial table clause
        in an UPDATE statement.

        MySQL overrides this.

        Tr   r  r   )r   rd  
from_tableextra_fromsr   rP   rP   rT   update_tables_clause	  s    z SQLCompiler.update_tables_clausec                 K   s   t ddS )z~Provide a hook to override the generation of an
        UPDATE..FROM clause.

        MySQL and MSSQL override this.

        zCThis backend does not support multiple-table criteria within UPDATENr   r   rd  rf  rg  
from_hintsr   rP   rP   rT   update_from_clause	  s    	zSQLCompiler.update_from_clausec                    s(  j  }|j}t|}|rHtt|jfdd|D }|}ng }|jh}j |||d d}	|j	r|	j
||j	f|7 }	j||j|f|}
tj|tjf|}|jrȈ||
\}}
nd }|	|
7 }	|	d7 }	|oj |	d fdd|D 7 }	js|jr:jr:|	d	|jp2|j 7 }	|rhj||j||f|}|rh|	d	| 7 }	|jd k	rj|jf|}|r|	d
| 7 }	|}|r|	d	| 7 }	js|jrjs|	d	|jp|j 7 }	jr|r |	 }	j d |r d|	 d S |	S d S )Nc                    s   g | ]}| kr|qS rP   rP   rB  )
main_fromsrP   rT   rg  	  s     z,SQLCompiler.visit_update.<locals>.<listcomp>r[  zUPDATE z SET r_   c                 3   s,   | ]$}|d  j  dd |d  V  qdS )r   )r  =r   Nr   rW  )r  r   rP   rT   r   
  s   z+SQLCompiler.visit_update.<locals>.<genexpr>rT   WHERE r   r   r   )r   _extra_fromsboolrE   r   r@  rG   rJ   r  r9  r  rh  r   r]  ZISUPDATEr7  rZ  'render_table_with_column_in_update_fromr/   r   r   r`  rT  rk  rE  r   re  r   r  r   )r   rd  r   r   rG  rg  Zis_multitableZrender_extra_fromsr}  r4  rX  rb  rY  extra_from_textrI  r  rP   )r  rl  r   rT   visit_update	  s    
      

 
 zSQLCompiler.visit_updatec                 C   s   t | | jS r   )r   _key_getters_for_crud_columnr}   r   rP   rP   rT   rt  L
  s    z(SQLCompiler._key_getters_for_crud_columnc                 K   s   t ddS )zProvide a hook to override the generation of an
        DELETE..FROM clause.

        This can be used to implement DELETE..USING for example.

        MySQL and MSSQL override this.

        zCThis backend does not support multiple-table criteria within DELETENr   ri  rP   rP   rT   delete_extra_from_clauseP
  s    z$SQLCompiler.delete_extra_from_clausec                 C   s   |j | dddS )NT)r   r  r   )r   delete_stmtrf  rg  rP   rP   rT   delete_table_clause`
  s    zSQLCompiler.delete_table_clausec                 K   s  | j  }tj| |tjf| |j}|jh|}| j |||d d}|jrd|| j	||jf|7 }|d7 }| 
||j|}|jr| ||\}	}nd }	||7 }|jr| jr|d| ||j 7 }|r| j||j||	f|}
|
r|d|
 7 }|jd k	r|jj| f|}|r|d| 7 }|jr@| js@|d| ||j 7 }| jrZ|rZ|  | }| j d |rxd| d S |S d S )	Nr[  zDELETE FROM rT  rn  r   r   r   )r   r   r]  ZISDELETEro  rG   rJ   r  r9  r  rw  r7  rZ  r   r`  rT  ru  rE  r   r   r  r   )r   rv  r   r   rG  rg  r}  r4  rX  rY  rr  rI  rP   rP   rT   visit_deletec
  s~        
  zSQLCompiler.visit_deletec                 C   s   d| j | S )NzSAVEPOINT %sr{   format_savepointr   Zsavepoint_stmtrP   rP   rT   visit_savepoint
  s    zSQLCompiler.visit_savepointc                 C   s   d| j | S )NzROLLBACK TO SAVEPOINT %srz  r|  rP   rP   rT   visit_rollback_to_savepoint
  s    z'SQLCompiler.visit_rollback_to_savepointc                 C   s   d| j | S )NzRELEASE SAVEPOINT %srz  r|  rP   rP   rT   visit_release_savepoint
  s    z#SQLCompiler.visit_release_savepoint)NF)NNT)F)F)F)NFFN)NT)NFT)N)FTr   )NF)F)FFF)NF)FFNN)FFFN)F)NTF)F)FTNr   FNF)FFFNT)F)F)F)F)r   r   r   r   EXTRACT_MAPrm  COMPOUND_KEYWORDSr  r   r   r   r:  r   r`  rq  r  r  rC  r   r   ra  r   r   r   r   r   r   Zmemoized_instancemethodr   
contextlibcontextmanagerr   r   memoized_propertyr   r   r   r   r   dependenciesr   r   r   r  r
  r  r  r!  r$  r&  r(  r,  r5  r  r=  rJ  rM  rQ  rS  r\  r^  r_  r`  rd  ri  rj  rl  rp  rx  r{  ry  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  r  r  r  r  r  r  r  r  r  r   r6  r  r  r  r  r  rD  r  r  r  r  r  r  r  r$  r   	frozensetrA  r)  r4  r8  r5  r;  r  r  rP  r:  r  r  rH  rT  r  rV  rW  rZ  rc  re  rh  rk  rs  rt  ru  rw  ry  r}  r~  r  rP   rP   rP   rT   r     s  	  
H




8

	
#
   
1  
(    
      
3			  


  
=  
   
|   
!
  
\]      
 6
		    
p
f
Jr   c                   @   sP   e Zd Z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S )StrSQLCompileraw  A :class:`.SQLCompiler` subclass which allows a small selection
    of non-standard SQL features to render into a string value.

    The :class:`.StrSQLCompiler` is invoked whenever a Core expression
    element is directly stringified without calling upon the
    :meth:`.ClauseElement.compile` method.   It can render a limited set
    of non-standard SQL constructs to assist in basic stringification,
    however for more substantial custom or dialect-specific SQL constructs,
    it will be necessary to make use of :meth:`.ClauseElement.compile`
    directly.

    .. seealso::

        :ref:`faq_sql_expression_string`

    c                 C   s   dS )Nz<name unknown>rP   r  rP   rP   rT   r  
  s    z$StrSQLCompiler._fallback_column_namec                 K   s$   d| j |jf|| j |jf|f S )Nz%s[%s])r   r1   rB   r  rP   rP   rT   visit_getitem_binary
  s    z#StrSQLCompiler.visit_getitem_binaryc                 K   s   | j ||f|S r   r  r  rP   rP   rT   visit_json_getitem_op_binary
  s    z+StrSQLCompiler.visit_json_getitem_op_binaryc                 K   s   | j ||f|S r   r  r  rP   rP   rT   !visit_json_path_getitem_op_binary
  s    z0StrSQLCompiler.visit_json_path_getitem_op_binaryc                 K   s   d| j | S )Nz<next sequence value: %s>)r{   format_sequence)r   seqr   rP   rP   rT   ry  
  s    zStrSQLCompiler.visit_sequencec                    s&    fddt |D }dd| S )Nc              	      s   g | ]}  d |ddi qS )NTFr+  rW  r   rP   rT   rg  
  s   z3StrSQLCompiler.returning_clause.<locals>.<listcomp>z
RETURNING r_   )r   Z_select_iterablesr/   )r   rK  rS  columnsrP   r   rT   rT  
  s    
zStrSQLCompiler.returning_clausec                    s    dd  fdd|D  S )Nrx  r_   c                 3   s&   | ]}|j fd  dV  qdS TrA  Nr   rR   rI  rj  r   r   rP   rT   r   
  s   z4StrSQLCompiler.update_from_clause.<locals>.<genexpr>r  ri  rP   r  rT   rk  
  s    z!StrSQLCompiler.update_from_clausec                    s    dd  fdd|D  S )Nr_   c                 3   s&   | ]}|j fd  dV  qdS r  r   r  r  rP   rT   r   
  s   z:StrSQLCompiler.delete_extra_from_clause.<locals>.<genexpr>r  ri  rP   r  rT   ru  
  s    z'StrSQLCompiler.delete_extra_from_clauseN)r   r   r   r   r  r  r  r  ry  rT  rk  ru  rP   rP   rP   rT   r  
  s   r  c                   @   sJ  e Zd Zejdd Zejdd ZdNddZdd	 Zd
d Z	dd Z
dd ZdOddZdPddZdd Zdd Zdd ZdQddZdd ZdRd d!Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Z d<d= Z!d>d? Z"d@dA Z#dBdC Z$dDdE Z%dFdG Z&dHdI Z'dJdK Z(dLdM Z)dS )SDDLCompilerc                 C   s   | j | j d S r   )ry   Zstatement_compilerr   rP   rP   rT   r   
  s    zDDLCompiler.sql_compilerc                 C   s   | j jS r   )ry   r*  r   rP   rP   rT   r*  
  s    zDDLCompiler.type_compilerNc                 C   s   d S r   rP   r   rP   rP   rT   r     s    zDDLCompiler.construct_paramsc                 K   s   |j }t|jtjr| }| j}||j}t|dkrJ|d d }}n|d |d  }}|	d| |	d| |	d|
|j | j|j| S )Nr   r   r   r   rG   r   fullname)contextr  targetr   ZTabler6  r{   format_table_seqr   
setdefaultr_  r   r5  r}   )r   Zddlr   r  r{   pathrG   ZschrP   rP   rT   	visit_ddl  s    zDDLCompiler.visit_ddlc                 C   s   | j |j}d| S )NzCREATE SCHEMA )r{   format_schemar   )r   r   r   rP   rP   rT   visit_create_schema  s    zDDLCompiler.visit_create_schemac                 C   s(   | j |j}d| }|jr$|d7 }|S )NzDROP SCHEMA  CASCADE)r{   r  r   cascade)r   dropr   r4  rP   rP   rT   visit_drop_schema  s
    zDDLCompiler.visit_drop_schemac                 C   sN  |j }| j}d}|jr*|d|jd 7 }|d|| d 7 }| |}|rZ||d 7 }|d7 }d}d}|jD ]}|j }	zD| j||	jo| d}
|
d k	r||7 }d}|d	|
 7 }|	jrd
}W qp t	j
k
r } z.tt	
td|j|	j|jd f  W 5 d }~X Y qpX qp| j||jd}|r8||d	 | 7 }|d| | 7 }|S )Nz
CREATE rT  zTABLE r   
Ffirst_pkrM  	Tz (in table '%s', column '%s'): %sr   ) _include_foreign_key_constraintsz
)%s

)r   r{   r9  r/   r_  create_table_suffixr  r   primary_keyr   r	  r   Zraise_from_causeudescriptionr   argscreate_table_constraintsZinclude_foreign_key_constraintspost_create_table)r   r   rG   r{   r4  r  	separatorr  Zcreate_columnr   	processedZceconstrP   rP   rT   visit_create_table"  sR    

 
zDDLCompiler.visit_create_tableFc                    sL   |j }|jrd S  j||d}d fdd|jD }|rH|d| 7 }|S )Nr  rT  c                 3   s   | ]}  |V  qd S r   )r   rR   r   r   rP   rT   r   Z  s    z2DDLCompiler.visit_create_column.<locals>.<genexpr>)r   systemget_column_specificationr/   constraints)r   r   r  r   r4  r  rP   r   rT   visit_create_columnS  s    zDDLCompiler.visit_create_columnc                    st   g }j r|j  j}|d k	r0|| nt  | fddjD  ddd fdd|D D S )Nc                    s"   g | ]}|j k	r| kr|qS rP   )r  rW  )	omit_fkcsrG   rP   rT   rg  s  s   
 z8DDLCompiler.create_table_constraints.<locals>.<listcomp>z, 
	c                 s   s   | ]}|d k	r|V  qd S r   rP   )rR   prP   rP   rT   r   z  s   z7DDLCompiler.create_table_constraints.<locals>.<genexpr>c                 3   s@   | ]8}|j d ks|  r jjr.t|dds |V  qd S )NZ	use_alterF)Z_create_rulery   Zsupports_alterr   r   r  r   rP   rT   r   |  s   

)r  r  Zforeign_key_constraintsr'  rE   extendZ_sorted_constraintsr/   )r   rG   r  r  Zall_fkcsrP   )r  r   rG   rT   r  b  s"    

z$DDLCompiler.create_table_constraintsc                 C   s   d| j |j S )Nz
DROP TABLE r{   r_  r   r   r  rP   rP   rT   visit_drop_table  s    zDDLCompiler.visit_drop_tablec                 C   s   d| j |j S )Nz
DROP VIEW r  r  rP   rP   rT   visit_drop_view  s    zDDLCompiler.visit_drop_viewc                 C   s   |j d krtd|j d S )Nz,Index '%s' is not associated with any table.)rG   r   r	  r   r   r'  rP   rP   rT   _verify_index_table  s    
zDDLCompiler._verify_index_tableTc              	      s~   |j } |  j}d}|jr(|d7 }|jd kr<td|d j||d|j|j	|dd
 fdd	|jD f 7 }|S )
NzCREATE zUNIQUE z0CREATE INDEX requires that the index have a namezINDEX %s ON %s (%s)include_schemarU  r_   c                 3   s    | ]} j j|d ddV  qdS )FTr  r  N)r   r   )rR   rL  r   rP   rT   r     s     z1DDLCompiler.visit_create_index.<locals>.<genexpr>)r   r  r{   rK   r   r   r	  _prepared_index_namer_  rG   r/   Zexpressions)r   r   r  Zinclude_table_schemar'  r{   r4  rP   r   rT   visit_create_index  s*    

 zDDLCompiler.visit_create_indexc                 C   s,   |j }|jd krtdd| j|dd S )Nz.DROP INDEX requires that the index have a namez
DROP INDEX Tr  )r   r   r   r	  r  )r   r  r'  rP   rP   rT   visit_drop_index  s    
 zDDLCompiler.visit_drop_indexc                 C   sX   |j d k	r| j|j }nd }|r4|r4| j|}nd }| j|}|rT|d | }|S Nr  )rG   r{   r  r  format_index)r   r'  r  r   Zschema_nameZ
index_namerP   rP   rT   r    s    
z DDLCompiler._prepared_index_namec                 C   s    d| j |jj| |jf S )NzALTER TABLE %s ADD %s)r{   r_  r   rG   r   r   r   rP   rP   rT   visit_add_constraint  s    
z DDLCompiler.visit_add_constraintc                 C   s(   d| j |j| j|jjt f S )NzCOMMENT ON TABLE %s IS %s)r{   r_  r   r   r  commentr	   Stringr  rP   rP   rT   visit_set_table_comment  s     z#DDLCompiler.visit_set_table_commentc                 C   s   d| j |j S )NzCOMMENT ON TABLE %s IS NULLr  r  rP   rP   rT   visit_drop_table_comment  s    z$DDLCompiler.visit_drop_table_commentc                 C   s.   d| j j|jddd| j|jjt f S )NzCOMMENT ON COLUMN %s IS %sT)	use_tablerU  )r{   r  r   r   r  r  r	   r  r  rP   rP   rT   visit_set_column_comment  s       z$DDLCompiler.visit_set_column_commentc                 C   s   d| j j|jdd S )NzCOMMENT ON COLUMN %s IS NULLT)r  )r{   r  r   r  rP   rP   rT   visit_drop_column_comment  s     z%DDLCompiler.visit_drop_column_commentc                 C   s   d| j |j }|jjd k	r.|d|jj 7 }|jjd k	rJ|d|jj 7 }|jjd k	rf|d|jj 7 }|jjd k	r|d|jj 7 }|jjd k	r|d7 }|jjd k	r|d7 }|jj	d k	r|d|jj	 7 }|jj
d	kr|d
7 }|jjd k	r|d7 }|S )NzCREATE SEQUENCE %sz INCREMENT BY %dz START WITH %dz MINVALUE %dz MAXVALUE %dz NO MINVALUEz NO MAXVALUEz	 CACHE %dTz ORDERz CYCLE)r{   r  r   	incrementstartZminvalueZmaxvalueZ
nominvalueZ
nomaxvaluecacher?   cycle)r   r   r4  rP   rP   rT   visit_create_sequence  s,    z!DDLCompiler.visit_create_sequencec                 C   s   d| j |j S )NzDROP SEQUENCE %s)r{   r  r   r  rP   rP   rT   visit_drop_sequence  s    zDDLCompiler.visit_drop_sequencec                 C   s^   |j }|jd k	r| j|}nd }|d kr:td|j  d| j|j j||jrVdpXdf S )Nz<Can't emit DROP CONSTRAINT for constraint %r; it has no namez#ALTER TABLE %s DROP CONSTRAINT %s%sr  r   )	r   r   r{   format_constraintr   r	  r_  rG   r  )r   r  r   formatted_namerP   rP   rT   visit_drop_constraint
  s    
z!DDLCompiler.visit_drop_constraintc                 K   sr   | j |d | jjj|j|d }| |}|d k	rB|d| 7 }|jd k	r`|d| |j 7 }|jsn|d7 }|S )NrT  )r)  z	 DEFAULT z	 NOT NULL)	r{   r  ry   r*  r   r   get_column_default_stringZcomputedZnullable)r   r   r   Zcolspecr   rP   rP   rT   r    s"    
 

z$DDLCompiler.get_column_specificationc                 C   s   dS Nr   rP   r   rG   rP   rP   rT   r  /  s    zDDLCompiler.create_table_suffixc                 C   s   dS r  rP   r  rP   rP   rT   r  2  s    zDDLCompiler.post_create_tablec                 C   sP   t |jtjrHt |jjtjr2| j|jjt	j
S | jj|jjddS nd S d S )NT)r  )r  Zserver_defaultr   ZDefaultClauseargr   string_typesr   r  r	   r  r   r  rP   rP   rT   r  5  s      z%DDLCompiler.get_column_default_stringc                 C   s\   d}|j d k	r.| j|}|d k	r.|d| 7 }|d| jj|jddd 7 }|| |7 }|S Nr   CONSTRAINT %s z
CHECK (%s)FTr  r   r{   r  r   r   r	  define_constraint_deferrabilityr   r   r4  r  rP   rP   rT   visit_check_constraintB  s    

  
z"DDLCompiler.visit_check_constraintc                 C   s\   d}|j d k	r.| j|}|d k	r.|d| 7 }|d| jj|jddd 7 }|| |7 }|S r  r  r  rP   rP   rT   visit_column_check_constraintN  s    

  
z)DDLCompiler.visit_column_check_constraintc                    s   t |dkrdS d}|jd k	r> j|}|d k	r>|d| 7 }|d7 }|dd fdd|jrd|jn|jD  7 }| |7 }|S )	Nr   r   r  zPRIMARY KEY r@  r_   c                 3   s   | ]} j |jV  qd S r   r{   r  r   rW  r   rP   rT   r   c  s   z;DDLCompiler.visit_primary_key_constraint.<locals>.<genexpr>)	r   r   r{   r  r/   Z_implicit_generatedZcolumns_autoinc_firstr  r  r  rP   r   rT   visit_primary_key_constraintZ  s    
z(DDLCompiler.visit_primary_key_constraintc              	      s   | j  d}|jd k	r4| j |}|d k	r4|d| 7 }t|jd jj}|dd fdd|jD | || d fdd|jD f 7 }|| 	|7 }|| 
|7 }|| |7 }|S )	Nr   r  r   z"FOREIGN KEY(%s) REFERENCES %s (%s)r_   c                 3   s   | ]}  |jjV  qd S r   )r  parentr   rB  r\  rP   rT   r   w  s    z;DDLCompiler.visit_foreign_key_constraint.<locals>.<genexpr>c                 3   s   | ]}  |jjV  qd S r   )r  r   r   rB  r\  rP   rT   r   }  s    )r{   r   r  r   r   r   rG   r/   define_constraint_remote_tabledefine_constraint_matchdefine_constraint_cascadesr  )r   r   r4  r  Zremote_tablerP   r\  rT   visit_foreign_key_constraintn  s0    
  z(DDLCompiler.visit_foreign_key_constraintc                 C   s
   | |S )z=Format the remote table clause of a CREATE CONSTRAINT clause.)r_  )r   r   rG   r{   rP   rP   rT   r    s    z*DDLCompiler.define_constraint_remote_tablec                    sp   t |dkrdS d}|jd k	r> j|}|d k	r>|d| 7 }|dd fdd|D  7 }| |7 }|S )Nr   r   r  zUNIQUE (%s)r_   c                 3   s   | ]} j |jV  qd S r   r  rW  r   rP   rT   r     s     z6DDLCompiler.visit_unique_constraint.<locals>.<genexpr>)r   r   r{   r  r/   r  r  rP   r   rT   visit_unique_constraint  s    
z#DDLCompiler.visit_unique_constraintc                 C   sL   d}|j d k	r&|d| j|j t 7 }|jd k	rH|d| j|jt 7 }|S )Nr   z ON DELETE %sz ON UPDATE %s)Zondeleter{   validate_sql_phraseFK_ON_DELETEZonupdateFK_ON_UPDATEr   r   r4  rP   rP   rT   r    s    

 

 z&DDLCompiler.define_constraint_cascadesc                 C   sL   d}|j d k	r&|j r|d7 }n|d7 }|jd k	rH|d| j|jt 7 }|S )Nr   z DEFERRABLEz NOT DEFERRABLEz INITIALLY %s)r    r,   r{   r  FK_INITIALLYr  rP   rP   rT   r    s    



 z+DDLCompiler.define_constraint_deferrabilityc                 C   s    d}|j d k	r|d|j  7 }|S )Nr   z	 MATCH %s)matchr  rP   rP   rT   r    s    
z#DDLCompiler.define_constraint_matchc                 C   sB   d| j j|jddd }|jdkr,|d7 }n|jdkr>|d7 }|S )NzGENERATED ALWAYS AS (%s)FTr  z STOREDz VIRTUAL)r   r   r	  Z	persisted)r   	generatedr4  rP   rP   rT   visit_computed_column  s      


z!DDLCompiler.visit_computed_column)N)F)N)FT)F)*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  r  r  r  r  r  r  r  r  r  r  rP   rP   rP   rT   r  
  sT   


1
 
)   


r  c                   @   s\  e Z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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'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAdB Z#dCdD Z$dEdF Z%dGdH Z&dIdJ Z'dKdL Z(dMdN Z)dOdP Z*dQdR Z+dSdT Z,dUS )VGenericTypeCompilerc                 K   s   dS )NFLOATrP   r   rP   rP   rT   visit_FLOAT  s    zGenericTypeCompiler.visit_FLOATc                 K   s   dS )NZREALrP   r   rP   rP   rT   
visit_REAL  s    zGenericTypeCompiler.visit_REALc                 K   s<   |j d krdS |jd kr&dd|j i S d|j |jd S d S )NZNUMERICzNUMERIC(%(precision)s)	precisionz!NUMERIC(%(precision)s, %(scale)s)r  Zscaler  r   rP   rP   rT   visit_NUMERIC  s    

z!GenericTypeCompiler.visit_NUMERICc                 K   s<   |j d krdS |jd kr&dd|j i S d|j |jd S d S )NZDECIMALzDECIMAL(%(precision)s)r  z!DECIMAL(%(precision)s, %(scale)s)r  r  r   rP   rP   rT   visit_DECIMAL  s    

z!GenericTypeCompiler.visit_DECIMALc                 K   s   dS )NZINTEGERrP   r   rP   rP   rT   visit_INTEGER  s    z!GenericTypeCompiler.visit_INTEGERc                 K   s   dS )NZSMALLINTrP   r   rP   rP   rT   visit_SMALLINT  s    z"GenericTypeCompiler.visit_SMALLINTc                 K   s   dS )NZBIGINTrP   r   rP   rP   rT   visit_BIGINT  s    z GenericTypeCompiler.visit_BIGINTc                 K   s   dS )N	TIMESTAMPrP   r   rP   rP   rT   visit_TIMESTAMP  s    z#GenericTypeCompiler.visit_TIMESTAMPc                 K   s   dS )NZDATETIMErP   r   rP   rP   rT   visit_DATETIME  s    z"GenericTypeCompiler.visit_DATETIMEc                 K   s   dS )NZDATErP   r   rP   rP   rT   
visit_DATE  s    zGenericTypeCompiler.visit_DATEc                 K   s   dS )NZTIMErP   r   rP   rP   rT   
visit_TIME  s    zGenericTypeCompiler.visit_TIMEc                 K   s   dS )NZCLOBrP   r   rP   rP   rT   
visit_CLOB  s    zGenericTypeCompiler.visit_CLOBc                 K   s   dS )NZNCLOBrP   r   rP   rP   rT   visit_NCLOB  s    zGenericTypeCompiler.visit_NCLOBc                 C   s0   |}|j r|d|j  7 }|jr,|d|j 7 }|S )N(%d)z COLLATE "%s")lengthr#  )r   r   r   r4  rP   rP   rT   _render_string_type  s    z'GenericTypeCompiler._render_string_typec                 K   s   |  |dS )NZCHARr	  r   rP   rP   rT   
visit_CHAR  s    zGenericTypeCompiler.visit_CHARc                 K   s   |  |dS )NZNCHARr
  r   rP   rP   rT   visit_NCHAR  s    zGenericTypeCompiler.visit_NCHARc                 K   s   |  |dS )NZVARCHARr
  r   rP   rP   rT   visit_VARCHAR
  s    z!GenericTypeCompiler.visit_VARCHARc                 K   s   |  |dS )NZNVARCHARr
  r   rP   rP   rT   visit_NVARCHAR  s    z"GenericTypeCompiler.visit_NVARCHARc                 K   s   |  |dS )NZTEXTr
  r   rP   rP   rT   
visit_TEXT  s    zGenericTypeCompiler.visit_TEXTc                 K   s   dS )NZBLOBrP   r   rP   rP   rT   
visit_BLOB  s    zGenericTypeCompiler.visit_BLOBc                 K   s   d|j rd|j  pd S )NBINARYr  r   r  r   rP   rP   rT   visit_BINARY  s    z GenericTypeCompiler.visit_BINARYc                 K   s   d|j rd|j  pd S )NZ	VARBINARYr  r   r  r   rP   rP   rT   visit_VARBINARY  s    z#GenericTypeCompiler.visit_VARBINARYc                 K   s   dS )NZBOOLEANrP   r   rP   rP   rT   visit_BOOLEAN  s    z!GenericTypeCompiler.visit_BOOLEANc                 K   s   | j |f|S r   )r  r   rP   rP   rT   visit_large_binary  s    z&GenericTypeCompiler.visit_large_binaryc                 K   s   | j |f|S r   )r  r   rP   rP   rT   visit_boolean"  s    z!GenericTypeCompiler.visit_booleanc                 K   s   | j |f|S r   )r  r   rP   rP   rT   
visit_time%  s    zGenericTypeCompiler.visit_timec                 K   s   | j |f|S r   )r  r   rP   rP   rT   visit_datetime(  s    z"GenericTypeCompiler.visit_datetimec                 K   s   | j |f|S r   )r  r   rP   rP   rT   
visit_date+  s    zGenericTypeCompiler.visit_datec                 K   s   | j |f|S r   )r  r   rP   rP   rT   visit_big_integer.  s    z%GenericTypeCompiler.visit_big_integerc                 K   s   | j |f|S r   )r  r   rP   rP   rT   visit_small_integer1  s    z'GenericTypeCompiler.visit_small_integerc                 K   s   | j |f|S r   )r  r   rP   rP   rT   visit_integer4  s    z!GenericTypeCompiler.visit_integerc                 K   s   | j |f|S r   )r  r   rP   rP   rT   
visit_real7  s    zGenericTypeCompiler.visit_realc                 K   s   | j |f|S r   )r  r   rP   rP   rT   visit_float:  s    zGenericTypeCompiler.visit_floatc                 K   s   | j |f|S r   )r  r   rP   rP   rT   visit_numeric=  s    z!GenericTypeCompiler.visit_numericc                 K   s   | j |f|S r   r  r   rP   rP   rT   visit_string@  s    z GenericTypeCompiler.visit_stringc                 K   s   | j |f|S r   r!  r   rP   rP   rT   visit_unicodeC  s    z!GenericTypeCompiler.visit_unicodec                 K   s   | j |f|S r   r  r   rP   rP   rT   
visit_textF  s    zGenericTypeCompiler.visit_textc                 K   s   | j |f|S r   r$  r   rP   rP   rT   visit_unicode_textI  s    z&GenericTypeCompiler.visit_unicode_textc                 K   s   | j |f|S r   r!  r   rP   rP   rT   
visit_enumL  s    zGenericTypeCompiler.visit_enumc                 K   s   t d| d S )NzKCan't generate DDL for %r; did you forget to specify a type on this Column?r  r   rP   rP   rT   rM  O  s
    zGenericTypeCompiler.visit_nullc                 K   s   | j || jf|S r   )r   Ztype_enginery   r   rP   rP   rT   visit_type_decoratorV  s    z(GenericTypeCompiler.visit_type_decoratorc                 K   s   |j f |S r   )Zget_col_specr   rP   rP   rT   visit_user_definedY  s    z&GenericTypeCompiler.visit_user_definedN)-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  r  r  r  r  r   r"  r#  r%  r&  r'  rM  r(  r)  rP   rP   rP   rT   r    sT   	r  c                   @   s   e Zd Zdd Zdd ZdS )StrSQLTypeCompilerc                 C   s   | dr| jS t|d S )NZvisit_)
startswith_visit_unknownAttributeError)r   r   rP   rP   rT   __getattr__^  s    
zStrSQLTypeCompiler.__getattr__c                 K   s   d|j j S )Nz%s)ru  r   r   rP   rP   rT   r,  d  s    z!StrSQLTypeCompiler._visit_unknownN)r   r   r   r.  r,  rP   rP   rP   rT   r*  ]  s   r*  c                   @   s  e Zd ZdZeZeZeZ	e
dZd5ddZdd	 Zd
d Zdd Zdd Zdd Zdd Zdd Zd6ddZd7ddZdd Zd8ddZd9ddZd:d d!Zd;d"d#Zed$d%d& Zd'd( Zd<d)d*Z d+d, Z!d=d-d.Z"d>d/d0Z#ej$d1d2 Z%d3d4 Z&dS )?IdentifierPreparerz@Handle quoting and case-folding of identifiers based on options.N"TFc                 C   sN   || _ || _|p| j| _|| _| jd | _|| _|| _i | _| j jdk| _	dS )as  Construct a new ``IdentifierPreparer`` object.

        initial_quote
          Character that begins a delimited identifier.

        final_quote
          Character that ends a delimited identifier. Defaults to
          `initial_quote`.

        omit_schema
          Prevent prepending schema name. Useful for databases that do
          not support schemae.
        r   )rZ   rY   N)
ry   initial_quotefinal_quoteescape_quoteescape_to_quoteomit_schemaquote_case_sensitive_collations_stringsr   r1  )r   ry   r1  r2  r3  r6  r5  rP   rP   rT   r   t  s    zIdentifierPreparer.__init__c                 C   s,   | j | j }|j| j t||_|S r   )ru  __new____dict__r  r   _schema_getterr  )r   r   preprP   rP   rT   r|     s    z)IdentifierPreparer._with_schema_translatec                 C   s&   | | j| j}| jr"| dd}|S )zEscape an identifier.

        Subclasses should override this to provide database-dependent
        escaping behavior.
        r.  r/  )r2  r3  r4  r1  r   r   rP   rP   rT   _escape_identifier  s    z%IdentifierPreparer._escape_identifierc                 C   s   | | j| jS )zCanonicalize an escaped identifier.

        Subclasses should override this to provide database-dependent
        unescaping behavior that reverses _escape_identifier.
        )r2  r4  r3  r<  rP   rP   rT   _unescape_identifier  s    z'IdentifierPreparer._unescape_identifierc                 C   s*   |dk	r&| |s&td||jf |S )zkeyword sequence filter.

        a filter for elements that are intended to represent keyword sequences,
        such as "INITIALLY", "INITIALLY DEFERRED", etc.   no special characters
        should be present.

        .. versionadded:: 1.3

        Nz/Unexpected SQL phrase: %r (matching against %r))r  r   r	  pattern)r   r   regrP   rP   rT   r    s    z&IdentifierPreparer.validate_sql_phrasec                 C   s   | j | | | j S )z~Quote an identifier.

        Subclasses should override this to provide database-dependent
        quoting behavior.
        )r1  r=  r2  r<  rP   rP   rT   quote_identifier  s    z#IdentifierPreparer.quote_identifierc                 C   s<   |  }|| jkp:|d | jkp:| jt| p:||kS )z5Return True if the given identifier requires quoting.r   )rs  reserved_wordsillegal_initial_characterslegal_charactersr  r   	text_type)r   r   Zlc_valuerP   rP   rT   r    s    
z#IdentifierPreparer._requires_quotesc                 C   s   | j t| S )zjReturn True if the given identifier requires quoting, but
        not taking case convention into account.)rD  r  r   rE  r<  rP   rP   rT   rq    s    z1IdentifierPreparer._requires_quotes_illegal_charsc                 C   s   |dk	rt d | |S )a  Conditionally quote a schema name.


        The name is quoted if it is a reserved word, contains quote-necessary
        characters, or is an instance of :class:`.quoted_name` which includes
        ``quote`` set to ``True``.

        Subclasses can override this to provide database-dependent
        quoting behavior for schema names.

        :param schema: string schema name
        :param force: unused

            .. deprecated:: 0.9

                The :paramref:`.IdentifierPreparer.quote_schema.force`
                parameter is deprecated and will be removed in a future
                release.  This flag has no effect on the behavior of the
                :meth:`.IdentifierPreparer.quote` method; please refer to
                :class:`.quoted_name`.

        NzThe IdentifierPreparer.quote_schema.force parameter is deprecated and will be removed in a future release.  This flag has no effect on the behavior of the IdentifierPreparer.quote method; please refer to quoted_name().)r   warn_deprecatedr  )r   r   forcerP   rP   rT   r    s
    zIdentifierPreparer.quote_schemac                 C   s   |dk	rt d t|dd}|dkrl|| jkr:| j| S | |rV| || j|< n
|| j|< | j| S n|rz| |S |S dS )a  Conditionally quote an identfier.

        The identifier is quoted if it is a reserved word, contains
        quote-necessary characters, or is an instance of
        :class:`.quoted_name` which includes ``quote`` set to ``True``.

        Subclasses can override this to provide database-dependent
        quoting behavior for identifier names.

        :param ident: string identifier
        :param force: unused

            .. deprecated:: 0.9

                The :paramref:`.IdentifierPreparer.quote.force`
                parameter is deprecated and will be removed in a future
                release.  This flag has no effect on the behavior of the
                :meth:`.IdentifierPreparer.quote` method; please refer to
                :class:`.quoted_name`.

        NzThe IdentifierPreparer.quote.force parameter is deprecated and will be removed in a future release.  This flag has no effect on the behavior of the IdentifierPreparer.quote method; please refer to quoted_name().r  )r   rF  r   r7  r  rA  )r   r  rG  rP   rP   rT   r    s    




zIdentifierPreparer.quotec                 C   s   | j r| |S |S d S r   )r6  r  )r   Zcollation_namerP   rP   rT   r"  8  s    
z#IdentifierPreparer.format_collationc                 C   s>   |  |j}| |}| js:|r:|d k	r:| |d | }|S r  )r  r   r  r5  r  )r   rz  rU  r   r   rP   rP   rT   r  >  s    
z"IdentifierPreparer.format_sequencec                 C   s   |  |p|jS r   r  r   )r   r   r   rP   rP   rT   r  K  s    zIdentifierPreparer.format_labelc                 C   s   |  |p|jS r   rH  )r   r  r   rP   rP   rT   r  N  s    zIdentifierPreparer.format_aliasc                 C   s"   |p|j }| |r| |}|S r   )r  r  rA  )r   Z	savepointr   r  rP   rP   rT   r{  Q  s    


z#IdentifierPreparer.format_savepointzsqlalchemy.sql.namingc                 C   s   t |jtjr>|||j}|d krDt |jtjr6d S |j}n|j}t |tjr|jdkrl| j	j
ph| j	j}n| j	j}t||kr|d|d  d t|dd   }n| j	| | |S )Nr'  r      r  )r  r   r   Z_defer_nameZ_constraint_name_for_tablerG   Z_defer_none_namer  r   ry   Zmax_index_name_lengthr   r   r   Zmd5_hexZvalidate_identifierr  )r   Znamingr   r   Zmax_rP   rP   rT   r  Z  s(     
(z$IdentifierPreparer.format_constraintc                 C   s
   |  |S r   )r  r  rP   rP   rT   r  x  s    zIdentifierPreparer.format_indexc                 C   sF   |dkr|j }| |}| |}| jsB|rB|rB| |d | }|S )z'Prepare a quoted table and schema name.Nr  )r   r  r  r5  r  )r   rG   rU  r   r   r   rP   rP   rT   r_  {  s    

zIdentifierPreparer.format_tablec                 C   s
   |  |S )zPrepare a quoted schema name.)r  r  rP   rP   rT   r    s    z IdentifierPreparer.format_schemac                 C   sp   |dkr|j }t|ddsJ|r>| j|j||dd | | S | |S n"|rh| j|j||dd | S |S dS )zPrepare a quoted column name.Nr  F)rU  r   r  )r   r   r_  rG   r  )r   r   r  r   Z
table_namerU  rP   rP   rT   r    s6    
    z IdentifierPreparer.format_columnc                 C   sD   |  |}| js0|r0|r0| || j|ddfS | j|ddfS dS )z(Format table name and schema as a tuple.Fr  N)r  r5  r  r_  )r   rG   rU  r   rP   rP   rT   r    s    
z#IdentifierPreparer.format_table_seqc                 C   s@   dd | j | j| | jfD \}}}td|||d }|S )Nc                 S   s   g | ]}t |qS rP   )r   r  rU  rP   rP   rT   rg    s   z5IdentifierPreparer._r_identifiers.<locals>.<listcomp>zM(?:(?:%(initial)s((?:%(escaped)s|[^%(final)s])+)%(final)s|([^\.]+))(?=\.|$))+)initialfinalescaped)r1  r2  r=  r   r   )r   rK  rL  Zescaped_finalrrP   rP   rT   _r_identifiers  s    

z!IdentifierPreparer._r_identifiersc                    s(    j } fdddd ||D D S )z:Unpack 'schema.table.column'-like strings into components.c                    s   g | ]}  |qS rP   )r>  )rR   r  r   rP   rT   rg    s   z;IdentifierPreparer.unformat_identifiers.<locals>.<listcomp>c                 S   s   g | ]\}}|p|qS rP   rP   )rR   abrP   rP   rT   rg    s     )rO  findall)r   ZidentifiersrN  rP   r   rT   unformat_identifiers  s    
z'IdentifierPreparer.unformat_identifiers)r0  Nr0  TF)N)N)T)N)N)N)TN)FNNF)T)'r   r   r   r   RESERVED_WORDSrB  LEGAL_CHARACTERSrD  ILLEGAL_INITIAL_CHARACTERSrC  r   r:  r  r   r|   r=  r>  r  rA  r  rq  r  r  r"  r  r  r  r{  r   r  r  r  r_  r  r  r  r  rO  rS  rP   rP   rP   rT   r/  h  sN   
     
$	

%
2



	

    
&

r/  )jr   r  r   r   r   r   r   r   r   r   r   r	   r
   r   r   rE   rT  r   IrU  ZLEGAL_CHARACTERS_PLUS_SPACErangerJ   rV  r  r  r  UNICODEr<  r;  r   and_or_addmulr   divmodtruedivnegltlenegtgeeqZis_distinct_fromZisnot_distinct_fromZ	concat_opr  Znotmatch_opZin_opZnotin_opZcomma_opr?  r  is_Zisnotr   existsZdistinct_opinvZany_opZall_opZdesc_opZasc_opZnullsfirst_opZnullslast_opr  rb   r   r   r   r   r4   r5   rc   rd   rD   rL   ZcubeZrollupZgrouping_setsrt  r  r  ru   Z	UNION_ALLrv   Z
EXCEPT_ALLrw   ZINTERSECT_ALLr  objectrx   with_metaclassZEnsureKWArgTyper   Z	Visitabler   r   r  r  r  r*  r/  rP   rP   rP   rT   <module>   sN  c                                       ,                    
                   "=   N 