
v^5                 @   sn   d  d l  Z  d  d l Z d  d l m Z m Z d  d l m Z m Z d  d l m	 Z	 Gd d   d e  Z
 d S)    N)BaseCommandCommandError)DEFAULT_DB_ALIASconnections)
LOOKUP_SEPc               @   sp   e  Z d  Z d Z d Z d Z d Z d d   Z d d   Z d	 d
   Z	 d d   Z
 d d   Z d d   Z d S)CommandzXIntrospects the database tables in the given database and outputs a Django model module.Ftable_name_filterz	django.dbc             C   sn   | j  d d d d t d d | j  d d t d d	 | j  d
 d d d d | j  d d d d d d  S)Ntablenargs*typehelpz4Selects what tables or views should be introspected.z
--databasedefaultzMNominates a database to introspect. Defaults to using the "default" database.z--include-partitionsaction
store_truez(Also output models for partition tables.z--include-viewsz&Also output models for database views.)add_argumentstrr   )selfparser r   K/tmp/pip-build-8lau8j11/django/django/core/management/commands/inspectdb.pyadd_arguments   s    zCommand.add_argumentsc             K   sW   y2 x+ |  j  |  D] } |  j j d |  q WWn t k
 rR t d   Yn Xd  S)Nz%s
zPDatabase inspection isn't supported for the currently selected database backend.)handle_inspectionstdoutwriteNotImplementedErrorr   )r   optionsliner   r   r   handle   s
    zCommand.handlec       !   $   #   sW  t  | d } | j d  } d d   } | j   } d Vd Vd Vd Vd	 Vd
 Vd Vd |  j Vg  } | j j |  } d h  | d r  j d  | d r  j d  x| d p t  f d d   | D  D]h  | d  k	 rt |  r|    sq y y | j j	 |    } Wn t
 k
 rCi  } Yn Xy | j j |    }	 Wn t
 k
 rwi  }	 Yn X| j j |    }
 d d   |	 j   D } | j j |    } Wn: t k
 r} z d   Vd | Vw WYd  d  } ~ Xn Xd Vd Vd |    V| j |     g  } i  } x| D]} g  } i  } | j } | | k } |  j | | |  \ } } } | j |  | j |  | j |  | | | <| |
 k rd | d <n | | k rd | d <| rs| j d d  s| j d  rd } n d  } | | d!   k r-d" n | | | d!  } | | k r`d# | | f } qd$ | | f } nB |  j |   |  \ } } } | j |  | j |  | d% 7} | d& k r| d d i k r| d' k rq8n& | d( k r| j j r| j d)  | j r(d | d* <d | d+ <d, | d- | k r@d n d. | f } | j d:  rf| d1 7} | r| j d%  s| d2 7} | d2 j d3 d   | j   D  7} | d4 7} | r| d5 d6 j |  7} d7 | Vq8Wt   f d8 d   | D  } t   f d9 d   | D  } x( |  j   |	 | | |  D] }  |  Vq9Wq WWd  QRXd  S);NZdatabaser   c             S   s   t  j d d |  j    S)Nz[^a-zA-Z0-9] )resubtitle)
table_namer   r   r   table2model+   s    z.Command.handle_inspection.<locals>.table2modelz0# This is an auto-generated Django model module.z<# You'll have to do the following manually to clean this up:z#   * Rearrange models' orderz>#   * Make sure each model has one field with primary_key=Truez]#   * Make sure each ForeignKey and OneToOneField has `on_delete` set to the desired behaviorzh#   * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the tablezR# Feel free to rename the models, but don't rename db_table values or field names.zfrom %s import modelstZinclude_partitionspZinclude_viewsvr	   c             3   s'   |  ] } | j    k r | j Vq d  S)N)r   name).0info)typesr   r   	<genexpr>D   s    z,Command.handle_inspection.<locals>.<genexpr>c             S   s>   g  |  ]4 } | d  r t  | d  d k r | d d  q S)uniquecolumns   r   )len)r)   cr   r   r   
<listcomp>S   s   	 z-Command.handle_inspection.<locals>.<listcomp>z# Unable to inspect table '%s'z# The error was: %sr   zclass %s(models.Model):TZprimary_keyr-   FZOneToOneFieldZ
ForeignKeyr/   r   z%s(%sz%s('%s'(idz
AutoField(zIntegerField(z
AutoField?Zblanknullz	%s = %s%s.zmodels.ForeignKey(OneToOneField(z, models.DO_NOTHINGz, c             s   s%   |  ] \ } } d  | | f Vq d S)z%s=%rNr   )r)   kr'   r   r   r   r,      s    )z  #  z    %sc             3   s-   |  ]# } | j    k o$ | j d  k Vq d S)r'   N)r(   r   )r)   r*   )r#   r   r   r,      s    c             3   s-   |  ]# } | j    k o$ | j d  k Vq d S)r&   N)r(   r   )r)   r*   )r#   r   r   r,      s    )r7   r8   ) r   getcursor	db_moduleintrospectionZget_table_listaddsortedcallableZget_relationsr   Zget_constraintsZget_primary_key_columnvaluesZget_table_description	Exceptionappendr(   normalize_col_nameupdateextendpopget_field_typefeaturesZcan_introspect_autofieldZnull_ok
startswithendswithjoinitemsanyget_meta)!r   r   
connectionr   r$   r=   Zknown_modelsZ
table_infoZ	relationsconstraintsZprimary_key_columnZunique_columnsZtable_descriptioneused_column_namescolumn_to_field_namerowZcomment_notesZextra_paramsZcolumn_nameis_relationZatt_nameparamsZnotesZrel_typeZrel_to
field_typefield_paramsfield_notesZ
field_descis_viewis_partitionZ	meta_liner   )r#   r+   r   r   &   s    	

