B
    \%                 @   s   d 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 i ZG dd deZG dd deZG dd deZe  G dd deZe  G dd deZe  dS )aq  Strategies for creating new instances of Engine types.

These are semi-private implementation classes which provide the
underlying behavior for the "strategy" keyword argument available on
:func:`~sqlalchemy.engine.create_engine`.  Current available options are
``plain``, ``threadlocal``, and ``mock``.

New strategies can be added via new ``EngineStrategy`` classes.
    )
attrgetter   )base)threadlocal)url   )event)pool)util)schemac               @   s    e Zd ZdZdd Zdd ZdS )EngineStrategyzAn adaptor that processes input arguments and produces an Engine.

    Provides a ``create`` method that receives input arguments and
    produces an instance of base.Engine or a subclass.

    c             C   s   | t | j< d S )N)
strategiesname)self r   ;lib/python3.7/site-packages/sqlalchemy/engine/strategies.py__init__(   s    zEngineStrategy.__init__c             O   s
   t  dS )z/Given arguments, returns a new Engine instance.N)NotImplementedError)r   argskwargsr   r   r   create+   s    zEngineStrategy.createN)__name__
__module____qualname____doc__r   r   r   r   r   r   r       s   r   c               @   s   e Zd ZdZdd ZdS )DefaultEngineStrategyz#Base class for built-in strategies.c          
      sF  t |}|}|jdd  dd  | }||ddr^d'fdd	}nj}i }x&tD ]}|krt||||< qtW dd }	|	d kri }
x(t	j
D ]}|kr|||
|< qW j
f |
}	|	|d< x|D ]}|| qW f ||\ |d	i  t  |d
d }|d kr d( fdd	}|d|}|dd }|d kr~|}di}dddddddddd	}x6t|D ](}|||}|kr||||< qW x|D ]}||| qW ||f|}n(t|tjjr|j }n|}|_| j}i }x*t|D ]}|kr>||||< q>W dd}rtdddd D jj|jj|jf |||f||r
 rfd d!}t|d"| t|d#| fd$d%}tj|d"|dd&   |k	r(|  x|D ]}|  q.W S ))NpluginpluginsZ_coerce_configFc                s(    | |}|  jkr$ j|  |}|S )N)popZengine_config_types)keydefaultvalue)dialect_clsr   r   r   	pop_kwargB   s    
z/DefaultEngineStrategy.create.<locals>.pop_kwargmoduledbapiZconnect_argsr	   c                s>   j r2x*jjD ]}||  }|d k	r|S qW j S )N)_has_eventsdispatchZ
do_connectconnect)connection_recordfn
connection)cargscparamsdialectr   r   r(   j   s    z-DefaultEngineStrategy.create.<locals>.connectcreator	poolclassr.   Zpool_logging_nameZ	echo_poolZpool_timeoutZpool_recycleZpool_eventsZpool_threadlocalZpool_reset_on_returnZpool_pre_pingZpool_use_lifo)	Zlogging_nameZechoZtimeoutZrecycleZeventsZuse_threadlocalZreset_on_returnZpre_pingZuse_lifo_initializeTzInvalid argument(s) %s sent to create_engine(), using configuration %s/%s/%s.  Please check that the keyword arguments are appropriate for this combination of components.,c             s   s   | ]}d | V  qdS )z'%s'Nr   ).0kr   r   r   	<genexpr>   s    z/DefaultEngineStrategy.create.<locals>.<genexpr>c                s$   t | d| }|d krd S  | d S )NZ_sqla_unwrap)getattr)dbapi_connectionr)   Zconn)do_on_connectr   r   
on_connect   s
    
z0DefaultEngineStrategy.create.<locals>.on_connectfirst_connectr(   c                s(   t j| dd}t |_ | d S )NF)r+   r&   )r   Z
Connectionr
   ZimmutabledictZ_execution_optionsZ
initialize)r7   r)   c)r.   enginer   r   r:      s    
z3DefaultEngineStrategy.create.<locals>.first_connect)once)N)N)!r   make_urlZ_instantiate_pluginsZqueryr   Z_get_entrypointZget_dialect_clsr
   get_cls_kwargsZget_func_kwargsr%   Zhandle_dialect_kwargsZcreate_connect_argsupdatelistZget_pool_classgetZhandle_pool_kwargs
