B
    CVvgÿ  ã               @   sR   d Z ddlmZmZ G dd„ deƒZG dd„ dƒZG dd„ dƒZG d	d
„ d
ƒZdS )z]
Useful auxiliary data structures for query construction. Not useful outside
the SQL domain.
é    )ÚINNERÚLOUTERc               @   s   e Zd ZdZdd„ ZdS )Ú	MultiJoinz¤
    Used by join construction code to indicate the point at which a
    multi-valued join was attempted (if the caller wants to treat that
    exceptionally).
    c             C   s   || _ || _d S )N)ÚlevelZnames_with_path)ÚselfZ	names_posZpath_with_names© r   úG/tmp/pip-install-o3oxmrkh/Django/django/db/models/sql/datastructures.pyÚ__init__   s    zMultiJoin.__init__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r   r   r   r   r      s   r   c               @   s   e Zd ZdS )ÚEmptyN)r
   r   r   r   r   r   r   r      s   r   c               @   s^   e Zd ZdZddd„Zdd„ Zdd„ Zed	d
„ ƒZdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ ZdS )ÚJoinaÙ  
    Used by sql.Query and sql.SQLCompiler to generate JOIN clauses into the
    FROM entry. For example, the SQL generated could be
        LEFT OUTER JOIN "sometable" T1 ON ("othertable"."sometable_id" = "sometable"."id")

    This class is primarily used in Query.alias_map. All entries in alias_map
    must be Join compatible by providing the following attributes and methods:
        - table_name (string)
        - table_alias (possible alias for the table, can be None)
        - join_type (can be None for those entries that aren't joined from
          anything)
        - parent_alias (which table is this join's parent, can be None similarly
          to join_type)
        - as_sql()
        - relabeled_clone()
    Nc             C   s8   || _ || _|| _|| _| ¡ | _|| _|| _|| _d S )N)	Ú
table_nameÚparent_aliasÚtable_aliasÚ	join_typeZget_joining_columnsÚ	join_colsÚ
join_fieldÚnullableÚfiltered_relation)r   r   r   r   r   r   r   r   r   r   r   r	   )   s    
zJoin.__init__c          	   C   s0  g }g }|j }|jj}x<| jD ]2\}}| d|| jƒ||ƒ|| jƒ||ƒf ¡ qW | j |j	j
| j| j¡}	|	r–| |	¡\}
}| d|
 ¡ | |¡ | jrÈ| | j¡\}
}|
rÈ| d|
 ¡ | |¡ |sêt| jd| jƒ}td|j ƒ‚d |¡}| j| jk�rdnd| j }d| j|| jƒ||f }||fS )	z™
        Generate the full
           LEFT OUTER JOIN sometable ON sometable.somecol = othertable.othercol, params
        clause for this join.
        z%s.%s = %s.%sz(%s)ÚfieldzaJoin generated an empty ON clause. %s did not yield either joining columns or extra restrictions.z AND Ú z %sz%s %s%s ON (%s))Úquote_name_unless_aliasÚopsZ
quote_namer   Úappendr   r   r   Zget_extra_restrictionÚqueryZwhere_classÚcompileÚextendr   ÚgetattrÚ
ValueErrorÚ	__class__Újoinr   r   )r   ÚcompilerÚ
connectionZjoin_conditionsÚparamsÚqnZqn2Zlhs_colZrhs_colZ
extra_condZ	extra_sqlZextra_paramsZdeclared_fieldZon_clause_sqlÚ	alias_strZsqlr   r   r   Úas_sql;   s<    



zJoin.as_sqlc          	      sr   ˆ   | j| j¡}ˆ   | j| j¡}| jd k	rN| j ¡ }‡ fdd„| jjD ƒ|_nd }| j| j||| j| j	| j
|d�S )Nc                s   g | ]}ˆ   ||¡‘qS r   )Úget)Ú.0Úp)Ú
change_mapr   r   ú
<listcomp>m   s    z(Join.relabeled_clone.<locals>.<listcomp>)r   )r*   r   r   r   ÚcloneÚpathr"   r   r   r   r   )r   r-   Znew_parent_aliasZnew_table_aliasr   r   )r-   r   Úrelabeled_cloneh   s    

zJoin.relabeled_clonec             C   s   | j | j| j| j| jfS )N)r"   r   r   r   r   )r   r   r   r   Úidentityu   s
    zJoin.identityc             C   s   t |tƒstS | j|jkS )N)Ú
isinstancer   ÚNotImplementedr2   )r   Úotherr   r   r   Ú__eq__   s    
zJoin.__eq__c             C   s
   t | jƒS )N)Úhashr2   )r   r   r   r   Ú__hash__„   s    zJoin.__hash__c             C   s(   |r| |kS | j d d… |j d d… kS )Néÿÿÿÿ)r2   )r   r5   Úwith_filtered_relationr   r   r   Úequals‡   s    zJoin.equalsc             C   s   |   i ¡}t|_|S )N)r1   r   r   )r   Únewr   r   r   ÚdemoteŒ   s    
zJoin.demotec             C   s   |   i ¡}t|_|S )N)r1   r   r   )r   r<   r   r   r   Úpromote‘   s    
zJoin.promote)N)r
   r   r   r   r	   r)   r1   Úpropertyr2   r6   r8   r;   r=   r>   r   r   r   r   r      s   
-
r   c               @   sX   e Zd ZdZdZdZdZdd„ Zdd„ Zdd„ Z	e
d	d
„ ƒZdd„ Zdd„ Zdd„ ZdS )Ú	BaseTablezÂ
    The BaseTable class is used for base table references in FROM clause. For
    example, the SQL "foo" in
        SELECT * FROM "foo" WHERE somecond
    could be generated by this class.
    Nc             C   s   || _ || _d S )N)r   r   )r   r   Úaliasr   r   r   r	   ¢   s    zBaseTable.__init__c             C   s2   | j | jkrdnd| j  }| | j¡}|| g fS )Nr   z %s)r   r   r   )r   r$   r%   r(   Zbase_sqlr   r   r   r)   ¦   s    zBaseTable.as_sqlc             C   s   |   | j| | j| j¡¡S )N)r"   r   r*   r   )r   r-   r   r   r   r1   «   s    zBaseTable.relabeled_clonec             C   s   | j | j| jfS )N)r"   r   r   )r   r   r   r   r2   ®   s    zBaseTable.identityc             C   s   t |tƒstS | j|jkS )N)r3   r@   r4   r2   )r   r5   r   r   r   r6   ²   s    
zBaseTable.__eq__c             C   s
   t | jƒS )N)r7   r2   )r   r   r   r   r8   ·   s    zBaseTable.__hash__c             C   s   | j |j kS )N)r2   )r   r5   r:   r   r   r   r;   º   s    zBaseTable.equals)r
   r   r   r   r   r   r   r	   r)   r1   r?   r2   r6   r8   r;   r   r   r   r   r@   —   s   r@   N)	r   Zdjango.db.models.sql.constantsr   r   Ú	Exceptionr   r   r   r@   r   r   r   r   Ú<module>   s
   