o
    c_S.                     @   s   d dl mZ d dlmZmZ d dlmZ ddlZddlZdZ	dZ
dZdZd	Zd
Zdd ZG dd dZG dd deZG dd dZG dd dZG dd dZdS )   )MBLENGTH)
FIELD_TYPESERVER_STATUS)err    NF            c                    s   dd z&t dt  tddD ]}t|}t d||jj|jf  qt d W n	 ty3   Y nw  fdd	td
t	t ddD }|D ]$}t d
dd |D ddt|   d d
fdd|D   qHt d t   d S )Nc                 S   s$   d|   krdk rt | S  dS dS )N       .)chrdata r   2/usr/lib/python3/dist-packages/pymysql/protocol.py	printable   s   zdump_packet.<locals>.printablezpacket length:r      zcall[%d]: %s (line %d)zB------------------------------------------------------------------c                    s   g | ]
} ||d   qS )   r   ).0ir   r   r   
<listcomp>#   s    zdump_packet.<locals>.<listcomp>r      r    c                 s   s    | ]}d  |V  qdS )z{:02X}N)formatr   xr   r   r   	<genexpr>&   s    zdump_packet.<locals>.<genexpr>z   z   c                 3   s    | ]} |V  qd S Nr   r   )r   r   r   r   )   s    )printlenrangesys	_getframef_codeco_namef_lineno
ValueErrorminjoin)r   r   f	dump_datadr   )r   r   r   dump_packet   s.   
$
r/   c                   @   s   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	d:ddZ
d;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#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9S )<MysqlPacketzrRepresentation of a MySQL response packet.

    Provides an interface for reading/parsing the packet results.
    	_position_datac                 C   s   d| _ || _d S )Nr   r1   selfr   encodingr   r   r   __init__7   s   
zMysqlPacket.__init__c                 C   s   | j S r    r3   r5   r   r   r   get_all_data;   s   zMysqlPacket.get_all_datac                 C   sl   | j | j| j|  }t||kr-d|t|| jt| j f }tr)t| |   t||  j|7  _|S )zCRead the first 'size' bytes in packet and advance cursor past them.z\Result length not requested length:
Expected=%s.  Actual=%s.  Position: %s.  Data Length: %s)r3   r2   r"   DEBUGr!   dumpAssertionError)r5   sizeresulterrorr   r   r   read>   s   zMysqlPacket.readc                 C   s   | j | jd }d| _|S )z`Read all remaining data in the packet.

        (Subsequent read() will return errors.)
        Nr3   r2   r5   r?   r   r   r   read_allN   s   zMysqlPacket.read_allc                 C   s:   | j | }|dk s|t| jkrtd||f || _ dS )z1Advance the cursor in data buffer 'length' bytes.r   z4Invalid advance amount (%s) for cursor.  Position=%sN)r2   r"   r3   	Exception)r5   lengthnew_positionr   r   r   advanceW   s   

zMysqlPacket.advancer   c                 C   s,   |dk s|t | jkrtd| || _dS )z9Set the position of the data buffer cursor to 'position'.r   z)Invalid position to rewind cursor to: %s.N)r"   r3   rE   r2   )r5   positionr   r   r   rewinda   s   
zMysqlPacket.rewindr   c                 C   s   | j |||  S )a>  Get 'length' bytes starting at 'position'.

        Position is start of payload (first four packet header bytes are not
        included) starting at index '0'.

        No error checking is done.  If requesting outside end of buffer
        an empty string (or string shorter than 'length') may be returned!
        r8   )r5   rI   rF   r   r   r   	get_bytesg   s   	zMysqlPacket.get_bytesc                 C   s   | j | j }|  jd7  _|S Nr   rB   rC   r   r   r   
