o
    \#f¡/  ã                   @   s„   d Z ddlZddlmZmZ ddlmZmZmZm	Z	m
Z
mZmZmZmZmZ ddddd	d
œZe d¡Zdd„ ZG dd„ dejƒZdS )a  
This module implements connections for MySQLdb. Presently there is
only one class: Connection. Others are unlikely. However, you might
want to make your own subclasses. In most cases, you will probably
override Connection.default_cursor with a non-standard Cursor class.
é    Né   )ÚcursorsÚ_mysql)
ÚWarningÚErrorÚInterfaceErrorÚ	DataErrorÚDatabaseErrorÚOperationalErrorÚIntegrityErrorÚInternalErrorÚNotSupportedErrorÚProgrammingErrorÚutf8Úcp1252Úkoi8_rÚkoi8_u)Úutf8mb4Úutf8mb3Úlatin1Úkoi8rÚkoi8uz^(\d+)c                 C   s    t  | ¡}|rt| d¡ƒS dS )zšReturns the leading numeric part of a string.

    >>> numeric_part("20-alpha")
    20
    >>> numeric_part("foo")
    >>> numeric_part("16b")
    16
    r   N)Úre_numeric_partÚmatchÚintÚgroup)ÚsÚm© r   úI/home/ubuntu/dev/venv/lib/python3.10/site-packages/MySQLdb/connections.pyÚnumeric_part#   s   

r    c                       s¶   e Zd ZdZejZ‡ fdd„Zdd„ Zdd„ Z	dd	„ Z
ddd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd‡ fdd„	Zdd„ Zdd„ ZeZeZeZeZeZeZeZeZeZeZ‡  ZS )Ú
Connectionz MySQL Database Connection Objectc                    s:  ddl m}m} ddlm}m} | ¡ }d|v r| d¡|d< d|v r*| d¡|d< d|v r3|d }n|}i }	| ¡ D ]\}
}t	|
t
ƒrRt	|tƒrR|d	d	… |	|
< q;||	|
< q;|	|d< | d
| j¡}| dd¡}| dd¡}| dd¡}| dd¡}| dd¡| _| dd¡}||jO }| dd¡}|r™||jO }||d< | dd¡}tƒ j|i |¤Ž || _dd„ | ¡ D ƒ| _tdd„ |  ¡  d¡d	d… D ƒƒ| _d| _|sÕ|  ¡ }|  ||¡ |râ|  |¡ |r|j|j|j|j |j!|j"|j#fD ]}|| j$|< qõt%| j$|j&< | j'|j(@ | _)| j)r|d	ur|  *|¡ g | _+d	S )au  
        Create a connection to the database. It is strongly recommended
        that you only use keyword parameters. Consult the MySQL C API
        documentation for more information.

        :param str host:        host to connect
        :param str user:        user to connect as
        :param str password:    password to use
        :param str passwd:      alias of password (deprecated)
        :param str database:    database to use
        :param str db:          alias of database (deprecated)
        :param int port:        TCP/IP port to connect to
        :param str unix_socket: location of unix_socket to use
        :param dict conv:       conversion dictionary, see MySQLdb.converters
        :param int connect_timeout:
            number of seconds to wait before the connection attempt fails.

        :param bool compress:   if set, compression is enabled
        :param str named_pipe:  if set, a named pipe is used to connect (Windows only)
        :param str init_command:
            command which is run once the connection is created

        :param str read_default_file:
            file from which default client values are read

        :param str read_default_group:
            configuration group to use from the default file

        :param type cursorclass:
            class object, used to create cursors (keyword only)

        :param bool use_unicode:
            If True, text-like columns are returned as unicode objects
            using the connection's character set. Otherwise, text-like
            columns are returned as bytes. Unicode objects will always
            be encoded to the connection's character set regardless of
            this setting.
            Default to True.

        :param str charset:
            If supplied, the connection character set will be changed
            to this character set.

        :param str collation:
            If ``charset`` and ``collation`` are both supplied, the
            character set and collation for the current connection
            will be set.

            If omitted, empty string, or None, the default collation
            for the ``charset`` is implied.

        :param str auth_plugin:
            If supplied, the connection default authentication plugin will be
            changed to this value. Example values:
            `mysql_native_password` or `caching_sha2_password`

        :param str sql_mode:
            If supplied, the session SQL mode will be changed to this
            setting.
            For more details and legal values, see the MySQL documentation.

        :param int client_flag:
            flags to use or 0 (see MySQL docs or constants/CLIENTS.py)

        :param bool multi_statements:
            If True, enable multi statements for clients >= 4.1.
            Defaults to True.

        :param str ssl_mode:
            specify the security settings for connection to the server;
            see the MySQL documentation for more details
            (mysql_option(), MYSQL_OPT_SSL_MODE).
            Only one of 'DISABLED', 'PREFERRED', 'REQUIRED',
            'VERIFY_CA', 'VERIFY_IDENTITY' can be specified.

        :param dict ssl:
            dictionary or mapping contains SSL connection parameters;
            see the MySQL documentation for more details
            (mysql_ssl_set()).  If this is set, and the client does not
            support SSL, NotSupportedError will be raised.
            Since mysqlclient 2.2.4, ssl=True is alias of ssl_mode=REQUIRED
            for better compatibility with PyMySQL and MariaDB.

        :param bool local_infile:
            enables LOAD LOCAL INFILE; zero disables

        :param bool autocommit:
            If False (default), autocommit is disabled.
            If True, autocommit is enabled.
            If None, autocommit isn't set and server default is used.

        :param bool binary_prefix:
            If set, the '_binary' prefix will be used for raw byte query
            arguments (e.g. Binary). This is disabled by default.

        There are a number of undocumented, non-standard methods. See the
        documentation for the MySQL C API for some hints on what they do.
        r   )ÚCLIENTÚ
