
 X+,                 @   s   d  d l  Z  d  d l m Z d  d l m Z m Z m Z e  j d  Z e d e j	 d  Z d d   Z
 Gd	 d
   d
 e  Z Gd d   d e  Z d S)    N)
namedtuple)BaseDatabaseIntrospection	FieldInfo	TableInfoz&^\s*(?:var)?char\s*\(\s*(\d+)\s*\)\s*$r   defaultc             C   s,   t  j |   } | r( t | j d   Sd S)z8 Extract the size number from a "varchar(11)" type name    N)field_size_researchintgroup)namem r   V/home/ubuntu/projects/ifolica/build/django/django/db/backends/sqlite3/introspection.pyget_field_size   s    r   c               @   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 6Z d  d!   Z d" S)#FlexibleFieldLookupDictZBooleanFieldboolbooleanZSmallIntegerFieldZsmallintZPositiveSmallIntegerFieldzsmallint unsignedZsmallintegerZIntegerFieldr
   integerZBigIntegerFieldZbigintZPositiveIntegerFieldzinteger unsignedZDecimalFielddecimalZ
FloatFieldrealZ	TextFieldtext	CharFieldcharZBinaryFieldZblobZ	DateFielddateZDateTimeFielddatetimeZ	TimeFieldtimec             C   sc   | j    } y |  j | SWnA t k
 r^ t |  } | d  k	 rT d i | d 6f St  Yn Xd  S)Nr   
max_length)lowerbase_data_types_reverseKeyErrorr   )selfkeysizer   r   r   __getitem__-   s    z#FlexibleFieldLookupDict.__getitem__N)__name__
__module____qualname__r   r$   r   r   r   r   r      s&   
r   c               @   s   e  Z d  Z e   Z d d   Z d d   Z d d   Z d d   Z d	 d
   Z	 d d   Z
 d d   Z d d   Z d d   Z d S)DatabaseIntrospectionc             C   s$   | j  d  d d   | j   D S)zQ
        Returns a list of table and view names in the current database.
        z
            SELECT name, type FROM sqlite_master
            WHERE type in ('table', 'view') AND NOT name='sqlite_sequence'
            ORDER BY namec             S   s+   g  |  ]! } t  | d  | d d    q S)r   r   )r   ).0rowr   r   r   
<listcomp>E   s   	 z8DatabaseIntrospection.get_table_list.<locals>.<listcomp>)executefetchall)r!   cursorr   r   r   get_table_list;   s    z$DatabaseIntrospection.get_table_listc             C   s   d d   |  j  | |  D S)zQReturns a description of the table, with the DB-API cursor.description interface.c             S   sE   g  |  ]; } t  | d  | d d | d d d | d | d   q S)r   typeNr#   null_okr   )r   )r)   infor   r   r   r+   J   s   		z?DatabaseIntrospection.get_table_description.<locals>.<listcomp>)_table_info)r!   r.   
table_namer   r   r   get_table_descriptionG   s    		z+DatabaseIntrospection.get_table_descriptionc             C   s7   |  j  j j d k  r/ | j d  d	 j d  S| Sd S)
a  
        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
                 .r   "N)r6   r7   r8   )
connectionZDatabaseZsqlite_version_infosplitstrip)r!   r   r   r   r   column_name_converterV   s    
z+DatabaseIntrospection.column_name_converterc             C   s3  i  } | j  d | d g  y | j   d j   } Wn t k
 rN | SYn X| | j d  d | j d   } x| j d  D]} | j   } | j d  r q n  t j	 d	 | t j
  } | s q n  d
 d   | j   D \ } } | j d  r2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)z
        Return a dictionary of {field_name: (field_name_other_table, other_table)}
        representing all relationships to the given table.
        z?SELECT sql FROM sqlite_master WHERE tbl_name = %s AND type = %stabler   (r   ),UNIQUEz$references (\S*) ?\(["|]?(.*)["|]?\)c             S   s   g  |  ] } | j  d    q S)r:   )r>   )r)   sr   r   r   r+      s   	 z7DatabaseIntrospection.get_relations.<locals>.<listcomp>zFOREIGN KEYzFOREIGN KEY\(([^\)]*)\).*r:   z1SELECT sql FROM sqlite_master WHERE tbl_name = %s )r,   fetchoner>   	TypeErrorindexrindexr=   
