ó
®â0_c           @   sœ   d  d l  Z  d  d l Z d  d l m Z m Z m Z d  d l m Z d  d l m	 Z	 e  j
 d ƒ Z d „  Z d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d S(   iÿÿÿÿN(   t   BaseDatabaseIntrospectiont	   FieldInfot	   TableInfo(   t   Index(   t   RemovedInDjango21Warnings&   ^\s*(?:var)?char\s*\(\s*(\d+)\s*\)\s*$c         C   s,   t  j |  ƒ } | r( t | j d ƒ ƒ Sd S(   s8    Extract the size number from a "varchar(11)" type name i   N(   t   field_size_ret   searcht   intt   groupt   None(   t   namet   m(    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/introspection.pyt   get_field_size   s    t   FlexibleFieldLookupDictc           B   sŽ   e  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 6Z d „  Z RS(    t   BooleanFieldt   boolt   booleant   SmallIntegerFieldt   smallintt   PositiveSmallIntegerFields   smallint unsignedt   smallintegert   IntegerFieldR   t   integert   BigIntegerFieldt   bigintt   PositiveIntegerFields   integer unsignedt   DecimalFieldt   decimalt
   FloatFieldt   realt	   TextFieldt   textt	   CharFieldt   chart   BinaryFieldt   blobt	   DateFieldt   datet   DateTimeFieldt   datetimet	   TimeFieldt   timec         C   sb   | j  ƒ  } y |  j | SWn@ t k
 r] t | ƒ } | d  k	 rT d i | d 6f St ‚ n Xd  S(   NR    t
   max_length(   t   lowert   base_data_types_reverset   KeyErrorR   R	   (   t   selft   keyt   size(    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/introspection.pyt   __getitem__.   s    (   t   __name__t
   __module__R,   R1   (    (    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/introspection.pyR      s&   
t   DatabaseIntrospectionc           B   sb   e  Z e ƒ  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z RS(	   c         C   s?   | j  d ƒ g  | j ƒ  D]! } t | d | d d ƒ ^ q S(   sQ   
        Returns a list of table and view names in the current database.
        s–   
            SELECT name, type FROM sqlite_master
            WHERE type in ('table', 'view') AND NOT name='sqlite_sequence'
            ORDER BY namei    i   (   t   executet   fetchallR   (   R.   t   cursort   row(    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/introspection.pyt   get_table_list<   s    c         C   sR   g  |  j  | | ƒ D]; } t | d | d d | d d d | d | d ƒ ^ q S(   sQ   Returns a description of the table, with the DB-API cursor.description interface.R
   t   typeR0   t   null_okt   defaultN(   t   _table_infoR   R	   (   R.   R7   t
   table_namet   info(    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/introspection.pyt   get_table_descriptionH   s    	c         C   s7   |  j  j j d k  r/ | j d ƒ d j d ƒ S| Sd S(	   s  
        SQLite will in some cases, e.g. when returning columns from views and
        subselects, return column names in 'alias."column"' format instead of
        simply 'column'.

        Affects SQLite < 3.7.15, fixed by http://www.sqlite.org/src/info/5526e0aa3c
        i   i   i   t   .iÿÿÿÿt   "N(   i   i   i   (   t
   connectiont   Databaset   sqlite_version_infot   splitt   strip(   R.   R
   (    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/introspection.pyt   column_name_converterW   s    
c         C   s8  i  } | j  d | d g ƒ y | j ƒ  d j ƒ  } Wn t k
 rJ | SX| | j d ƒ d | j d ƒ !} xÃ| j d ƒ D]²} | j ƒ  } | j d ƒ r¥ q~ n  t j	 d	 | t j
 ƒ } | sÉ q~ n  g  | j ƒ  D] } | j d
 ƒ ^ qÖ \ } }	 | j d ƒ r:t j d | t j
 ƒ } | j ƒ  d j d
 ƒ }
 n | j ƒ  d j d
 ƒ }
 | j  d | g ƒ | j ƒ  d } | d j ƒ  } | j d ƒ | j d ƒ } } | | d | !} xw | j d ƒ D]f } | j ƒ  } | j d ƒ ríqÆn  | j d d ƒ d j d
 ƒ } | |	 k rÆ| | f | |
 <PqÆqÆWq~ W| S(   s—   
        Return a dictionary of {field_name: (field_name_other_table, other_table)}
        representing all relationships to the given table.
        s?   SELECT sql FROM sqlite_master WHERE tbl_name = %s AND type = %st   tablei    t   (i   t   )t   ,t   UNIQUEs$   references (\S*) ?\(["|]?(.*)["|]?\)RB   s   FOREIGN KEYs   FOREIGN KEY\s*\(([^\)]*)\).*s1   SELECT sql FROM sqlite_master WHERE tbl_name = %st    (   R5   t   fetchoneRG   t	   TypeErrort   indext   rindexRF   t
   startswitht   reR   t   It   groupst   matchR6   (   R.   R7   R>   t	   relationst   resultst
   field_descR   t   sRI   t   columnt
   field_namet   resultt   other_table_resultst   lit   rit
   other_desct
   other_name(    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/introspection.pyt   get_relationsf   sB    #.c         C   sç   g  } | j  d | d g ƒ | j ƒ  d j ƒ  } | | j d ƒ d | j d ƒ !} x‹ t | j d ƒ ƒ D]t \ } } | j ƒ  } | j d ƒ r˜ qk n  t j	 d	 | t j
 ƒ } | s¼ qk n  | j t d
 „  | j ƒ  Dƒ ƒ ƒ qk W| S(   sŒ   
        Returns a list of (column_name, referenced_table_name, referenced_column_name) for all
        key columns in given table.
        s?   SELECT sql FROM sqlite_master WHERE tbl_name = %s AND type = %sRI   i    RJ   i   RK   RL   RM   s(   "(.*)".*references (.*) \(["|](.*)["|]\)c         s   s   |  ] } | j  d  ƒ Vq d S(   RB   N(   RG   (   t   .0R[   (    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/introspection.pys	   <genexpr>¶   s    (   R5   RO   RG   RQ   RR   t	   enumerateRF   RS   RT   R   RU   t   appendt   tupleRV   (   R.   R7   R>   t   key_columnsRY   t   field_indexRZ   R   (    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/introspection.pyt   get_key_columns   s    #"'c   	      C   sL  t  j d t d d ƒi  } xI |  j | | ƒ D]5 } | d d k r/ i t d 6t d 6| | d <q/ q/ W| j d	 |  j j j	 | ƒ ƒ x½ g  | j
 ƒ  D] } | d
 | d f ^ q˜ D]Ž \ } } | j d |  j j j	 | ƒ ƒ | j
 ƒ  } t | ƒ d
 k rq¶ n  | d d } i | j | i  ƒ j d t ƒ d 6| d 6| | <q¶ W| S(   Ns:   get_indexes() is deprecated in favor of get_constraints().t
   stackleveli   t   pki    t   primary_keyt   uniqueR
   s   PRAGMA index_list(%s)i   s   PRAGMA index_info(%s)(   t   warningst   warnR   R=   t   Truet   FalseR5   RC   t   opst
   quote_nameR6   t   lent   get(	   R.   R7   R>   t   indexesR?   t   fieldRQ   Ro   R
   (    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/introspection.pyt   get_indexesº   s$    
 : "c         C   sÄ   | j  d | d g ƒ | j ƒ  } | d
 k rA t d | ƒ ‚ n  | d j ƒ  } | | j d ƒ d | j d ƒ !} xI | j d ƒ D]8 } | j ƒ  } t j	 d	 | ƒ } | r„ | j
 ƒ  d Sq„ Wd
 S(   sM   
        Get the column name of the primary key for the given table.
        s?   SELECT sql FROM sqlite_master WHERE tbl_name = %s AND type = %sRI   s   Table %s does not existi    RJ   i   RK   RL   s$   "(.*)".*PRIMARY KEY( AUTOINCREMENT)?N(   R5   RO   R	   t
   ValueErrorRG   RQ   RR   RF   RT   R   RV   (   R.   R7   R>   R8   RY   RZ   R   (    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/introspection.pyt   get_primary_key_columnÑ   s    #c         C   s†   | j  d |  j j j | ƒ ƒ g  | j ƒ  D]U } i | d d 6| d d 6t | d ƒ d 6| d d 6| d	 d
 6| d d 6^ q- S(   Ns   PRAGMA table_info(%s)i   R
   i   R:   R0   i   R;   i   R<   i   Rm   (   R5   RC   Rt   Ru   R6   R   (   R.   R7   R
   Ry   (    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/introspection.pyR=   ã   s     c         C   s  i  } | j  d |  j j j | ƒ ƒ x›| j ƒ  D]} | d  \ } } } | j  d |  j j j | ƒ ƒ x{ | j ƒ  D]m \ } }	 }
 | | k rÑ i g  d 6t d 6t | ƒ d 6t d 6t d 6t d	 6| | <n  | | d j |
 ƒ qy W| | d	 r3 | | d r3 t	 j
 | | d
 <| j  d |  j j j | ƒ ƒ g  } xn | j ƒ  D]` \ } | j d ƒ d j d ƒ d j d ƒ } g  | D]! } | j d ƒ rŸd n d ^ q„} qKW| | | d <q3 q3 W|  j | | ƒ } | ri | g d 6t d 6t d 6t d 6t d 6t d	 6| d <n  | S(   sn   
        Retrieves any constraints or keys (unique, pk, fk, check, index) across one or more columns.
        s   PRAGMA index_list(%s)i   s   PRAGMA index_info(%s)t   columnsRn   Ro   t   foreign_keyt   checkRQ   R:   s<   SELECT sql FROM sqlite_master WHERE type='index' AND name=%sRJ   iÿÿÿÿRK   i    RL   t   DESCt   ASCt   orderst   __primary__(   R5   RC   Rt   Ru   R6   Rs   R   Rr   Rg   R   t   suffixRF   t   endswithR|   (   R.   R7   R>   t   constraintsR8   t   numberRQ   Ro   t
   index_rankt   column_rankR\   R‚   t   sqlt
   order_infoR?   t	   pk_column(    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/introspection.pyt   get_constraintsï   sF      )2
(   R2   R3   R   t   data_types_reverseR9   R@   RH   Rd   Rk   Rz   R|   R=   R   (    (    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/introspection.pyR4   9   s   					7				(   RT   Rp   t%   django.db.backends.base.introspectionR    R   R   t   django.db.models.indexesR   t   django.utils.deprecationR   t   compileR   R   t   objectR   R4   (    (    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/sqlite3/introspection.pyt   <module>   s   		#