o
    ˷eB                  
   @   sR  d 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 ddlmZ zdd	lZW n eyC Z zed
ed	Z[ww ddlmZ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  ddl!m"Z" ddl#m$Z$ ddl%m&Z& ej'Z(e(dk redej) i eej*ej+iZ,edZ-G dd dZ.G dd de	Z/d	S )za
MySQL database backend for Django.

Requires mysqlclient: https://pypi.org/project/mysqlclient/
    )ImproperlyConfigured)IntegrityError)utils)BaseDatabaseWrapper)async_unsafe)cached_property)_lazy_re_compileNz:Error loading MySQLdb module.
Did you install mysqlclient?)CLIENT
FIELD_TYPE)conversions   )DatabaseClient)DatabaseCreation)DatabaseFeatures)DatabaseIntrospection)DatabaseOperations)DatabaseSchemaEditor)DatabaseValidation)r      r   z4mysqlclient 1.4.0 or newer is required; you have %s.z(\d{1,2})\.(\d{1,2})\.(\d{1,2})c                   @   s>   e Zd ZdZdZdd ZdddZdd	 Zd
d Zdd Z	dS )CursorWrappera6  
    A thin wrapper around MySQLdb's normal cursor class that catches particular
    exception instances and reraises them with the correct types.

    Implemented as a wrapper, rather than a subclass, so that it isn't stuck
    to the particular underlying representation returned by Connection.cursor().
    )i  i  i  i  c                 C   s
   || _ d S N)cursorselfr    r   T/var/www/ideatree/venv/lib/python3.10/site-packages/django/db/backends/mysql/base.py__init__E      
zCursorWrapper.__init__Nc              
   C   L   z| j ||W S  tjy% } z|jd | jv r tt|j  d }~ww Nr   )r   executeDatabaseOperationalErrorargscodes_for_integrityerrorr   tupler   queryr#   er   r   r   r    H   s   zCursorWrapper.executec              
   C   r   r   )r   executemanyr!   r"   r#   r$   r   r%   r&   r   r   r   r)   S   s   zCursorWrapper.executemanyc                 C   s   t | j|S r   )getattrr   )r   attrr   r   r   __getattr__]   s   zCursorWrapper.__getattr__c                 C   s
   t | jS r   )iterr   r   r   r   r   __iter__`   r   zCursorWrapper.__iter__r   )
__name__
__module____qualname____doc__r$   r   r    r)   r,   r/   r   r   r   r   r   5   s    

r   c                       s  e Zd ZdZi dddddddd	d
ddddddddddddddddddddddddd dd!d"d#dd$d%d&d'd(d)
Zd*Zd+d,d-d,d.d/d0d1d-d-d,d,d2Zd3Zd4d5d6d7d8d9d:Zh d;Z	e
Z
eZeZeZeZeZeZeZd<d= Zd>d? Zed@dA Z fdBdCZedadEdFZdGdH ZdIdJ Z dKdL Z!dMdN Z"dadOdPZ#dQdR Z$e%dSdT Z&e%dUdV Z'e%dWdX Z(e%dYdZ Z)e%d[d\ Z*e%d]d^ Z+e%d_d` Z,  Z-S )bDatabaseWrappermysql	AutoFieldzinteger AUTO_INCREMENTBigAutoFieldzbigint AUTO_INCREMENTBinaryFieldlongblobBooleanFieldbool	CharFieldzvarchar(%(max_length)s)	DateFielddateDateTimeFieldzdatetime(6)DecimalFieldz+numeric(%(max_digits)s, %(decimal_places)s)DurationFieldbigint	FileFieldFilePathField
FloatFieldzdouble precisionIntegerFieldintegerBigIntegerFieldIPAddressFieldzchar(15)GenericIPAddressFieldzchar(39)	JSONFieldjsonzbigint UNSIGNEDzinteger UNSIGNEDzsmallint UNSIGNEDzsmallint AUTO_INCREMENTsmallintlongtextztime(6)zchar(32))
OneToOneFieldPositiveBigIntegerFieldPositiveIntegerFieldPositiveSmallIntegerField	SlugFieldSmallAutoFieldSmallIntegerField	TextField	TimeField	UUIDField)	tinyblobblob
mediumblobr9   tinytexttext
mediumtextrN   rL   z= %szLIKE %szLIKE BINARY %sz> %sz>= %sz< %sz<= %s)exactiexactcontains	icontainsgtgteltlte
startswithendswithistartswith	iendswithzCREPLACE(REPLACE(REPLACE({}, '\\', '\\\\'), '%%', '\%%'), '_', '\_')z"LIKE BINARY CONCAT('%%', {}, '%%')zLIKE CONCAT('%%', {}, '%%')zLIKE BINARY CONCAT({}, '%%')zLIKE CONCAT({}, '%%')zLIKE BINARY CONCAT('%%', {})zLIKE CONCAT('%%', {}))ra   rb   rg   ri   rh   rj   >   repeatable readread uncommittedserializableread committedc                 C   s   | j S r   )mysql_versionr.   r   r   r   get_database_version   s   z$DatabaseWrapper.get_database_versionc                 C   s  t dd}| j}|d r|d |d< |d r|d |d< |d r&|d |d< |d	 d
r4|d	 |d< n
|d	 r>|d	 |d< |d rJt|d |d< tj|d< |d  }|dd}|ry| }|| j	vryt
d|ddd t| j	D f || _|| |S )Nutf8)convcharsetUSERuserNAMEdatabasePASSWORDpasswordHOST/unix_sockethostPORTportclient_flagOPTIONSisolation_levelrn   zKInvalid transaction isolation level '%s' specified.
Use one of %s, or None.z, c                 s   s    | ]}d | V  qdS )z'%s'Nr   ).0sr   r   r   	<genexpr>       z8DatabaseWrapper.get_connection_params.<locals>.<genexpr>)django_conversionssettings_dictrg   intr	   
FOUND_ROWScopypoplowerisolation_levelsr   joinsortedr   update)r   kwargsr   optionsr   r   r   r   get_connection_params   s@   


