ok

Mini Shell

Direktori : /opt/alt/python38/lib64/python3.8/site-packages/__pycache__/
Upload File :
Current File : //opt/alt/python38/lib64/python3.8/site-packages/__pycache__/peewee.cpython-38.pyc

U

S��W���3@s�
ddlZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
mZddl
mZddlmZddlmZzddlmZWnek
r�eZYnXddlmZddlmZdd	lmZd
Zddd
ddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=g3Zzdd>lmZWn(ek
�r�Gd?d@�d@ej�ZYnXe� dA�Z!e!�"e��dBZ#e$fdCdD�Z%ej&ddEkZ'ej&ddFkZ(ej&ddE�dGkZ)e(�r@ddl*Z*ddHlm+Z+ddIlm,Z,dJdK�Z-e.Z/e0Z1e.Z2e3e*dL�Z4dMdK�Z5e6Z7�d]dNdO�Z8n,e'�rde9Z/e2Z1e:Z5dPdQ�Z4e;dR�ne<dS��e)�r�dTZ=dUdK�Z>ndVdK�Z>zddWl?m@ZAWnek
�r�dZAYnXzddlBZBWnek
�r�eAZBYnXeA�r�eAjCeBjCk�r�eAZBzddXlDmEZEeE�F�Wnek
�r"YnXzddlGZGddYlGmHZIWnek
�rVdZGYnXzddlJZKWn>ek
�r�zddlLZKWnek
�r�dZKYnXYnXz(ddZlMmNZNdd[lMmOZOdd\lMmPZPWn2ek
�r��d^d]d^�ZNd_d`�ZOdadb�ZPYnXz4ddclMmQZQdddlMmRZRddelMmSZSddflMmTZTWn&ek
�rZdZQZRZSZTYnXeB�r�eB�UejVe.�eB�UejWe.�eB�Uej
e.�dgdhdidjdkdlgZXeYeX�ZZdmZ[dndo�Z\dpdqdrdsdtdudv�Z]e]��Z^dwe^dk<dxe^dl<dydz�Z_d{d|�Z`Gd}d~�d~e�Zaeadd�d�d�d�d�d�d�d�d�d�d�d�d�d�d�d�d�d�d�d�d�d�d�d�d��Zbead�d�d�d�d��ZcecjdZeecjfZgecjhZid�ZjdEZkdFZld�Zmd�Znebjoebjpebjqebjrebjsebjtebjuebjvebjwebjxebjyd��Zzd�d��Z{d�d��Z|d�d��Z}Gd�d3�d3e$�Z~Gd�d�de$�ZGd�d��d�e$�Z�Gd�d��d�e$�Z�Gd�d7�d7e��Z�e�Z�Gd�d��d�e��Z�Gd�d��d�e��Z�e�d�Z�Gd�d��d�e��Z�Gd�d.�d.e��Z�Gd�d��d�e��Z�Gd�d�de��Z�Gd�d��d�e��Z�Gd�d��d�e��Z�Gd�d=�d=e��Z�d�d�Z�Gd�d�de��Z�Gd�d��d�e��Z�ed�dÃZ�Gd�dńd�ed�dǃ�Z�Gd�dɄd�e$�Z�Gd�d�de��Z�Gd�d�de��Z�Gd�d"�d"e��Z�Gd�d�de��Z�Gd�d5�d5e��Z�Gd�d1�d1e��Z�Gd�dфd�e��Z�Gd�d�de��Z�Gd�d�de��Z�Gd�d�de��Z��d_d�dׄZ�Gd�d�de��Z�Gd�d�de��Z�Gd�d8�d8e��Z�Gd�d
�d
e��Z�Gd�d<�d<e��Z�d�dބZ�Gd�d�d�e��Z�Gd�d�de��Z�Gd�d�de��Z�Gd�d9�d9e��Z�Gd�d:�d:e��Z�Gd�d�de��Z�Gd�d�d�e��Z�Gd�d�d�e$�Z�Gd�d�d�e$�Z�Gd�d �d e��Z�Gd�d�de$�Z�Gd�d�d�e$�Z�Gd�d�d�e$�Z�Gd�d�d�e��Z�Gd�d��d�e$�Z�Gd�d��d�e$�Z�Gd�d��d�e��Z�Gd�d��d�e��Z�eTdk�	r*e�ZTGd�d��d�e��Z�eRdk�	rHe�ZRGd�d��d�e��Z�eQdk�	rfe�ZQG�d�d��de��Z�e�d�d�Z�G�d�d��de��Z�G�d�d��de��Z�G�d�d	��d	e��Z��d
�d�Z�G�d�d
��d
e��Z�G�d�d��de��Z�G�d�d��de��Z�G�d�d��de��Z�G�d�d��deƒZ�G�d�d��deƒZ�G�d�d��deƒZ�e�d�d�Z�e�d�d�Z�e�d�d�Z�G�d �d!��d!eɃZ�G�d"d!�d!eʃZ�G�d#d�deʃZ�G�d$d�dẽZ�G�d%d#�d#ẽZ�G�d&d$�d$eʃZ�G�d'd%�d%ẽZ�G�d(d,�d,ẽZ�G�d)d-�d-ẽZ�G�d*d2�d2ẽZ�G�d+�d,��d,e$�Z�G�d-�d.��d.e$�Z�G�d/�d0��d0e�e	jփZ�G�d1�d2��d2e$�Z�G�d3d6�d6e؃Z�G�d4d/�d/e؃Z�G�d5d+�d+e؃Z�G�d6�d7��d7e$�Z�G�d8�d9��d9e܃Z�G�d:d;�d;e݃Z�G�d;�d<��d<e܃Z�G�d=�d>��d>e܃Z�G�d?�d@��d@e܃Z�G�dA�dB��dBe�Z�G�dC�dD��dDe��Z�G�dE�dF��dFe$�Z�eSdk�r�G�dG�dH��dHe$�ZSG�dId�deɃZ�eB�r�eِdJ�Z�ndZ�G�dK�dL��dLe$�Z�G�dM�dN��dNe�Z�G�dOd*�d*e%e��Z�dP�dQ�Z�dR�dS�Z�e�dT�dU�Z�G�dV�dW��dWe�Z�dXd0�Z�dY�dZ�Z�d[�d\�Z�dS(`�N)�bisect_left)�bisect_right)�deque)�
namedtuple)�OrderedDict)�deepcopy��wraps)�isclassz2.8.4�	BareField�BigIntegerField�	BlobField�BooleanField�	CharField�Check�Clause�CompositeKey�
DatabaseError�	DataError�	DateField�
DateTimeField�DecimalField�DeferredRelation�DoesNotExist�DoubleField�DQ�Field�FixedCharField�
FloatField�fn�ForeignKeyField�ImproperlyConfigured�IntegerField�IntegrityError�InterfaceError�
InternalError�JOIN�	JOIN_FULL�
JOIN_INNER�JOIN_LEFT_OUTER�Model�
MySQLDatabase�NotSupportedError�OperationalError�Param�PostgresqlDatabase�prefetch�PrimaryKeyField�ProgrammingError�Proxy�R�SmallIntegerField�SqliteDatabase�SQL�	TextField�	TimeField�TimestampField�Using�	UUIDField�Window)�NullHandlerc@seZdZdd�ZdS)r>cCsdS�N�)�self�recordr@r@�
/peewee.py�emitiszNullHandler.emitN)�__name__�
__module__�__qualname__rDr@r@r@rCr>hsr>ZpeeweeZ_metaclass_helper_cCs|t|fi�Sr?)�_METACLASS_)�meta�baser@r@rC�with_metaclassssrK��)rL�)�Callable)�reducecCs
t|t�Sr?)�
isinstancerO)�cr@r@rC�<lambda>}�rS�printcCst|�d��S�NZraw_unicode_escape)�bytes�encode��sr@r@rCrS�rTcCs|j|k	r|�|��|�dSr?)�
__traceback__�with_traceback)�tp�value�tbr@r@rC�reraise�s