0			

!	
	




&
"zCommand.handle_inspectionc       	      C   s  i  } g  } | j    } | | k r1 | j d  | rc | j d  rY | d d  } n
 | | d <t j d d |  \ } } | d k r | j d	  | j t  d k rx+ | j t  d k r | j t d  } q W| j    j t  d k r| j d
  | j d  r(d | } | j d  | j d  rNd | } | j d  t	 j
 |  rt| d 7} | j d  | d j   rd | } | j d  | | k rd } x$ d | | f | k r| d 7} qWd | | f } | j d  | | k r| r| | d <| | | f S)zU
        Modify the column name to make it Python-compatible as a field name
        zField name made lowercase._idN   Z	db_columnz\W_r   z.Field renamed to remove unsuitable characters.z>Field renamed because it contained more than one '_' in a row.zfield%sz*Field renamed because it started with '_'.z%sfieldz(Field renamed because it ended with '_'.Z_fieldz4Field renamed because it was a Python reserved word.z	number_%sz:Field renamed because it wasn't a valid Python identifier.z%s_%dr/   z'Field renamed because of name conflict.)lowerrE   rM   r    subnfindr   replacerL   keyword	iskeywordisdigit)	r   Zcol_namerU   rX   r[   r\   new_nameZnum_replnumr   r   r   rF      sL    





zCommand.normalize_col_namec             C   s  i  } g  } y | j  j | j |  } Wn% t k
 rO d } | j d  Yn X| d k rx | j rx t | j  | d <| d k r| j d k s | j d k r | j d  | j d k	 r | j n d | d	 <| j d k	 r | j n d
 | d <n | j | d	 <| j | d <| | | f S)z
        Given the database connection, the table name, and the cursor row
        description, this routine will return the given field type name, as
        well as any additional keyword parameters and notes for the field.
        Z	TextFieldzThis field type is a guess.Z	CharField
max_lengthZDecimalFieldNzamax_digits and decimal_places have been guessed, as this database handles decimal fields as float
   Z
max_digits   Zdecimal_places)	r?   rJ   Z	type_codeKeyErrorrE   Zinternal_sizeintZ	precisionZscale)r   rR   r#   rW   r[   r\   rZ   r   r   r   rJ      s$    "%zCommand.get_field_typec                s'  g  } d } x | j    D]} } | d r | d }	 d |	 k rE d } d d   |	 D }	 t |	  d k r | j t t   f d	 d
   |	 D    q W| r d }
 n | r d }
 n d }
 d g } | r | j d  | d d |
 d | g 7} | r#d d j |  d } | d | g 7} | S)z
        Return a sequence comprising the lines of code necessary
        to construct the inner Meta class for the model corresponding
        to the given database table name.
        Fr-   r.   NTc             S   s"   g  |  ] } | d  k	 r |  q S)Nr   )r)   xr   r   r   r2     s   	 z$Command.get_meta.<locals>.<listcomp>r/   c             3   s   |  ] }   | Vq d  S)Nr   )r)   r1   )rV   r   r   r,     s    z#Command.get_meta.<locals>.<genexpr>z&  # Created from a view. Don't remove.z+  # Created from a partition. Don't remove.r   z4    # A unique constraint could not be introspected.z    class Meta:z        managed = False%sz        db_table = %rr3   z, z,)z        unique_together = %s)rC   r0   rE   r   tuplerN   )r   r#   rS   rV   r]   r^   Zunique_togetherZhas_unsupported_constraintrY   r.   Zmanaged_commentmetatupr   )rV   r   rQ     s4    

0			zCommand.get_metaN)ztable_name_filter)__name__
__module____qualname__r   Zrequires_system_checksZstealth_optionsr>   r   r   r   rF   rJ   rQ   r   r   r   r   r   	   s   8 r   )rg   r    Zdjango.core.management.baser   r   Z	django.dbr   r   Zdjango.db.models.constantsr   r   r   r   r   r   <module>   s
   