a
    Lcòf8C  ã                   @   sP   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	 G dd„ deƒZ
dS )é    N)ÚBaseCommandÚCommandError)ÚDEFAULT_DB_ALIASÚconnections)Ú
LOOKUP_SEPc                   @   sL   e Zd ZdZg 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.)Útable_name_filterz	django.dbc                 C   sF   |j ddtdd� |j dtdd� |j dd	d
d� |j dd	dd� d S )NÚtableÚ*z4Selects what tables or views should be introspected.)ÚnargsÚtypeÚhelpz
--databasezMNominates a database to introspect. Defaults to using the "default" database.)Údefaultr   z--include-partitionsÚ
store_truez(Also output models for partition tables.)Úactionr   z--include-viewsz&Also output models for database views.)Úadd_argumentÚstrr   )ÚselfÚparser© r   úf/home/httpd/docs/test/DocsMgr/lib/python3.9/site-packages/django/core/management/commands/inspectdb.pyÚadd_arguments   s*    üüýýzCommand.add_argumentsc                 K   s@   z |   |¡D ]}| j |¡ qW n ty:   tdƒ‚Y n0 d S )NzPDatabase 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           )      #   s$  t |d  }| d¡}dd„ }| ¡ ��æ}dV  dV  dV  dV  d	V  d
V  dV  d| j V  g }dh‰ |d r|ˆ  d¡ |d rŽˆ  d¡ |j |¡}‡ fdd„|D ƒ}|d pÄtdd„ |D ƒƒD �]6}|d uræt|ƒræ||ƒsæqÆzœz|j 	||¡}	W n t
�y   i }	Y n0 z|j ||¡}
W n t
�y>   i }
Y n0 |j ||¡}|�r\|d nd }dd„ |
 ¡ D ƒ}|j ||¡}W nF t�yÈ } z,d| V  d| V  W Y d }~qÆW Y d }~n
d }~0 0 ||ƒ}dV  dV  d| V  | |¡ g }i }tƒ }|D �]š}g }i }|j}||	v }|  |||¡\}}}| |¡ | |¡ | |¡ |||< ||k�r�d|d< t|ƒd k�r¢| d!d" |¡ ¡ n||v �r¢d|d#< |�rb|	| \}}| d#d$¡�sÎ| d¡�rÔd%}n*d&}|j ||¡}|�rþ||k�rþ||d'< ||k�rd(n||ƒ} | |v �r,d)|| f }!nd*|| f }!| |v �rVd+| ¡ |f |d,< | | ¡ n0|  |||¡\}!}"}#| |"¡ | |#¡ |!d-7 }!|d.k�rÚ|ddik�rÚ|!d/k�rº�qn |!|jjd0 d- k�rÚ| d1¡ |j�ròd|d2< d|d3< d4|d5|!v �rdnd6|!f }$|! d7¡�r"|$d87 }$|jj �r>|j!�r>|j!|d9< |�rt|$ "d-¡�sX|$d"7 }$|$d" d:d„ | #¡ D ƒ¡7 }$|$d;7 }$|�r”|$d<d= |¡ 7 }$d>|$ V  �qd }%| |¡ }&�rÜ|&j$dk}'|&j$dk}(|jj �rä|&j!}%nd$}'d$}(|  %||
||'|(|%¡E d H  qÆW d   ƒ n1 �s0    Y  d S )?NZdatabaser   c                 S   s   t  dd|  ¡ ¡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Úvc                    s   i | ]}|j ˆ v r|j|“qS r   )r   Úname)Ú.0Úinfo©Útypesr   r   Ú
<dictcomp>X   ó    z-Command.handle_inspection.<locals>.<dictcomp>r	   c                 s   s   | ]
}|V  qd S ©Nr   )r)   r(   r   r   r   Ú	<genexpr>Z   r.   z,Command.handle_inspection.<locals>.<genexpr>r   c                 S   s0   g | ](}|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_keyr3   zZThe composite primary key (%s) found, that is not supported. The first column is selected.ú, r1   FZOneToOneFieldZ
ForeignKeyZto_fieldr   z%s(%sz%s('%s'z	%s_%s_setZrelated_nameú(Úidz
AutoField(Z	AutoFieldz
AutoField?ÚblankÚnullz	%s = %s%sÚ.zmodels.)zForeignKey(zOneToOneField(z, models.DO_NOTHINGZ
db_commentc                 s   s   | ]\}}d ||f V  qdS )z%s=%rNr   )r)   Úkr'   r   r   r   r0   ð   s   ú)z  # ú z    %s)&r   ÚgetÚcursorÚ	db_moduleÚaddÚintrospectionZget_table_listÚsortedÚcallableZget_relationsr   Zget_constraintsZget_primary_key_columnsÚvaluesZget_table_descriptionÚ	ExceptionÚappendÚsetr(   Únormalize_col_nameÚupdateÚextendr4   ÚjoinÚpopZget_primary_key_columnÚlowerÚget_field_typeÚfeaturesZintrospected_field_typesZnull_okÚ
startswithZsupports_commentsÚcommentÚendswithÚitemsr   Úget_meta))r   r   Ú
connectionr   r$   rB   Zknown_modelsZ
table_infor#   Z	relationsÚconstraintsZprimary_key_columnsZprimary_key_columnZunique_columnsZtable_descriptionÚeZ
model_nameÚused_column_namesÚcolumn_to_field_nameZused_relationsÚrowZcomment_notesZextra_paramsZcolumn_nameÚis_relationZatt_nameÚparamsÚnotesZref_db_columnZref_db_tableZrel_typeZref_pk_columnZrel_toÚ
field_typeÚfield_paramsÚfield_notesZ
field_descrU   r*   Úis_viewÚis_partitionr   r+   r   r   6   sN   
ÿÿÿ

 ÿ