r`cCstj�|�tj�d�dS)N�
)�sys�stdout�writerYr@r@rC�print_�srez4def reraise(tp, value, tb=None): raise tp, value, tbzUnsupported python version.�@BcCs&|jd|j|jddttS)Ng�i)�microsecondsZsecondsZdays�_M��tr@r@rCrS�rTcCs|��Sr?)�
total_secondsrjr@r@rCrS�rT)�dbapi2)�compat)�
extensions)�format_date_time)�sort_models_topologically)�strip_parensc	CsH|p
dd�}|D]2}z|tj�||��WStk
r@YqXq|S)NcSs|Sr?r@��xr@r@rCrS�rTz"format_date_time.<locals>.<lambda>)�datetime�strptime�
ValueError)r^�formatsZpost_processZfmtr@r@rCrp�srpcsTt���t��g�����fdd��dd�}t�|dd�D]}�|�q:tt���S)z@Sort models topologically so that parents will precede children.csD|�kr@|�kr@��|�|jj��D]}�|j�q&��|�dSr?)�add�_meta�reverse_rel�values�model_class�append)�model�foreign_key��dfs�modelsZordering�seenr@rCr��s

z&sort_models_topologically.<locals>.dfscSs|jj|jjfSr?)rz�name�db_table)�mr@r@rCrS�rTz+sort_models_topologically.<locals>.<lambda>T)�key�reverse)�set�sorted�list�reversed)r��namesr�r@r�rCrq�s
rqcCs�|r|ddkr|Sd}}t|�}||krf||dkrf||ddkrf|d7}|d7}|d8}q$qfq$|r�d}d}t|||�D]N}||dkr�|d7}n||dkr�|d8}|dkr�|d7}d}||kr�q�q�||8}|dkr�|||�S|S)Nr�(��))�len�range)rZZct�i�lZ
unbalanced_ctZrequiredr@r@rCrr�s6

rr)�_DictQueryResultWrapper)�_ModelQueryResultWrapper)�_SortedFieldList)�_TuplesQueryResultWrapper�year�month�day�hour�minute�second)�%Y-%m-%d %H:%M:%S�%Y-%m-%d %H:%M:%S.%f�%Y-%m-%d�%H:%M:%S�%H:%M:%S.%f�%H:%McCs(|tkst�|sdSt|t�}t||�Sr?)�DATETIME_LOOKUPS�AssertionErrorrp�SQLITE_DATETIME_FORMATS�getattr�Zlookup_typeZdatetime_string�dtr@r@rC�_sqlite_date_parts

r�z%Yz%Y-%mr�z%Y-%m-%d %Hz%Y-%m-%d %H:%Mr�)r�r�r�r�r�r�z%Y-%m-%d %H:%iz%Y-%m-%d %H:%i:%ScCs,|tkst�|sdSt|t�}|�t|�Sr?)�SQLITE_DATE_TRUNC_MAPPINGr�rpr��strftimer�r@r@rC�_sqlite_date_trunc&s

r�cCst�||tj�dk	Sr?)�re�search�I)Zregexr^r@r@rC�_sqlite_regexp-sr�c@seZdZdd�ZdS)�attrdictcCs||Sr?r@�rA�attrr@r@rC�__getattr__1szattrdict.__getattr__N)rErFrGr�r@r@r@rCr�0sr��and�or�+�-�*�/�&�|�^�%�=�<�<=�>�>=�!=�inznot in�iszis not�like�ilike�between�regexp�||)�AND�OR�ADD�SUB�MUL�DIV�BIN_AND�BIN_OR�XOR�MOD�EQ�LT�LTE�GT�GTE�NE�IN�NOT_IN�IS�IS_NOT�LIKE�ILIKE�BETWEEN�REGEXP�CONCAT�INNERz
LEFT OUTERzRIGHT OUTER�FULL)r��
LEFT_OUTER�RIGHT_OUTERr�r���)�eq�ltZlte�gtZgte�ner�r�r�r�r�cCs|��}|�|�|Sr?)�copy�update)�sourceZ	overridesZmergedr@r@rC�
merge_dictrs
r�cs�fdd�}�|_|S)z�
    Method decorator that will "clone" the object before applying the given
    method.  This ensures that state is mutated in a more predictable fashion,
    and promotes the use of method-chaining.
    cs|��}�|f|�|�|Sr?��clone)rA�args�kwargsr���funcr@rC�inner}szreturns_clone.<locals>.inner)Z
call_local�r�r�r@r�rC�
returns_clonewsr�cs�fdd�}|S)zu
    Method decorator to indicate a method is not allowed to be called.  Will
    raise a `NotImplementedError`.
    cstd�t|�jf��dS)Nz!%s is not allowed on %s instances)�NotImplementedError�typerE�rAr�r�r�r@rCr��s�znot_allowed.<locals>.innerr@r�r@r�rC�not_allowed�srcsHeZdZdZddgZdd�Zdd�Zdd	�Zd
d�Z�fdd
�Z	�Z
S)r3zg
    Proxy class useful for situations when you wish to defer the initialization
    of an object.
    �obj�
_callbackscCsg|_|�d�dSr?)r�
initialize�rAr@r@rC�__init__�szProxy.__init__cCs||_|jD]}||�qdSr?)rr)rAr�callbackr@r@rCr�s
zProxy.initializecCs|j�|�|Sr?)rr~)rArr@r@rC�attach_callback�szProxy.attach_callbackcCs|jdkrtd��t|j|�S)NzCannot use uninitialized Proxy.)r�AttributeErrorr�r�r@r@rCr��s
zProxy.__getattr__cs$||jkrtd��tt|��||�S)NzCannot set attribute on proxy.)�	__slots__r
�superr3�__setattr__�rAr�r^��	__class__r@rCr
�s
zProxy.__setattr__)rErFrG�__doc__rrrr	r�r
�
__classcell__r@r@rrCr3�sc@s8eZdZe�Zd
dd�Zdd�Zdd�Zedd	��Z	dS)rNcCs(g|_|dk	r$|��|_|j�|�dSr?)�fields�lower�_rel_model_name�_unresolvedry)rAZrel_model_namer@r@rCr�s
zDeferredRelation.__init__cCs|j�|||f�dSr?)rr~)rAr}�fieldr�r@r@rC�	set_field�szDeferredRelation.set_fieldcCs(|jD]\}}}||_|�||�qdSr?)r�	rel_model�add_to_class)rArrrr�r@r@rC�	set_model�szDeferredRelation.set_modelcCs>ttj�}|D]*}|j|j��kr|�|�tj�|�qdSr?)r�rrrrErr�discard)Z	model_clsZ
unresolvedZdrr@r@rC�resolve�s


zDeferredRelation.resolve)N)
rErFrGr�rrrr�staticmethodrr@r@r@rCr�s
c@seZdZddd�ZdS)�_CDescriptorNcCs|dk	rt|j�S|Sr?)�Entity�_alias�rA�instanceZ
instance_typer@r@rC�__get__�s
z_CDescriptor.__get__)N)rErFrGr$r@r@r@rCr�src@seZdZdZe�ZdZdd�Zed5dd��Z	d	d
�Z
dd�Zed
d��Z
ed6dd��Zedd��Zedd��Zedd��Zdd�Zdd�Zd7dd�Zeej�Zeej�Zeej�Zeej�Zeej�Zeej �Z!Z"eej#�Z$eejdd�Z%eejdd�Z&eejdd�Z'eej dd�Z(Z)eejdd�Z*eejdd�Z+eej#dd�Z,dd �Z-d!d"�Z.eej/�Z0eej1�Z2eej3�Z4eej5�Z6eej7�Z8eej9�Z:eej;�Z<eej=�Z>eej?�Z@eejA�ZBd#d$�ZCd%d&�ZDd8d'd(�ZEd)d*�ZFd+d,�ZGd-d.�ZHd/d0�ZId1d2�ZJd3d4�ZKdS)9�Nodez=Base-class for any part of a query which shall be composable.�nodecCsd|_d|_d|_d|_dS�NF)�_negatedr!�_bind_to�	_orderingrr@r@rCr�sz
Node.__init__NFcs���fdd�}|S)Ncs&�p|j}�rt|�}t�||�|Sr?)rEr��setattr)�methodZmethod_name�r��clsr�r@rC�	decorator�s

zNode.extend.<locals>.decoratorr@)r.r�r�r/r@r-rC�extend�szNode.extendcCs
t|��Sr?)rrr@r@rC�
clone_base�szNode.clone_basecCs,|��}|j|_|j|_|j|_|j|_|Sr?)r1r(r!r*r))rA�instr@r@rCr��sz
Node.clonecCs|j|_dSr?)r(rr@r@rC�
__invert__�szNode.__invert__cCs
||_dSr?�r!)rA�ar@r@rC�alias�sz
Node.aliascCs
||_dS)z�
        Bind the results of an expression to a specific model type. Useful
        when adding expressions to a select, where the result of the expression
        should be placed on a joined instance.
        N)r))rAZbtr@r@rC�bind_to�szNode.bind_tocCs
d|_dS)NZASC�r*rr@r@rC�asc�szNode.asccCs
d|_dS)N�DESCr8rr@r@rC�descsz	Node.desccCs|��Sr?)r9rr@r@rC�__pos__szNode.__pos__cCs|��Sr?)r;rr@r@rC�__neg__
szNode.__neg__cs��fdd�}|S)z�
        Lightweight factory which returns a method that builds an Expression
        consisting of the left-hand and right-hand operands, using `op`.
        cs�rt|�|�St|�|�Sr?)�
Expression�rA�rhs��inv�opr@rCr�szNode._e.<locals>.innerr@)rCrBr�r@rArC�_e
szNode._eT)rBcCs$|dkrt|tjd�St|tj|�Sr?)r>�OPr�r�r?r@r@rC�__eq__'szNode.__eq__cCs$|dkrt|tjd�St|tj|�Sr?)r>rEr�r�r?r@r@rC�__ne__+szNode.__ne__cCst|tj|�Sr?)r>rEr�r?r@r@rC�in_=szNode.in_cCst|tj|�Sr?)r>rEr�r?r@r@rC�not_in?szNode.not_incCs |rt|tjd�St|tjd�Sr?)r>rEr�r�)rA�is_nullr@r@rCrJAszNode.is_nullcCst|tjd|�S)Nz%%%s%%�r>rEr�r?r@r@rC�containsEsz
Node.containscCst|tjd|�S)Nz%s%%rKr?r@r@rC�
startswithGszNode.startswithcCst|tjd|�S)Nz%%%srKr?r@r@rC�endswithIsz
Node.endswithcCst|tjt|td�|��S)Nr�)r>rEr�rr4)rAZlowZhighr@r@rCr�KszNode.betweencCst|tj|�Sr?)r>rEr�)rA�
expressionr@r@rCr�MszNode.regexpcCst|tj|�Sr?)r>rEr�r?r@r@rC�concatOszNode.concat)NF)N)F)T)LrErFrGrrrR�
_node_typer�classmethodr0r1r�r�r3r6r7r9r;r<r=rDrEr��__and__r��__or__r��__add__r��__sub__r��__mul__r�Z__div__�__truediv__r��__xor__�__radd__�__rsub__�__rmul__Z__rdiv__�__rtruediv__�__rand__�__ror__�__rxor__rFrGr��__lt__r��__le__r��__gt__r��__ge__r��
__lshift__r��
__rshift__r��__mod__r��__pow__r�Zbin_andr�Zbin_orrHrIrJrLrMrNr�r�rPr@r@r@rCr%�sp	






















r%cs,eZdZdZdZ�fdd�Zdd�Z�ZS)r7z2An unescaped SQL string, with optional parameters.�sqlcs||_||_tt|���dSr?)r^�paramsrr7r)rAr^rjrr@rCrVszSQL.__init__cCst|jf|j��Sr?)r7r^rjrr@r@rCr1[szSQL.clone_base�rErFrGrrQrr1rr@r@rrCr7Rscs4eZdZdZdZ�fdd�Zdd�Zdd�Z�ZS)	r z/A quoted-name or entity, e.g. "table"."column".�entitycstt|���||_dSr?)rr r�path)rArmrr@rCrcszEntity.__init__cCs
t|j�Sr?)r rmrr@r@rCr1gszEntity.clone_basecCsttd|j|f��Sr?)r �filterrmr�r@r@rCr�jszEntity.__getattr__)	rErFrGrrQrr1r�rr@r@rrCr _s
r csTeZdZdZdZed�Z�fdd�Zeddd��Z	d	d
�Z
ddd
�Zdd�Z�Z
S)�FunczAn arbitrary SQL function call.r�)�count�sumcs6||_||_|r|��|jknd|_tt|���dSr')r��	argumentsr�
_no_coerce�_coercerror)rAr�rrrr@rCrrsz
Func.__init__TcCs
||_dSr?)rt)rA�coercer@r@rCruxszFunc.coercecCst|jf|j��}|j|_|Sr?)ror�rrrt)rA�resr@r@rCr1|szFunc.clone_baseNcCsJt|t�r|dkr|}|dkr0t||d���}n
t|j�}t|td�|�S)N)�partition_by�order_byZOVER)rQr=�__sql__r7r!r)rArwrx�windowrir@r@rC�over�s�
z	Func.overcs�fdd�}|S)Ncst�f|�|�Sr?)ro�r�r��r�r@rC�dec�szFunc.__getattr__.<locals>.decr@)rAr�r~r@r}rCr��szFunc.__getattr__)T)NNN)rErFrGrrQr�rsrr�rur1r{r�rr@r@rrCroms

rocs.eZdZdZdZd�fdd�	Zdd�Z�ZS)	r>z0A binary expression, e.g `foo + 1` or `bar < 7`.rOFcs*tt|���||_||_||_||_dSr?)rr>r�lhsrCr@�flat)rArrCr@r�rr@rCr�s
zExpression.__init__cCst|j|j|j|j�Sr?)r>rrCr@r�rr@r@rCr1�szExpression.clone_base)Frkr@r@rrCr>�sr>cs.eZdZdZdZd�fdd�	Zdd�Z�ZS)	r.z�
    Arbitrary parameter passed into a query. Instructs the query compiler to
    specifically treat this value as a parameter, useful for `list` which is
    special-cased for `IN` lookups.
    �paramNcs||_||_tt|���dSr?)r^�adaptrr.r)rAr^r�rr@rCr�szParam.__init__cCst|j|j�Sr?)r.r^r�rr@r@rCr1�szParam.clone_base)Nrkr@r@rrCr.�sc@seZdZdZdS)�Passthrough�passthroughN)rErFrGrQr@r@r@rCr��sr�cs4eZdZdZdZdZdZ�fdd�Zdd�Z�Z	S)	rz8A SQL clause, one or more Node objects joined by spaces.�clause� Fcs@d|kr|d|_d|kr$|d|_tt|���t|�|_dS)N�glue�parens)r�r�rrrr��nodes)rAr�r�rr@rCr�s

zClause.__init__cCst|j�}|j|_|j|_|Sr?)rr�r�r��rAr�r@r@rCr1�s
zClause.clone_base)
rErFrGrrQr�r�rr1rr@r@rrCr�sc@seZdZdZdZdS)�CommaClausez5One or more Node objects joined by commas, no parens.�, N)rErFrGrr�r@r@r@rCr��sr�c@seZdZdZdZdS)�EnclosedClausezAOne or more Node objects joined by commas and enclosed in parens.TN)rErFrGrr�r@r@r@rCr��sr�cs.eZdZd�fdd�	Zdd�Zdd�Z�ZS)	r=Ncs*tt|���||_||_|jp"d|_dS)N�w)rr=rrwrxr!)rArwrxrr@rCr�szWindow.__init__cCsPg}|jr$|�ttd�t|j���|jrD|�ttd�t|j���tt|��S)NzPARTITION BY�ORDER BY)rwr~rr7r�rxr�)rAZover_clausesr@r@rCry�s��zWindow.__sql__cCst|j|j�Sr?)r=rwrxrr@r@rCr1�szWindow.clone_base)NN)rErFrGrryr1rr@r@rrCr=�scCstd|�S)Nz
CHECK (%s)�r7)r^r@r@rCr�scs(eZdZdZ�fdd�Zdd�Z�ZS)rz:A "django-style" filter expression, e.g. {'foo__eq': 'x'}.cstt|���||_dSr?)rrr�query�rAr�rr@rCr�szDQ.__init__cCstf|j�Sr?)rr�rr@r@rCr1�sz
DQ.clone_base)rErFrGrrr1rr@r@rrCr�scs eZdZdZ�fdd�Z�ZS)�_StripParensrrcstt|���||_dSr?)rr�rr&)rAr&rr@rCr�sz_StripParens.__init__)rErFrGrQrrr@r@rrCr��sr��JoinMetadata��	src_model�
dest_model�src�destr��primary_keyr��
is_backrefr6�is_self_join�
is_expressionc@s:eZdZddd�Zdd�Zdd�Zdd	�Zed
d��ZdS)
�JoinNcCsXt|t�st|t�rdS|j�||�}|dk	r6|dfS|j�||�}|dk	rT|dfSdS)N�NNFT)rQ�SelectQueryrz�
rel_for_model�reverse_rel_for_model)rAr�r�r�fk_fieldr{r@r@rC�get_foreign_key
szJoin.get_foreign_keycCs|jp
tjSr?)�	join_typer&r�rr@r@rC�
get_join_typeszJoin.get_join_typecCs$t|t�r|jSt|t�r |jS|Sr?)rQ�
ModelAliasr}r�)rA�model_or_aliasr@r@rC�model_from_aliass


zJoin.model_from_aliasc

CsB|�|j�}|�|j�}t|jt�r,|jjp.d}t|jttt	f�}t|jt
tf�rX|jpZd}|rt|}|j|j
jk}n6|�|||j�\}}|dkr�|jdk	r�|�||�\}}|dk	r�|j}nd}|�s|dk	r�|r�|j
j}	n|j}	n.z|jjj}	Wntk
�r|j
j}	YnXnd}	t|||j|j|�p,|	||||||k|d�S)Nr�)r�r�r�rQ�onr%r!r>ror7r�
FieldProxyr�rzrr��to_fieldr�rr
r�)
rAr�r�Z
join_aliasr�Zon_fieldr�r�r�Ztarget_attrr@r@rC�_join_metadatasJ
�zJoin._join_metadatacCst|d�s|��|_|jS)N�_cached_metadata)�hasattrr�r�rr@r@rC�metadataPs

z
Join.metadata)N)	rErFrGr�r�r�r��propertyr�r@r@r@rCr�	s
1r�Z_Join)r�r�r�r�c@s&eZdZdd�Zddd�Zdd�ZdS)	�FieldDescriptorcCs||_|jj|_dSr?)rr��att_name�rArr@r@rCrYszFieldDescriptor.__init__NcCs|dk	r|j�|j�S|jSr?)�_data�getr�rr"r@r@rCr$]szFieldDescriptor.__get__cCs||j|j<|j�|j�dSr?)r�r��_dirtyry�rAr#r^r@r@rC�__set__bszFieldDescriptor.__set__)N)rErFrGrr$r�r@r@r@rCr�Vs
r�cs�eZdZdZdZdZdZdZd#�fdd�	Zd	d
�Z	dd�Z
d
d�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zd$dd�Zdd�Zdd �Zd!d"�Z�ZS)%rzA column on a table.rr�unknownFNc

s�||_||_||_||_||_||_||_||_|	|_|
|_	||_
||_tj
d7_
tj
|_|jrhdpjd|jf|_d|_tt|���dS)Nr�rLF)�null�index�unique�verbose_name�	help_text�	db_column�default�choicesr��sequence�constraints�schemar�_field_counter�_order�	_sort_key�	_is_boundrr)
rAr�r�r�r�r�r�r�r�r�r�r�r�rr@rCrms"zField.__init__cKsft|�f|j|j|j|j|j|j|j|j|j	|j
|j|jd�|��}|j
rZ|j|_|j|_|j
|_
|S)N)r�r�r�r�r�r�r�r�r�r�r�r�)rr�r�r�r�r�r�r�r�r�r�r�r�r�r�r})rAr�r2r@r@rCr1�s*�
�zField.clone_basecCsZ||_||_|jp|j|_|js4t�dd|���|_|j�|�t	||t
|��d|_dS)z�
        Hook that replaces the `Field` attribute on a class with a named
        `FieldDescriptor`. Called by the metaclass during construction of the
        `Model`.
        �_+r�TN)r�r}r�r�r��sub�titlerz�	add_fieldr+r�r��rAr}r�r@r@rCr�szField.add_to_classcCs
|jjjSr?)r}rz�databaserr@r@rC�get_database�szField.get_databasecCs|��}|�����|�Sr?)�get_db_fieldr��compiler�get_column_type)rAZ
field_typer@r@rCr��szField.get_column_typecCs|jSr?)�db_fieldrr@r@rCr��szField.get_db_fieldcCsdSr?r@rr@r@rC�
get_modifiers�szField.get_modifierscCs|Sr?r@�rAr^r@r@rCru�szField.coercecCs|dkr|S|�|�S)z5Convert the python value for storage in the database.N�rur�r@r@rC�db_value�szField.db_valuecCs|dkr|S|�|�S)z/Convert the database value to a pythonic value.Nr�r�r@r@rC�python_value�szField.python_valuecCs |rt|jjj|j�St|j�Sr?)r r}rzr�r�)rA�
with_tabler@r@rC�	as_entity�szField.as_entitycCs0|��}|r(td|d�tt|��f�St|�S)z2Return the column type, e.g. VARCHAR(255) or REAL.�%s(%s)r�)r�r7�join�map�str)rA�column_typeZ	modifiersr@r@rC�__ddl_column__�s�zField.__ddl_column__cCsj|��|�|�g}|js&|�td��|jr:|�td��|jrT|�td|j��|jrf|�|j�|S)z8Return a list of Node instances that defines the column.zNOT NULL�PRIMARY KEYzDEFAULT NEXTVAL('%s'))	r�r�r�r~r7r�r�r�r0)rAr��ddlr@r@rC�__ddl__�sz
Field.__ddl__cCst|jd|jj�S�N�.)�hashr�r}rErr@r@rC�__hash__�szField.__hash__)FFFNNNNNFNNN)F)rErFrGrr�r�rQr�rr1rr�r�r�r�rur�r�r�r�r�r�rr@r@rrCrfs:�

cs.eZdZdZd�fdd�	Z�fdd�Z�ZS)r�bareNcs$tt|�j||�|dk	r ||_dSr?)rrrru)rArur�r�rr@rCr�szBareField.__init__cstt|�jfd|ji|��S)Nru)rrr1ru�rAr�rr@rCr1�szBareField.clone_base)N)rErFrGr�rr1rr@r@rrCr�sc@seZdZdZeZdS)r"�intN)rErFrGr�r�rur@r@r@rCr"�sc@seZdZdZdS)r�bigintN�rErFrGr�r@r@r@rCr�sc@seZdZdZdS)r5�smallintNr�r@r@r@rCr5�scs eZdZdZ�fdd�Z�ZS)r1r�csd|d<tt|�j||�dS)NTr�)rr1rrrr@rCr�szPrimaryKeyField.__init__)rErFrGr�rrr@r@rrCr1�scs eZdZdZ�fdd�Z�ZS)�_AutoPrimaryKeyFieldNcs4||jkrtdt|�|f��tt|��||�dS)Nz%s must be named `%s`.)�_column_namerwrrr�rr�rr@rCrs
z!_AutoPrimaryKeyField.add_to_class)rErFrGr�rrr@r@rrCr��sr�c@seZdZdZeZdS)r�floatN)rErFrGr�r�rur@r@r@rCrsc@seZdZdZdS)r�doubleNr�r@r@r@rCr	scsFeZdZdZd�fdd�	Z�fdd	�Zd
d�Zdd
�Zdd�Z�Z	S)r�decimal�
r�FNcs6||_||_||_|ptjj|_tt|�j||�dSr?)	�
max_digits�decimal_places�
auto_roundr�ZDefaultContext�roundingrrr)rAr�r�r�r�r�r�rr@rCrs
zDecimalField.__init__cs(tt|�jf|j|j|j|jd�|��S)N)r�r�r�r�)rrr1r�r�r�r�r�rr@rCr1s��zDecimalField.clone_basecCs|j|jgSr?)r�r�rr@r@rCr�szDecimalField.get_modifierscCsTtj}|s|dkr|S|d�S|jrP|d�|j}|j}|t|��j||d�S|S)Nrr�)r�)r��Decimalr�r�r�r�Zquantize)rAr^�DZexpr�r@r@rCr�"szDecimalField.db_valuecCs*|dk	r&t|tj�r|St�t|��SdSr?)rQr�r�r�r�r@r@rCr�,szDecimalField.python_value)r�r�FN)
rErFrGr�rr1r�r�r�rr@r@rrCrs�
�utf-8cCs*t|t�r|St|t�r"|�|�St|�Sr?)rQ�unicode_type�string_type�decode)rZ�encodingr@r@rC�coerce_to_unicode2s



rcs>eZdZdZd�fdd�	Z�fdd�Zdd�Zd	d
�Z�ZS)r�string�cs||_tt|�j||�dSr?)�
max_lengthrrr)rArr�r�rr@rCr<szCharField.__init__cstt|�jfd|ji|��S)Nr)rrr1rr�rr@rCr1@s
��zCharField.clone_basecCs|jr|jgpdSr?)rrr@r@rCr�EszCharField.get_modifierscCst|pd�S�N��rr�r@r@rCruHszCharField.coerce)r)	rErFrGr�rr1r�rurr@r@rrCr9s
cs eZdZdZ�fdd�Z�ZS)r�
fixed_charcs tt|��|�}|r|��}|Sr?)rrr��stripr�rr@rCr�NszFixedCharField.python_value)rErFrGr�r�rr@r@rrCrKsc@seZdZdZdd�ZdS)r8�textcCst|pd�Srrr�r@r@rCruWszTextField.coerceN)rErFrGr�rur@r@r@rCr8Tscs4eZdZdZeZ�fdd�Zdd�Zdd�Z�Z	S)r
�blobcs0t|jjt�r|jj�|j�tt|��||�Sr?)	rQrzr�r3r	�_set_constructorrr
rr�rr@rCr^szBlobField.add_to_classcCs|��|_dSr?)�get_binary_type�_constructor)rAr�r@r@rCrcszBlobField._set_constructorcCs,t|t�r|�d�}t|t�r(|�|�S|SrV)rQr�rX�
basestringr
r�r@r@rCr�fs




zBlobField.db_value)
rErFrGr��binary_constructr
rrr�rr@r@rrCr
Zs
c@s eZdZdZdd�Zdd�ZdS)r<�uuidcCs6t|tj�r|jSzt�|�jWS|YSXdSr?)rQr�UUID�hexr�r@r@rCr�pszUUIDField.db_valuecCs&t|tj�r|S|dkrdSt�|�Sr?)rQrrr�r@r@rCr�xszUUIDField.python_valueN)rErFrGr�r�r�r@r@r@rCr<mscs�fdd�}|S)Ncs|jjj��|�Sr?)r}rzr��extract_dater��	date_partr@rCr~~sz_date_part.<locals>.decr@)rr~r@rrC�
_date_part}srcs.eZdZdZd�fdd�	Z�fdd�Z�ZS)�_BaseFormattedFieldNcs$|dk	r||_tt|�j||�dSr?)rxrrr)rArxr�r�rr@rCr�sz_BaseFormattedField.__init__cstt|�jfd|ji|��S)Nrx)rrr1rxr�rr@rCr1�s
��z_BaseFormattedField.clone_base)N)rErFrGrxrr1rr@r@rrCr�src@sjeZdZdZdddgZdd�Zeed��Zeed��Z	eed	��Z
eed
��Zeed��Zeed��Z
d
S)rrur�r�r�cCs|rt|t�rt||j�S|Sr?)rQrrprxr�r@r@rCr��szDateTimeField.python_valuer�r�r�r�r�r�N)rErFrGr�rxr�r�rr�r�r�r�r�r�r@r@r@rCr�s�c@sFeZdZdZdddgZdd�Zeed��Zeed��Z	eed	��Z
d
S)r�dater�r�r�cCs@|r$t|t�r$dd�}t||j|�S|r<t|tj�r<|��S|S)NcSs|��Sr?)rrsr@r@rCrS�rTz(DateField.python_value.<locals>.<lambda>)rQrrprxrur�rAr^Zppr@r@rCr��szDateField.python_valuer�r�r�N)rErFrGr�rxr�r�rr�r�r�r@r@r@rCr�s�c@sJeZdZdZdddddgZdd�Zeed	��Zeed
��Z	eed��Z
dS)
r9�timer�r�r�r�r�cCs`|r8t|t�r$dd�}t||j|�St|tj�r8|��S|dk	r\t|tj�r\tjj|��S|S)NcSs|��Sr?)rrsr@r@rCrS�rTz(TimeField.python_value.<locals>.<lambda>)rQrrprxrurZ	timedelta�minrr@r@rCr��s
zTimeField.python_valuer�r�r�N)rErFrGr�rxr�r�rr�r�r�r@r@r@rCr9�s�csFeZdZdd�ed�D�Z�fdd�Zdd�Zdd	�Zd
d�Z�Z	S)r:cCsg|]}d|�qS)r�r@��.0r�r@r@rC�
<listcomp>�szTimestampField.<listcomp>�cs�|�dd�pd|_|j|jkr<tdd�dd�|jD����|�dd�pJd|_tj}|jr`|jn|j|_	|jrt|j
n|j}|�d	|�t
t|�j||�dS)
N�
resolutionr�z,TimestampField resolution must be one of: %sr�css|]}t|�VqdSr?)r�rr@r@rC�	<genexpr>�sz*TimestampField.__init__.<locals>.<genexpr>�utcFr�)�popr �valid_resolutionsrwr�r"ruZutcfromtimestampZ
fromtimestamp�_convZutcnowZnow�
setdefaultrr:r)rAr�r�Z_dtZ_defaultrr@rCr�s�zTimestampField.__init__cCs|jdkr|jStjS�Nr�)r r�rrr@r@rCr��s�zTimestampField.get_db_fieldcCs�|dkrdSt|tj�rn4t|tj�r<t�|j|j|j�}ntt||j��S|j	rdt
�|���}nt
�|���}||jd7}|jdkr�||j9}tt|��S)Ng���ư>r�)rQrurr�r�r�r��roundr r"�calendarZtimegmZutctimetupler�mktimeZ	timetuple�microsecond)rAr^Z	timestampr@r@rCr��s

zTimestampField.db_valuecCsp|dk	rlt|tttf�rl|dkr$dS|jdkrbd|j}t||j�\}}||}|�|�j|d�S|�|�S|S)Nrr�rf)r+)rQr�r��longr �divmodr%�replace)rAr^Zticks_to_microsecondZticksrhr@r@rCr��s


zTimestampField.python_value)
rErFrGr�r$rr�r�r�rr@r@rrCr:�s

c@seZdZdZeZdS)r�boolN)rErFrGr�r/rur@r@r@rCrscs:eZdZdZ�fdd�Zdd�Zddd�Zd	d
�Z�ZS)�RelationDescriptorzEForeign-key abstraction to replace a related PK with a related model.cs||_tt|��|�dSr?)rrr0r)rArrrr@rCrszRelationDescriptor.__init__cCsn|j�|j�}|dk	s"|j|jkrZ|j|jkrN|j�|jj|k�}||j|j<|j|jS|jjsj|jj�|Sr?)	r�r�r��
_obj_cacherrr�r�r)rAr#Zrel_idrr@r@rC�get_object_or_idsz#RelationDescriptor.get_object_or_idNcCs|dk	r|�|�S|jSr?)r2rr"r@r@rCr$s
zRelationDescriptor.__get__cCs|t||j�r2t||jjj�|j|j<||j|j<n8|j�	|j�}||j|j<||krj|j|jkrj|j|j=|j
�|j�dSr?)rQrr�rr�r�r�r�r1r�r�ry)rAr#r^Z
orig_valuer@r@rCr�"s�
zRelationDescriptor.__set__)N)	rErFrGrrr2r$r�rr@r@rrCr0s

r0c@s"eZdZdZdd�Zddd�ZdS)�ReverseRelationDescriptorz<Back-reference to expose related objects as a `SelectQuery`.cCs||_|j|_dSr?)rr}rr�r@r@rCr0sz"ReverseRelationDescriptor.__init__NcCs.|dk	r*|j���|jt||jjj�k�S|Sr?)r�select�whererr�r�r�r"r@r@rCr$4s

�z!ReverseRelationDescriptor.__get__)N)rErFrGrrr$r@r@r@rCr3.sr3c@s*eZdZdZdd�Zd	dd�Zdd�ZdS)
�ObjectIdDescriptorz(Gives direct access to the underlying idcCs|j|_t�|�|_dSr?)r��	attr_name�weakref�refrr�r@r@rCr<szObjectIdDescriptor.__init__NcCs|dk	r|j�|j�S|��Sr?)r�r�r7rr"r@r@rCr$@szObjectIdDescriptor.__get__cCst||j|�dSr?)r+r7r�r@r@rCr�EszObjectIdDescriptor.__set__)N)rErFrGrrr$r�r@r@r@rCr6:s
r6cs�eZdZd�fdd�	Z�fdd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Z	�fdd�Z
�fdd�Zdd�Zdd�Z
dd�Z�ZS)r Nc	sr|dkr(t|ttf�s(t|t�s(td��||_||_t|ttf�|_||_	||_
||_||_t
t|�j||�dS)NrAz[Unexpected value for `rel_model`.  Expected `Model`, `Proxy`, `DeferredRelation`, or "self")rQr3r�
issubclassr*�	TypeErrorr�
_related_name�deferred�	on_delete�	on_update�extrar�rr r)	rAr�related_namer>r?r@r�r�r�rr@rCrIs��zForeignKeyField.__init__c	s2tt|�jf|j|��|j|j|j|jd�|��S)N)rrAr>r?r@r�)	rr r1r�_get_related_namer>r?r@r�r�rr@rCr1Ys��zForeignKeyField.clone_basecCst||j�Sr?)r0rrr@r@rC�_get_descriptorcszForeignKeyField._get_descriptorcCst|�Sr?)r6rr@r@rC�_get_id_descriptorfsz"ForeignKeyField._get_id_descriptorcCst|�Sr?)r3rr@r@rC�_get_backref_descriptorisz'ForeignKeyField._get_backref_descriptorcCs.|jrt|j�r|�|�S|jp,d|jjjS)Nz%s_set)r<�callabler}rzr�rr@r@rCrBls
z!ForeignKeyField._get_related_namecs�t�jt�r,���fdd�}�j�|�dSt�jt�rL�j�����dS��_��_�jpfd�j�_���jkr��d7��j	s�t
�dd�����_	�j
�������_�jdkrˆj�_�jdk	r�t�jt�s�t�j�j��_n�jj
j�_�j
j�rt����fdd	�}�j�jj
jk�r.|d
�n�j�jj
jk�rH|d���j
jk�r`|d�n��jk�rt|d
�t������t������t�j�j����d�_��j
j�j<��jj
j�j<dS)Ncs|�_�����dSr?)rr�r)r}r�rAr@rCrssz.ForeignKeyField.add_to_class.<locals>.callbackz%s_idZ_idr�r�rAcs.|jd�jj�f�j�d�t||��dS)Nz%s.%s)r�backref�obj_id_name)r�rzr�rAr
)�msg�context�r}r�rIrAr@rC�invalid�s�z-ForeignKeyField.add_to_class.<locals>.invalidzVThe related_name of %(field)s ("%(backref)s") conflicts with a field of the same name.zWThe related_name of %(field)s ("%(backref)s") is already in use by another foreign key.zRThe object id descriptor of %(field)s conflicts with a field named %(obj_id_name)sz]Model attribute "%(obj_id_name)s" would be shadowed by the object id descriptor of %(field)s.T)rQrr3r	rrr�r}r�r�r�r�r�rzr�rBrAr�rr�r��validate_backrefsrr{�__dict__r+rCrDrEr��rel)rAr}r�rrMr@rLrCrqsR