isinstancepoollibZdbapi_proxyZ_DBProxyZget_pool_dialect
engine_cls	TypeErrorjoin	__class__r   r9   r   ZlistenZengine_created)r   name_or_urlr   ur   Z
entrypointr#   dialect_argsr4   r%   Z
dbapi_argsr   r	   r(   r/   r0   Z	pool_args	translateZtkZengineclassZengine_argsr1   r9   r:   r   )r,   r-   r.   r"   r8   r<   r   r   r   4   s    


















zDefaultEngineStrategy.createN)r   r   r   r   r   r   r   r   r   r   1   s   r   c               @   s   e Zd ZdZdZejZdS )PlainEngineStrategyz*Strategy for configuring a regular Engine.ZplainN)r   r   r   r   r   r   ZEnginerF   r   r   r   r   rN      s   rN   c               @   s   e Zd ZdZdZejZdS )ThreadLocalEngineStrategyz=Strategy for configuring an Engine with threadlocal behavior.r   N)r   r   r   r   r   r   ZTLEnginerF   r   r   r   r   rO      s   rO   c               @   s.   e Zd ZdZdZdd ZG dd dejZdS )MockEngineStrategyzStrategy for configuring an Engine-like object with mocked execution.

    Produces a single mock Connectable object which dispatches
    statement execution to a passed-in function.

    Zmockc       	      K   sV   t |}| }i }x(t|D ]}||kr"||||< q"W |f |}t||S )N)r   r>   Zget_dialectr
   r?   r   rP   MockConnection)	r   rJ   Zexecutorr   rK   r"   rL   r4   r.   r   r   r   r      s    

zMockEngineStrategy.createc               @   s   e Zd Zdd Zedd ZeedZedd Ze	
dZdd	 Zd
d Zdd Zdd Zdd Zdd ZdddZdd ZdS )z!MockEngineStrategy.MockConnectionc             C   s   || _ || _d S )N)rE   execute)r   r.   rR   r   r   r   r     s    z*MockEngineStrategy.MockConnection.__init__c             C   s   | S )Nr   )sr   r   r   <lambda>	  s    z*MockEngineStrategy.MockConnection.<lambda>rE   c             C   s   | j jS )N)rE   r   )rS   r   r   r   rT     s    Nc             K   s   | S )Nr   )r   r   r   r   r   contextual_connect  s    z4MockEngineStrategy.MockConnection.contextual_connectc             K   s   | S )Nr   )r   r   r   r   r   r(     s    z)MockEngineStrategy.MockConnection.connectc             K   s   | S )Nr   )r   kwr   r   r   execution_options  s    z3MockEngineStrategy.MockConnection.execution_optionsc             K   s   | j j||fd| i|S )Nr<   )rE   compiler)r   Z	statementZ
parametersr   r   r   r   rX     s    z*MockEngineStrategy.MockConnection.compilerc             K   s0   d|d< ddl m} |j| j| f|| d S )NF
checkfirstr   )ddl)sqlalchemy.enginerZ   ZSchemaGeneratorr.   traverse_single)r   entityr   rZ   r   r   r   r     s    z(MockEngineStrategy.MockConnection.createc             K   s0   d|d< ddl m} |j| j| f|| d S )NFrY   r   )rZ   )r[   rZ   ZSchemaDropperr.   r\   )r   r]   r   rZ   r   r   r   drop%  s    z&MockEngineStrategy.MockConnection.dropc             K   s"   d|d< || j | f|| d S )NFrY   )r.   r\   )r   Zvisitorcallableelementr+   r   r   r   r   _run_visitor-  s    z.MockEngineStrategy.MockConnection._run_visitorc             O   s
   t  d S )N)r   )r   Zobject_ZmultiparamsZparamsr   r   r   rR   5  s    z)MockEngineStrategy.MockConnection.execute)N)r   r   r   r   propertyr<   r   r.   r   r   Z_schema_getterZschema_for_objectrU   r(   rW   rX   r   r^   r`   rR   r   r   r   r   rQ     s   
	
rQ   N)	r   r   r   r   r   r   r   ZConnectablerQ   r   r   r   r   rP      s   rP   N)r   operatorr    r   r   r   r   r	   rD   r
   Zsqlr   r   objectr   r   rN   rO   rP   r   r   r   r   <module>   s$    %P