FIELD_TYPE)ÚconversionsÚ_bytes_or_strÚdbÚdatabaseÚpasswdÚpasswordÚconvNÚcursorclassÚcharsetÚ Ú	collationÚuse_unicodeTÚsql_modeÚbinary_prefixFÚclient_flagÚmulti_statementsÚ
autocommitc                 S   s"   i | ]\}}t |ƒtur||“qS r   )Útyper   )Ú.0ÚkÚvr   r   r   Ú
<dictcomp>Å   s
    þz'Connection.__init__.<locals>.<dictcomp>c                 S   s   g | ]}t |ƒ‘qS r   )r    )r6   Únr   r   r   Ú
<listcomp>Ì   s    z'Connection.__init__.<locals>.<listcomp>Ú.é   Úascii),ÚMySQLdb.constantsr"   r#   ÚMySQLdb.convertersr$   r%   ÚcopyÚpopÚitemsÚ
isinstancer   ÚlistÚdefault_cursorÚgetÚ_binary_prefixÚMULTI_RESULTSÚMULTI_STATEMENTSÚsuperÚ__init__r+   ÚencodersÚtupleÚget_server_infoÚsplitÚ_server_versionÚencodingÚcharacter_set_nameÚset_character_setÚset_sql_modeÚSTRINGÚ
VAR_STRINGÚVARCHARÚ	TINY_BLOBÚMEDIUM_BLOBÚ	LONG_BLOBÚBLOBÚ	converterÚstrÚJSONÚserver_capabilitiesÚTRANSACTIONSÚ_transactionalr4   Úmessages)ÚselfÚargsÚkwargsr"   r#   r$   r%   Úkwargs2r*   Úconv2r7   r8   r+   r,   r.   r/   r0   r2   r3   r4   Út©Ú	__class__r   r   rL   8   sv   c



þÿ
ù	