�zForeignKeyField.add_to_classcs$t|jt�s|j��Stt|���S)zr
        Overridden to ensure Foreign Keys use same column type as the primary
        key they point to.
        )rQr�r1r�rr rrr@rCr��s
zForeignKeyField.get_db_fieldcs$t|jt�s|j��Stt|���Sr?)rQr�r1r�rr rrr@rCr��s
zForeignKeyField.get_modifierscCs|j�|�Sr?)r�rur�r@r@rCru�szForeignKeyField.coercecCs t||j�r|��}|j�|�Sr?)rQr�
_get_pk_valuer�r�r�r@r@rCr��szForeignKeyField.db_valuecCst||j�r|S|j�|�Sr?)rQrr�r�r�r@r@rCr��szForeignKeyField.python_value)NNNNN)rErFrGrr1rCrDrErBrr�r�rur�r�rr@r@rrCr Hs �
@	c@sFeZdZdZdZdd�Zdd�Zddd�Zd	d
�Zdd�Z	d
d�Z
dS)rz+A primary key composed of multiple columns.NcGs
||_dSr?)�field_names)rArRr@r@rCr�szCompositeKey.__init__cCs||_||_t|||�dSr?)r�r}r+r�r@r@rCr�szCompositeKey.add_to_classcs$�dk	r t�fdd�|jD��S|S)Ncsg|]}t�|��qSr@)r��r�
field_name�r#r@rCr�s�z(CompositeKey.__get__.<locals>.<listcomp>)�tuplerRr"r@rUrCr$�s
�zCompositeKey.__get__cCsdSr?r@r�r@r@rCr��szCompositeKey.__set__cs&�fdd�t�j|�D�}ttj|�S)Ncs"g|]\}}�jjj||k�qSr@)r}rzr)rrr^rr@rCr�s�z'CompositeKey.__eq__.<locals>.<listcomp>)�ziprRrP�operator�and_)rA�other�expressionsr@rrCrF�s

�zCompositeKey.__eq__cCst|jj|jf�Sr?)r�r}rErRrr@r@rCr��szCompositeKey.__hash__)N)rErFrGrr�rrr$r�rFr�r@r@r@rCr�s
c@sDeZdZdZddd�Zdd�Zddd	�Zd
d�Zdd
�Zdd�Z	dS)�AliasMaprkrcCsi|_||_dSr?)�
_alias_map�_counter)rA�startr@r@rCr�szAliasMap.__init__cCs
d|jS)Nz<AliasMap: %s>�r]rr@r@rC�__repr__�szAliasMap.__repr__NcCs:||jkrdS|jd7_|p.d|j|jf|j|<dS)Nr�z%s%s)r]r^�prefix)rArr6r@r@rCry�s
zAliasMap.addcCs||jkr|�|�|j|Sr?)r]ry�rArr@r@rC�__getitem__�s

zAliasMap.__getitem__cCs
||jkSr?r`rcr@r@rC�__contains__szAliasMap.__contains__cCs.|r*|j��D]\}}||kr||j|<q|Sr?)r]�items)rA�	alias_maprr6r@r@rCr�s
zAliasMap.update)r)N)
rErFrGrbrraryrdrer�r@r@r@rCr\�s

r\c2@s�eZdZddddddddd	dd
d
dddd
d�Zejdejdejdejdej	dej
dejdejdej
dejdejdejdejdejdejdejdejdejd ejd!ejd"ejd#ejd$ejd%ejd&ejd'iZej d(ej!d)ej"d*ej#d+iZ$e%Z&d�d/d0�Z'd1d2�Z(d3d4�Z)d5d6�Z*d7d8�Z+d9d:�Z,d;d<�Z-d=d>�Z.d?d@�Z/dAdB�Z0dCdD�Z1dEdF�Z2dGdH�Z3dIdJ�Z4dKdL�Z5dMdN�Z6dOdP�Z7dQdR�Z8dSdT�Z9d�dUdV�Z:d�dXdY�Z;d�dZd[�Z<d�d\d]�Z=d^d_�Z>d�d`da�Z?dbdc�Z@eAfddde�ZBdfdg�ZCdhdi�ZDdjdk�ZEdldm�ZFdndo�ZGd�dpdq�ZHeGdr�ZId�dtdu�ZJeGdv�ZKd�dwdx�ZLeGdy�ZMd�dzd{�ZNeGd|�ZOd}d~�ZPdd��ZQeGd��ZRd�d�d��ZSeGd��ZTd�d��ZUeGd��ZVd�d��ZWeGd��ZXd.S)��
QueryCompilerrZBIGINTZBLOBZSMALLINTZDATEZDATETIMEZDECIMALZREALZCHAR�INTEGERZVARCHAR�TEXTZTIME)r�r�r
r/rrur�r�rr�r�r�r�rr	rr�r�r�r�r�r�r�zNOT INr�zIS NOTr�r�r�r�r�r�r�r�r��#r�r�r�r�r�z
INNER JOINzLEFT OUTER JOINzRIGHT OUTER JOINz	FULL JOIN�"�?NcCsJ||_||_t|j|pi�|_t|j|p*i�|_|��|_t	dg�|_
dS)Nr�)�
quote_char�
interpolationr��	field_map�
_field_map�op_map�_op_map�
get_parse_map�
_parse_mapr��_unknown_types)rArnro�field_overrides�op_overridesr@r@rCrHs
zQueryCompiler.__init__cCs2|j|j|j|j|j|j|j|j|j|j	|j
d�S)N)rOr�r�r�r�rlrri�select_query�compound_select_queryrr)�_parse_expression�_parse_param�_parse_passthrough�_parse_func�
_parse_clause�
_parse_entity�_parse_field�
_parse_sql�_parse_select_query�_parse_compound_select_query�_parse_strip_parensrr@r@rCrtQs�zQueryCompiler.get_parse_mapcCsd|j||jfS)Nz%s%s%s)rn)rArZr@r@rC�quotebszQueryCompiler.quotecCs||jkr|j|S|��Sr?)rq�upper)rA�fr@r@rCr�eszQueryCompiler.get_column_typecCs
|j|Sr?)rs)rA�qr@r@rC�get_ophszQueryCompiler.get_opcCst|��dd�d�S)NcSs
|djS�Nr)r�)r�r@r@rCrSlrTz.QueryCompiler._sorted_fields.<locals>.<lambda>�r�)r�rf)rA�
field_dictr@r@rC�_sorted_fieldskszQueryCompiler._sorted_fieldscCs|j|gfSr?)ro�rAr&rg�convr@r@rC�_parse_defaultnszQueryCompiler._parse_defaultc
Cs�t|jt�r|j}|�|j||�\}}|�|j||�\}}|jtjkrd|dkrd|sd|jr\dndgfS|jrndnd}|||�	|j�|f}	|	||fS)Nz()z0 = 1z(0 = 1)�%s %s %s�
(%s %s %s))
rQrr�
parse_noder@rCrEr�r�r�)
rAr&rgr�rZlparamsr@Zrparams�templaterir@r@rCr{qszQueryCompiler._parse_expressioncCs*|jr|�|�|j�|d�S|j|jgfSr?)r�r�r^ror�r@r@rCr}|sz QueryCompiler._parse_passthroughcCs^|jr0|r|j|jkrd}|�|�|j�||�S|dk	rL|�|�|j�|�S|j|jgfSdSr?)r�r�r�r^ror�r@r@rCr|�szQueryCompiler._parse_paramcCs8|jr
|pd}|�|j||�\}}d|jt|�f|fS)Nr�)rt�parse_node_listrrr�rr�rAr&rgr�rirjr@r@rCr~�szQueryCompiler._parse_funccCs2|�|j|||j�\}}|jr*dt|�}||fS�N�(%s))r�r�r�r�rrr�r@r@rCr�s�zQueryCompiler._parse_clausecCsd�t|j|j��gfSr�)r�r�r�rmr�r@r@rCr��szQueryCompiler._parse_entitycCs|jt|j�fSr?)r^r�rjr�r@r@rCr��szQueryCompiler._parse_sqlcCs<|r(d�|�||j�|�|j�f�}n|�|j�}|gfSr�)r�r�r}r�)rAr&rgr�rir@r@rCr��s
�zQueryCompiler._parse_fieldcCs�d}|jj|kr0|jj|kr0|j|j}}d}n|j|j}}d}|��}|j|kr\|j|_|�||�\}	}
|�||�||��\}}|r�|||	|
f\}
}}}n|	|
||f\}
}}}|jjr�d|
|j	|f}nd|
|j	|f}|||fS)NrzTFz((%s) %s (%s))r�)
r@rQr�alias_map_classr^�generate_select�calculate_alias_mapr��compound_select_parenthesesrX)rAr&rgr�ZcsqZfirst_qZsecond_qrB�new_map�firstZfirst_pr�Zsecond_pr�Zlp�rZrprir@r@rCr��s*

�z*QueryCompiler._parse_compound_select_querycCsV|��}|js6|r$t|t�r$|j}n
|jjj}|f|_|�	||�\}}dt
|�|fSr�)r��_explicit_selectionrQr r�r}rzr��_selectr�rr)rAr&rgr�r�Zselect_fieldr�rjr@r@rCr��s
z!QueryCompiler._parse_select_querycCs |�|j||�\}}t|�|fSr?)r�r&rrr�r@r@rCr��sz!QueryCompiler._parse_strip_parenscCsht|dd�}d}||jkrP|j||||�\}}||jkoJ|jdkoJ|dk}�nt|tttf�r||�|||�\}}d|}n�t|t	�r�|j
}|r�t|t�r�|j}t|t�r�|�
|�}	q�|�
t||j��}	n|��}	|	g}n�t|�r�t|t	�s�t|t��r|���||�}
|�|
||�\}}n@|dk	�rH|�
|�}	|�|	|d�\}}}n|�||d�\}}d}|||fS)NrQFr�T)r�rurvr�rQr�rVr�r�r*ror r�r�r�rQr
r:r�r�r6r��_parser�)rAr&rgr�Z	node_typer�rirjr�r^rl�_r@r@rCr��s@

��


�

zQueryCompiler._parsec
s�|�||��\}}}|r6�dk	r6|r6�fdd�|D�}t|t�r||jrNd|}|jrfd�|d|jf�}|jr|d�||jf�}|r�tdd�|D��r�g}g}t|�D] \}	}
t|
t�r�|�	|
�\}}q�||fS)Ncsg|]}��|��qSr@)r�r�r�r@rCr�sz,QueryCompiler.parse_node.<locals>.<listcomp>zNOT %sr��AScss|]}t|t�VqdSr?)rQr%)r�pr@r@rCr!sz+QueryCompiler.parse_node.<locals>.<genexpr>)
r�rQr%r(r!r�r*�any�	enumerater�)
rAr&rgr�rirjr�Zclean_paramsZ	clean_sql�idxr�ZcsqlZcparamsr@r�rCr��s"

zQueryCompiler.parse_noder�c
CsFg}g}|D]*}|�|||�\}}	|�|�|�|	�q|�|�|fSr?)r�r~r0r�)
rAr�rgr�r�rirjr&Znode_sqlZnode_paramsr@r@rCr�s
zQueryCompiler.parse_node_listcCs�|��}|dk	r|j|_|�|j|jjj�|j��D]V\}}|�||jj�|D]8}t|j	t
�rv|�|j	|j	j�qR|�|j	|j	jj�qRq6|�|�Sr?)
r�r^ryr}rz�table_alias�_joinsrfrQr�r%r6r�)rAr�rgr�r�Z
joined_modelsZjoin_objr@r@rCr�sz!QueryCompiler.calculate_alias_mapcCs|�t|�|�Sr?)r�r)rA�clausesrgr@r@rC�build_query*szQueryCompiler.build_querycCsZg}t�}|g}|�rV|��}||ks||kr0q|�|�||D�]}|}	|j}
t|jtttt	f�rv|j�
���}nl|j}|j
r�|j}
|j}n|j}
|j}|j}|r�t|
|j�}t||jj�}|j
r�||}}||k}ntd��t|
t�r�|
}n|�|
�|
���||
�}|��}||jk�r2t|j|�}nt|�}|�t||td�|��qBq|S)Nz Missing required join predicate.�ON)r�r#ryr�rQr�r>rorr r�r6r�r�r�r�r�r�r�rwr%r~r�r��join_mapr7)rA�joinsr}rgr�r�r��currr�r�r��
constraintr�Zfk_modelZpk_model�fkrr@Zdest_nr�Zjoin_sqlr@r@rC�generate_joins-sN




�zQueryCompiler.generate_joinscCs.|j}|jj}|�||�}t|t�r0t|�g}n�|jsBtd�g}n*td�g}|jdkrl|td�t	|j�g7}t
|j�}d|_|�
|td�f�|jdkr�|�|���||��n|�t|j��|jdk	r�|�td��|�tdd	�|jD���|�|j||�}|�r|�
|�|jdk	�r4|�
td
�|jg�|j�rT|�
td�t|j�g�|j�rp|�
td�|jg�|j�r�|�
td
�t|j�g�|jdk	�s�|j�r�|j�r�|jdk	�r�|jn|j}|�td|��|jdk	�r�|�td|j��|j\}	}
|	�r"|
�rdnd}|�t|��|�||�S)NZSELECTzSELECT DISTINCT)TFr�r��FROMZWINDOWcSs&g|]}tt|j�td�|����qS)r�)rr7r!ry)rrzr@r@rCr}s��z1QueryCompiler.generate_select.<locals>.<listcomp>�WHEREzGROUP BYZHAVINGr�zLIMIT %sz	OFFSET %szFOR UPDATE NOWAITz
FOR UPDATE)r}rzr�r�rQ�CompoundSelectr��	_distinctr7r�rr�r�r0�_fromr~r�r6r��_windowsr�r��_where�	_group_by�_having�	_order_by�_limit�_offset�	limit_max�_for_updater�)rAr�rgr�dbr�Z
select_clauseZjoin_clauses�limit�
for_updateZno_waitZstmtr@r@rCr�bsT





