ó
ø¢TQc           @   s@  y d  d l  m Z Wn! e k
 r7 d  d l  m Z n Xd  d l m Z d  d l m Z m Z d  d l	 m
 Z
 d  d l m Z d  d l m Z e
 j Z d e
 j f d	 „  ƒ  YZ d
 e
 j e f d „  ƒ  YZ d e
 j e f d „  ƒ  YZ d e
 j e f d „  ƒ  YZ d e
 j e f d „  ƒ  YZ d e
 j e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   zip_longest(   t   izip_longest(   t   zip(   t   truncate_namet   typecast_timestamp(   t   compiler(   t   MULTI(   t   sixt   GeoSQLCompilerc           B   sk   e  Z e d  „ Z e d d d e e d „ Z d d „ Z d „  Z d d d „ Z d „  Z	 d d d „ Z
 RS(	   c         C   s  |  j  } |  j j j } g  t j |  j j ƒ D]3 \ } } d |  j | ƒ | d | | ƒ f ^ q. } t	 |  j j j
 ƒ  ƒ } | r” | j ƒ  } n	 t	 ƒ  } |  j j rg|  j ƒ  }	 xðt |  j j |  j j ƒ D]\ } }
 t | t t f ƒ r| \ } } |  j j | j } | |	 k r3| |	 | k r3qÑ n  |  j |
 | | ƒ } | rä| d | k r¢d t | ƒ } | j d | | f ƒ | j | ƒ | j | ƒ q| j d | | | d ƒ f ƒ | j | ƒ | j | d ƒ q`| j | ƒ | j | ƒ | j | d ƒ qÑ | j | j | |  j ƒ ƒ t | d ƒ rÑ | j | j ƒ | j | j ƒ qÑ qÑ WnA |  j j r¨|  j | | ƒ \ } } | j | ƒ | j | ƒ n  |  j j j  ƒ  } | j g  |  j j! j" ƒ  D]] \ } } d |  j | ƒ | j | |  j ƒ | d
 k	 r&d | t$ | | ƒ ƒ p)d	 f ^ qÓƒ xÁ t |  j j% |  j j& ƒ D]¤ \ \ } } }
 |  j |
 | | ƒ } | rÐ| | k rÐd t | ƒ } | j d | | f ƒ | j | ƒ | j | ƒ qS| j | ƒ | j | ƒ | j | ƒ qSW| |  _' | S(   só  
        Return the list of columns to use in the select statement. If no
        columns have been specified, returns all columns relating to fields in
        the model.

        If 'with_aliases' is true, any column names that are duplicated
        (without the table names) are given unique aliases. This is needed in
        some cases to avoid ambiguitity with nested queries.

        This routine is overridden from Query to handle customized selection of
        geometry columns.
        s
   (%s) AS %si    i   s   Col%ds   %s AS %st   aliass   %s%ss    AS %st    N((   t   quote_name_unless_aliast
   connectiont   opst
   quote_nameR   t	   iteritemst   queryt   extra_selectt   get_extra_select_formatt   sett   keyst   copyt   selectt   deferred_to_columnsR   t   select_fieldst
   isinstancet   listt   tuplet	   alias_mapt
   table_namet   get_field_selectt   lent   appendt   addt   as_sqlt   hasattrR	   t   default_colst   get_default_columnst   extendt   updatet   max_name_lengtht   aggregate_selectt   itemst   NoneR   t   related_select_colst   related_select_fieldst   _select_aliases(   t   selft   with_aliasest   qnt   qn2R	   t   colt   resultt   aliasest   col_aliasest	   only_loadt   fieldt   columnt   tablet   rt   c_aliast   colst   new_aliasesR(   t	   aggregate(    (    sB   ../Django//lib/python/django/contrib/gis/db/models/sql/compiler.pyt   get_columns   sl    	L	(!	t.	c         C   s5  g  } | d k r$ |  j j j } n  t ƒ  } |  j ƒ  }	 | rO i | d 6}
 n  xÙ| j ƒ  D]Ë\ } } | | j k r€ d } n  | r˜ | d k	 r˜ q\ n  | ry |
 | } Wqt k
 r| j	 | ƒ } |  j j
 | | j j | j | j j j f ƒ } | |
 | <qXn |  j j | } |  j j | j } | |	 k rT| j |	 | k rTq\ n  | rƒ| j | | j f ƒ | j | ƒ q\ n  |  j | | ƒ } | rî| j | k rîd t | ƒ } | j d | | f ƒ | j | ƒ | j | ƒ q\ | } | j | ƒ | j | ƒ | r\ | j | j ƒ q\ q\ W| | f S(   sÂ  
        Computes the default columns for selecting every field in the base
        model. Will sometimes be called to pull in related models (e.g. via
        select_related), in which case "opts" and "start_alias" will be given
        to provide a starting point for the traversal.

        Returns a list of strings, quoted appropriately for use in SQL
        directly, as well as a set of aliases used in the select statement (if
        'as_pairs' is True, returns a list of (alias, col_name) pairs instead
        of strings as the first component and None as the second component).

        This routine is overridden from Query to handle customized selection of
        geometry columns.
        s   Col%ds   %s AS %sN(   R+   R   t   modelt   _metaR   R   t   get_fields_with_modelt   concrete_modelt   KeyErrort   get_ancestor_linkt   joint   db_tableR9   t   pkt   included_inherited_modelsR   R   R    R!   R   R   (   R/   R0   R6   t   start_aliast   optst   as_pairst
   local_onlyR4   R5   R7   t   seenR8   RA   R	   t
   link_fieldR:   t	   field_selR<   R;   (    (    sB   ../Django//lib/python/django/contrib/gis/db/models/sql/compiler.pyR%   g   sP    		c         C   sE  g  } t  |  j j ƒ } d } |  j j j rW |  j j d k	 sK |  j j rW d } qW n  | t	 | ƒ } g  t
 | | | !| ƒ D]9 \ } } |  j j | |  j j j | d ƒ |  j ƒ ^ q~ } |  j j j sá t |  j d t ƒ r*xW t | | | ƒ D]. \ }	 }
 | j |  j j |	 |
 |  j ƒ ƒ qõ Wn | j | | ƒ t | ƒ S(   s®   
        This routine is necessary so that distances and geometries returned
        from extra selection SQL get resolved appropriately into Python
        objects.
        i    i   t
   geo_valuesN(   R   R   R   R   R   t   oraclet	   high_markR+   t   low_markR   R   t   convert_valuest   extra_select_fieldst   gett   getattrt   FalseR    R    R&   R   (   R/   t   rowt   fieldst   valuesR5   t	   rn_offsett   index_startt   vt   at   valueR8   (    (    sB   ../Django//lib/python/django/contrib/gis/db/models/sql/compiler.pyt   resolve_columns«   s     S$ )c         C   sE   d } t  |  j d ƒ rA | |  j j k rA | |  j j | } n  | S(   Ns   %st   custom_select(   R#   R   Rd   (   R/   R	   t   sel_fmt(    (    sB   ../Django//lib/python/django/contrib/gis/db/models/sql/compiler.pyR   Ì   s    $c         C   sU   |  j  | ƒ } | |  j j k r8 | |  j j | } n | |  j | | | ƒ } | S(   s¼  
        Returns the SELECT SQL string for the given field.  Figures out
        if any custom selection SQL is needed for the column  The `alias`
        keyword may be used to manually specify the database table where
        the column exists, if not in the model associated with this
        `GeoQuery`.  Similarly, `column` may be used to specify the exact
        column name, rather than using the `column` attribute on `field`.
        (   t   get_select_formatR   Rd   t   _field_column(   R/   R8   R	   R9   Re   RQ   (    (    sB   ../Django//lib/python/django/contrib/gis/db/models/sql/compiler.pyR   Ò   s
    	c         C   s}   |  j  j j rs t | d ƒ rs |  j  j j } |  j j ry |  j  j j sW |  j  j j ry d |  j j | f } qy n d } | S(   s.  
        Returns the selection format string, depending on the requirements
        of the spatial backend.  For example, Oracle and MySQL require custom
        selection formats in order to retrieve geometries in OGC WKT. For all
        other fields a simple '%s' format string is returned.
        t	   geom_types   'SRID=%d;'||%ss   %s(   R   R   R   R#   R   t   transformed_sridRS   t
   spatialite(   R/   t   fldRe   (    (    sB   ../Django//lib/python/django/contrib/gis/db/models/sql/compiler.pyRf   â   s    c         C   sP   | d k r! |  j j j j } n  d |  j | ƒ |  j j j | pH | j	 ƒ f S(   s¤  
        Helper function that returns the database column for the given field.
        The table and column are returned (quoted) in the proper format, e.g.,
        `"geoapp_city"."point"`.  If `table_alias` is not specified, the
        database table associated with the model of this `GeoQuery` will be
        used.  If `column` is specified, it will be used instead of the value
        in `field.column`.
        s   %s.%sN(
   R+   R   RA   RB   RH   R   R   R   R   R9   (   R/   R8   t   table_aliasR9   (    (    sB   ../Django//lib/python/django/contrib/gis/db/models/sql/compiler.pyRg   ý   s    	 N(    (   t   __name__t
   __module__RZ   R@   R+   R%   Rc   R   R   Rf   Rg   (    (    (    sB   ../Django//lib/python/django/contrib/gis/db/models/sql/compiler.pyR      s   VC!		t   SQLInsertCompilerc           B   s   e  Z RS(    (   Rm   Rn   (    (    (    sB   ../Django//lib/python/django/contrib/gis/db/models/sql/compiler.pyRo   
  s   t   SQLDeleteCompilerc           B   s   e  Z RS(    (   Rm   Rn   (    (    (    sB   ../Django//lib/python/django/contrib/gis/db/models/sql/compiler.pyRp     s   t   SQLUpdateCompilerc           B   s   e  Z RS(    (   Rm   Rn   (    (    (    sB   ../Django//lib/python/django/contrib/gis/db/models/sql/compiler.pyRq     s   t   SQLAggregateCompilerc           B   s   e  Z RS(    (   Rm   Rn   (    (    (    sB   ../Django//lib/python/django/contrib/gis/db/models/sql/compiler.pyRr     s   t   SQLDateCompilerc           B   s   e  Z d  Z d „  Z RS(   s¢   
    This is overridden for GeoDjango to properly cast date columns, since
    `GeoQuery.resolve_columns` is used for spatial values.
    See #14648, #16757.
    c         c   sÐ   |  j  j j r. d d l m } | ƒ  g } n |  j  j j } t |  j j	 ƒ } xz |  j
 t ƒ D]i } x` | D]X } | | } |  j  j j r¤ |  j | | ƒ | } n | r¿ t t | ƒ ƒ } n  | Vql Wq_ Wd  S(   Niÿÿÿÿ(   t   DateTimeField(   R   R   RS   t   django.db.models.fieldsRt   t   featurest   needs_datetime_string_castR   R   R   t   execute_sqlR   Rc   R   t   str(   R/   Rt   R\   t   needs_string_castt   offsett   rowsR[   t   date(    (    sB   ../Django//lib/python/django/contrib/gis/db/models/sql/compiler.pyt   results_iter  s    
(   Rm   Rn   t   __doc__R~   (    (    (    sB   ../Django//lib/python/django/contrib/gis/db/models/sql/compiler.pyRs     s   N(   t	   itertoolsR    t   ImportErrorR   t   django.utils.six.movesR   t   django.db.backends.utilR   R   t   django.db.models.sqlR   t   django.db.models.sql.constantsR   t   django.utilsR   t   SQLCompilerR   Ro   Rp   Rq   Rr   Rs   (    (    (    sB   ../Django//lib/python/django/contrib/gis/db/models/sql/compiler.pyt   <module>   s   	û