î
Ý^Q\4  ã               @   sˆ   d  Z  d d l m Z d d l m Z m Z Gd d „  d e ƒ Z Gd d „  d e ƒ Z	 Gd d	 „  d	 e ƒ Z
 Gd
 d „  d e ƒ Z d S)z]
Useful auxiliary data structures for query construction. Not useful outside
the SQL domain.
é    )ÚEmptyResultSet)Ú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   úI/var/www/dbchiro/venv/build/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 „  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()
    c             C   sI   | |  _  | |  _ | |  _ | |  _ | j ƒ  |  _ | |  _ | |  _ d  S)N)Ú
table_nameÚparent_aliasÚtable_aliasÚ	join_typeZget_joining_columnsÚ	join_colsÚ
join_fieldÚnullable)r   r   r   r   r   r   r   r   r   r	   r
   +   s    					zJoin.__init__c             C   sv  g  } g  } | j  } | j j } x^ t |  j ƒ D]M \ } \ } }	 | j d | |  j ƒ | | ƒ | |  j ƒ | |	 ƒ f ƒ q1 W|  j j	 | j
 j |  j |  j ƒ }
 |
 râ | j |
 ƒ \ } } | j d | ƒ | j | ƒ n  | st |  j d |  j ƒ } t d | j ƒ ‚ n  d j | ƒ } |  j |  j k r=d n
 d |  j } d |  j | |  j ƒ | | f } | | f S)	zš
        Generates 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_nameÚ	enumerater   Úappendr   r   r   Zget_extra_restrictionÚqueryZwhere_classÚcompileÚextendÚgetattrÚ
ValueErrorÚ	__class__Újoinr   r   )r   ÚcompilerÚ
connectionZjoin_conditionsÚparamsÚqnZqn2ÚindexZlhs_colZrhs_colZ
extra_condZ	extra_sqlÚextra_paramsZdeclared_fieldZon_clause_sqlÚ	alias_strZsqlr   r   r	   Úas_sql<   s2    	"			%"zJoin.as_sqlc             C   sX   | j  |  j |  j ƒ } | j  |  j |  j ƒ } |  j |  j | | |  j |  j |  j ƒ S)N)Úgetr   r   r#   r   r   r   r   )r   Ú
change_mapZnew_parent_aliasZnew_table_aliasr   r   r	   Úrelabeled_clonee   s
    zJoin.relabeled_clonec             C   sJ   t  | |  j ƒ rF |  j | j k oE |  j | j k oE |  j | j k Sd S)NF)Ú
isinstancer#   r   r   r   )r   Úotherr   r   r	   Ú__eq__l   s
    zJoin.__eq__c             C   s   |  j  i  ƒ } t | _ | S)N)r/   r   r   )r   Únewr   r   r	   Údemoteu   s    	zJoin.demotec             C   s   |  j  i  ƒ } t | _ | S)N)r/   r   r   )r   r3   r   r   r	   Úpromotez   s    	zJoin.promoteN)
r   r   r   r   r
   r,   r/   r2   r4   r5   r   r   r   r	   r      s   )	r   c               @   sF   e  Z d  Z d Z d Z 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   sE   |  j  |  j k r d n
 d |  j  } | 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 |  j |  j ƒ ƒ S)N)r#   r   r-   r   )r   r.   r   r   r	   r/   “   s    zBaseTable.relabeled_clone)	r   r   r   r   r   r   r
   r,   r/   r   r   r   r	   r6   €   s   r6   N)r   Zdjango.core.exceptionsr   Zdjango.db.models.sql.constantsr   r   Ú	Exceptionr   Úobjectr   r   r6   r   r   r   r	   Ú<module>   s   f