read_uint8r   s   zMysqlPacket.read_uint8c                 C   (   t d| j| jd }|  jd7  _|S )Nz<Hr      structunpack_fromr3   r2   rC   r   r   r   read_uint16w      zMysqlPacket.read_uint16c                 C   s0   t d| j| j\}}|  jd7  _||d>  S )Nz<HB   r   rP   )r5   lowhighr   r   r   read_uint24|   s   zMysqlPacket.read_uint24c                 C   rN   )Nz<Ir      rP   rC   r   r   r   read_uint32   rT   zMysqlPacket.read_uint32c                 C   rN   )Nz<Qr      rP   rC   r   r   r   read_uint64   rT   zMysqlPacket.read_uint64c                 C   s:   | j d| j}|dk rd S | j | j| }|d | _|S )N    r   r   )r3   findr2   )r5   end_posr?   r   r   r   read_string   s   
zMysqlPacket.read_stringc                 C   sT   |   }|tkr
dS |tk r|S |tkr|  S |tkr |  S |tkr(|  S dS )zRead a 'Length Coded Binary' number from the data buffer.

        Length coded numbers can be anywhere from 1 to 9 bytes depending
        on the value of the first byte.
        N)	rM   NULL_COLUMNUNSIGNED_CHAR_COLUMNUNSIGNED_SHORT_COLUMNrS   UNSIGNED_INT24_COLUMNrX   UNSIGNED_INT64_COLUMNr\   )r5   cr   r   r   read_length_encoded_integer   s   z'MysqlPacket.read_length_encoded_integerc                 C   s   |   }|du r
dS | |S )a  Read a 'Length Coded String' from the data buffer.

        A 'Length Coded String' consists first of a length coded
        (unsigned, positive) integer represented in 1-9 bytes followed by
        that many bytes of binary data.  (For example "cat" would be "3cat".)
        N)rg   rA   )r5   rF   r   r   r   read_length_coded_string   s   
z$MysqlPacket.read_length_coded_stringc                 C   s.   t |}|| j| j}|  j|j7  _|S r    )rQ   StructrR   r3   r2   r>   )r5   fmtsr?   r   r   r   read_struct   s   
zMysqlPacket.read_structc                 C   s   | j d dkot| j dkS )Nr   r   r3   r"   r9   r   r   r   is_ok_packet   s   zMysqlPacket.is_ok_packetc                 C   s   | j d dkot| j dk S )Nr   r
   	   rm   r9   r   r   r   is_eof_packet   s   zMysqlPacket.is_eof_packetc                 C      | j d dkS )Nr   r
   r8   r9   r   r   r   is_auth_switch_request      z"MysqlPacket.is_auth_switch_requestc                 C   rq   )Nr   r   r8   r9   r   r   r   is_extra_auth_data   rs   zMysqlPacket.is_extra_auth_datac                 C   s"   | j d }d|  kodkS   S )Nr   r      r8   )r5   field_countr   r   r   is_resultset_packet   s   
zMysqlPacket.is_resultset_packetc                 C   rq   )Nr   r   r8   r9   r   r   r   is_load_local_packet      z MysqlPacket.is_load_local_packetc                 C   rq   )Nr      r8   r9   r   r   r   is_error_packet   ry   zMysqlPacket.is_error_packetc                 C   s   |   r
|   d S d S r    )r{   raise_for_errorr9   r   r   r   check_error   s   zMysqlPacket.check_errorc                 C   s8   |    | d |  }trtd| t| j d S )Nr   zerrno =)rJ   rH   rS   r;   r!   r   raise_mysql_exceptionr3   )r5   errnor   r   r   r|      s   

