ó
®â0_c           @  sÃ  d  Z  d d l m Z d d l Z d d l Z d d l 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 m Z m Z d d
 l m Z d d l m Z d d l m Z y< y d d l  m! Z" Wn! e# k
 rDd d l$ m! Z" n XWn# e# k
 rkZ% e d e% ƒ ‚ n Xd 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 l0 m1 Z1 d „  Z2 d „  Z3 e" j4 e5 d ƒ e3 d „  ƒ ƒ e" j4 e5 d ƒ e3 e ƒ ƒ e" j4 e5 d ƒ e3 e ƒ ƒ e" j4 e5 d ƒ e3 e ƒ ƒ e" j4 e5 d ƒ e3 e ƒ ƒ e" j4 e5 d ƒ e3 e ƒ ƒ e" j4 e5 d ƒ e3 e j6 ƒ ƒ e" j7 e j e2 ƒ e" j7 e j8 e j9 ƒ e j: re" j7 e5 d  „  ƒ e" j7 e d! „  ƒ n  d" e f d# „  ƒ  YZ; e j< d$ ƒ Z= d% e" j> f d& „  ƒ  YZ? d' „  Z@ d( „  ZA d) „  ZB d* „  ZC d+ „  ZD d, „  ZE d- „  ZF d. „  ZG d/ „  ZH d0 „  ZI d1 „  ZJ d2 „  ZK d3 „  ZL d4 „  ZM d S(5   ut   
SQLite3 backend for django.