�


zQueryCompiler.generate_selectc
	Cs|j}|��}|�||jj�|jr0d|j}nd}t|�|��td�g}g}|�|j	�D]D\}}t
|ttf�s~t
||jd�}|�t|jdd�tj|dd��qZ|�t|��|jr�|�td	�|jg�|jdk	r�t|j�}	d
|	_|�td�|	g�|�||�S)NzUPDATE OR %sZUPDATEZSET�r�F�r�T)r�r�r��	RETURNING)r}r�ryrzr��_on_conflictr7r�r��_updaterQr%r*r.r�r~r>rEr�r�r�r0�
_returningrr�r�)
rAr�rrg�	statementr�r�rr^�returning_clauser@r@rC�generate_update�s2
�


zQueryCompiler.generate_updatecCs|dd�|D��S)NcSsg|]}|jdd��qS)Fr��r��rrr@r@rCr�sz3QueryCompiler._get_field_clause.<locals>.<listcomp>r@)rAr�clause_typer@r@rC�_get_field_clause�s�zQueryCompiler._get_field_clausecCs�|j}|j}|��}|�||jj�|jr4|jj}n|jrFd|j}nd}t	|�|�
�g}|jdk	r�|jr||�
|�|j��|�
t|j��n�|jdk	�rfgg}}d}	|��D]n}
|	s�t|
��t�d�d�}d}	g}|D]4}|
|}
t|
ttf��st|
|jd�}
|�
|
�q�|�
t|��q�|�rF|�|�|�t	d�t|�g�n |jjj�rf|�
|j�|j��|j �r�|�t	d	�|j|�!�td
�g�n.|j"dk	�r�t#|j"�}d|_$|�t	d	�|g�|�%||�S)NzINSERT OR %s INTOzINSERT INTOFr�r�Tr�ZVALUESr�)r�r�)&r}rzr�ryr��_upsertr��
upsert_sqlr�r7r��_query�_fieldsr~r�r��_rows�
_iter_rowsr��keysrX�
attrgetterrQr%r*r.r�r�r0r��auto_increment�default_insert_clause�is_insert_returning�get_primary_key_fieldsr�rr�r�)rAr�rrIrgr�r�rZ
value_clausesZhave_fields�row_dictr|rr^r�r@r@rC�generate_insert�sl


��
���
zQueryCompiler.generate_insertcCsf|j}td�|��g}|jr0|�td�|jg�|jdk	r\t|j�}d|_|�td�|g�|�|�S)NzDELETE FROMr�r�r�)	r}r7r�r�r0r�rr�r�)rAr�rr�r�r@r@rC�generate_deletes

zQueryCompiler.generate_deletecCs |�|���}|�|�}t|�Sr?)r�r�r�r)rArr�r�r@r@rC�field_definition
s
zQueryCompiler.field_definitioncCsjtd�t|���td�|j��t|j���g}|jrH|�td|j��|jrb|�td|j��t|�S)NzFOREIGN KEYZ
REFERENCESzON DELETE %szON UPDATE %s)	r7r�r�rr�r>r~r?r)rArr�r@r@rC�foreign_key_constraints
�z$QueryCompiler.foreign_key_constraintcs�fdd�}|S)Ncst|��}|�|||��Sr?)r�r�)rAr�r�r��
function_namer@rCr�"s
z/QueryCompiler.return_parsed_node.<locals>.innerr@)r�r�r@r�rC�return_parsed_nodesz QueryCompiler.return_parsed_nodecCsN|pd|jj|j|jjjf}|�|�}ttd�|��td�t|�f|j	��S)Nzfk_%s_%s_refs_%szALTER TABLEzADD CONSTRAINT)
rzr�r�rr�rr7r�r r�)rAr}rr�Z	fk_clauser@r@rC�_create_foreign_key's�
��z!QueryCompiler._create_foreign_keyr�Fc	s�|rdnd}|j�gg}}�jrP�fdd��jjD�}|�ttd�t|����jD]4}|�|�	|��t
|t�rV|jsV|�|�
|��qV|jjr�|jjD] }t
|t�s�t|�}|�|�q�tt|�|��t||��S)NzCREATE TABLE IF NOT EXISTSzCREATE TABLEcsg|]}�j|���qSr@)rr��rr��rIr@rCr;s�z/QueryCompiler._create_table.<locals>.<listcomp>r�)rz�
composite_keyr�rRr~rr7r��declared_fieldsr�rQr r=r�r�r%r�)	rAr}�safer��columnsr�Zpk_colsrr�r@r�rC�
_create_table5s2

��


�zQueryCompiler._create_tabler�cCs6|rdnd}t|�|��g}|r.|�td��t|�S)NzDROP TABLE IF EXISTSz
DROP TABLE�CASCADE�r7r�r~r)rAr}�
fail_silently�cascader�r�r@r@rC�_drop_tablePs
zQueryCompiler._drop_tabler�cCs<td�|��g}|r"|�td��|r4|�td��t|�S)NzTRUNCATE TABLEzRESTART IDENTITYr�r�)rAr}�restart_identityr�r�r@r@rC�_truncate_tableXszQueryCompiler._truncate_tabler�cCsRd|d�|�f}t|�dkrNt�|�d����}d|dd�|dd�f}|S)Nz%s_%sr��@r��7�)r�r��hashlibZmd5rXZ	hexdigest)rA�tabler�r�Z
index_hashr@r@rC�
index_namebs
zQueryCompiler.index_namecGs^|jj}|rdnd}|�|dd�|D��}tt|�t|�td�|��tdd�|D��f|��S)NzCREATE UNIQUE INDEXzCREATE INDEXcSsg|]
}|j�qSr@�r�r�r@r@rCrlsz/QueryCompiler._create_index.<locals>.<listcomp>r�cSsg|]}|���qSr@r�r�r@r@rCrrs)rzr�r�rr7r r�r�)rAr}rr�r@�tbl_namer�r�r@r@rC�
_create_indexis��zQueryCompiler._create_indexr�cCs<|jj}|rdnd}|�|dd�|D��}tt|�t|��S)NzDROP INDEX IF EXISTSz
DROP INDEXcSsg|]
}|j�qSr@r�r�r@r@rCrysz-QueryCompiler._drop_index.<locals>.<listcomp>)rzr�r�rr7r )rAr}rr�r�r�r�r@r@rC�_drop_indexvszQueryCompiler._drop_indexrcCsttd�t|��S)NzCREATE SEQUENCE�rr7r �rAZ
sequence_namer@r@rC�_create_sequence}szQueryCompiler._create_sequencercCsttd�t|��S)Nz
DROP SEQUENCErrr@r@rC�_drop_sequence�szQueryCompiler._drop_sequencer)rlrmNN)NN)Nr�)N)N)N)N)F)FF)FF)F)YrErFrGrprEr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrr&r�r�r�r�r�r\r�rrtr�r�r�r�r�r{r}r|r~rr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r��create_foreign_keyr��create_tabler��
drop_tabler��truncate_tabler�r��create_indexr�
drop_indexr�create_sequencer�
drop_sequencer@r@r@rCrhs����
	
	'

	

5
?>



�

rhc@seZdZddd�ZdS)�SqliteQueryCompilerFcCs|����Sr?)�deleteri)rAr}r�r�r@r@rCr�sz"SqliteQueryCompiler.truncate_tableN)FF)rErFrGrr@r@r@rCr
�s�r
c@s eZdZdd�Zdd�ZeZdS)�ResultIteratorcCs||_d|_dSr�)�qrw�_idx)rArr@r@rCr�szResultIterator.__init__cCsf|j|jjkr|jj|j}n6|jjsP|j��}|jj�|�|jjd7_nt�|jd7_|Sr')rr�_ct�
_result_cache�
_populated�iterater~�
StopIterationrcr@r@rC�next�s
zResultIterator.nextN)rErFrGrr�__next__r@r@r@rCr�src@sdeZdZdZddd�Zdd�Zedd��Zd	d
�Zdd�Z	d
d�Z
dd�Zdd�ZeZ
ddd�ZdS)�QueryResultWrapperz�
    Provides an iterator over the results of a raw Query, additionally doing
    two things:
    - converts rows from the database into python representations
    - ensures that multiple iterations do not result in multiple queries
    NcCsP||_||_d|_d|_g|_d|_d|_|dk	r@|\|_|_nd|_|_dS)NrF)	r�cursorrrrr�_initialized�column_meta�	join_meta)rArrrIr@r@rCr�szQueryResultWrapper.__init__cCs|jrt|j�St|�SdSr?)r�iterrrrr@r@rC�__iter__�s
zQueryResultWrapper.__iter__cCs|��|jSr?)�
fill_cacherrr@r@rCrp�szQueryResultWrapper.countcCs|jSr?)rprr@r@rC�__len__�szQueryResultWrapper.__len__cCs|Sr?r@�rA�rowr@r@rC�process_row�szQueryResultWrapper.process_rowcCsV|j��}|s2d|_t|jdd�s,|j��t�n|jsL|�|jj�d|_|�	|�S)NTr�)
r�fetchonerr��closerrr�descriptionr$r"r@r@rCr�s

zQueryResultWrapper.iterateccs|��VqdSr?)rrr@r@rC�iterator�szQueryResultWrapper.iteratorcCsh|j|jkr*|j|j}|jd7_|S|jr4t�|��}|j�|�|jd7_|jd7_|Sr')rrrrrrr~)rAr2rr@r@rCr�szQueryResultWrapper.nextcCs`|p
td�}|dkrtd��|j|_|js\||jkr\zt|�Wq$tk
rXYq\Yq$Xq$dS)NZInfrz"Negative values are not supported.)r�rwrrrrr�rA�nr@r@rCr �szQueryResultWrapper.fill_cache)N)N)rErFrGrrrr�rpr!r$rr(rrr r@r@r@rCr�s


rc@s$eZdZdd�Zdd�Zdd�ZdS)�ExtQueryResultWrappercCs�t|�}g|_}|jdk	rht|j�}t|j�D](\}}|�||�s0|�||d|�q0||krldSnd}t||�D]}|�||d|�qvdSr�)r�r�rr��_initialize_node�_initialize_by_namer�)rAr'Zn_colsr�Zn_metar�r&r@r@rCr�s


z ExtQueryResultWrapper.initializecCsH|jjj}||kr2||}|j�||j|jf�n|j�||df�dSr?)rrzr�r�r~r�r�)rAr�r�Z
model_colsrr@r@rCr-	s

z)ExtQueryResultWrapper._initialize_by_namecCs�t|t�r*|j�||jp|j|jf�dSt|t�r�t|j	�r�|j	d}t|t�r�|jpb|jpb|j}|j
rp|jprd}|j�|||f�dSdS)NTrF)rQrr�r~r!r�r�ror�rrrt)rAr&r��argr�r�r@r@rCr,
	s


z&ExtQueryResultWrapper._initialize_nodeN)rErFrGrr-r,r@r@r@rCr+�sr+c@seZdZdd�ZdS)�TuplesQueryResultWrappercst�fdd�t|�D��S)Ncs8g|]0\}}�j|ddkr"|n�j|d|��qS)rLNr�)rr��colrr@rCr	s�z8TuplesQueryResultWrapper.process_row.<locals>.<listcomp>)rVr�r"r@rrCr$	s�z$TuplesQueryResultWrapper.process_rowN�rErFrGr$r@r@r@rCr/	sr/c@seZdZdd�ZdS)�NaiveQueryResultWrappercCsJ|��}|jD].\}}}t|||dk	r2|||�n||�q|��|Sr?)rr�r+�_prepare_instance)rAr#r#r��columnr�r@r@rCr$!	s
&z#NaiveQueryResultWrapper.process_rowNr1r@r@r@rCr2 	sr2c@seZdZdd�ZdS)�DictQueryResultWrappercCs:i}|jD]*\}}}|dk	r(|||�n||||<q
|Sr?r�)rAr#rvr�r4r�r@r@rCr$,	s"z"DictQueryResultWrapper.process_rowNr1r@r@r@rCr5+	sr5c@s>eZdZdd�Zdd�Zdd�Zdd�Zdd
d�Zdd
�Zd	S)�ModelQueryResultWrappercCs4|��\|_}tdd�|jD��|_|�|�|_dS)Ncss|]}t|t�r|VqdSr?)rQr)rr0r@r@rCr!8	s
�z5ModelQueryResultWrapper.initialize.<locals>.<genexpr>)�generate_column_map�
column_mapr�r�_col_set�generate_join_list�	join_list)rAr'Z	model_setr@r@rCr6	sz"ModelQueryResultWrapper.initializec	Cs�g}t|jg�}t|j�D]�\}}d}}t|t�rrt|t�rT|j}|j}|jj	}n|j
}}|j	}|jpn|j}nL|j
dkr�|j}}n
|j
}}t|t�r�|jr�|j}nt|t�r�|jd}|�||||f�|�|�q||fS�N���)r�rr�rrQrr��_model_alias�field_instancer�r}r!r�r)r%r rmr~ry)	rAr8r�r�r&r�r�r��constructorr@r@rCr7<	s,







z+ModelQueryResultWrapper.generate_column_mapcCs�g}|j}|jg}|r�|��}||kr(q||D]|}|j}|j|ksN|j|kr0|jdk	r�|j|jk}|j|jk}	|jj	o~|p~|	}
nd}
}}	|�
||
||	f�|�
|j�q0q|Sr')rrr#r�r�r�r�r9r�r�r~)rAr�r;r��stack�currentr�r��
fk_present�
pk_presentZcheckr@r@rCr:X	s2
��z*ModelQueryResultWrapper.generate_join_listcCs.|�|�}|�|�}|D]}|��q|dSr�)�construct_instances�follow_joinsr3)rAr#�	collected�	instancesr�r@r@rCr$u	s



z#ModelQueryResultWrapper.process_rowNcCs�i}t|j�D]x\}\}}}}|dk	r0||kr0q||}	||krJ|�||<||}
|dkrj|jj|d}t|
||dkr||	n||	��q|Sr�)r�r8rr'r+)rAr#r�Zcollected_modelsr�r�r@r�r�r^r#r@r@rCrE|	s
z+ModelQueryResultWrapper.construct_instancesc
	CsN||jg}|jD�]4\}}}}||j}z||j}Wntk
rV||j}YnXd}	|r�|rv|j�|jj	�}	n|r�|j�|j
j	�}	|	s�q|j
dk	}
|
o�|j|jko�t||j
j	�dk}|r�t
||j
j	|j|j�|j�r0|
�o|jdk	�ot||j
j	�dk	�o|j�|jj	�dk}|�r0t
||jj	|�t
||j|�|�|�q|S�NT)rr;r�r��KeyErrorr�r�r�r�r�r�r�r�r+r�r~)
rArG�preparedr�Z
check_nullrCrDr2�joined_instZhas_fkZmpkZcan_populate_joined_pkZcan_populate_joined_fkr@r@rCrF�	sX


��
�����z$ModelQueryResultWrapper.follow_joins)N)	rErFrGrr7r:r$rErFr@r@r@rCr65	s
r6�	JoinCache�r�r�cs8eZdZ�fdd�Z�fdd�Zdd�Zdd�Z�ZS)	�AggregateQueryResultWrappercsg|_tt|�j||�dSr?)�_rowrrOrrrr@rCr�	sz$AggregateQueryResultWrapper.__init__cs�tt|��|�t�|_|jD]\}}}}|j�|�qt�|_i|_i|_	i|_
|jD]�\}}}}|jrt|j
j}n|j}|jp�|j}|r�|j�|j�n&|j
�|jt��|j
|j�|j�|j	�|ji�t||jp�|d�|j	|j|j<qXi|_i}t|j�D]^\}\}}	}
}||jk�rD|j�|g�|j|�||
f�|�|g�||�||
f��q|jD]D}||jk�r��ql|j
�|d�}|D]}
|j|�||
��q��qldS)NrNr@)rrOrr��
all_modelsr8ryZmodels_with_aggregateZback_references�source_to_destZdest_to_sourcer;r�r�rAr�r�r�r&r�rMr6�columns_to_comparer�r~r�r0)rAr'r�r�r�r�r�Zkey_to_columnsr�r}�col_namer�ZsourcesZjoined_modelrr@rCr�	sL
�

�z&AggregateQueryResultWrapper.initializecCsDi}|j��D]0\}}g||<|D]\}}||�||�q"q|Sr?)rSrfr~)rAr#r�r}Zcolumn_datar�rTr@r@rC�read_model_data�	sz+AggregateQueryResultWrapper.read_model_datac
Cs8|jr|j��}n
|j��}|sDd|_t|jdd�s>|j��t�n|js^|�	|jj
�d|_dd�}i}|�|�}||j}|�
�D]"\}}t�||<|||||�<q�|�|�}|j��}	|	dkrʐq~t�}
|�|	�}|�
�D]\}}
|||
kr�|
�|�q�|
�s|j�|	��q~|j|
}|�|	|�}|�
�D]D\}}d}|j��D]}|dk	�rLd}�qL|�s6|||||�<�q6q�|jg}|g}|�r |��}||jk�r��q�|j|D�]d}z|j||j\}}Wntk
�r�Y�q�YnX|j�s|j�r�||��D]}t||g��q|j|k�r0�q�||j�
�D]j\}}|dk�rT�q>z|||j|jj}Wntk
�r�Y�q>YnXt||��|�|�|��q>n`|�r|j|k�rq�||�
�D]<\}}||j|j|jj}t||jj|�|�|��q�|�|j��q��q�|D]}|���q$|S)NTr�cs,�jjr$t�fdd��jjjD��S���S)Ncsg|]}�j|�qSr@)r�rSrUr@rCr
s�zHAggregateQueryResultWrapper.iterate.<locals>._get_pk.<locals>.<listcomp>)rzr�rVr�rRrQrUr@rUrC�_get_pk
s
�z4AggregateQueryResultWrapper.iterate.<locals>._get_pkF) rPr#rr%rr�r&rrrr'rErrfrrUr�ryr~rQr�r|rrRr�rJr�r�r+r�r�r3)rAr#rVZidentity_mapZ_constructedZprimary_instancer�r#Z
model_dataZcur_rowZduplicate_modelsZcur_row_datar}�dataZdifferent_modelsZ
new_instancesZall_noner^rArHrBr�r�r��pkr2rLr@r@rCr
s�










���z#AggregateQueryResultWrapper.iterate)rErFrGrrrUrrr@r@rrCrO�	s6rOcs�eZdZdZdZ�fdd�Zdd�Zdd�Zd	d
�Zdd�Z	d+dd�Z
dd�Zedd��Z
edd��Zed,dd��Zed-dd��Zd.dd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd/d)d*�Z�ZS)0�Queryz?Base class representing a database query on one or more tables.Tcs@tt|���||_|jj|_d|_||_|jgi|_d|_	dSrI)
rrYrr}rzr�r��
_query_ctxr�r��rAr}rr@rCrv
s
zQuery.__init__cCs|��\}}d|j||fS)Nr�)rir}�rArirjr@r@rCra�
szQuery.__repr__cCs t|�|j�}|j|_|�|�Sr?)rr}r��_clone_attributesr�r@r@rCr��
szQuery.clonecCs,|jdk	r|j��|_|��|_|j|_|Sr?)r�r��_clone_joinsr�rZr�r@r@rCr]�
s


zQuery._clone_attributescCstdd�|j��D��S)Ncss|]\}}|t|�fVqdSr?)r�)rZmc�jr@r@rCr!�
sz%Query._clone_joins.<locals>.<genexpr>)�dictr�rfrr@r@rCr^�
s�zQuery._clone_joinsNcCs,ttj|�}|dkr|S|p tj}|||�Sr?)rPrXrY)rA�initialr[ZconjunctionZreducedr@r@rC�_add_query_clauses�
s

zQuery._add_query_clausescCsxg}|D]j}t|t�r"|�|�qt|t�r8|�|�qt|t�rR|�|���qt|�rt|t	�r|�|j
j�q|Sr?)rQr%r~rYr�r0�get_proxy_fieldsr
r:r*rzr�)rAr��accumr.r@r@rC�_model_shorthand�
s


zQuery._model_shorthandcGs|�|j|�|_dSr?)rbr��rAr[r@r@rCr5�
szQuery.wherecGs|�|j|tj�|_dSr?)rbr�rX�or_rfr@r@rC�orwhere�
s
�z
Query.orwherecCs�|j}|s8t|t�p(t|�o(|j�|�}|rNtd��nt|t�rN|jj|}|j	�
|g�|j	|�t||||��t|t�s�||_dS)Nz#A join condition must be specified.)
rZrQr�r
rz�
rel_existsrwrrr�r&r~r�)rAr�r�r�r�Zrequire_join_conditionr@r@rCr��
s
�


z
Query.joincCs|p|j|_dS)z"Change or reset the query context.N)r}rZr[r@r@rC�switch�
szQuery.switchcKsN|j}|j�|g�D]}|j|kr|Sq|�|�j|fd|i|���|�S)Nr�)rZr�r�r�rjr�)rA�lm�rmr�Zjoin_kwargsZctxr�r@r@rC�ensure_join�
s


zQuery.ensure_joincCs�g}g}ttf}t|���D]�\}}|j}d|kr`|�dd�dtkr`|�dd�\}}t|}ntj}|�	d�D](}	t
||	�}
t|
|�rp|
j}|�
|
�qp|�
t|
||��q||fS)N�__r�)r r3r�rfr}�rsplit�
DJANGO_MAPrEr��splitr�rQrr~r>)rAZqdictrdr�Zrelationshipr�r^r�rC�piece�
model_attrr@r@rC�convert_dict_to_node�
s 


zQuery.convert_dict_to_nodecOsHt�}|r$|ttjdd�|D��M}|r6|tf|�M}t|g�}t�}|r�|��}t|t	�s^qFd|j
fd|jffD]`\}}t|t�r�|�|j
�\}	}
|�|
�ttj|	�}|j|_|j|_t|||�qr|�|�qrqF|j}|��}	|D]T}t|t��r|j|j}
}|}n"t|t��r.|jj|j}
}|j}|	�|
||�}	q�|	�|�S)NcSsg|]}|���qSr@r�)rr5r@r@rCr�
sz Query.filter.<locals>.<listcomp>rr@)r%rPrXrYrrr��popleftrQr>rr@rtr�r�r(r!r+r~r�r r}rr3rrmr5)rAr�r�Zdq_noder�Zdq_joinsr�Zsiderrr�r�rOrrkrl�	field_objr@r@rCrn�
s>



zQuery.filtercCs
|j��Sr?)r�r�rr@r@rCr�	szQuery.compilercCst�dSr?�r�rr@r@rCrisz	Query.sqlcCs|��\}}|j�|||j�Sr?)rir��execute_sql�require_commitr\r@r@rC�_executeszQuery._executecCst�dSr?rwrr@r@rC�executesz
Query.executeFcCs6|r|����}n|����}|r.|s.|dS|SdSr�)�tuplesr�rzr%)rAZas_tuple�convertr#r@r@rC�scalarszQuery.scalar)N)NN)N)N)FF)rErFrGrryrrar�r]r^rbrer�r5rhr�rjrmrtrnr�rirzr{r~rr@r@rrCrYr
s2




(rYcsteZdZdZ�fdd�Zdd�Zed�Zed�Zed�Z	e
dd
d��Ze
ddd
��Zdd�Z
dd�Zdd�Z�ZS)�RawQueryzh
    Execute a SQL query, returning a standard iterable interface that returns
    model instances.
    cs6||_t|�|_d|_d|_d|_tt|��|�dSr')	�_sqlr��_params�_qr�_tuples�_dictsrrr)rArr�rjrr@rCr%s
zRawQuery.__init__cCs*t|j|jf|j��}|j|_|j|_|Sr?)rr}r�r�r�r�r�r@r@rCr�-szRawQuery.clone�joiningr5rjTcCs
||_dSr?�r��rAr|r@r@rCr|7szRawQuery.tuplescCs
||_dSr?�r��rA�dictsr@r@rCr�;szRawQuery.dictscCs|j|jfSr?)r�r�rr@r@rCri?szRawQuery.sqlcCsX|jdkrR|jr|j�t�}n |jr2|j�t�}n|j�t�}||j|�	�d�|_|jSr?)
r�r�r��get_result_wrapper�RESULTS_TUPLESr��
RESULTS_DICTS�
RESULTS_NAIVEr}rz)rAZQRWr@r@rCr{Bs
zRawQuery.executecCst|���Sr?�rr{rr@r@rCrMszRawQuery.__iter__)T)T)rErFrGrrr�rr�r5rjr�r|r�rir{rrr@r@rrCr srcKs8|�dd�}|rtd��|r,|p"g|p*dS|r4|SdS)Nr0Fz,"extend" is the only valid keyword argument.)r#rw)ZorigZnew_valr�r0r@r@rC�allow_extendPsr�cs�eZdZdZ�fdd�Z�fdd�Zdd�Zee�Zed�Z	ed	�Z
ed
�Zdd�Zd
d�Z
dd�Zee�Zedd��Zedd��Zedd��Zedd��Zedd��Zedd��Zedd��ZedZd d!��Zed[d#d$��Zed\d&d'��Zed]d(d)��Zed^d*d+��Zed_d,d-��Zed`d.d/��Zedad1d2��Zdbd3d4�Z dcd5d6�Z!ddd7d8�Z"ded9d:�Z#dfd;d<�Z$d=d>�Z%d?d@�Z&dgdBdC�Z'dhdDdE�Z(dFdG�Z)dHdI�Z*dJdK�Z+dLdM�Z,dNdO�Z-dPdQ�Z.dRdS�Z/dTdU�Z0dVdW�Z1e2�r�dXdY�Z3�Z4S)ir�rycs�tt|��|�|jj|_|j|�d|_d|_d|_	d|_
d|_d|_d|_
d|_d|_d|_d|_d|_d|_d|_d|_dS)NF)FF)rr�rr��
commit_selectry�_SelectQuery__selectr�r�r�r�r�r�r�r�r��_naiver�r��_aggregate_rowsr!r�)rAr}�	selectionrr@rCr\s$

zSelectQuery.__init__cstt|��|�}|j|_t|j�|_|jdk	rhg|_|jD],}t|t�rZ|j�	|�
��q:|j�	|�q:|jdk	r~t|j�|_|jr�|j�
�|_|j
dk	r�t|j
�|_
|jdk	r�t|j�|_|j|_|j|_|j|_|j|_|j|_|j|_|j|_|j|_|j|_|Sr?)rr�r]r�r�r�r�rQr%r~r�r�r�r�r�r�r�r�r�r�r�r�r�r!)rAr�r�rr@rCr]ps6





zSelectQuery._clone_attributescs�fdd�}|S)Ncs0|jjjj}�|kr td���t|j|�|�S)Nz!Your database does not support %s)r}rzr��compound_operationsrwr�)rArZZ
supported_ops�rXr@rCr��s�z&SelectQuery.compound_op.<locals>.innerr@)rXr�r@r�rC�compound_op�szSelectQuery.compound_op�UNION�	INTERSECT�EXCEPTcCs4|j�td���t||@��d����}||B|S)Nr�r�)r}r4r7�from_r�r6rx)rAr@Zwrapped_rhsr@r@rCrY�s�zSelectQuery.__xor__cCst�d�||�S)N�	UNION ALL)r��_compound_op_staticr?r@r@rC�	union_all�szSelectQuery.union_allcGs,t|�dk|_|p|jjj}|�|�|_dSr�)r�r�r}rzr�rer��rAr�r@r@rCZ__select�szSelectQuery.__selectcGs|rt|�nd|_dSr?)r�r�)rAr�r@r@rCr��szSelectQuery.from_cOs|r|�|�nd|_dSr?)rer�rr@r@rC�group_by�szSelectQuery.group_bycGs|�|j|�|_dSr?)rbr�rfr@r@rC�having�szSelectQuery.havingcOst|jt|�f|�|_dSr?)r�r�r�rr@r@rCrx�szSelectQuery.order_bycOst|jt|�f|�|_dSr?)r�r�r�)rAZwindowsr�r@r@rCrz�szSelectQuery.windowcCs
||_dSr?)r�)rAZlimr@r@rCr��szSelectQuery.limitcCs
||_dSr?)r�)rAZoffr@r@rC�offset�szSelectQuery.offset�cCs$|dkr|d8}||_|||_dS�Nrr�)r�r�)rAZpageZpaginate_byr@r@rC�paginate�szSelectQuery.paginateTcCs
||_dSr?)r�)rAZis_distinctr@r@rC�distinct�szSelectQuery.distinctFcCs||f|_dSr?)r�)rAr�Znowaitr@r@rCr��szSelectQuery.for_updatecCs
||_dSr?)r�)rA�naiver@r@rCr��szSelectQuery.naivecCs
||_dSr?r�r�r@r@rCr|�szSelectQuery.tuplescCs
||_dSr?r�r�r@r@rCr��szSelectQuery.dictscCs
||_dSr?)r�)rA�aggregate_rowsr@r@rCr��szSelectQuery.aggregate_rowsNcCs
||_dSr?r4)rAr6r@r@rCr6�szSelectQuery.aliascCsx|dkrt�|jj��d�}|j|kr4|�|j�}n|��}|�	|j|�}|j
sbdd�|jD�|_
t|j�|f|_|S)NrpcSsg|]}|���qSr@)r6)rrtr@r@rCr�sz(SelectQuery.annotate.<locals>.<listcomp>)
r�Countrzr�r6rZrjr}r�rmr�r�rV)rArZ
annotationr�r@r@rC�annotate�s
zSelectQuery.annotatecCs*|dkrt�td��}|��}|g|_|S)Nr�)rr�r7rxr�)rA�aggregationr�r@r@rC�
_aggregate�s
zSelectQuery._aggregatecCs|�|�j|d�S)N�r})r�r~)rAr�r}r@r@rC�	aggregate�szSelectQuery.aggregatecCs4|js|js|js|jr$|j|d�S|jdd�p2dS)N��clear_limitFr�r)r�r�r�r��
wrapped_countr��rAr�r@r@rCrpszSelectQuery.countcCsJ|��}|rd|_|_|��\}}d|}|jj|f|��}|��pHdS)Nz+SELECT COUNT(1) FROM (%s) AS wrapped_selectr)rxr�r�rir}�rawr~)rAr�r�rirj�wrappedZrqr@r@rCr�szSelectQuery.wrapped_countcCs$|�dd�}td�g|_t|���S)Nr��1)r�r7r�r/r~r�r@r@rC�existsszSelectQuery.existscCsH|�dd�}zt|���WStk
rB|j�d|����YnXdS)Nr�z:Instance matching query does not exist:
SQL: %s
PARAMS: %s)r�rr{rr}rrir�r@r@rCr�s��zSelectQuery.getr�cCs<|��}|�|�|jd|�}|r8|dkr4|dS|SdS�Nr�r)r{r r)rAr*rvr�r@r@rC�peeks

zSelectQuery.peekcCs"|j|kr||_d|_|j|d�S)NT)r*)r�r�r�r)r@r@rCr�&s
zSelectQuery.firstcCs|���|�Sr?)r�r�rr@r@rCri,szSelectQuery.sqlcCsT|j}|jD]B}t|t�r*|j|kr*dSt|t�r|jdk	r|j|krdSqdS)NFT)r}r�rQrr%r))rAr}r&r@r@rC�verify_naive/s

zSelectQuery.verify_naivecCs|j|jfSr?)r�r�rr@r@rC�get_query_meta9szSelectQuery.get_query_metacCsf|jr|j�t�S|jr$|j�t�S|js8|jr8|��rD|j�t	�S|j
rV|j�t�S|j�t�SdSr?)
r�r�r�r�r�r�r�r�r�r�r��RESULTS_AGGREGATE_MODELS�RESULTS_MODELSrr@r@rC�_get_result_wrapper<szSelectQuery._get_result_wrappercCsN|js|jdkrD|j}|��}|��}|||��|�|_d|_|jS|jSdSr')r�r�r}r�r�rz)rAr}Z
query_meta�
ResultWrapperr@r@rCr{HszSelectQuery.executecCst|���Sr?r�rr@r@rCrSszSelectQuery.__iter__cCst|�����Sr?�rr{r(rr@r@rCr(VszSelectQuery.iteratorcCsN|��}t|t�r|j}n|}|dk	r:|dkr6|dnd}|�|�|j|Sr�)r{rQ�slice�stopr r)rAr^rvr�r@r@rCrdYs

zSelectQuery.__getitem__cCst|���Sr?)r�r{rr@r@rCr!dszSelectQuery.__len__cCst|�Sr?)�idrr@r@rCr�hszSelectQuery.__hash__)r�)T)TF)T)T)T)T)N)N)N)NT)F)F)r�)r�)5rErFrGrQrr]r�rr�rTrSrVrYr�r�r�r4r�r�r�rxrzr�r�r�r�r�r�r|r�r�r6r�r�r�rpr�r�r�r�r�rir�r�r�r{rr(rdr!�PY3r�rr@r@rrCr�Ys|












	


r�c@s$eZdZdd�Zdd�Zdd�ZdS)�NoopSelectQuerycCs|j��dfS�Nr@)r��get_noop_sqlrr@r@rCrilszNoopSelectQuery.sqlcCsdS)Nr�r@rr@r@rCr�oszNoopSelectQuery.get_query_metacCs|j�t�Sr?)r�r�r�rr@r@rCr�rsz#NoopSelectQuery._get_result_wrapperN)rErFrGrir�r�r@r@r@rCr�ksr�csPeZdZdZd�fdd�	Z�fdd�Zddd	�Zd
d�Zdd
�Zdd�Z	�Z
S)r�rzNcs(||_||_||_tt|��|g�dSr?)rrXr@rr�r)rAr}rrXr@rr@rCrxszCompoundSelect.__init__cs,tt|��|�}|j|_|j|_|j|_|Sr?)rr�r]rrXr@r�rr@rCr]~s
z CompoundSelect._clone_attributesFcCs|j|d�S)Nr�)r�r�r@r@rCrp�szCompoundSelect.countcCs
|j��Sr?)rr�rr@r@rCr��szCompoundSelect.get_query_metacCs|j��o|j��Sr?)rr�r@rr@r@rCr��szCompoundSelect.verify_naivecCs�|jr|j�t�S|jr$|j�t�S|jr6|j�t�S|jj	pD|j
j	}|jjpZ|j
jpZ|j}|sl|rl|��rx|j�t
�S|j�t�SdSr?)r�r�r�r�r�r�r�r�rr�r@r�r�r�r�)rAZ	has_joinsZis_naiver@r@rCr��sz"CompoundSelect._get_result_wrapper)NNN)F)rErFrGrQrr]rpr�r�r�rr@r@rrCr�us
r�csteZdZ�fdd�Z�fdd�Zdd�Zeedd���Zeedd
d���Zeeddd
���Z	dd�Z
dd�Z�ZS)�_WriteQuerycs,d|_d|_d|_d|_tt|��|�dSr')r�r�r�r�rr�rr[rr@rCr�s
z_WriteQuery.__init__cs6tt|��|�}|jr2t|j�|_|j|_|j|_|Sr?)rr�r]r�r�r�r�r�rr@rCr]�sz_WriteQuery._clone_attributescs�fdd�}|S)Ncs0|jjj}|js tdt|����|f|�|�S)Nz/RETURNING is not supported by your database: %s)r}rzr�r�rwr)rAr�r�r��r,r@rCr��s
�z-_WriteQuery.requires_returning.<locals>.innerr@)r,r�r@r�rC�requires_returning�sz_WriteQuery.requires_returningcGs>t|�dkr |ddkr d|_n|s.|jjj}|�|�|_dSr�)r�r�r}rzr�rer�r@r@rC�	returning�s

z_WriteQuery.returningTcCs
||_dSr?r�r�r@r@rCr|�sz_WriteQuery.tuplescCs
||_dSr?r�r�r@r@rCr��sz_WriteQuery.dictscCs:|jdk	r.|jr|j�t�S|jr.|j�t�S|j�t�Sr?)r�r�r�r�r�r�r�r�rr@r@rCr��s
z_WriteQuery.get_result_wrappercCs2|��}|j|jgif}||j|��|�|_|jSr?)r�r�r}rzr�)rAr�rIr@r@rC�_execute_with_result_wrapper�sz(_WriteQuery._execute_with_result_wrapper)T)T)
rErFrGrr]r�r�r�r|r�r�r�rr@r@rrCr��s	r�cs`eZdZd�fdd�	Z�fdd�Zeddd��Zed�Zd	d
�Z	dd�Z
d
d�Zdd�Z�Z
S)�UpdateQueryNcs ||_d|_tt|��|�dSr?)r�r�rr�r)rAr}r�rr@rCr�szUpdateQuery.__init__cs(tt|��|�}t|j�|_|j|_|Sr?)rr�r]r`r�r�r�rr@rCr]�szUpdateQuery._clone_attributescCs
||_dSr?�r��rA�actionr@r@rC�on_conflict�szUpdateQuery.on_conflictr�cCs|���|�Sr?)r�r�rr@r@rCri�szUpdateQuery.sqlcCs@|jdk	r|jdkr|��S|jdk	r,|jS|j�|���SdSr?�r�r�r�r��
rows_affectedrzrr@r@rCr{�s

zUpdateQuery.executecCs |jjjjstd��t|���S)NzwUPDATE queries cannot be iterated over unless they specify a RETURNING clause, which is not supported by your database.)r}rzr�r�rwrr{rr@r@rCr�szUpdateQuery.__iter__cCst|�����Sr?r�rr@r@rCr(�szUpdateQuery.iterator)N)N)rErFrGrr]r�r�rr�rir{rr(rr@r@rrCr��sr�cs�eZdZd�fdd�	Zdd�Z�fdd�Zed	�Zed
�Ze	ddd
��Z
e	ddd��Ze	ddd��Ze
dd��Zdd�Zdd�Zdd�Z�ZS)�InsertQueryNFcsftt|��|�d|_|dk	p$|dk	|_d|_|dk	r>||_n|pDig|_||_||_||_	d|_
dSr')rr�rr��_is_multi_row_insert�_return_id_listr�r�r��_validate_fieldsr�)rAr}r��rowsrr��validate_fieldsrr@rCr
szInsertQuery.__init__c
#s�|jj}|jr |j��fdd�}|j}|j}|jD]�}|��}t�}|D]B}|jrZ||�||j	krp|j	|}	n|}	||||	<|�
|	�qH|r�|D]}	|	|kr�||	�||	<q�|Vq2dS)Ncs|�krtd|��dS)Nz"%s" is not a recognized field.)rJ)r��valid_fieldsr@rC�validate_field
sz.InsertQuery._iter_rows.<locals>.validate_field)r}rzr�r��
_default_dict�_default_callablesr�r�r�rry)
rAZ
model_metar��defaultsZ	callablesr�Z	field_rowr�r�rr@r�rCr�
s,