zMysqlPacket.raise_for_errorc                 C   s   t | j d S r    )r/   r3   r9   r   r   r   r<      ry   zMysqlPacket.dumpN)r   )r   )__name__
__module____qualname____doc__	__slots__r7   r:   rA   rD   rH   rJ   rK   rM   rS   rX   rZ   r\   r`   rg   rh   rl   rn   rp   rr   rt   rw   rx   r{   r}   r|   r<   r   r   r   r   r0   /   s:    	


r0   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )FieldDescriptorPacketzA MysqlPacket that represents a specific column's metadata in the result.

    Parsing is automatically done and the results are exported via public
    attributes on the class such as: db, table_name, name, length, type_code.
    c                 C   s   t | || | | d S r    )r0   r7   _parse_field_descriptorr4   r   r   r   r7      s   zFieldDescriptorPacket.__init__c                 C   sv   |   | _|   | _|   || _|   || _|   || _|   || _| d\| _	| _
| _| _| _dS )zParse the 'Field Descriptor' (Metadata) packet.

        This is compatible with MySQL 4.1+ (not compatible with MySQL 4.0).
        z	<xHIBHBxxN)rh   catalogdbdecode
table_name	org_tablenameorg_namerl   	charsetnrrF   	type_codeflagsscale)r5   r6   r   r   r   r      s   

z-FieldDescriptorPacket._parse_field_descriptorc                 C   s*   | j | jd|  |  | j| jd dkfS )zBProvides a 7-item tuple compatible with the Python PEP249 DB Spec.NrO   r   )r   r   get_column_lengthr   r   r9   r   r   r   description  s   z!FieldDescriptorPacket.descriptionc                 C   s*   | j tjkrt| jd}| j| S | jS rL   )r   r   
VAR_STRINGr   getr   rF   )r5   mblenr   r   r   r     s   
z'FieldDescriptorPacket.get_column_lengthc                 C   s    d| j | j| j| j| j| jf S )Nz%s %r.%r.%r, type=%s, flags=%x)	__class__r   r   r   r   r   r9   r   r   r   __str__  s   zFieldDescriptorPacket.__str__N)	r   r   r   r   r7   r   r   r   r   r   r   r   r   r      s    r   c                   @       e Zd ZdZdd Zdd ZdS )OKPacketWrapperz
    OK Packet Wrapper. It uses an existing packet object, and wraps
    around it, exposing useful variables while still providing access
    to the original packet objects variables and methods.
    c                 C   sz   |  stdt| jj d || _| jd | j | _| j | _	| 
d\| _| _| j | _| jtj@ | _d S )NzCannot create z  object from invalid packet typer   z<HH)rn   r)   strr   r   packetrH   rg   affected_rows	insert_idrl   server_statuswarning_countrD   messager   SERVER_MORE_RESULTS_EXISTShas_nextr5   from_packetr   r   r   r7   '  s   
zOKPacketWrapper.__init__c                 C      t | j|S r    getattrr   r5   keyr   r   r   __getattr__8     zOKPacketWrapper.__getattr__Nr   r   r   r   r7   r   r   r   r   r   r      s    r   c                   @   r   )EOFPacketWrapperz
    EOF Packet Wrapper. It uses an existing packet object, and wraps
    around it, exposing useful variables while still providing access
    to the original packet objects variables and methods.
    c                 C   sV   |  std| j d|| _| jd\| _| _tr"td| j | jt	j
@ | _d S )NCannot create '!' object from invalid packet typez<xhhzserver_status=)rp   r)   r   r   rl   r   r   r;   r!   r   r   r   r   r   r   r   r7   C  s   zEOFPacketWrapper.__init__c                 C   r   r    r   r   r   r   r   r   O  r   zEOFPacketWrapper.__getattr__Nr   r   r   r   r   r   <  s    r   c                   @   r   )LoadLocalPacketWrapperz
    Load Local Packet Wrapper. It uses an existing packet object, and wraps
    around it, exposing useful variables while still providing access
    to the original packet objects variables and methods.
    c                 C   sL   |  std| j d|| _| j dd  | _tr$td| j d S d S )Nr   r   r   z	filename=)rx   r)   r   r   r:   filenamer;   r!   r   r   r   r   r7   Z  s   zLoadLocalPacketWrapper.__init__c                 C   r   r    r   r   r   r   r   r   e  r   z"LoadLocalPacketWrapper.__getattr__Nr   r   r   r   r   r   S  s    r   )charsetr   	constantsr   r   r   r   rQ   r$   r;   ra   rb   rc   rd   re   r/   r0   r   r   r   r   r   r   r   r   <module>   s$    5=