Works with either the pysqlite2 module or the sqlite3 module in the
standard library.
iÿÿÿÿ(   t   unicode_literalsN(   t   settings(   t   ImproperlyConfigured(   t   utils(   t   BaseDatabaseWrapper(   t   sixt   timezone(   t
   parse_datet   parse_datetimet   parse_durationt
   parse_time(   t   RemovedInDjango20Warning(   t
   force_text(   t	   SafeBytes(   t   dbapi2uK   Error loading either pysqlite2 or sqlite3 modules (tried in that order): %si   (   t   DatabaseClient(   t   DatabaseCreation(   t   DatabaseFeatures(   t   DatabaseIntrospection(   t   DatabaseOperations(   t   DatabaseSchemaEditorc         C  s\   t  j rI t j |  ƒ rI t j d t ƒ |  j t j ƒ j	 d d  ƒ }  n  |  j t d ƒ ƒ S(   Nu¿   The SQLite database adapter received an aware datetime (%s), probably from cursor.execute(). Update your code to pass a naive datetime in the database connection's time zone (UTC by default).t   tzinfou    (   R   t   USE_TZR   t   is_awaret   warningst   warnR   t
   astimezonet   utct   replacet   Nonet	   isoformatt   str(   t   value(    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyt%   adapt_datetime_warn_on_aware_datetime.   s    !c           s   ‡  f d †  S(   u¹    The Python sqlite3 interface returns always byte strings.
        This function converts the received value to a regular string before
        passing it to the receiver function.
    c           s   ˆ  |  j  d ƒ ƒ S(   Nu   utf-8(   t   decode(   t   s(   t	   conv_func(    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyt   <lambda>A   t    (    (   R$   (    (   R$   sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyt   decoder<   s    u   boolc         C  s
   |  d k S(   Nu   1(    (   R#   (    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyR%   D   R&   u   timeu   dateu   datetimeu	   timestampu	   TIMESTAMPu   decimalc         C  s   |  j  d ƒ S(   Nu   utf-8(   R"   (   R#   (    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyR%   O   R&   c         C  s   |  j  d ƒ S(   Nu   utf-8(   R"   (   R#   (    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyR%   P   R&   t   DatabaseWrapperc           B  s  e  Z d  Z i d d 6d d 6d d 6d d 6d d	 6d d
 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d  6d! d" 6d d# 6d$ d% 6d& d' 6d( d) 6d* d+ 6Z i d, d 6d, d 6Z i d- d. 6d/ d0 6d/ d1 6d/ d2 6d3 d4 6d5 d6 6d7 d8 6d9 d: 6d; d< 6d= d> 6d/ d? 6d/ d@ 6d/ dA 6d/ dB 6Z dC Z i dD d1 6dE d2 6dF d? 6dG dA 6dH d@ 6dI dB 6Z e Z e	 Z
 e Z e Z e Z e Z e Z dJ „  Z dK „  Z dL „  Z dU dM „ Z dN „  Z dO „  Z dP „  Z dU dQ „ Z dR „  Z dS „  Z dT „  Z  RS(V   u   sqliteu   integeru	   AutoFieldu   BigAutoFieldu   BLOBu   BinaryFieldu   boolu   BooleanFieldu   varchar(%(max_length)s)u	   CharFieldu   CommaSeparatedIntegerFieldu   dateu	   DateFieldu   datetimeu   DateTimeFieldu   decimalu   DecimalFieldu   bigintu   DurationFieldu	   FileFieldu   FilePathFieldu   realu
   FloatFieldu   IntegerFieldu   BigIntegerFieldu   char(15)u   IPAddressFieldu   char(39)u   GenericIPAddressFieldu   NullBooleanFieldu   OneToOneFieldu   integer unsignedu   PositiveIntegerFieldu   smallint unsignedu   PositiveSmallIntegerFieldu	   SlugFieldu   smallintu   SmallIntegerFieldu   textu	   TextFieldu   timeu	   TimeFieldu   char(32)u	   UUIDFieldu   AUTOINCREMENTu   = %su   exactu   LIKE %s ESCAPE '\'u   iexactu   containsu	   icontainsu	   REGEXP %su   regexu   REGEXP '(?i)' || %su   iregexu   > %su   gtu   >= %su   gteu   < %su   ltu   <= %su   lteu
   startswithu   endswithu   istartswithu	   iendswithu@   REPLACE(REPLACE(REPLACE({}, '\', '\\'), '%%', '\%%'), '_', '\_')u"   LIKE '%%' || {} || '%%' ESCAPE '\'u)   LIKE '%%' || UPPER({}) || '%%' ESCAPE '\'u   LIKE {} || '%%' ESCAPE '\'u!   LIKE UPPER({}) || '%%' ESCAPE '\'u   LIKE '%%' || {} ESCAPE '\'u!   LIKE '%%' || UPPER({}) ESCAPE '\'c         C  s¹   |  j  } | d s" t d ƒ ‚ n  i | d d 6t j t j Bd 6} | j | d ƒ d | k r~ | d r~ t j d t ƒ n  | j i t	 d 6ƒ |  j
 j rµ | j i t d 6ƒ n  | S(	   Nu   NAMEuJ   settings.DATABASES is improperly configured. Please supply the NAME value.u   databaseu   detect_typesu   OPTIONSu   check_same_threaduÃ   The `check_same_thread` option was provided and set to True. It will be overridden with False. Use the `DatabaseWrapper.allow_thread_sharing` property instead for controlling thread shareability.u   uri(   t   settings_dictR   t   Databaset   PARSE_DECLTYPESt   PARSE_COLNAMESt   updateR   R   t   RuntimeWarningt   Falset   featurest   can_share_in_memory_dbt   True(   t   selfR)   t   kwargs(    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyt   get_connection_params§   s     	

c         C  s
  t  j |   } | j d d t ƒ | j d d t ƒ | j d d t ƒ | j d d t ƒ | j d d t ƒ | j d d t ƒ | j d	 d t	 ƒ | j d
 d t
 ƒ | j d d t ƒ | j d d t ƒ | j d d t ƒ | j d d t ƒ | j d d t ƒ | S(   Nu   django_date_extracti   u   django_date_truncu   django_datetime_cast_dateu   django_datetime_cast_timeu   django_datetime_extracti   u   django_datetime_truncu   django_time_extractu   django_time_truncu   django_time_diffu   django_timestamp_diffu   regexpu   django_format_dtdeltau   django_power(   R*   t   connectt   create_functiont   _sqlite_date_extractt   _sqlite_date_trunct   _sqlite_datetime_cast_datet   _sqlite_datetime_cast_timet   _sqlite_datetime_extractt   _sqlite_datetime_trunct   _sqlite_time_extractt   _sqlite_time_trunct   _sqlite_time_difft   _sqlite_timestamp_difft   _sqlite_regexpt   _sqlite_format_dtdeltat   _sqlite_power(   R3   t   conn_paramst   conn(    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyt   get_new_connectionÅ   s    c         C  s   d  S(   N(    (   R3   (    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyt   init_connection_stateÖ   s    c         C  s   |  j  j d t ƒ S(   Nt   factory(   t
   connectiont   cursort   SQLiteCursorWrapper(   R3   t   name(    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyt   create_cursorÙ   s    c         C  s*   |  j  ƒ  |  j ƒ  s& t j |  ƒ n  d  S(   N(   t   validate_thread_sharingt   is_in_memory_dbR   t   close(   R3   (    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyRQ   Ü   s    
c         C  s   |  j  j o |  j S(   N(   R0   t   uses_savepointst   in_atomic_block(   R3   (    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyt   _savepoint_allowedä   s    
c         C  s5   | r d  } n d } |  j  | |  j _ Wd  QXd  S(   Nu    (   R   t   wrap_database_errorsRJ   t   isolation_level(   R3   t
   autocommitt   level(    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyt   _set_autocommitð   s
    	
c   
      C  s  |  j  ƒ  } | d k r- |  j j | ƒ } n  xÎ | D]Æ } |  j j | | ƒ } | s[ q4 n  |  j j | | ƒ } x‡ | D] \ } } } | j d | | | | | | | | f ƒ xD | j ƒ  D]6 }	 t j	 d | |	 d | | |	 d | | f ƒ ‚ q¼ Wqw Wq4 Wd S(   u¶  
        Checks each table name in `table_names` for rows with invalid foreign
        key references. This method is intended to be used in conjunction with
        `disable_constraint_checking()` and `enable_constraint_checking()`, to
        determine if rows with invalid references were entered while constraint
        checks were off.

        Raises an IntegrityError on the first invalid foreign key reference
        encountered (if any) and provides detailed information about the
        invalid reference in the error message.

        Backends can override this method if they can more directly apply
        constraint checking (e.g. via "SET CONSTRAINTS ALL IMMEDIATE")
        u  
                    SELECT REFERRING.`%s`, REFERRING.`%s` FROM `%s` as REFERRING
                    LEFT JOIN `%s` as REFERRED
                    ON (REFERRING.`%s` = REFERRED.`%s`)
                    WHERE REFERRING.`%s` IS NOT NULL AND REFERRED.`%s` IS NULL
                    u–   The row in table '%s' with primary key '%s' has an invalid foreign key: %s.%s contains a value '%s' that does not have a corresponding value in %s.%s.i    i   N(
   RK   R   t   introspectiont   table_namest   get_primary_key_columnt   get_key_columnst   executet   fetchallR   t   IntegrityError(
   R3   R[   RK   t
   table_namet   primary_key_column_namet   key_columnst   column_namet   referenced_table_namet   referenced_column_namet   bad_row(    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyt   check_constraintsü   s&    		c         C  s   t  S(   N(   R2   (   R3   (    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyt	   is_usable+  s    c         C  s   |  j  ƒ  j d ƒ d S(   uÂ   
        Start a transaction explicitly in autocommit mode.

        Staying in autocommit mode works around a bug of sqlite3 that breaks
        savepoints when autocommit is disabled.
        u   BEGINN(   RK   R^   (   R3   (    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyt#   _start_transaction_under_autocommit.  s    c         C  s   |  j  j |  j d ƒ S(   Nu   NAME(   t   creationRP   R)   (   R3   (    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyRP   7  s    N(!   t   __name__t
   __module__t   vendort
   data_typest   data_types_suffixt	   operatorst   pattern_esct   pattern_opsR*   R   t   SchemaEditorClassR   t   client_classR   t   creation_classR   t   features_classR   t   introspection_classR   t	   ops_classR5   RG   RH   R   RN   RQ   RT   RY   Rh   Ri   Rj   RP   (    (    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyR(   S   s   



						/			u   (?<!%)%sRL   c           B  s,   e  Z d  Z d d „ Z d „  Z d „  Z RS(   uÃ   
    Django uses "format" style placeholders, but pysqlite2 uses "qmark" style.
    This fixes it -- but note that if you want to use a literal "%s" in a query,
    you'll need to use "%%s".
    c         C  sD   | d  k r t j j |  | ƒ S|  j | ƒ } t j j |  | | ƒ S(   N(   R   R*   t   CursorR^   t   convert_query(   R3   t   queryt   params(    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyR^   D  s    c         C  s%   |  j  | ƒ } t j j |  | | ƒ S(   N(   R{   R*   Rz   t   executemany(   R3   R|   t
   param_list(    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyR~   J  s    c         C  s   t  j d | ƒ j d d ƒ S(   Nu   ?u   %%u   %(   t   FORMAT_QMARK_REGEXt   subR   (   R3   R|   (    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyR{   N  s    N(   Rl   Rm   t   __doc__R   R^   R~   R{   (    (    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyRL   >  s   	c         C  s‡   | d  k r d  Sy t j | ƒ } Wn t t f k
 r= d  SX|  d k r\ | j ƒ  d d S|  d k rv | j ƒ  d St | |  ƒ Sd  S(   Nu   week_dayi   i   u   week(   R   t   backend_utilst   typecast_timestampt
   ValueErrort	   TypeErrort
   isoweekdayt   isocalendart   getattr(   t   lookup_typet   dt(    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyR8   R  s    c         C  s   y t  j | ƒ } Wn t t f k
 r- d  SX|  d k rE d | j S|  d k re d | j | j f S|  d k r‹ d | j | j | j f Sd  S(   Nu   yearu   %i-01-01u   monthu
   %i-%02i-01u   dayu   %i-%02i-%02i(   Rƒ   R„   R…   R†   R   t   yeart   montht   day(   RŠ   R‹   (    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyR9   a  s    c         C  s   y t  j | ƒ } Wn t t f k
 r- d  SX|  d k rE d | j S|  d k re d | j | j f S|  d k r‹ d | j | j | j f Sd  S(   Nu   houru
   %02i:00:00u   minuteu   %02i:%02i:00u   secondu   %02i:%02i:%02i(   Rƒ   t   typecast_timeR…   R†   R   t   hourt   minutet   second(   RŠ   R‹   (    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyR?   n  s    c         C  sl   |  d  k r d  Sy t j |  ƒ }  Wn t t f k
 r= d  SX| d  k	 rh t j |  t j | ƒ ƒ }  n  |  S(   N(   R   Rƒ   R„   R…   R†   R   t	   localtimet   pytz(   R‹   t   tzname(    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyt   _sqlite_datetime_parse{  s    c         C  s/   t  |  | ƒ }  |  d  k r d  S|  j ƒ  j ƒ  S(   N(   R–   R   t   dateR   (   R‹   R•   (    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyR:   ‡  s    c         C  s/   t  |  | ƒ }  |  d  k r d  S|  j ƒ  j ƒ  S(   N(   R–   R   t   timeR   (   R‹   R•   (    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyR;   Ž  s    c         C  sh   t  | | ƒ } | d  k r d  S|  d k r= | j ƒ  d d S|  d k rW | j ƒ  d St | |  ƒ Sd  S(   Nu   week_dayi   i   u   week(   R–   R   R‡   Rˆ   R‰   (   RŠ   R‹   R•   (    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyR<   •  s    c         C  s  t  | | ƒ } | d  k r d  S|  d k r6 d | j S|  d k rV d | j | j f S|  d k r| d | j | j | j f S|  d k r¨ d | j | j | j | j f S|  d	 k rÚ d
 | j | j | j | j | j f S|  d k rd | j | j | j | j | j | j f Sd  S(   Nu   yearu   %i-01-01 00:00:00u   monthu   %i-%02i-01 00:00:00u   dayu   %i-%02i-%02i 00:00:00u   houru   %i-%02i-%02i %02i:00:00u   minuteu   %i-%02i-%02i %02i:%02i:00u   secondu   %i-%02i-%02i %02i:%02i:%02i(   R–   R   RŒ   R   RŽ   R   R‘   R’   (   RŠ   R‹   R•   (    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyR=   ¡  s     &c         C  sK   | d  k r d  Sy t j | ƒ } Wn t t f k
 r= d  SXt | |  ƒ S(   N(   R   Rƒ   R   R…   R†   R‰   (   RŠ   R‹   (    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyR>   ³  s    c         C  s  yï t  | t j ƒ r: t t j | ƒ t j d ƒ ƒ } n  t | ƒ } | d k rd t j	 | ƒ } n  t  | t j ƒ r› t t j | ƒ t j d ƒ ƒ } n  t | ƒ } | d k rÅ t j	 | ƒ } n  |  j
 ƒ  d k rä | | } n
 | | } Wn t t f k
 r	d SXt | ƒ S(   u®   
    LHS and RHS can be either:
        - An integer number of microseconds
        - A string representing a timedelta object
        - A string representing a datetime
    i@B u   +N(   t
   isinstanceR   t   integer_typesR   t   decimalt   DecimalR	   R   Rƒ   R„   t   stripR…   R†   (   RF   t   lhst   rhst   real_lhst   real_rhst   out(    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyRC   ½  s"    %%c         C  s†   t  j |  ƒ } t  j | ƒ } | j d d d | j d d | j d | j | j d d d | j d d | j d | j S(   Ni<   i@B (   Rƒ   R   R   R‘   R’   t   microsecond(   Rž   RŸ   t   leftt   right(    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyR@   Ú  s    `c         C  s0   t  j |  ƒ } t  j | ƒ } | | j ƒ  d S(   Ni@B (   Rƒ   R„   t   total_seconds(   Rž   RŸ   R¤   R¥   (    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyRA   é  s    c         C  s,   | d  k	 r( t t j |  t | ƒ ƒ ƒ St S(   N(   R   t   boolt   ret   searchR   R/   (   t
   re_patternt	   re_string(    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyRB   ï  s    c         C  s   |  | S(   N(    (   t   xt   y(    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyRD   ó  s    (N   R‚   t
   __future__R    t   datetimeR›   R¨   R   R”   t   django.confR   t   django.core.exceptionsR   t	   django.dbR   t   django.db.backendsRƒ   t   django.db.backends.base.baseR   t   django.utilsR   R   t   django.utils.dateparseR   R   R	   R
   t   django.utils.deprecationR   t   django.utils.encodingR   t   django.utils.safestringR   t	   pysqlite2R   R*   t   ImportErrort   sqlite3t   exct   clientR   Rk   R   R0   R   RZ   R   t
   operationsR   t   schemaR   R!   R'   t   register_converterR   t   typecast_decimalt   register_adapterRœ   t   rev_typecast_decimalt   PY2R(   t   compileR€   Rz   RL   R8   R9   R?   R–   R:   R;   R<   R=   R>   RC   R@   RA   RB   RD   (    (    (    sA   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/base.pyt   <module>   sx   "			è									
				