zInsertQuery._iter_rowscsTtt|��|�}|j|_|j|_|j|_|j|_|j|_|j|_|j	|_	|j
|_
|Sr?)rr�r]r�r�r�r�r�r�r�r�r�rr@rCr]/
szInsertQuery._clone_attributesr�zwhere clauseTcCs
||_dSr?)r�)rA�upsertr@r@rCr�>
szInsertQuery.upsertcCs
||_dSr?r�r�r@r@rCr�B
szInsertQuery.on_conflictcCs
||_dSr?)r�)rA�return_id_listr@r@rCr�F
szInsertQuery.return_id_listcCs|jjr|jr|jrdSdS�NTF)r��insert_returningr�r�rr@r@rCr�J
szInsertQuery.is_insert_returningcCs|���|�Sr?)r�r�rr@r@rCriQ
szInsertQuery.sqlcCsPg}d}|j}|jD]*}t|j|��|j���}|r|�|�q|rH|S|SdSr?)r�r�r�r}r�r�r{r~)rAZid_listZlast_idr�r#r@r@rC�_insert_with_loopT
s
�zInsertQuery._insert_with_loopcCs�|jo"|jdko"|jdko"|jj}|r0|��S|jdk	rL|jdkrL|��S|jdk	r\|jS|��}|js�|jj	r�|�
�}|jj}dd�t
|��|�D�}|jjjr�|S|dS|j�||j�S|jr�tt�d�|���SdSdS)NcSsg|]\}}|�|��qSr@)r�)rrr4r@r@rCrw
s�z'InsertQuery.execute.<locals>.<listcomp>rT)r�r�r�r��insert_manyr�r�r�rzr�r%r}rzrWr�r��last_insert_idr�r�rX�
itemgetter�fetchall)rAZinsert_with_looprZpk_rowrIZ
clean_datar@r@rCr{d
s8���
�
zInsertQuery.execute)NNNNF)T)N)T)rErFrGrr�r]rr�r5r�r�r�r�r�r�rir�r{rr@r@rrCr�
s(�
r�c@s$eZdZed�Zdd�Zdd�ZdS)�DeleteQueryr�cCs|���|�Sr?)r�r�rr@r@rCri�
szDeleteQuery.sqlcCs@|jdk	r|jdkr|��S|jdk	r,|jS|j�|���SdSr?r�rr@r@rCr{�
s

zDeleteQuery.executeN)rErFrGrr�rir{r@r@r@rCr��
sr��
IndexMetadata)r�rir�r�r��ColumnMetadata)r�Z	data_typer�r�r��ForeignKeyMetadata)r4�
dest_table�dest_columnr�c@seZdZdS)�PeeweeExceptionN�rErFrGr@r@r@rCr��
sr�c@seZdZdS)r!Nr�r@r@r@rCr!�
sc@seZdZdS)rNr�r@r@r@rCr�
sc@seZdZdS)rNr�r@r@r@rCr�
sc@seZdZdS)r#Nr�r@r@r@rCr#�
sc@seZdZdS)r$Nr�r@r@r@rCr$�
sc@seZdZdS)r%Nr�r@r@r@rCr%�
sc@seZdZdS)r,Nr�r@r@r@rCr,�
sc@seZdZdS)r-Nr�r@r@r@rCr-�
sc@seZdZdS)r2Nr�r@r@r@rCr2�
sc@s*eZdZdgZdd�Zdd�Zdd�ZdS)	�ExceptionWrapper�
exceptionscCs
||_dSr?)r�)rAr�r@r@rCr�
szExceptionWrapper.__init__cCsdSr?r@rr@r@rC�	__enter__�
rTzExceptionWrapper.__enter__cCsH|dkrdS|j|jkrD|j|j}tr.|}n|j}t|||�|�dSr?)rEr��PY26r�r`)rA�exc_type�	exc_value�	tracebackZnew_typeZexc_argsr@r@rC�__exit__�
szExceptionWrapper.__exit__N)rErFrGrrr�r�r@r@r@rCr��
sr�cseZdZ�fdd�Z�ZS)�_BaseConnectionLocalcs4tt|�jf|�d|_d|_d|_g|_g|_dSrI)rr�r�
autocommit�closed�conn�
context_stack�transactionsr�rr@rCr�
sz_BaseConnectionLocal.__init__)rErFrGrrr@r@rrCr��
sr�c@seZdZdS)�_ConnectionLocalNr�r@r@r@rCr��
sr�c
@s|eZdZdZeZddddgZdZdZdZ	iZ
dZdZdZ
dZdZdZdZiZd	ZgZdZdZdZdZdZdZeeeeeee e!e"d
�	Z#d}dd�Z$d
d�Z%dd�Z&dd�Z'dd�Z(dd�Z)dd�Z*dd�Z+dd�Z,dd�Z-dd �Z.e/d!d"��Z0e/d#d$��Z1d%d&�Z2d'd(�Z3d)d*�Z4d+d,�Z5d-d.�Z6d~d/d0�Z7d1d2�Z8d3d4�Z9d5d6�Z:d7d8�Z;d9d:�Z<d;d<�Z=d=d>�Z>d?d@�Z?ddAdB�Z@dCdD�ZAdEdF�ZBdGdH�ZCdIdJ�ZDdKdL�ZEd�dMdN�ZFdOdP�ZGd�dQdR�ZHd�dSdT�ZId�dUdV�ZJd�dWdX�ZKd�dYdZ�ZLd[d\�ZMd�d]d^�ZNd�d_d`�ZOd�dadb�ZPd�dcdd�ZQd�dedf�ZRdgdh�ZSd�didj�ZTd�dkdl�ZUd�dmdn�ZVd�dodp�ZWdqdr�ZXdsdt�ZYdudv�ZZdwdx�Z[dydz�Z\d{d|�Z]dS)��DatabaseFr�r�r�r�TrmNrl)	ZConstraintErrorrrr#r$r%r,r-r2c	Ksni|_|rt�|_nt�|_|j|f|�t��|_||_||_	||_
t|j|pRi�|_t|j
|pdi�|_
dSr?)�connect_kwargsr��_localr��init�	threadingZLock�
_conn_lockr��autorollback�use_speedupsr�rwrx)	rAr�Zthreadlocalsr�r�opsrrrr@r@rCr�
s

zDatabase.__init__cKs0|��s|��|dk|_||_|j�|�dSr?)�	is_closedr&r=r�rr�)rAr�rr@r@rCr�
s

z
Database.initcCs
t|j�Sr?)r�r�rr@r@rC�exception_wrapperszDatabase.exception_wrapperc
Csd|j�T|jrtd��|���2|j|jf|j�|j_d|j_	|�
|jj�W5QRXW5QRXdS)NzBError, database not properly initialized before opening connectionF)rr=�	Exceptionr
�_connectr�rrr�r��initialize_connectionrr@r@rC�connects
��zDatabase.connectcCsdSr?r@�rAr�r@r@rCr
szDatabase.initialize_connectionc
CsN|j�>|jrtd��|���|�|jj�d|j_W5QRXW5QRXdS)NzBError, database not properly initialized before closing connectionT)rr=rr
�_closerr�r�rr@r@rCr&s
zDatabase.closecCs:|jjr"|jjdj}|dk	r"|S|jjr2|��|jjSr<)rr��
connectionr�rr�rr@r@rC�get_conn szDatabase.get_conncCs|jjSr?)rr�rr@r@rCr	)szDatabase.is_closedcCs|����Sr?)rrrr@r@rC�
get_cursor,szDatabase.get_cursorcCs|��dSr?)r&rr@r@rCr/szDatabase._closecKst�dSr?rw)rAr�r�r@r@rCr2szDatabase._connectcCst|j|�|_dSr?)r�rw)r.rr@r@rC�register_fields5szDatabase.register_fieldscCst|j|�|_dSr?)r�rx)r.rr@r@rC�register_ops9szDatabase.register_opscCsl|tkr|jrtStS|tkr"tS|tkr8|jr4tStS|t	krN|jrJt
StS|tkrZt
S|jrdtStSdSr?)r�rr�r2r�r6r�r�r/r�r�r5r�rO)rAZwrapper_typer@r@rCr�=s&
�
�
�
�zDatabase.get_result_wrappercCs|jjr|jSdSr?)rzr�Z	lastrowid)rArrr@r@rCr�OszDatabase.last_insert_idcCs|jSr?)Zrowcount)rArr@r@rCr�SszDatabase.rows_affectedcCs|�|j|j|j|j�Sr?)�compiler_classrnrorwrxrr@r@rCr�Vs�zDatabase.compilercCs|j|���|��Sr?)rxr�r�)rAr�r@r@rCr{[szDatabase.executec	Cs�t�||f�|���d|��}z|�||p.d�Wn,tk
r`|��rZ|jrZ|���YnX|rv|��rv|�	�W5QRX|Sr�)
�logger�debugr
rr{r�get_autocommitr�rollback�commit)rArirjryrr@r@rCrx^s
zDatabase.execute_sqlcCsdSr?r@rr@r@rC�beginmszDatabase.begincCs|����dSr?)rrrr@r@rCrpszDatabase.commitcCs|����dSr?)rrrr@r@rCrsszDatabase.rollbackcCs||j_dSr?)rr�)rAr�r@r@rC�set_autocommitvszDatabase.set_autocommitcCs |jjdkr|�|j�|jjSr?)rr�rrr@r@rCryszDatabase.get_autocommitcCs|jj�|�dSr?)rr�r~�rA�transactionr@r@rC�push_execution_context~szDatabase.push_execution_contextcCs|jj��dSr?)rr�r#rr@r@rC�pop_execution_context�szDatabase.pop_execution_contextcCst|jj�Sr?)r�rr�rr@r@rC�execution_context_depth�sz Database.execution_context_depthcCst||d�S)N)�with_transaction)�ExecutionContext)rAr#r@r@rC�execution_context�szDatabase.execution_contextcCs|jj�|�dSr?)rr�r~rr@r@rC�push_transaction�szDatabase.push_transactioncCs|jj��dSr?)rr�r#rr@r@rC�pop_transaction�szDatabase.pop_transactioncCst|jj�Sr?)r�rr�rr@r@rC�transaction_depth�szDatabase.transaction_depthcCst|�Sr?�rrr@r@rCr�szDatabase.transactioncst����fdd��}|S)Nc
s*�����||�W5QR�SQRXdSr?r)r|�r�rAr@rCr��s
z)Database.commit_on_success.<locals>.innerr)rAr�r�r@r*rC�commit_on_success�szDatabase.commit_on_successcCs|js
t�t||�Sr?)�
savepointsr��	savepoint�rA�sidr@r@rCr-�szDatabase.savepointcCst|�Sr?)�_atomicrr@r@rC�atomic�szDatabase.atomiccCst�dSr?rw�rAr�r@r@rC�
get_tables�szDatabase.get_tablescCst�dSr?rw�rAr�r�r@r@rC�get_indexes�szDatabase.get_indexescCst�dSr?rwr4r@r@rC�get_columns�szDatabase.get_columnscCst�dSr?rwr4r@r@rC�get_primary_keys�szDatabase.get_primary_keyscCst�dSr?rwr4r@r@rC�get_foreign_keys�szDatabase.get_foreign_keyscCst�dSr?rw)rA�seqr@r@rC�sequence_exists�szDatabase.sequence_existscCs|��}|j|�||��Sr?)r�rxr)rAr}r��qcr@r@rCr�szDatabase.create_tablecCst||d�dS)N)r�)�create_model_tables)rAr�r�r@r@rC�
create_tables�szDatabase.create_tablescsH|��}t|ttf�s"td|���fdd�|D�}|j|��||��S)Nz=Fields passed to "create_index" must be a list or tuple: "%s"cs&g|]}t|t�r�jj|n|�qSr@�rQrrzrr��r}r@rCr�s�z)Database.create_index.<locals>.<listcomp>)r�rQr�rVrwrxr	)rAr}rr�r;�fobjsr@r?rCr	�s�
�zDatabase.create_indexcsH|��}t|ttf�s"td|���fdd�|D�}|j|��||��S)Nz;Fields passed to "drop_index" must be a list or tuple: "%s"cs&g|]}t|t�r�jj|n|�qSr@r>r�r?r@rCr�s�z'Database.drop_index.<locals>.<listcomp>)r�rQr�rVrwrxr
)rAr}rr�r;r@r@r?rCr
�s�
�zDatabase.drop_indexcCs|��}|j|�|||��Sr?)r�rxr)rAr}rr�r;r@r@rCr�s�zDatabase.create_foreign_keycCs"|jr|��}|j|�|��SdSr?)�	sequencesr�rxr�rAr9r;r@r@rCr�szDatabase.create_sequencecCs|��}|j|�|||��Sr?)r�rxr)rAr}r�r�r;r@r@rCr�s�zDatabase.drop_tablecCst|||d�dS)N)r�r�)�drop_model_tables)rAr�r�r�r@r@rC�drop_tables�szDatabase.drop_tablescCs|��}|j|�|||��Sr?)r�rxr)rAr}r�r�r;r@r@rCr�s�zDatabase.truncate_tablecCs"tt|��D]}|�||�qdSr?)r�rqr)rAr�r�r�rr@r@rC�truncate_tables�szDatabase.truncate_tablescCs"|jr|��}|j|�|��SdSr?)rAr�rxrrBr@r@rCr�szDatabase.drop_sequencecCst�t|td�|��S�Nr��rZEXTRACTrr4�rArZ
date_fieldr@r@rCr�szDatabase.extract_datecCst�||�Sr?)rZ
DATE_TRUNCrHr@r@rC�
truncate_date�szDatabase.truncate_datecCstd�S)NzDEFAULT VALUESr�r[r@r@rCr��szDatabase.default_insert_clausecCsdS)NzSELECT 0 WHERE 0r@rr@r@rCr��szDatabase.get_noop_sqlcCstSr?)rrr@r@rCr�szDatabase.get_binary_type)TTNNFT)NT)T)N)N)N)N)N)N)F)F)F)F)N)FF)FF)FF)FF)^rErFrGr�rhrr�r��distinct_on�drop_cascaderw�foreign_keysr��for_update_nowaitr�r�ror�rxrn�reserved_tablesr�r,rA�subquery_delete_same_tabler��window_functionsr#rrr$r%r,r-r2r�rrr
rr
r&rr	rrrrRrrr�r�r�r�r{rxrrrrrr r!r"r%r&r'r(rr+r-r1r3r5r6r7r8r:rr=r	r
rrrrDrrErrrIr�r�rr@r@r@rCr�
s���
		


















�

rcs�eZdZeZdddd�ZdZeo(ejdkZ	dZ
ejdej
diZd	Zd*�fdd�	Zd
d�Zdd�Zdd�Zd+dd�Zd,dd�Zd-dd�Zd.dd�Zd/dd�Zd0dd�Zd1d d!�Zd2d"d#�Zd$d%�Zd&d'�Zd(d)�Z�ZS)3r6rirj)r/r�rF)rMr�rr=ZGLOBr�zINSERT OR REPLACE INTONcsF|pg|_|�dd�}|r*|j�d|f�tt|�j|f|�|�dS)N�journal_mode)�_pragmasr#r~rr6r)rAr�Zpragmasr�r�rRrr@rCrs

zSqliteDatabase.__init__cKsJtstd��tj|f|�}d|_z|�|�Wn|���YnX|S)Nz&pysqlite or sqlite3 must be installed.)�sqlite3r!r�isolation_level�_add_conn_hooksr&)rAr�r�r�r@r@rCrszSqliteDatabase._connectcCs8|�|�|�ddt�|�ddt�|�ddt�dS)NrrLZ
date_truncr�)�_set_pragmasZcreate_functionr�r�r�rr@r@rCrV&s
zSqliteDatabase._add_conn_hookscCs<|jr8|��}|jD]\}}|�d||f�q|��dS)NzPRAGMA %s = %s;)rSrr{r&)rAr�rZpragmar^r@r@rCrW,s
zSqliteDatabase._set_pragmas�DEFERREDcCs|jd|dd�dS)NzBEGIN %sF�ry�rx)rAZ	lock_typer@r@rCr3szSqliteDatabase.begincCstd��dS)NzBSQLite does not support ALTER TABLE statements to add constraints.)r-)rAr}rr�r@r@rCr6sz!SqliteDatabase.create_foreign_keycCs|�dd�}dd�|��D�S)Nz<SELECT name FROM sqlite_master WHERE type = ? ORDER BY name;�r�cSsg|]}|d�qS)rr@�rr#r@r@rCr=sz-SqliteDatabase.get_tables.<locals>.<listcomp>�rxr�)rAr�rr@r@rCr3:s�zSqliteDatabase.get_tablesc	s�d}|�|�df�}t|����t��|�d��}|��D]*}|d}t|d�dk}|r<��|�q<i�t��D](}|�d|�}dd�|��D��|<qt����fd	d�t��D�S)
NzQSELECT name, sql FROM sqlite_master WHERE tbl_name = ? AND type = ? ORDER BY namer�zPRAGMA index_list("%s")r�rLzPRAGMA index_info("%s")cSsg|]}|d�qS)rLr@r\r@r@rCrRsz.SqliteDatabase.get_indexes.<locals>.<listcomp>cs(g|] }t|�|�||�k���qSr@�r��rr��Z
index_columnsZindex_to_sqlr�Zunique_indexesr@rCrTs��)rxr`r�r�r�ryr�)	rAr�r�r�rr#r��	is_uniquer�r@r`rCr5?s"�zSqliteDatabase.get_indexescs$|�d��}�fdd�|��D�S)N�PRAGMA table_info("%s")c	s2g|]*}t|d|d|dt|d����qS)r�rLrMr�)r�r/r\r[r@rCr_s�z.SqliteDatabase.get_columns.<locals>.<listcomp>r]�rAr�r�rr@r[rCr6]s
�zSqliteDatabase.get_columnscCs |�d|�}dd�|��D�S)NrbcSsg|]}|dr|d�qS)r=r�r@r\r@r@rCrdsz3SqliteDatabase.get_primary_keys.<locals>.<listcomp>r]rcr@r@rCr7bszSqliteDatabase.get_primary_keyscs$|�d��}�fdd�|��D�S)NzPRAGMA foreign_key_list("%s")cs&g|]}t|d|d|d���qS)rMrLr��r�r\r[r@rCrhs�z3SqliteDatabase.get_foreign_keys.<locals>.<listcomp>r]rcr@r[rCr8fs
�zSqliteDatabase.get_foreign_keyscCs
t||�Sr?)�savepoint_sqliter.r@r@rCr-kszSqliteDatabase.savepointcCst�||�Sr?)rrrHr@r@rCrnszSqliteDatabase.extract_datecCst�t||�Sr?)rr�r�rHr@r@rCrIqszSqliteDatabase.truncate_datecCstjSr?)rT�Binaryrr@r@rCrtszSqliteDatabase.get_binary_type)N)rX)N)N)N)N)N)N)N) rErFrGr
rrwrLrT�sqlite_version_infor�r�rEr�r�rxr�rrrVrWrrr3r5r6r7r8r-rrIrrr@r@rrCr6s:��







c@s�eZdZdZdZdZdZdddddddd	�ZdZdZ	dZ
d
Zej
diZdgZdZdZdZdZd'dd�Zdd�Zdd�Zd(dd�Zd)dd�Zd*dd�Zd+dd�Zd,dd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd
S)-r/TZBYTEAZBOOLEAN�	TIMESTAMP�NUMERIC�DOUBLE PRECISIONZSERIALr)r
r/rur�r�r�r�%s�~�userNcKsTtstd��tjfd|i|��}|jrBt�tj|�t�tj|�|rP|�|�|S)Nzpsycopg2 must be installed.r�)	�psycopg2r!r�register_unicode�
pg_extensionsZ
register_type�UNICODEZUNICODEARRAYZset_client_encoding)rAr�r�r�r�r@r@rCr�s
zPostgresqlDatabase._connectcCs<|j}|jdk	r |jjr |jjS|jr8d|j|jjfSdS)NFz	%s_%s_seq)rzr�r�r�r�r�)rArrIr@r@rC�_get_pk_sequence�s
z#PostgresqlDatabase._get_pk_sequencecCs`|�|�}|sdS|j}|jr*d|j}nd}|�d||f�|��d}|��r\|��|S)Nz%s.rzSELECT CURRVAL('%s"%s"')r)rrrzr�r{r%rr)rArrr�rIr��resultr@r@rCr��s
z!PostgresqlDatabase.last_insert_id�publiccCs d}dd�|�||f���D�S)NzSSELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = %s ORDER BY tablenamecSsg|]
\}|�qSr@r@)rr�r@r@rCr�sz1PostgresqlDatabase.get_tables.<locals>.<listcomp>r])rAr�r�r@r@rCr3�szPostgresqlDatabase.get_tablescs,d}|�|�d|f�}�fdd�|��D�S)Na
            SELECT
                i.relname, idxs.indexdef, idx.indisunique,
                array_to_string(array_agg(cols.attname), ',')
            FROM pg_catalog.pg_class AS t
            INNER JOIN pg_catalog.pg_index AS idx ON t.oid = idx.indrelid
            INNER JOIN pg_catalog.pg_class AS i ON idx.indexrelid = i.oid
            INNER JOIN pg_catalog.pg_indexes AS idxs ON
                (idxs.tablename = t.relname AND idxs.indexname = i.relname)
            LEFT OUTER JOIN pg_catalog.pg_attribute AS cols ON
                (cols.attrelid = t.oid AND cols.attnum = ANY(idx.indkey))
            WHERE t.relname = %s AND t.relkind = %s AND idxs.schemaname = %s
            GROUP BY i.relname, idxs.indexdef, idx.indisunique
            ORDER BY idx.indisunique DESC, i.relname;r�cs2g|]*}t|d|d|d�d�|d���qS)rr�rM�,rL)r�rqr\r[r@rCr�s�z2PostgresqlDatabase.get_indexes.<locals>.<listcomp>r]�rAr�r�r�rr@r[rCr5�s

�zPostgresqlDatabase.get_indexescs<d}|�|�|f�}t|��|�����fdd�|��D�S)Nz�
            SELECT column_name, is_nullable, data_type
            FROM information_schema.columns
            WHERE table_name = %s AND table_schema = %s
            ORDER BY ordinal_positioncs*g|]"\}}}t|||dk|�k���qS�ZYES�r��rr�r�r���pksr�r@rCr�s�z2PostgresqlDatabase.get_columns.<locals>.<listcomp>�rxr�r7r�rvr@rzrCr6�s�zPostgresqlDatabase.get_columnscCs(d}|�|d||f�}dd�|��D�S)Na�
            SELECT kc.column_name
            FROM information_schema.table_constraints AS tc
            INNER JOIN information_schema.key_column_usage AS kc ON (
                tc.table_name = kc.table_name AND
                tc.table_schema = kc.table_schema AND
                tc.constraint_name = kc.constraint_name)
            WHERE
                tc.constraint_type = %s AND
                tc.table_name = %s AND
                tc.table_schema = %sr�cSsg|]
\}|�qSr@r@r\r@r@rCr�sz7PostgresqlDatabase.get_primary_keys.<locals>.<listcomp>r]rvr@r@rCr7�sz#PostgresqlDatabase.get_primary_keyscs*d}|�|�|f�}�fdd�|��D�S)Na�
            SELECT
                kcu.column_name, ccu.table_name, ccu.column_name
            FROM information_schema.table_constraints AS tc
            JOIN information_schema.key_column_usage AS kcu
                ON (tc.constraint_name = kcu.constraint_name AND
                    tc.constraint_schema = kcu.constraint_schema)
            JOIN information_schema.constraint_column_usage AS ccu
                ON (ccu.constraint_name = tc.constraint_name AND
                    ccu.constraint_schema = tc.constraint_schema)
            WHERE
                tc.constraint_type = 'FOREIGN KEY' AND
                tc.table_name = %s AND
                tc.table_schema = %scs&g|]}t|d|d|d���qS)rr�rLrdr\r[r@rCr�s�z7PostgresqlDatabase.get_foreign_keys.<locals>.<listcomp>r]�rAr�r�rirr@r[rCr8�s

�z#PostgresqlDatabase.get_foreign_keyscCs|�d|f�}t|��d�S)Nz�
            SELECT COUNT(*) FROM pg_class, pg_namespace
            WHERE relkind='S'
                AND pg_class.relnamespace = pg_namespace.oid
                AND relname=%sr)rxr/r%)rAr�rvr@r@rCr:�s�z"PostgresqlDatabase.sequence_existscGs(d�dgt|��}|�d||�dS)NrurkzSET search_path TO %s)r�r�rx)rAZsearch_pathZpath_paramsr@r@rC�set_search_pathsz"PostgresqlDatabase.set_search_pathcCsdS)NzSELECT 0 WHERE falser@rr@r@rCr�szPostgresqlDatabase.get_noop_sqlcCstjSr?)rnrfrr@r@rCr
sz"PostgresqlDatabase.get_binary_type)N)rt)rt)rt)rt)rt) rErFrGr�r�rJrKrwr�rMr�rorEr�rxrNr�rArProrrrr�r3r5r6r7r8r:r~r�rr@r@r@rCr/wsH�	�





c@s�eZdZdZddgZdddddd	d
d�ZdZdZd
Ze	j
de	jde	jdiZ
dZdZdZdd�Zd+dd�Zd,dd�Zd-dd�Zd.dd�Zd/dd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�ZdS)0r+Tr�r�ZBOOLrirjZFLOATzINTEGER AUTO_INCREMENTZLONGTEXTzVARCHAR(40))r/r�r�r�r�r	rrkl����zLIKE BINARYr�r��`FzREPLACE INTOcKsJtstd��ddd�}|�|�d|kr6|�d�|d<tjfd|i|��S)Nz%MySQLdb or PyMySQL must be installed.�utf8T)�charsetZuse_unicodeZpasswordZpasswdr�)�mysqlr!r�r#r)rAr�r�Zconn_kwargsr@r@rCr%s�
zMySQLDatabase._connectNcCsdd�|�d�D�S)NcSsg|]
\}|�qSr@r@r\r@r@rCr2sz,MySQLDatabase.get_tables.<locals>.<listcomp>zSHOW TABLESrZr2r@r@rCr31szMySQLDatabase.get_tablescsx|�d��}t��i�|��D]@}|ds:��|d���|dg��|d�|d�q ���fdd��D�S)N�SHOW INDEX FROM `%s`r�rLr�cs$g|]}t|d�||�k���qSr?r^r_��indexesr�r�r@rCr=s�z-MySQLDatabase.get_indexes.<locals>.<listcomp>)rxr�r�ryr&r~)rAr�r�rr#r@r�rCr54s�zMySQLDatabase.get_indexescs8d}|�|�f�}t|�������fdd�|��D�S)Nz�
            SELECT column_name, is_nullable, data_type
            FROM information_schema.columns
            WHERE table_name = %s AND table_schema = DATABASE()cs*g|]"\}}}t|||dk|�k���qSrwrxryrzr@rCrGs�z-MySQLDatabase.get_columns.<locals>.<listcomp>r|r}r@rzrCr6@s�zMySQLDatabase.get_columnscCs |�d|�}dd�|��D�S)Nr�cSs g|]}|ddkr|d�qS)rLZPRIMARYr�r@r\r@r@rCrLsz2MySQLDatabase.get_primary_keys.<locals>.<listcomp>r]rcr@r@rCr7JszMySQLDatabase.get_primary_keyscs(d}|�|�f�}�fdd�|��D�S)Na@
            SELECT column_name, referenced_table_name, referenced_column_name
            FROM information_schema.key_column_usage
            WHERE table_name = %s
                AND table_schema = DATABASE()
                AND referenced_table_name IS NOT NULL
                AND referenced_column_name IS NOT NULLcs g|]\}}}t|||���qSr@rd)rr4r�r�r[r@rCrWs�z2MySQLDatabase.get_foreign_keys.<locals>.<listcomp>r]rvr@r[rCr8Ns

�zMySQLDatabase.get_foreign_keyscCst�tt|�td�|��SrFrGrHr@r@rCr[szMySQLDatabase.extract_datecCst�|t|�Sr?)rZDATE_FORMAT�MYSQL_DATE_TRUNC_MAPPINGrHr@r@rCrI^szMySQLDatabase.truncate_datecCstt|jj�td��S)NzVALUES (DEFAULT))rr�rzr�r7r[r@r@rCr�as
�z#MySQLDatabase.default_insert_clausecCsdS)NzDO 0r@rr@r@rCr�fszMySQLDatabase.get_noop_sqlcCstjSr?)r�rfrr@r@rCriszMySQLDatabase.get_binary_type)N)N)N)N)N)rErFrGr�r�rwr�ror�rEr�r�r�rxrnrOr�rr3r5r6r7r8rrIr�r�rr@r@r@rCr+
sD�	�






c@seZdZdd�ZdS)�_callable_context_managercst����fdd��}|S)Nc
s&���||�W5QR�SQRXdSr?r@r|�rrAr@rCr�osz1_callable_context_manager.__call__.<locals>.innerr)rArr�r@r�rC�__call__nsz"_callable_context_manager.__call__N)rErFrGr�r@r@r@rCr�msr�c@s&eZdZd	dd�Zdd�Zdd�ZdS)
r$TcCs||_||_d|_dSr?)r�r#r)rAr�r#r@r@rCrvszExecutionContext.__init__c	CsZ|jj�H|j�|�|jj|jjf|jj�|_|jrL|j��|_|j�	�W5QRX|Sr?)
r�rr rrrr#r�txnr�rr@r@rCr�{s
��zExecutionContext.__enter__c
Cst|jj�b|jdkr |j��nFz*|jrH|s8|j�d�|j�|||�W5|j��|j�|j�XW5QRXdSr')	r�rrr!rr#r�rr��rAr��exc_val�exc_tbr@r@rCr��s


zExecutionContext.__exit__N)T�rErFrGrr�r�r@r@r@rCr$us
r$cs6eZdZd�fdd�	Z�fdd�Z�fdd�Z�ZS)	r;Tcstt|��||�||_dSr?)rr;rr�)rAr�r�r#rr@rCr�szUsing.__init__cs:g|_|jD]}|j�|jj�|j|j_qtt|���Sr?)�_origr�r~rzr�rr;r��rArrr@rCr��s

zUsing.__enter__cs:tt|��|||�t|j�D]\}}|j||j_qdSr?)rr;r�r�r�r�rzr�)rAr�r�r�r�rrr@rCr��szUsing.__exit__)T)rErFrGrr�r�rr@r@rrCr;�sc@s$eZdZdd�Zdd�Zdd�ZdS)r0cCs
||_dSr?�r��rAr�r@r@rCr�sz_atomic.__init__cCs2|j��dkr|j��|_n|j��|_|j��Sr�)r�r(r�_helperr-r�rr@r@rCr��sz_atomic.__enter__cCs|j�|||�Sr?)r�r�r�r@r@rCr��sz_atomic.__exit__Nr�r@r@r@rCr0�sr0c@s@eZdZdd�Zdd�Zddd�Zddd	�Zd
d�Zdd
�ZdS)rcCs
||_dSr?r�r�r@r@rCr�sztransaction.__init__cCs|j��dSr?)r�rrr@r@rC�_begin�sztransaction._beginTcCs|j��|r|��dSr?)r�rr��rArr@r@rCr�s
ztransaction.commitcCs|j��|r|��dSr?)r�rr�r�r@r@rCr�s
ztransaction.rollbackcCs>|j��|_|j�d�|j��dkr.|��|j�|�|S)NFr)r�rr�rr(r�r&rr@r@rCr��sztransaction.__enter__c	CsjzJ|r|�d�n6|j��dkrHz|�d�Wn|�d��YnXW5|j�|j�|j��XdS�NFr�)r�rr�r'rr(rr�r@r@rCr��s
ztransaction.__exit__N)T)T)	rErFrGrr�rrr�r�r@r@r@rCr�s

rc@s>eZdZddd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�ZdS)r-NcCs4||_|��}|pdt��j|_|�|j�|_dS)NrZ)r�r�rZuuid4rr/r��
quoted_sid)rAr�r/Z	_compilerr@r@rCr�szsavepoint.__init__cCs|jj|dd�dS)NFrY)r�rxr�r@r@rCrz�szsavepoint._executecCs|�d|j�dS)NzRELEASE SAVEPOINT %s;�rzr�rr@r@rCr�szsavepoint.commitcCs|�d|j�dS)NzROLLBACK TO SAVEPOINT %s;r�rr@r@rCr�szsavepoint.rollbackcCs,|j��|_|j�d�|�d|j�|S)NFz
SAVEPOINT %s;)r�r�_orig_autocommitrrzr�rr@r@rCr��szsavepoint.__enter__c	CsLz6|r|��n$z|��Wn|���YnXW5|j�|j�XdSr?)r�rr�rrr�r@r@rCr��s
zsavepoint.__exit__)N)	rErFrGrrzrrr�r�r@r@r@rCr-�s
r-cs(eZdZ�fdd�Z�fdd�Z�ZS)recs8|j��}|jdk	r$|j|_d|_nd|_tt|���Sr?)r�rrU�_orig_isolation_levelrrer�rrr@rCr��s

zsavepoint_sqlite.__enter__c	s8ztt|��|||�W�S|jdk	r2|j|j��_XdSr?)r�r�rrUrrer�r�rr@rCr�
s
�
zsavepoint_sqlite.__exit__)rErFrGr�r�rr@r@rrCre�srec@s<eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)r�cCs||_|jj|_||_dSr?)r>r}rr?)rAr6r?r@r@rCrs
zFieldProxy.__init__cCst|j|j�Sr?)r�r>r?rr@r@rCr1szFieldProxy.clone_basecCs|j�|�Sr?)r?rur�r@r@rCruszFieldProxy.coercecCs|j�|�Sr?)r?r�r�r@r@rCr�szFieldProxy.python_valuecCs|j�|�Sr?)r?r�r�r@r@rCr�!szFieldProxy.db_valuecCs|dkr|jSt|j|�S�Nr})r>r�r?r�r@r@rCr�$szFieldProxy.__getattr__N)	rErFrGrr1rur�r�r�r@r@r@rCr�sr�c@s>eZdZdd�Zdd�Zdd�Zddd	�Zd
d�Zdd
�ZdS)r�cCs||jd<dSr�)rOr[r@r@rCr*szModelAlias.__init__cCs$t|j|�}t|t�r t||�S|Sr?)r�r}rQrr�)rAr�rsr@r@rCr�-s

zModelAlias.__getattr__cCstd��dS)Nz-Cannot set attributes on ModelAlias instances)r
rr@r@rCr
3szModelAlias.__setattr__Fcs*�jj}|r|jn|j}�fdd�|D�S)Ncsg|]}t�|��qSr@)r�r�rr@rCr9sz/ModelAlias.get_proxy_fields.<locals>.<listcomp>)r}rzr��
sorted_fields)rAr�Zmmrr@rrCrc6szModelAlias.get_proxy_fieldscGs4|s|��}t|f|��}|jjr0|j|jj�}|Sr?)rcr�rzrx)rAr�r�r@r@rCr4;szModelAlias.selectcKs|jf|�Sr?r?r�r@r@rCr�CszModelAlias.__call__N)F)	rErFrGrr�r
rcr4r�r@r@r@rCr�)s
r�c@sHeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dS)r���_keys�_itemscCsg|_g|_dSr?r�rr@r@rCrJsz_SortedFieldList.__init__cCs
|j|Sr?)r�)rAr�r@r@rCrdNsz_SortedFieldList.__getitem__cCs
t|j�Sr?)rr�rr@r@rCrQsz_SortedFieldList.__iter__cCs0|j}t|j|�}t|j|�}||j||�kSr?)r�rr�rr�)rA�item�kr�r_r@r@rCreTsz_SortedFieldList.__contains__cCs|j�|j�Sr?)r�r�r�r�r@r@rCr�Zsz_SortedFieldList.indexcCs2|j}t|j|�}|j�||�|j�||�dSr?)r�rr��insertr�)rAr�r�r�r@r@rCr�]sz_SortedFieldList.insertcCs|�|�}|j|=|j|=dSr?)r�r�r�)rAr�r�r@r@rC�removecs
z_SortedFieldList.removeN)rErFrGrrrdrrer�r�r�r@r@r@rCr�Gsr�c@seZdZdS)rNr�r@r@r@rCrhsz	peewee.dbc@s|eZdZddd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Zdd�Z	dd�Z
dd�Zddd�Zd dd�Z
dd�Zd!dd�ZdS)"�ModelOptionsNTFc
Ks||_|j��|_i|_i|_i|_i|_i|_i|_	g|_
t�|_g|_
g|_t�|_g|_|dk	rj|nt|_||_||_t|p�g�|_||_||_||_|	|_|
|_||_||_d|_d|_ i|_!i|_"|
�#�D]\}}t$|||�q�t|
�%��|_&|j�r|j�s|�|�|_dSr')'r}rErr�rr�r��_default_by_namer�r��_default_callable_listr��_sorted_field_listr��sorted_field_namesr�r�r��default_databaser�r��
db_table_funcr�r�rxr�r�r�r�rN�only_save_dirtyr�r�rPr{rfr+r��_additional_keys)rAr.r�r�r�r�rxr�r�r�r�rNr�r�r�r^r@r@rCrpsDzModelOptions.__init__cCsd|jj|jfS)Nz<%s: %s>)rrEr�rr@r@rCra�szModelOptions.__repr__cCs~|jrzg}|jD]b}t|t�r:|jdkr,dnd}||j}|j|�d�}|�d�rd|�|�	��q|�|�
��q||_dS)Nr:r�r)rxrQrr*r�r�lstriprMr~r;r9)rAZ
norm_order_byr�rbrr@r@rCrK�s



zModelOptions.preparedcCs^t|j�|_dd�|jD�|_t|j���t|j���Bt|jf�B|_	dd�|jD�|_
dS)NcSsg|]
}|j�qSr@�r�r�r@r@rCr�sz4ModelOptions._update_field_lists.<locals>.<listcomp>cSsg|]}t|t�s|�qSr@)rQr�r�r@r@rCr�s
�)r�r�r�r�r�rr�r|r�r�r�rr@r@rC�_update_field_lists�s�
�z ModelOptions._update_field_listscCs�|�|j�||j|j<||j|j<|j�|�|��|jdk	r�|j|j	|<t
|j�rz|j|j|<|j�
|j|jf�n|j|j|<|j|j|j<dSr?)�remove_fieldr�rr�r�r�r�r�r�r�rFr�r�r~r�r�r�r@r@rCr��s

zModelOptions.add_fieldcCs�||jkrdS|j�|�}|j|j=|j�|�|��|jdk	r�|j|=|j	�|d�r�t
|j�D]$\}\}}||krb|j�|�q�qbn|j�|d�|j
�|jd�dSr?)rr#r�r�r�r�r�r�r�r�r�r�r�r�r�)rArTZoriginalr�r�r�r@r@rCr��s


zModelOptions.remove_fieldcCs(|j��}|jD]\}}|�||<q|Sr?)r�r�r�)rAZddrTr�r@r@rC�get_default_dict�s
zModelOptions.get_default_dictcCs*z|j�|�WStk
r$YdSXdSr<)r�r�rwr�r@r@rC�get_field_index�szModelOptions.get_field_indexcs$�jr�fdd��jjD�S�jgS)Ncsg|]}�j|�qSr@)rrSrr@rCr�s�z7ModelOptions.get_primary_key_fields.<locals>.<listcomp>)r�r�rRrr@rrCr��s

�z#ModelOptions.get_primary_key_fieldsc	Cs�t|t�}|ot|t�}|r"g}|jD]Z}t|t�r(|j|kr(|dkpf|rX|j|jkpf|of|j|jk}|r(|sx|S|�|�q(|r�|SdSr?)	rQrr%r�r rr�r!r~)	rArrv�multiZis_fieldZis_noderdrZis_matchr@r@rCr��s"

��zModelOptions.rel_for_modelcCs|j�|j||�Sr?)rzr�r})rArrvr�r@r@rCr��sz"ModelOptions.reverse_rel_for_modelcCs|�|�p|�|�Sr?)r�r�r�r@r@rCriszModelOptions.rel_existscCsrg}|jg}|rn|��}||kr"q|�|�|jj��D]}|�|j�q8|r|jj��D]}|�|j�qZq|Sr?)r}r#r~rzrPr|rr{)rA�backrefsr�rArr�r@r@rC�related_modelss
zModelOptions.related_models)NNNNNNNNNTF)NF)NF)F)rErFrGrrarKr�r�r�r�r�r�r�r�rir�r@r@r@rCr�os,�
+	

r�c
s>eZdZeddddddddd	g	�Z�fd
d�Zdd
�Z�ZS)�	BaseModelr�r�r�r�rxr�r�rNr�cs�|tks|djtkr,tt����|||�Si}|�dd�}|rf|j��D]\}}|�d�sJ|||<qJt	|dd�}d}	|D]�}
t
|
d�s�qzt	|
d�}|	dkr�t|j�}	�j
|jB}|j��D] \}}||kr�||kr�|||<q�|
j��D]2\}}||kr�q�t|t�r�|jjs�t|j�||<q�qztt����|||��|�dt�}
|
�f|��_d�_t�jj��j_�jj�s�t�dd�j����j_g}�j��D]P\}}t|t��r�|j�r�|�r�td��n"|j�r�||}}n|�||f��q�d	}|dk�r|	�r|	|	j}}nt d
d�d}}nt|t!��r.d
}d
}|d	k	�rn|�"�|�|�j_t|t ��p`t#|j$��j_%|�j_&|D]\}}|�"�|��qrt
�d��r�t'�d�fdd��d�j}d�j(i}t)|t*f|�}|�_*�j�+�t
�d��r��,�t-�.���S)NrZMetar�r�rzZmodel_options_basez[^\w]+zprimary key is overdetermined.FT)r�r�Z_composite_key�__unicode__racsd�j|��fS)Nz<%s: %r>)rEr�r�r.r@rCrSis�z#BaseModel.__new__.<locals>.<lambda>z%sDoesNotExistrF�validate_model)/rHrErr��__new__r#rOrfrMr�r�rr��inheritabler�rQr�rr�r�rzr�r�r�r�r�r�rrrwr~r�r1rrr/r�r�r�r+rFrrrKr�rr)r.r��basesZattrsZmeta_optionsrIr��vZmodel_pkZ	parent_pk�b�	base_metaZall_inheritableZModelOptionsBaserr�Zpk_namer�rZexc_nameZ	exc_attrsZexception_classrr�rCr�s�










�



zBaseModel.__new__cCst|���Sr?)rr4rr@r@rCryszBaseModel.__iter__)rErFrGr�r�r�rrr@r@rrCr�s�`r�c@s�eZdZdd�Zedd��Zedd��ZedTdd	��ZedUd
d��ZedVd
d��Z	edd��Z
edd��Zedd��Zedd��Z
edd��Zedd��Zedd��Zedd��Zedd ��ZedWd"d#��Zed$d%��Zed&d'��Zed(d)��ZedXd*d+��Zed,d-��ZedYd.d/��ZedZd0d1��Zed2d3��Zed4d5��Zd6d7�ZeZd8d9�ZeZ d:d;�Z!d<d=�Z"d>d?�Z#d@dA�Z$dBdC�Z%d[dDdE�Z&dFdG�Z'e(dHdI��Z)d\dJdK�Z*d]dLdM�Z+dNdO�Z,dPdQ�Z-dRdS�Z.dS)^r*cOs@|j��|_t|j�|_i|_|��D]\}}t|||�q&dSr?)rzr�r�r�r�r1rfr+)rAr�r�r�r�r@r@rCr}s
zModel.__init__cCst|�Sr?)r�r�r@r@rCr6�szModel.aliascGs(t|f|��}|jjr$|j|jj�}|Sr?)r�rzrx)r.r�r�r@r@rCr4�szModel.selectNcs,|pi}|���fdd��D��t�|�S)Ncs g|]}�jj|�|f�qSr@�rzrr��r.r�r@rCr�sz Model.update.<locals>.<listcomp>)r�r�)r.�_Model__datar��fdictr@r�rCr��szModel.updatecs,|pi}|���fdd��D��t�|�S)Ncs g|]}�jj|�|f�qSr@r�r��r.r�r@rCr�sz Model.insert.<locals>.<listcomp>)r�r�)r.r�r�r�r@r�rCr��szModel.insertTcCst|||d�S)N)r�r��r�)r.r�r�r@r@rCr��szModel.insert_manycCst|||d�S)N)rr�r�)r.rr�r@r@rC�insert_from�szModel.insert_fromcCst|�Sr?)r�r�r@r@rCr�szModel.deletecGst||f|��Sr?)r)r.rirjr@r@rCr��sz	Model.rawcKs"|f|�}|jdd�|��|S)NT)�force_insert)�saver3)r.r�r2r@r@rC�create�s
zModel.createcOs2|����}|r|j|�}|r*|jf|�}|��Sr?)r4r�r5rnr�)r.r�r��sqr@r@rCr��s
z	Model.getcKs8|�di�}|��}|��D]6\}}d|kr>|jf||i�}q|�t||�|k�}qz|��dfWS|jk
�r2zZtdd�|��D��}|�	|�|j
j���$|j
f|�dfW5QR�WYSQRXWn\tk
�r,}z<z|��dfWWY�&YS|jk
�r|�YnXW5d}~XYnXYnXdS)Nr�rnFcss"|]\}}d|kr||fVqdS)rnNr@)rr�r�r@r@rCr!�s�z&Model.get_or_create.<locals>.<genexpr>T)r#r4rfrnr5r�r�rr`r�rzr�r1r�r#)r.r�r�r�rr^rj�excr@r@rC�
get_or_create�s&
,zModel.get_or_createc
Ks�z6|jj��� |jf|�dfW5QR�WSQRXWn\tk
r�g}|��D],\}}t||�}|jsp|jrR|�	||k�qR|j
|�dfYSXdSr�)rzr�r1r�r#rfr�r�r�r~r�)r.r�r�rTr^rr@r@rC�
create_or_get�s(
zModel.create_or_getcOs|��j||�Sr?)r4rn)r.Zdqr�r@r@rCrn�szModel.filtercCs0i}|jjr|jj|d<|jj|jjjf|�kS)Nr�)rzr�r�r�r3)r.r�r@r@rC�table_exists�szModel.table_existsFcCsb|r|��rdS|jj}|jj}|jrL|dk	rL|jrL|�|j�sL|�|j�|�|�|�	�dSr')
r�rzr�r�rAr�r:rr�_create_indexes)r.r�r�rXr@r@rCr�s
zModel.create_tablecCsDg}|jjD]2}|jrqt|j|jt|t�f�}|r|�|�q|Sr?)	rzr�r�r�r�r�rQr r~)r.rrZrequires_indexr@r@rC�_fields_to_index�s�zModel._fields_to_indexcCs"t�dd�|��D�|jjpd�S)NcSsg|]}|f|jf�qSr@)r�r�r@r@rCrsz%Model._index_data.<locals>.<listcomp>r@)�	itertools�chainr�rzr�r�r@r@rC�_index_data	s
�zModel._index_datacCs(|��D]\}}|jj�|||�qdSr?)r�rzr�r	)r.�
field_listrar@r@rCr�szModel._create_indexescCs(|��D]\}}|jj�|||�qdSr?)r�rzr�r
)r.r�r�rar@r@rC�
_drop_indexesszModel._drop_indexescs�g}�jj��}�jj}�jjjr:|jr:|�|�|j��|�|������	�D]}|�|�
�|g|j��qR�jjr��jjD].\}}�fdd�|D�}|�|�
�||��q�dd�|D�S)Ncsg|]}�jj|�qSr@r�r�r�r@rCr%sz Model.sqlall.<locals>.<listcomp>cSsg|]\}}|�qSr@r@)rrir�r@r@rCr's)
rzr�r�r�rAr�r~rrr�r	r�r�)r.Zqueriesr�rXrrRr�rr@r�rC�sqlallszModel.sqlallcCs|jj�|||�dSr?)rzr�r)r.r�r�r@r@rCr)szModel.drop_tablecCs|jj�|||�dSr?)rzr�r)r.r�r�r@r@rCr-szModel.truncate_tablecCs&|jjrt|jj|jj�St|jj�Sr?)rzr�r r�r�r@r@rCr�1szModel.as_entitycOst|f|�|�Sr?)r�)r.r�r�r@r@rC�noop7sz
Model.noopcCst||jjj�Sr?)r�rzr�r�rr@r@rCrQ;szModel._get_pk_valuecCs|jjst||jjj|�dSr?)rzr�r+r�r�r�r@r@rC�
_set_pk_value?szModel._set_pk_valuecCs|jj|��kSr?)rzr�rQrr@r@rC�_pk_exprDszModel._pk_exprcCs|j��|��dSr?)r��clearrKrr@r@rCr3Gs
zModel._prepare_instancecCsdSr?r@rr@r@rCrKKszModel.preparedcCs,i}|D]}|j|kr||j||j<q|Sr?r�)rAr��onlyZnew_datarr@r@rC�
_prune_fieldsNs

zModel._prune_fieldscCsd|jjD]V}||jko8||ko8||dko8|j�|�dk	}|rt||t||��|j|||<qdSr?)rzrPr�r1r�r+r�r�)rAr�r�Z
conditionsr@r@rC�_populate_unsaved_relationsUs
�
��z!Model._populate_unsaved_relationsc	Cs(t|j�}|jjdk	r(|jj}|��}nd}}|rB|�||�}n,|jjrn|sn|�||j�}|sn|j�	�dS|�
|�|dk	r�|s�|jjr�|jD]}|�
|d�q�n|�
|jd�|jf|��|�����}nJ|dkr�|jf|���d}n,|jf|���}|dk	�r|}|�|�d}|j�	�|Sr�)r`r�rzr�rQr�r��dirty_fieldsr�r�r�r�rRr#r�r�r5r�r{r�r�)	rAr�r�r�Zpk_fieldZpk_valueZpk_part_namer�Zpk_from_cursorr@r@rCr�`s@

�





z
Model.savecCs
t|j�Sr?)r/r�rr@r@rC�is_dirty�szModel.is_dirtycs�fdd��jjD�S)Ncsg|]}|j�jkr|�qSr@)r�r�r�rr@rCr�sz&Model.dirty_fields.<locals>.<listcomp>)rzr�rr@rrCr��szModel.dirty_fieldsccs�t|�}|���|���}t|�|fg}t�}|r�|��\}}||krHq.|�|�|jj�	�D]r\}}|j
}	|j|kr�||j|j
jk}
|	���|
�}n||>}
|	���|
�}|jr�|r�|�|	|f�|
|fVq^q.dSr?)rr4r5r�r�r#ryrzr{rfr}rr�r�r�r�r~)rAZsearch_nullabler}r�rAr��klassZrel_namer�rr&�subqueryr@r@rC�dependencies�s&


zModel.dependenciescCsz|rd|�|�}tt|��D]H\}}|j}|jrP|sP|jf|jdi��|���q|�	��|���q|�	��|�
����Sr?)r�r�r�r}r�r�r�r5r{rr�)rA�	recursiveZdelete_nullabler�r�r�rr@r@rC�delete_instance�s

zModel.delete_instancecCst|j|��f�Sr?)r�rrQrr@r@rCr��szModel.__hash__cCs(|j|jko&|��dk	o&|��|��kSr?)rrQ�rArZr@r@rCrF�s

��zModel.__eq__cCs
||kSr?r@r�r@r@rCrG�szModel.__ne__)N)N)T)F)F)FF)FF)FN)F)FF)/rErFrGrrRr6r4r�r�r�r�rr�r�r�r�r�rnr�rr�r�r�r�r�rrr�r�rQZget_idr�Zset_idr�r3rKr�r�r�r�r�r�r�r�r�rFrGr@r@r@rCr*|s�



















%


cCs|��}d|_|_|Sr?)r�r�r�)r�r@r@rC�clean_prefetch_subquery�sr�cs�t|�g}t|�D�]�\}}t|t�r0|\}}nd}t|t�sPt|t�rP|��}|j�d}}t	t
|d��D]~}||}	|	j}
|	j��j
j�dd�}|r��fdd�|D�}�fdd�|D�}n�j
j�dd�}|s�|rn|�ks�|dkrnq�qn|�s|�s|�rd|nd}
td	||
f��|�rht|
��ttj�fd
d�t||�D��}|�|�}|�t||d��q|rt|
��ttj�fdd�|D��}|�|�}|�t||d��q|S)
Nr�T)r�csg|]}t�|j��qSr@)r�r��rr�)�subquery_modelr@rCr�sz)prefetch_add_subquery.<locals>.<listcomp>csg|]}t�|jj��qSr@)r�r�r�r�)�
last_modelr@rCr�sz	 using %srz1Error: unable to find foreign key for query: %s%scsg|]\}}|��|�>�qSr@)r4)rr�rX��cleanedr@rCr�s�Fcsg|]}|j��|�>�qSr@)r�r4)rrHr�r@rCr�s�)�PrefetchResultr�rQrVrYr:r*r4r}r�r�r�rrzr�r
r�rPrXrgrWr5r~)r��
subqueries�
fixed_queriesr�r�Ztarget_modelZfksr�r_�prefetch_resultZ
last_queryZrelsr{Ztgt_err�exprr@)r�r�r�rC�prefetch_add_subquery�sZ


����
�
r��__prefetched)r�rrH�
rel_models�
field_to_namercs.eZdZd�fdd�	Zdd�Zdd�Z�ZS)	r�Nc	sr|rP|r&dd�|D�}dd�|D�}ndd�|D�}dd�|D�}tt||��}|j}tt|��|||||||�S)NcSsg|]
}|j�qSr@r?r�r@r@rCr�sz*PrefetchResult.__new__.<locals>.<listcomp>cSsg|]}|jj�qSr@)r�r�r�r@r@rCr�scSsg|]
}|j�qSr@rGr�r@r@rCr�scSsg|]
}|j�qSr@r�r�r@r@rCr�s)r�rWr}rr�r�)r.r�rrHr�r�rZforeign_key_attrsrr@rCr��s"
�zPrefetchResult.__new__c
Cs�|jrB|jD]2}|j|j}||f}||krt||j||�qn^|jD]V\}}|j|jj}||f}|�|g�}d|j}|D]}	t|	||�q�t|||�qHdS)Nz%s_prefetch)	rHrr�r�r+r�r�r�rA)
rAr#�id_maprZ
identifierr��attnameZ
rel_instancesr�r2r@r@rC�populate_instances

z PrefetchResult.populate_instancecCsX|jD]L\}}|j�|j|�}||f}|jr8|||<q|�|g�||�|�qdSr?)r�r�r�r�rHr&r~)rAr#r�rr�Zidentityr�r@r@rC�store_instances
zPrefetchResult.store_instance)NNNNN)rErFrGr�r�r�rr@r@rrCr��s�r�cGs�|s|St||�}i}i}t|�D]�}|j}|jrX|jD]}|�|g�||�|�q8i||<||}t|�|��}	|j	D]:}
|jr�|�
|
|�|	r|||D]}|�|
||j�q�q|q"|j	Sr?)r�r�rrr�r&r~r/r�r�r�r�)r�r�r�ZdepsZrel_mapr�Zquery_modelrr�Z
has_relationsr#rPr@r@rCr0s*


cKst|�D]}|jf|�qdS)z8Create tables for all given models (in the right order).N)rqr)r�Zcreate_table_kwargsr�r@r@rCr<;sr<cKs"tt|��D]}|jf|�qdS)z6Drop tables for all given models (in the right order).N)r�rqr)r�Zdrop_table_kwargsr�r@r@rCrC@srC)N)N)r�)�r)rur�r�r�ZloggingrXr�rbrrrr8Zbisectrr�collectionsrrr�ImportErrorr`r�r�	functoolsr	�inspectr
�__version__�__all__r>ZHandlerZ	getLoggerrZ
addHandlerrH�objectrK�version_infoZPY2r�r��builtinsrOrPrFr�r�rWr�rr�rerr�r,r`Zunicode�buffer�exec�RuntimeErrorrirlZ	pysqlite2rmZpysq3rTrgZpsycopg2cffirn�registerrnrorpZMySQLdbr�ZpymysqlZplayhouse._speedupsrprqrrr�r�r�r�Zregister_adapterr�rZDATETIME_PARTSr�r�r�r�r�r�r�r�r�rEr&r�r(r�r)r�r'r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rpr�r�rr3rrr%r7r4r rorr>r.r�rr�r�r=rrr�r�r�r�rrr"rr5r1r�rrrrrrr8r
r<rrrrr9r:rr0r3r6r rr\rhr
rrr+r/r2r5r6rMrOrYrr�r�r�r�r�r�r�r�r�r�r�rr�r!rrr#r$r%r,r-r2r�r�Zlocalr�rr6r/r+r�r$r;r0rr-rer�r�rr�r�rr�r*r�r�r�r�r0r<rCr@r@r@rC�<module>s�
�8






	"������

%	
M{&	9" R'


4/0	
(<'���	<u`'#
!&h@2+


Zerion Mini Shell 1.0