ÿ
ÿÿÿþÿ

"


ÿÿ




þÿ
ÿÿÿÿý

þ
ÿ




ÿÿÿ
ü
ÿ



úzCommand.handle_inspectionc           	      C   sŠ  i }g }|  ¡ }||kr"| d¡ |rF| d¡r>|dd… }n||d< t dd|¡\}}|dkrj| d	¡ | t¡dkr°| t¡dkr”| td¡}qx|  ¡  t¡dkr°| d
¡ | d¡rÌd| }| d¡ | d¡rèd| }| d¡ t	 
|¡�r|d7 }| d¡ |d  ¡ �r&d| }| d¡ ||v �rhd}d||f |v �rR|d7 }�q4d||f }| 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.Z_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_%dr3   z'Field renamed because of name conflict.)rQ   rJ   rV   r    ÚsubnÚfindr   ÚreplacerT   ÚkeywordÚ	iskeywordÚisdigit)	r   Zcol_namer\   r_   rc   rd   Únew_nameZnum_replÚnumr   r   r   rL   	  sT    


ÿ




ÿ

zCommand.normalize_col_namec                 C   sø   i }g }z|j  |j|¡}W n  ty<   d}| d¡ Y n0 |dkrj|jrjt|jƒ }rj|dkrj||d< |dv r‚|jr‚|j|d< |dkrî|jd	u sž|j	d	u rÚ| d
¡ |jd	ur¸|jnd|d< |j	d	u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.
        Ú	TextFieldzThis field type is a guess.Ú	CharFieldr   Ú
max_length>   rq   rr   Zdb_collationZDecimalFieldNzamax_digits and decimal_places have been guessed, as this database handles decimal fields as floaté
   Z
max_digitsé   Zdecimal_places)
rE   rR   Z	type_codeÚKeyErrorrJ   Zdisplay_sizeÚintZ	collationÚ	precisionÚscale)r   rY   r#   r^   rc   rd   rb   Úsizer   r   r   rR   E  s0    
ÿÿÿ

zCommand.get_field_typec                    sî   g }d}|  ¡ D ]^}	|	d r|	d }
d|
v r0d}‡ fdd„|
D ƒ}
t|
ƒdkr| tt‡ fd	d
„|
D ƒƒƒ¡ q|rzd}n|r„d}nd}dg}|rœ| d¡ |dd| d| g7 }|rÖdd |¡ d }|d| g7 }|rê|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.
        Fr1   r2   NTc                    s    g | ]}|d ur|ˆ v r|‘qS r/   r   )r)   Úx©r]   r   r   r7   ƒ  s   z$Command.get_meta.<locals>.<listcomp>r3   c                 3   s   | ]}ˆ | V  qd S r/   r   r5   r|   r   r   r0   ˆ  r.   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 = %rr9   r8   z,)z        unique_together = %sz        db_table_comment = )rH   r4   rJ   r   ÚtuplerO   )r   r#   rZ   r]   re   rf   rU   Zunique_togetherZhas_unsupported_constraintr`   r2   Zmanaged_commentÚmetaÚtupr   r|   r   rX   n  sB    
ÿÿ
ýzCommand.get_metaN)Ú__name__Ú
__module__Ú__qualname__r   Zrequires_system_checksZstealth_optionsrC   r   r   r   rL   rR   rX   r   r   r   r   r   	   s   ÿ
 T<)r   )rl   r    Zdjango.core.management.baser   r   Z	django.dbr   r   Zdjango.db.models.constantsr   r   r   r   r   r   Ú<module>   s
   