startswithrer	   Igroupsmatchr-   )r!   r.   r4   Z	relationsresults
field_descr   r@   column
field_nameresultZother_table_resultsliriZ
other_descZ
other_namer   r   r   get_relationse   sB    	&z#DatabaseIntrospection.get_relationsc             C   s   g  } | j  d | d g  | j   d j   } | | j d  d | j d   } x t | j d   D]w \ } } | j   } | j d  r qn n  t j	 d	 | t j
  } | s qn n  | j t d
 d   | j   D   qn W| S)z
        Returns a list of (column_name, referenced_table_name, referenced_column_name) for all
        key columns in given table.
        z?SELECT sql FROM sqlite_master WHERE tbl_name = %s AND type = %sr@   r   rA   r   rB   rC   rD   z("(.*)".*references (.*) \(["|](.*)["|]\)c             s   s   |  ] } | j  d   Vq d S)r:   N)r>   )r)   rE   r   r   r   	<genexpr>   s    z8DatabaseIntrospection.get_key_columns.<locals>.<genexpr>)r,   rG   r>   rI   rJ   	enumerater=   rK   rL   r	   rM   appendtuplerN   )r!   r.   r4   Zkey_columnsrP   Zfield_indexrQ   r   r   r   r   get_key_columns   s    &"*z%DatabaseIntrospection.get_key_columnsc             C   s"  i  } xI |  j  | |  D]5 } | d d k r i d d 6d d 6| | d <q q W| j d |  j j j |   x d	 d
   | j   D D] \ } } | j d |  j j j |   | j   } t |  d k r q n  | d d } i | j | i   j d d  d 6| d 6| | <q W| S)Npkr   Tprimary_keyFuniquer   zPRAGMA index_list(%s)c             S   s$   g  |  ] } | d  | d f  q S)r      r   )r)   fieldr   r   r   r+      s   	 z5DatabaseIntrospection.get_indexes.<locals>.<listcomp>zPRAGMA index_info(%s)r   r`   )r3   r,   r<   ops
quote_namer-   lenget)r!   r.   r4   indexesr2   rI   r_   r   r   r   r   get_indexes   s    
 & "z!DatabaseIntrospection.get_indexesc             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)zM
        Get the column name of the primary key for the given table.
        z?SELECT sql FROM sqlite_master WHERE tbl_name = %s AND type = %sr@   NzTable %s does not existr   rA   r   rB   rC   z%"(.*)".*PRIMARY KEY( AUTOINCREMENT)?$)
r,   rG   
ValueErrorr>   rI   rJ   r=   rL   r	   rN   )r!   r.   r4   r*   rP   rQ   r   r   r   r   get_primary_key_column   s    &z,DatabaseIntrospection.get_primary_key_columnc             C   s7   | j  d |  j j j |   d d   | j   D S)NzPRAGMA table_info(%s)c             S   s_   g  |  ]U } i | d  d 6| d d 6t  | d  d 6| d d 6| d d 6| d	 d
 6 q S)r   r   r`   r0   r#   r6   r1      r      r]   )r   )r)   ra   r   r   r   r+      s   	z5DatabaseIntrospection._table_info.<locals>.<listcomp>)r,   r<   rb   rc   r-   )r!   r.   r   r   r   r   r3      s     	z!DatabaseIntrospection._table_infoc             C   sJ  i  } | j  d |  j j j |   x | j   D] } | d d  \ } } } | j  d |  j j j |   x{ | j   D]m \ } }	 }
 | | k r i g  d 6d d 6t |  d 6d d	 6d d
 6d d 6| | <n  | | d j |
  q Wq3 W|  j | |  } | rFi | g d 6d d 6d d 6d d	 6d d
 6d d 6| d <n  | S)zn
        Retrieves any constraints or keys (unique, pk, fk, check, index) across one or more columns.
        zPRAGMA index_list(%s)Nr6   zPRAGMA index_info(%s)columnsFr^   r_   Zforeign_keycheckTrI   Z__primary__)r,   r<   rb   rc   r-   r   rZ   ri   )r!   r.   r4   constraintsr*   numberrI   r_   Z
index_rankZcolumn_rankrR   Z	pk_columnr   r   r   get_constraints   s2      
z%DatabaseIntrospection.get_constraintsN)r%   r&   r'   r   Zdata_types_reverser/   r5   r?   rW   r\   rg   ri   r3   rp   r   r   r   r   r(   8   s   	7r(   )zdefault)rL   collectionsr   Z%django.db.backends.base.introspectionr   r   r   compiler   _fieldsr   objectr   r(   r   r   r   r   <module>   s   	#