z%DatabaseWrapper.get_connection_paramsc                 C   s0   t jdi |}|jttu r|jt |S )Nr   )r!   connectencodersgetbytesr   )r   conn_params
connectionr   r   r   get_new_connection   s   z"DatabaseWrapper.get_new_connectionc                    s   t    g }| jjr|d | jr|d| j   |r>|  }|d	| W d    d S 1 s7w   Y  d S d S )NzSET SQL_AUTO_IS_NULL = 0z*SET SESSION TRANSACTION ISOLATION LEVEL %sz; )
superinit_connection_statefeaturesis_sql_auto_is_null_enabledappendr   upperr   r    r   )r   assignmentsr   	__class__r   r   r      s   


"z%DatabaseWrapper.init_connection_stateNc                 C   s   | j  }t|S r   )r   r   r   )r   namer   r   r   r   create_cursor  s   
zDatabaseWrapper.create_cursorc                 C   s(   zt |  W d S  tjy   Y d S w r   )r   	_rollbackr!   NotSupportedErrorr.   r   r   r   r     s
   zDatabaseWrapper._rollbackc                 C   s8   | j  | j| W d    d S 1 sw   Y  d S r   )wrap_database_errorsr   
autocommit)r   r   r   r   r   _set_autocommit  s   "zDatabaseWrapper._set_autocommitc                 C   s8   |   }|d W d   dS 1 sw   Y  dS )z
        Disable foreign key checks, primarily for use in adding rows with
        forward references. Always return True to indicate constraint checks
        need to be re-enabled.
        zSET foreign_key_checks=0NT)r   r    r   r   r   r   disable_constraint_checking#  s   

z+DatabaseWrapper.disable_constraint_checkingc                 C   sb   d| j | _ }z%|  }|d W d   n1 sw   Y  W || _ dS W || _ dS || _ w )zM
        Re-enable foreign key checks after they have been disabled.
        FzSET foreign_key_checks=1N)needs_rollbackr   r    )r   r   r   r   r   r   enable_constraint_checking-  s   