zConnection.__init__c                 C   s   | S ©Nr   ©rd   r   r   r   Ú	__enter__í   s   zConnection.__enter__c                 C   s   |   ¡  d S rl   )Úclose)rd   Úexc_typeÚ	exc_valueÚ	tracebackr   r   r   Ú__exit__ð   s   zConnection.__exit__c                 C   s*   t |ƒ}|  ¡ |krtj | |¡ d S d S rl   )ÚboolÚget_autocommitr   Ú
connectionr4   )rd   Úonr   r   r   r4   ó   s   ÿzConnection.autocommitNc                 C   s   |p| j | ƒS )zÒ
        Create a cursor on which queries may be performed. The
        optional cursorclass parameter is used to create the
        Cursor. By default, self.cursorclass=cursors.Cursor is
        used.
        )r+   )rd   r+   r   r   r   Úcursorø   s   zConnection.cursorc                 C   s$   t |tƒr	t|ƒ}tj | |¡ d S rl   )rD   Ú	bytearrayÚbytesr   rv   Úquery)rd   r{   r   r   r   r{     s   
zConnection.queryc                 C   s.   t |ttfƒs	J ‚|  |¡}| jrd| S |S )Ns   _binary)rD   rz   ry   Ústring_literalrH   )rd   ÚbsÚxr   r   r   Ú_bytes_literal  s
   
zConnection._bytes_literalc                 C   s   dd  t| j|ƒ¡ S )Ns   (%s)ó   ,)ÚjoinÚmapÚliteral)rd   ri   r   r   r   Ú_tuple_literal  s   zConnection._tuple_literalc                 C   sš   t |tƒr|  | | j¡¡}n5t |tƒr|  |¡}n*t |tƒr%|  |¡}nt |tt	fƒr2|  
|¡}n|  || j¡}t |tƒrD| | j¡}t |tƒsKJ ‚|S )a  If o is a single object, returns an SQL literal as a string.
        If o is a non-string sequence, the items of the sequence are
        converted and returned as a sequence.

        Non-standard. For internal use; do not use this in your
        applications.
        )rD   r^   r|   ÚencoderR   ry   r   rz   rN   rE   r„   ÚescaperM   )rd   Úor   r   r   r   rƒ     s   



zConnection.literalc                 C   s   |   d¡ dS )ziExplicitly begin a connection.

        This method is not used when autocommit=False (default).
        s   BEGINN)r{   rm   r   r   r   Úbegin(  s   zConnection.beginc                    sD   t ƒ  |¡ t ||¡| _|r |  d|› d|› ¡ |  ¡  dS dS )z,Set the connection character set to charset.z
SET NAMES z	 COLLATE N)rK   rT   Ú_charset_to_encodingrG   rR   r{   Ústore_result)rd   r,   r.   rj   r   r   rT   /  s   þzConnection.set_character_setc                 C   s,   | j dk r	tdƒ‚|  d| ¡ |  ¡  dS )zNSet the connection sql_mode. See MySQL documentation for
        legal values.©é   r   z!server is too old to set sql_modezSET SESSION sql_mode='%s'N)rQ   r   r{   rŠ   )rd   r0   r   r   r   rU   7  s   
zConnection.set_sql_modec                 C   s.   | j dk rdS |  d¡ |  ¡ }| d¡}|S )zæReturn detailed information about warnings as a
        sequence of tuples of (Level, Code, Message). This
        is only supported in MySQL-4.1 and up. If your server
        is an earlier version, an empty sequence is returned.r‹   r   zSHOW WARNINGSr   )rQ   r{   rŠ   Ú	fetch_row)rd   ÚrÚwarningsr   r   r   Úshow_warnings?  s   


zConnection.show_warningsrl   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚCursorrF   rL   rn   rs   r4   rx   r{   r   r„   rƒ   rˆ   rT   rU   r   r   r   r   r	   r   r
   r   r   r   r   Ú__classcell__r   r   rj   r   r!   3   s6     6
	r!   )r”   Úrer-   r   r   Ú_exceptionsr   r   r   r   r	   r
   r   r   r   r   r‰   Úcompiler   r    rv   r!   r   r   r   r   Ú<module>   s    0û
