ó
®â0_c           @  sê   d  d l  m Z d  d l Z d  d l Z d  d l m Z d  d l m Z d  d l m	 Z	 d  d l
 m	 Z d  d l m Z d  d l m Z m Z d  d l m Z m Z d  d	 l m Z m Z m Z d  d
 l m Z d e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   unicode_literalsN(   t   settings(   t
   FieldError(   t   utils(   t   BaseDatabaseOperations(   t
   aggregatest   fields(   t   sixt   timezone(   t
   parse_datet   parse_datetimet
   parse_time(   t   duration_stringt   DatabaseOperationsc           B  s+  e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z e d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z  d „  Z! d „  Z" RS(    c         C  sH   t  | ƒ d k r d n d } t  | ƒ d k r> | t  | ƒ St  | ƒ S(   uà   
        SQLite has a compile-time default (SQLITE_LIMIT_VARIABLE_NUMBER) of
        999 variables per query.

        If there's only a single field to insert, the limit is 500
        (SQLITE_MAX_COMPOUND_SELECT).
        i   iç  iô  i    (   t   len(   t   selfR   t   objst   limit(    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyt   bulk_batch_size   s    c         C  s¢   t  j t  j t  j f } t j t j t j t j f } t	 | | ƒ rž xV | j
 ƒ  D]E } y+ | j } t	 | | ƒ r‚ t d ƒ ‚ n  WqR t k
 r– qR XqR Wn  d  S(   Nu{   You cannot use Sum, Avg, StdDev, and Variance aggregations on date/time fields in sqlite3 since date/time is saved as text.(   R   t	   DateFieldt   DateTimeFieldt	   TimeFieldR   t   Sumt   Avgt   Variancet   StdDevt
   isinstancet   get_source_expressionst   output_fieldt   NotImplementedErrorR   (   R   t
   expressiont
   bad_fieldst   bad_aggregatest   exprR   (    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyt   check_expression_support   s    	c         C  s   d | j  ƒ  | f S(   Nu   django_date_extract('%s', %s)(   t   lower(   R   t   lookup_typet
   field_name(    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyt   date_extract_sql/   s    c         C  s   d t  | ƒ g  f S(   Nu   '%s'(   R   (   R   t	   timedelta(    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyt   date_interval_sql6   s    c         C  s   | S(   u:   Do nothing here, we will handle it in the custom function.(    (   R   t   sql(    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyt   format_for_duration_arithmetic9   s    c         C  s   d | j  ƒ  | f S(   Nu   django_date_trunc('%s', %s)(   R#   (   R   R$   R%   (    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyt   date_trunc_sql=   s    c         C  s   d | j  ƒ  | f S(   Nu   django_time_trunc('%s', %s)(   R#   (   R   R$   R%   (    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyt   time_trunc_sqlD   s    c         C  s   d | | g f S(   Nu"   django_datetime_cast_date(%s, %%s)(    (   R   R%   t   tzname(    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyt   datetime_cast_date_sqlK   s    c         C  s   d | | g f S(   Nu"   django_datetime_cast_time(%s, %%s)(    (   R   R%   R-   (    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyt   datetime_cast_time_sqlN   s    c         C  s   d | j  ƒ  | f | g f S(   Nu&   django_datetime_extract('%s', %s, %%s)(   R#   (   R   R$   R%   R-   (    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyt   datetime_extract_sqlQ   s    c         C  s   d | j  ƒ  | f | g f S(   Nu$   django_datetime_trunc('%s', %s, %%s)(   R#   (   R   R$   R%   R-   (    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyt   datetime_trunc_sqlV   s    c         C  s   d | j  ƒ  | f S(   Nu   django_time_extract('%s', %s)(   R#   (   R   R$   R%   (    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyt   time_extract_sql[   s    c         C  s   d S(   Nu   NULL(    (   R   (    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyt   pk_default_valueb   s    c         C  sÇ   d } t  | ƒ | k ri d } xD t d t  | ƒ | ƒ D]* } | | | | !} | |  j | ƒ 7} q7 W| Sd d j d g t  | ƒ ƒ } |  j j j ƒ  } z | j | | ƒ j ƒ  SWd | j ƒ  Xd S(   uV   
        Only for last_executed_query! Don't use this to execute SQL queries!
        iç  i    u   SELECT u   , u   QUOTE(?)N(    (	   R   t   ranget%   _quote_params_for_last_executed_queryt   joint
   connectiont   cursort   executet   fetchonet   close(   R   t   paramst
   BATCH_SIZEt   resultst   indext   chunkR)   R8   (    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyR5   e   s    	 c         C  s   | rw t  | t t f ƒ r- |  j | ƒ } nB | j ƒ  } t | j ƒ  ƒ } |  j | ƒ } t t | | ƒ ƒ } | | S| Sd  S(   N(   R   t   listt   tupleR5   t   keyst   valuest   dictt   zip(   R   R8   R)   R<   RC   RD   (    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyt   last_executed_query€   s    c         C  s*   | j  d ƒ r" | j d ƒ r" | Sd | S(   Nu   "u   "%s"(   t
   startswitht   endswith(   R   t   name(    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyt
   quote_name“   s    c         C  s   d S(   Niÿÿÿÿ(    (   R   (    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyt   no_limit_value˜   s    c         C  sN   g  | D]= } d | j  d ƒ | j  d ƒ | j |  j | ƒ ƒ f ^ q } | S(   Nu	   %s %s %s;u   DELETEu   FROM(   t   SQL_KEYWORDt	   SQL_FIELDRK   (   R   t   stylet   tablest	   sequencest   allow_cascadet   tableR)   (    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyt	   sql_flush›   s    Gc         C  sr   | d  k r d  St | d ƒ r# | St j | ƒ re t j rV t j | |  j j ƒ } qe t d ƒ ‚ n  t	 j
 | ƒ S(   Nu   resolve_expressionuN   SQLite backend does not support timezone-aware datetimes when USE_TZ is False.(   t   Nonet   hasattrR   t   is_awareR   t   USE_TZt
   make_naiveR7   t
   ValueErrorR   t	   text_type(   R   t   value(    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyt   adapt_datetimefield_value¨   s    	c         C  sN   | d  k r d  St | d ƒ r# | St j | ƒ rA t d ƒ ‚ n  t j | ƒ S(   Nu   resolve_expressionu5   SQLite backend does not support timezone-aware times.(   RU   RV   R   RW   RZ   R   R[   (   R   R\   (    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyt   adapt_timefield_value¹   s    c         C  så   t  t |  ƒ j | ƒ } | j j ƒ  } | d k rF | j |  j ƒ n› | d k re | j |  j ƒ n| | d k r„ | j |  j ƒ n] | d k r£ | j |  j	 ƒ n> | d k rÂ | j |  j
 ƒ n | d k rá | j |  j ƒ n  | S(	   Nu   DateTimeFieldu	   DateFieldu	   TimeFieldu   DecimalFieldu	   UUIDFieldu   NullBooleanFieldu   BooleanField(   u   NullBooleanFieldu   BooleanField(   t   superR   t   get_db_convertersR   t   get_internal_typet   appendt   convert_datetimefield_valuet   convert_datefield_valuet   convert_timefield_valuet   convert_decimalfield_valuet   convert_uuidfield_valuet   convert_booleanfield_value(   R   R   t
   converterst   internal_type(    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyR`   Ç   s    c         C  sh   | d  k	 rd t | t j ƒ s- t | ƒ } n  t j rd t j | ƒ rd t j | |  j	 j ƒ } qd n  | S(   N(
   RU   R   t   datetimeR
   R   RX   R   RW   t
   make_awareR7   (   R   R\   R   R7   t   context(    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyRc   Ø   s    c         C  s4   | d  k	 r0 t | t j ƒ s0 t | ƒ } q0 n  | S(   N(   RU   R   Rk   t   dateR	   (   R   R\   R   R7   Rm   (    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyRd   à   s    c         C  s4   | d  k	 r0 t | t j ƒ s0 t | ƒ } q0 n  | S(   N(   RU   R   Rk   t   timeR   (   R   R\   R   R7   Rm   (    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyRe   æ   s    c         C  s4   | d  k	 r0 | j j | ƒ } t j | ƒ } n  | S(   N(   RU   R   t   format_numbert   backend_utilst   typecast_decimal(   R   R\   R   R7   Rm   (    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyRf   ì   s    c         C  s"   | d  k	 r t j | ƒ } n  | S(   N(   RU   t   uuidt   UUID(   R   R\   R   R7   Rm   (    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyRg   ò   s    c         C  s   | d k r t  | ƒ S| S(   Ni   i    (   i   i    (   t   bool(   R   R\   R   R7   Rm   (    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyRh   ÷   s    c         C  s   d j  d „  | Dƒ ƒ S(   Nu    UNION ALL c         s  s"   |  ] } d  d j  | ƒ Vq d S(   u	   SELECT %su   , N(   R6   (   t   .0t   row(    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pys	   <genexpr>ü   s   (   R6   (   R   R   t   placeholder_rows(    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyt   bulk_insert_sqlú   s    c         C  s6   | d k r d d j  | ƒ St t |  ƒ j | | ƒ S(   Nu   ^u   django_power(%s)u   ,(   R6   R_   R   t   combine_expression(   R   t	   connectort   sub_expressions(    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyRz      s    c         C  se   | d	 k r" t  j d | ƒ ‚ n  d | g | } t | ƒ d k rT t d ƒ ‚ n  d d j | ƒ S(
   Nu   +u   -u$   Invalid connector for timedelta: %s.u   '%s'i   u)   Too many params for timedelta operations.u   django_format_dtdelta(%s)u   , (   u   +u   -(   R   t   DatabaseErrorR   RZ   R6   (   R   R{   R|   t	   fn_params(    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyt   combine_duration_expression  s    c         C  s   d S(   N(   NN(   RU   (   R   Rj   (    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyt   integer_field_range  s    c         C  sT   | \ } } | \ } } | d k r< d | | f | | f Sd | | f | | f S(   Nu	   TimeFieldu   django_time_diff(%s, %s)u   django_timestamp_diff(%s, %s)(    (   R   Rj   t   lhst   rhst   lhs_sqlt
   lhs_paramst   rhs_sqlt
   rhs_params(    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyt   subtract_temporals  s
    (#   t   __name__t
   __module__R   R"   R&   R(   R*   R+   R,   R.   R/   R0   R1   R2   R3   R5   RG   RK   RL   t   FalseRT   R]   R^   R`   Rc   Rd   Re   Rf   Rg   Rh   Ry   Rz   R   R€   R‡   (    (    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyR      s@   																														(   t
   __future__R    Rk   Rs   t   django.confR   t   django.core.exceptionsR   t	   django.dbR   t   django.db.backendsRq   t"   django.db.backends.base.operationsR   t   django.db.modelsR   R   t   django.utilsR   R   t   django.utils.dateparseR	   R
   R   t   django.utils.durationR   R   (    (    (    sG   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/operations.pyt   <module>   s   