z*DatabaseWrapper.enable_constraint_checkingc           
      C   s   |   Z}|du r| j|}|D ]D}| j||}|sq| j||}| D ],\}\}}|d||||||||f  | D ]}	td||	d |||	d ||f q(qW d   dS 1 saw   Y  dS )a]  
        Check 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.
        Na0  
                        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
                        zThe 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.r   r   )	r   introspectiontable_namesget_primary_key_columnget_relationsitemsr    fetchallr   )
r   r   r   
table_nameprimary_key_column_name	relationscolumn_namereferenced_column_namereferenced_table_namebad_rowr   r   r   check_constraints:  sX   

"z!DatabaseWrapper.check_constraintsc                 C   s(   z| j   W dS  tjy   Y dS w )NFT)r   pingr!   Errorr.   r   r   r   	is_usabler  s   zDatabaseWrapper.is_usablec                 C   s   | j rdS dS )NMariaDBMySQL)mysql_is_mariadbr.   r   r   r   display_namez     zDatabaseWrapper.display_namec                 C   s4   | j jrdddd}| jr| jdk rd|d< |S i S )Nz`%(column)s` >= 0)rP   rQ   rR   )
   r      zJSON_VALID(`%(column)s`)rK   )r   !supports_column_check_constraintsr   ro   )r   r   r   r   r   data_type_check_constraints~  s   z+DatabaseWrapper.data_type_check_constraintsc                 C   sp   |   }|d | }W d    n1 sw   Y  |d |d |d t|d t|d t|d dS )	Na2  
                SELECT VERSION(),
                       @@sql_mode,
                       @@default_storage_engine,
                       @@sql_auto_is_null,
                       @@lower_case_table_names,
                       CONVERT_TZ('2001-01-01 01:00:00', 'UTC', 'UTC') IS NOT NULL
            r   r      r   r      )versionsql_modedefault_storage_enginesql_auto_is_nulllower_case_table_nameshas_zoneinfo_database)temporary_connectionr    fetchoner;   )r   r   rowr   r   r   mysql_server_data  s   





z!DatabaseWrapper.mysql_server_datac                 C   s
   | j d S )Nr   )r   r.   r   r   r   mysql_server_info  s   
z!DatabaseWrapper.mysql_server_infoc                 C   s4   t | j}|std| j tdd | D S )Nz8Unable to determine MySQL version from version string %rc                 s   s    | ]}t |V  qd S r   )r   )r   xr   r   r   r     r   z0DatabaseWrapper.mysql_version.<locals>.<genexpr>)server_version_rematchr   	Exceptionr%   groups)r   r   r   r   r   ro     s   zDatabaseWrapper.mysql_versionc                 C   s   d| j  v S )Nmariadb)r   r   r.   r   r   r   r     r   z DatabaseWrapper.mysql_is_mariadbc                 C   s"   | j d }t|r|dS dS )Nr   ,r   )r   setsplit)r   r   r   r   r   r     s   
zDatabaseWrapper.sql_moder   ).r0   r1   r2   vendor
data_types_limited_data_types	operatorspattern_escpattern_opsr   r!   r   SchemaEditorClassr   client_classr   creation_classr   features_classr   introspection_classr   	ops_classr   validation_classrp   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ro   r   r   __classcell__r   r   r   r   r4   d   s    	
#	'



8




	
r4   )0r3   django.core.exceptionsr   	django.dbr   django.db.backendsr   backend_utilsdjango.db.backends.base.baser   django.utils.asyncior   django.utils.functionalr   django.utils.regex_helperr   MySQLdbr!   ImportErrorerrMySQLdb.constantsr	   r
   MySQLdb.convertersr   clientr   creationr   r   r   r   r   
operationsr   schemar   
validationr   version_infor   __version__TIMEtypecast_timer   r   r   r4   r   r   r   r   <module>   sP    
/