B
    ɢ]              	   @   sT  d dl mZ ddlmZmZmZmZmZmZ d dl	Z	d dl
Z
d dlZd dlZd dlZd dlZd dlZd dlZddlmZ ddlmZmZ ddlmZmZmZmZmZ ddlmZ dd	lmZ dd
lm Z  ddl!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z' ddl(m)Z)m*Z* ddlm+Z+m,Z, yd dl-Z-dZ.W n e/k
r6   dZ-dZ.Y nX yd dl0Z0e01 Z2[0W n e/e3fk
rl   dZ2Y nX dZ4ej5dd Z6ern.e6dk rdd e7dD Z8dd Z9ndd Z9eresddl:m;Z; dd Z<ndd Z<ej=ej>ej?ej@ejAejBejCejDejEh	ZFdZGdZHdd ZId d! ZJG d"d# d#eKZLG d$d% d%eKZMG d&d' d'eKZNdS )(    )print_function   )PY2
range_type	text_typestr_typeJYTHON
IRONPYTHONN)_auth)charset_by_namecharset_by_id)CLIENTCOMMANDCR
FIELD_TYPESERVER_STATUS)
converters)Cursor)Parser)dump_packetMysqlPacketFieldDescriptorPacketOKPacketWrapperEOFPacketWrapperLoadLocalPacketWrapper)byte2intint2byte)errVERSION_STRINGTF   )      c             C   s(   g | ] }|d k rt |n
t |d qS )   i   )chr).0i r&   TC:\Users\jaco\AppData\Local\Temp\pip-install-vjvq8kq0\pymysql\pymysql\connections.py
<listcomp>6   s    r(      c             C   s   |  dtS )Nlatin1)decode	translate_surrogateescape_table)sr&   r&   r'   _fast_surrogateescape8   s    r/   c             C   s   |  ddS )Nasciisurrogateescape)r+   )r.   r&   r&   r'   r/   ;   s    )SocketIOc             C   s   t t| |S )N)ioBufferedReaderr2   )sockmoder&   r&   r'   	_makefileF   s    r7   c             C   s
   |  |S )N)makefile)r5   r6   r&   r&   r'   r7   J   s    Zutf8mb4i c             C   s   t d| d d S )Nz<Ir    )structpack)nr&   r&   r'   
pack_int24`   s    r<   c             C   s   | dk rt d|  np| dk r&t| S | dk r>dtd|  S | dk r^dtd	| d d
  S | dk rvdtd|  S t d| df d S )Nr   zFEncoding %d is less than 0 - no representation in LengthEncodedInteger   i      z<Hi      z<Ir    l               z<QzIEncoding %x is larger than %x - no representation in LengthEncodedInteger)
ValueErrorr   r9   r:   )r%   r&   r&   r'   
lenenc_inte   s    rB   c                @   s  e Zd ZdZdZdZdZdZddddddddddddedddddddddddddddddddf dd	Z	d
d Z
dd Zedd Zdd Ze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dad$d%Zd&d' Zd(d) Zd*d+ Zdbd,d-Zd.d/ Zd0d1 Zdcd2d3Z ddd4d5Z!d6d7 Z"d8d9 Z#ded;d<Z$d=d> Z%dfd?d@Z&dAdB Z'e(fdCdDZ)dEdF Z*dGdH Z+dgdIdJZ,dKdL Z-dMdN Z.dOdP Z/dQdR Z0dSdT Z1dUdV Z2dWdX Z3dYdZ Z4d[d\ Z5d]d^ Z6d_d` Z7e8j9Z9e8j:Z:e8j;Z;e8j<Z<e8j=Z=e8j>Z>e8j?Z?e8j@Z@e8jAZAe8jBZBdS )h
Connectiona  
    Representation of a socket with a mysql server.

    The proper way to get an instance of this class is to call
    connect().

    Establish a connection to the MySQL database. Accepts several
    arguments:

    :param host: Host where the database server is located
    :param user: Username to log in as
    :param password: Password to use.
    :param database: Database to use, None to not use a particular one.
    :param port: MySQL port to use, default is usually OK. (default: 3306)
    :param bind_address: When the client has multiple network interfaces, specify
        the interface from which to connect to the host. Argument can be
        a hostname or an IP address.
    :param unix_socket: Optionally, you can use a unix socket rather than TCP/IP.
    :param read_timeout: The timeout for reading from the connection in seconds (default: None - no timeout)
    :param write_timeout: The timeout for writing to the connection in seconds (default: None - no timeout)
    :param charset: Charset you want to use.
    :param sql_mode: Default SQL_MODE to use.
    :param read_default_file:
        Specifies  my.cnf file to read these parameters from under the [client] section.
    :param conv:
        Conversion dictionary to use instead of the default one.
        This is used to provide custom marshalling and unmarshaling of types.
        See converters.
    :param use_unicode:
        Whether or not to default to unicode strings.
        This option defaults to true for Py3k.
    :param client_flag: Custom flags to send to MySQL. Find potential values in constants.CLIENT.
    :param cursorclass: Custom cursor class to use.
    :param init_command: Initial SQL statement to run when connection is established.
    :param connect_timeout: Timeout before throwing an exception when connecting.
        (default: 10, min: 1, max: 31536000)
    :param ssl:
        A dict of arguments similar to mysql_ssl_set()'s parameters.
    :param read_default_group: Group to read from in the configuration file.
    :param compress: Not supported
    :param named_pipe: Not supported
    :param autocommit: Autocommit mode. None means use server default. (default: False)
    :param local_infile: Boolean to enable the use of LOAD DATA LOCAL command. (default: False)
    :param max_allowed_packet: Max size of packet sent to server in bytes. (default: 16MB)
        Only used to limit size of "LOAD LOCAL INFILE" data packet smaller than default (16KB).
    :param defer_connect: Don't explicitly connect on contruction - wait for connect call.
        (default: False)
    :param auth_plugin_map: A dict of plugin names to a class that processes that plugin.
        The class will take the Connection object as the argument to the constructor.
        The class needs an authenticate method taking an authentication packet as
        an argument.  For the dialog plugin, a prompt(echo, prompt) method can be used
        (if no authenticate method) for returning a string from the user. (experimental)
    :param server_public_key: SHA256 authenticaiton plugin public key value. (default: None)
    :param db: Alias for database. (for compatibility to MySQLdb)
    :param passwd: Alias for password. (for compatibility to MySQLdb)
    :param binary_prefix: Add _binary prefix on bytes and bytearray. (default: False)

    See `Connection <https://www.python.org/dev/peps/pep-0249/#connection-objects>`_ in the
    specification.
    N Fr   
   i   c!       $         s|  |d krt jd dkrd}|d k	r.|d kr.|}|d k	r>|s>|}|sF|rNtdt|| _| jrh|tjO }r|	st jdrd}	nd}	|	rVsdt	   
tj|	  fd	d
}!|!d|}|!d|}|!d|}|!d|}|!d|}t|!d|}|!d|}|!d|}|si }t|trVx0dD ](}"|!d|" ||"}#|#r*|#||"< q*W d| _|rtsptdd| _|tjO }| || _|pd| _|pd| _|pt| _|pd| _t| jtr| jd| _|| _|| _|| _ d|  k rdksn t!d|pd | _"|d k	r4|dkr4t!d|| _#|d k	rV|dkrVt!d|| _$|rp|| _%d| _&nt'| _%d| _&|d k	r|| _&t(| j%j)| _)|tj*O }| jr|tj+O }|| _,|| _-d | _.d| _/d| _0|| _1|
d krt2j3}
d d! |
4 D | _5d"d! |
4 D | _6|| _7|| _8|| _9|p,i | _:|| _;| | _<d#t=t> t?d$| _@|rb|| j@d%< |rpd | _An| B  d S )&Nr   r   Tz3compress and named_pipe arguments are not supportedwinz	c:\my.iniz/etc/my.cnfclientc                s.   |r|S y  | S  tk
r(   |S X d S )N)get	Exception)keyarg)cfgread_default_groupr&   r'   _config   s    z$Connection.__init__.<locals>._configuserpasswordhostdatabasesocketportzbind-addresszdefault-character-set)cacapathcertrJ   cipherzssl-Fzssl module not found	localhosti      r*   i3z+connect_timeout should be >0 and <=31536000zread_timeout should be >= 0zwrite_timeout should be >= 0zNot connectedc             S   s"   i | ]\}}t |tk	r||qS r&   )typeint)r$   kvr&   r&   r'   
<dictcomp>0  s    z'Connection.__init__.<locals>.<dictcomp>c             S   s"   i | ]\}}t |tkr||qS r&   )r[   r\   )r$   r]   r^   r&   r&   r'   r_   1  s    Zpymysql)Z_client_nameZ_pidZ_client_versionprogram_name)Csysversion_infoNotImplementedErrorbool_local_infiler   ZLOCAL_FILESplatform
startswithr   readospath
expanduserr\   
isinstancedictrH   sslSSL_ENABLEDSSL_create_ssl_ctxctxrQ   rT   DEFAULT_USERrO   rP   r   encodedbunix_socketbind_addressrA   connect_timeout_read_timeout_write_timeoutcharsetuse_unicodeDEFAULT_CHARSETr   encodingZCAPABILITIESCONNECT_WITH_DBclient_flagcursorclass_result_affected_rows	host_infoautocommit_moder   Zconversionsitemsencodersdecoderssql_modeinit_commandmax_allowed_packet_auth_plugin_map_binary_prefixserver_public_keystrgetpidr   _connect_attrs_sockconnect)$selfrQ   rO   rP   rR   rT   rv   r{   r   Zread_default_fileconvr|   r   r   r   rx   rn   rM   compressZ
named_pipe
autocommitru   passwdZlocal_infiler   Zdefer_connectZauth_plugin_mapread_timeoutZwrite_timeoutrw   Zbinary_prefixr`   r   rN   rJ   valuer&   )rL   rM   r'   __init__   s    

















zConnection.__init__c             C   s   t |tjr|S |d}|d}|d ko2|d k}tj||d}| oR|dd|_|r`tjntj|_d|kr|j	|d |dd d	|kr|
|d	  | jtjO  _| jtjO  _|S )
NrU   rV   )cafilerV   check_hostnameTrW   rJ   )keyfilerX   )rl   rn   
SSLContextrH   create_default_contextr   	CERT_NONECERT_REQUIREDverify_modeload_cert_chainset_ciphersoptionsOP_NO_SSLv2OP_NO_SSLv3)r   ZsslprU   rV   Zhasnocarr   r&   r&   r'   rq   G  s    

zConnection._create_ssl_ctxc             C   sl   | j rtdd| _ | jdkr$dS tddtj}z(y| | W n t	k
rX   Y nX W d| 
  X dS )z
        Send the quit message and close the socket.

        See `Connection.close() <https://www.python.org/dev/peps/pep-0249/#Connection.close>`_
        in the specification.

        :raise Error: If the connection is already closed.
        zAlready closedTNz<iBr   )_closedr   Errorr   r9   r:   r   ZCOM_QUIT_write_bytesrI   _force_close)r   	send_datar&   r&   r'   closeX  s    	


zConnection.closec             C   s
   | j dk	S )z%Return True if the connection is openN)r   )r   r&   r&   r'   openn  s    zConnection.openc             C   s2   | j r"y| j   W n   Y nX d| _ d| _dS )z%Close connection without QUIT messageN)r   r   _rfile)r   r&   r&   r'   r   s  s    zConnection._force_closec             C   s&   t || _|  }||kr"|   d S )N)rd   r   get_autocommit_send_autocommit_mode)r   r   currentr&   r&   r'   r     s    
zConnection.autocommitc             C   s   t | jtj@ S )N)rd   server_statusr   ZSERVER_STATUS_AUTOCOMMIT)r   r&   r&   r'   r     s    zConnection.get_autocommitc             C   s0   |   }| stddt|}|j| _|S )Ni  zCommand Out of Sync)_read_packetis_ok_packetr   OperationalErrorr   r   )r   pktokr&   r&   r'   _read_ok_packet  s    zConnection._read_ok_packetc             C   s&   |  tjd| | j  |   dS )z2Set whether or not to commit after every execute()zSET AUTOCOMMIT = %sN)_execute_commandr   	COM_QUERYescaper   r   )r   r&   r&   r'   r     s    
z Connection._send_autocommit_modec             C   s   |  tjd |   dS )zBegin transaction.ZBEGINN)r   r   r   r   )r   r&   r&   r'   begin  s    zConnection.beginc             C   s   |  tjd |   dS )z
        Commit changes to stable storage.

        See `Connection.commit() <https://www.python.org/dev/peps/pep-0249/#commit>`_
        in the specification.
        ZCOMMITN)r   r   r   r   )r   r&   r&   r'   commit  s    zConnection.commitc             C   s   |  tjd |   dS )z
        Roll back the current transaction.

        See `Connection.rollback() <https://www.python.org/dev/peps/pep-0249/#rollback>`_
        in the specification.
        ZROLLBACKN)r   r   r   r   )r   r&   r&   r'   rollback  s    zConnection.rollbackc             C   s$   |  tjd t| }|  |jS )z%Send the "SHOW WARNINGS" SQL command.zSHOW WARNINGS)r   r   r   MySQLResultrh   rows)r   resultr&   r&   r'   show_warnings  s    zConnection.show_warningsc             C   s   |  tj| |   dS )zI
        Set current db.

        :param db: The name of the db.
        N)r   r   ZCOM_INIT_DBr   )r   ru   r&   r&   r'   	select_db  s    zConnection.select_dbc             C   sX   t |trd| | d S t |ttfrF| |}| jrBd| }|S tj|| j	|dS )z}Escape whatever value you pass to it.

        Non-standard, for internal use; do not use this in your applications.
        '_binary)mapping)
rl   r   escape_stringbytes	bytearray_quote_bytesr   r   Zescape_itemr{   )r   objr   retr&   r&   r'   r     s    

zConnection.escapec             C   s   |  || jS )zjAlias for escape()

        Non-standard, for internal use; do not use this in your applications.
        )r   r   )r   r   r&   r&   r'   literal  s    zConnection.literalc             C   s"   | j tj@ r|ddS t|S )Nr   z'')r   r   "SERVER_STATUS_NO_BACKSLASH_ESCAPESreplacer   r   )r   r.   r&   r&   r'   r     s    zConnection.escape_stringc             C   s,   | j tj@ r"dt|ddf S t|S )Nz'%s'   's   '')r   r   r   r/   r   r   Zescape_bytes)r   r.   r&   r&   r'   r     s    zConnection._quote_bytesc             C   s   |r|| S |  | S )a	  
        Create a new cursor to execute queries with.

        :param cursor: The type of cursor to create; one of :py:class:`Cursor`,
            :py:class:`SSCursor`, :py:class:`DictCursor`, or :py:class:`SSDictCursor`.
            None means use Cursor.
        )r   )r   cursorr&   r&   r'   r     s    zConnection.cursorc             C   s   t dt |  S )z%Context manager that returns a CursorzHContext manager API of Connection object is deprecated; Use conn.begin())warningswarnDeprecationWarningr   )r   r&   r&   r'   	__enter__  s    zConnection.__enter__c             C   s   |r|    n|   dS )z2On successful exit, commit. On exception, rollbackN)r   r   )r   excr   	tracebackr&   r&   r'   __exit__  s    
zConnection.__exit__c             C   sT   t |tr2ts2ts2tr$|| j}n|| jd}| tj	| | j
|d| _| jS )Nr1   )
unbuffered)rl   r   r   r	   r   rt   r~   r   r   r   _read_query_resultr   )r   sqlr   r&   r&   r'   query  s    zConnection.queryc             C   s   | j |d| _| jS )N)r   )r   r   )r   r   r&   r&   r'   next_result  s    zConnection.next_resultc             C   s   | j S )N)r   )r   r&   r&   r'   affected_rows  s    zConnection.affected_rowsc             C   s"   t d|}| tj| |  S )Nz<I)r9   r:   r   r   ZCOM_PROCESS_KILLr   )r   	thread_idrK   r&   r&   r'   kill  s    zConnection.killTc             C   st   | j dkr&|r|   d}n
tdy| tjd |   W n. tk
rn   |rh|   | 	d n Y nX dS )z
        Check if the server is alive.

        :param reconnect: If the connection is closed, reconnect.
        :raise Error: If the connection is closed and reconnect=False.
        NFzAlready closedrD   )
r   r   r   r   r   r   ZCOM_PINGr   rI   ping)r   Z	reconnectr&   r&   r'   r     s    

zConnection.pingc             C   s:   t |j}| tjd| |  |   || _|| _d S )NzSET NAMES %s)r   r~   r   r   r   r   r   r{   )r   r{   r~   r&   r&   r'   set_charset+  s
    
zConnection.set_charsetc          
   C   s\  d| _ y|d kr"| jr\ttjtj}|| j || j d| _d| _	t
rZtd ni }| jd k	rx| jdf|d< x\y tj| j| jf| jf|}P W qz ttfk
r } z|jtjkrwz W d d }~X Y qzX qzW d| j| jf | _t
rtd |tjtjd	 |d  |tjtjd	 || _t|d
| _d| _|   |   | jd k	rn|   }|!d| jf | j"d k	r|   }|!| j" |#  | $  | j%d k	r| &| j% W n t'k
rV } z~d | _|d k	ry|#  W n   Y nX t(|tttj)frDt*+dd| j|f }||_,t-. |_-t
r@t|j- | W d d }~X Y nX d S )NFzLocalhost via UNIX socketTzconnected using unix_socketr   source_addresszsocket %s:%dzconnected using socketr   rbzSET sql_mode=%si  z(Can't connect to MySQL server on %r (%s))/r   rv   rS   AF_UNIXSOCK_STREAM
settimeoutrx   r   r   _secureDEBUGprintrw   create_connectionrQ   rT   OSErrorIOErrorerrnoEINTR
setsockoptIPPROTO_TCPTCP_NODELAY
SOL_SOCKETSO_KEEPALIVEr   r7   r   _next_seq_id_get_server_information_request_authenticationr   r   executer   r   r   r   r   BaseExceptionrl   errorr   r   Zoriginal_exceptionr   
format_exc)r   r5   kwargsecr   r&   r&   r'   r   4  s~    
 

 


 
zConnection.connectc             C   sD   t t|t| j | }tr&t| | | | jd d | _dS )zwWrites an entire "mysql packet" in its entirety to the network
        addings its length and sequence number.
        r   r)   N)r<   lenr   r   r   r   r   )r   payloaddatar&   r&   r'   write_packet}  s
     
zConnection.write_packetc       
      C   s   d}x|  d}td|\}}}||d>  }|| jkrj|   |dkrVttjdt	d|| jf | jd d	 | _|  |}t
rt| ||7 }|d
krq|tk rP qW ||| j}	|	  |	S )a!  Read an entire "mysql packet" in its entirety from the network
        and return a MysqlPacket type that represents the results.

        :raise OperationalError: If the connection to the MySQL server is lost.
        :raise InternalError: If the packet sequence number is wrong.
        rZ      z<HBB   r   z,Lost connection to MySQL server during queryz1Packet sequence number wrong - got %d expected %dr   r)   i )_read_bytesr9   unpackr   r   r   r   r   CR_SERVER_LOSTInternalErrorr   r   MAX_PACKET_LENr~   check_error)
r   Zpacket_typeZbuffZpacket_headerZbtrlZbtrhZpacket_numberZbytes_to_readZ	recv_datapacketr&   r&   r'   r     s4    


 zConnection._read_packetc          
   C   s   | j | j xy| j|}P W q ttfk
rr } z.|jtjkrFw| 	  t
tjd|f W d d }~X Y q tk
r   | 	   Y qX qW t||k r| 	  t
tjd|S )Nz1Lost connection to MySQL server during query (%s)z,Lost connection to MySQL server during query)r   r   ry   r   rh   r   r   r   r   r   r   r   r   r	  r   r  )r   	num_bytesr  r   r&   r&   r'   r    s(    
zConnection._read_bytesc          
   C   sb   | j | j y| j | W n> tk
r\ } z |   ttj	d|f W d d }~X Y nX d S )NzMySQL server has gone away (%r))
r   r   rz   sendallr   r   r   r   r   ZCR_SERVER_GONE_ERROR)r   r  r   r&   r&   r'   r     s    zConnection._write_bytesc             C   sj   d | _ |r<yt| }|  W qL   d|_d |_ Y qLX nt| }|  || _ |jd k	rd|j| _|jS )NF)r   r   init_unbuffered_queryunbuffered_active
connectionrh   r   r   )r   r   r   r&   r&   r'   r     s    

zConnection._read_query_resultc             C   s   | j r| j jS dS d S )Nr   )r   	insert_id)r   r&   r&   r'   r    s    zConnection.insert_idc             C   s  | j std| jdk	rR| jjr6td | j  x| jjrJ| 	  q8W d| _t
|trh|| j}ttt|d }td||}||d|d   }| | trt| d| _|tk rdS ||d d }x>ttt|}| |d|  ||d }|s|tk rP qW dS )z~
        :raise InterfaceError: If the connection is closed.
        :raise ValueError: If no username was specified.
        z(0, '')Nz.Previous unbuffered result was left incompleter   z<iB)r   r   InterfaceErrorr   r  r   r   _finish_unbuffered_queryhas_nextr   rl   r   rt   r~   minr  r  r9   r:   r   r   r   r   r  )r   commandr   packet_sizeZpreluder  r&   r&   r'   r     s6    






 zConnection._execute_commandc       
      C   s  t | jddd dkr*|  jtjO  _| jd kr<tdt| j	j
}t| jtrd| j| j| _td| jt|d}| jr| jtj@ r| | | jj| j| jd| _t| jd	| _d
| _|| j d }d}d }| jdkrd}t| j | j!}n| jdkrd}t| j | j!}n| jdkr^d}| j rNt"r<t#d t$| j | j!}nt"rt#d nD| jdkrd}| jr| jtj@ r| j d }n| j rd}nd}| jtj%@ r|t&t'|| 7 }n4| jtj(@ r|tdt'|| 7 }n||d 7 }| j)r<| jtj*@ r<t| j)tr.| j)| j| _)|| j)d 7 }| jtj+@ r\||pTdd 7 }| jtj,@ rd}x\| j-. D ]N\}}|d}|tdt'|| 7 }|d}|tdt'|| 7 }qzW |tdt'|| 7 }| | | / }	|	0 rlt"rt#d |	1  |	2 }| jtj+@ rD|d k	rD| 3||	}	n&t4| j | j!d }| | | / }	nZ|	5 rt"rt#d | jdkrt6| |	}	n(| jdkrt7| |	}	nt89d| jt"rt#d d S )N.r   r      zDid not specify a usernamez<iIB23srZ   )server_hostnamer   T    rD   Zmysql_native_passwords   mysql_native_passwordZcaching_sha2_passwords   caching_sha2_passwordzcaching_sha2: trying fast pathzcaching_sha2: empty passwordZsha256_passwords   sha256_password   Bzutf-8zreceived auth switchzreceived extra dataz(Received extra packet for auth method %rzSucceed to auth):r\   server_versionsplitr   r   ZMULTI_RESULTSrO   rA   r   r{   idrl   r   rt   r~   r9   r:   r  rn   server_capabilitiesrp   r  rr   wrap_socketr   rQ   r7   r   r   _auth_plugin_namer
   scramble_native_passwordrP   saltr   r   Zscramble_caching_sha2ZPLUGIN_AUTH_LENENC_CLIENT_DATArB   r  ZSECURE_CONNECTIONru   r   PLUGIN_AUTHZCONNECT_ATTRSr   r   r   Zis_auth_switch_request
read_uint8read_string_process_authscramble_old_passwordZis_extra_auth_datacaching_sha2_password_authsha256_password_authr   r   )
r   Z
charset_idZ	data_initr  Zauthrespplugin_nameZconnect_attrsr]   r^   auth_packetr&   r&   r'   r     s    







 


 z"Connection._request_authenticationc             C   s  |  |}|rNy
||S  tk
rL   |dkrHtdd|t|f Y nX |dkrbt| |S |dkrvt| |S |dkrt	| j
| }n\|dkrt| j
| d }n:|d	kr| j
d }n$|dkr|} x| }|d
@ dk}|d@ dk}| }	|	dkr"| | j
d  n|rd}
y|||	}
| |
d  W nV tk
rt   tdd||f Y n. tk
r   tdd|||
|	f Y nX ntdd||f |  }|  | s|rP qW |S tdd| | | |  }|  |S )Ns   dialogi  zGAuthentication plugin '%s' not loaded: - %r missing authenticate methods   caching_sha2_passwords   sha256_passwords   mysql_native_passwords   mysql_old_passwordr  s   mysql_clear_passwordr!   r   r   s
   Password: z3no response - TypeError within plugin.prompt methodzAAuthentication plugin '%s' not loaded: - %r missing prompt methodi  zTAuthentication plugin '%s' %r didn't respond with string. Returned '%r' to prompt %rz.Authentication plugin '%s' (%r) not configuredz)Authentication plugin '%s' not configured)_get_auth_plugin_handlerZauthenticateAttributeErrorr   r   r[   r
   r-  r.  r&  rP   Zread_allr,  r)  r  prompt	TypeErrorr   r  r   )r   r/  r0  handlerr  r   flagZecholastr3  respr&   r&   r'   r+  z  sb    




zConnection._process_authc          	   C   sp   | j |}|s,t|tr,| j |d}|rhy|| }W ql tk
rd   tdd||f Y qlX nd }|S )Nr0   i  zXAuthentication plugin '%s' not loaded: - %r cannot be constructed with connection object)r   rH   rl   r   r+   r4  r   r   )r   r/  Zplugin_classr5  r&   r&   r'   r1    s    z#Connection._get_auth_plugin_handlerc             C   s
   | j d S )Nr   )server_thread_id)r   r&   r&   r'   r     s    zConnection.thread_idc             C   s   | j S )N)r{   )r   r&   r&   r'   character_set_name  s    zConnection.character_set_namec             C   s   | j S )N)r   )r   r&   r&   r'   get_host_info  s    zConnection.get_host_infoc             C   s   | j S )N)protocol_version)r   r&   r&   r'   get_proto_info  s    zConnection.get_proto_infoc       	      C   s  d}|   }| }t|||d  | _|d7 }|d|}||| d| _|d }td|||d  | _	|d7 }|||d  | _
|d7 }td	|||d
  d | _|d
7 }t||d krrtd|||d  \}}}}|d7 }|| _yt|j| _W n tk
r(   d | _Y nX || _trBtd|  |  j|d> O  _trdtd| td|d }|d7 }t||| kr|  j
||||  7  _
||7 }|d7 }| jtj@ rt||kr|d|}|dk r||d  d| _n||| d| _d S )Nr   r   r  r*   z<Ir     	   z<Hr   r!   z<BHHBzserver_status: %xr  z	salt_len:   rE   zutf-8)r   Zget_all_datar   r<  findr+   r   r9   r  r9  r'  r#  r  Zserver_languager   nameZserver_charsetKeyErrorr   r   r   maxr   r(  r%  )	r   r%   r  r  Z
server_endlangstatZcap_hZsalt_lenr&   r&   r'   r     sN       

z"Connection._get_server_informationc             C   s   | j S )N)r   )r   r&   r&   r'   get_server_info  s    zConnection.get_server_info)N)N)F)F)T)N)F)C__name__
__module____qualname____doc__r   r%  r   r   r   r   rq   r   propertyr   r   __del__r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r   r  r   r   r  r   r   r+  r1  r   r:  r;  r=  r   rG  r   Warningr   r  ZDatabaseErrorZ	DataErrorr   ZIntegrityErrorr
  ZProgrammingErrorZNotSupportedErrorr&   r&   r&   r'   rC   t   s   < 


	




	
I'

*h8?rC   c               @   st   e Z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S )r   c             C   sF   || _ d| _d| _d| _d| _d| _d| _d| _d| _d| _	d| _
dS )z.
        :type connection: Connection
        Nr   F)r  r   r  r   warning_countmessagefield_countdescriptionr   r  r  )r   r  r&   r&   r'   r     s    zMySQLResult.__init__c             C   s   | j r|   d S )N)r  r  )r   r&   r&   r'   rM  -  s    zMySQLResult.__del__c             C   sN   z@| j  }| r | | n| r4| | n
| | W d d | _ X d S )N)r  r   r   r   is_load_local_packet_read_load_local_packet_read_result_packet)r   first_packetr&   r&   r'   rh   1  s    
zMySQLResult.readc             C   sl   d| _ | j }| r0| | d| _ d| _n8| rP| | d| _ d| _n| | _| 	  d| _
dS )zw
        :raise OperationalError: If the connection to the MySQL server is lost.
        :raise InternalError:
        TFNl    )r  r  r   r   r   rS  rT  read_length_encoded_integerrQ  _get_descriptionsr   )r   rV  r&   r&   r'   r  >  s    



z!MySQLResult.init_unbuffered_queryc             C   s<   t |}|j| _|j| _|j| _|j| _|j| _|j| _d S )N)r   r   r  r   rO  rP  r  )r   rV  	ok_packetr&   r&   r'   r   W  s    zMySQLResult._read_ok_packetc             C   sx   | j jstdt|}t|j| j }y|  W n   | j    Y nX | j  }| sjt	
dd| | d S )NzF**WARN**: Received LOAD_LOCAL packet but local_infile option is false.i  zCommands Out of Sync)r  re   RuntimeErrorr   LoadLocalFilefilenamer   r   r   r   r   r   )r   rV  Zload_packetZsenderrY  r&   r&   r'   rT  `  s    

z#MySQLResult._read_load_local_packetc             C   s(   |  sdS t|}|j| _|j| _dS )NFT)is_eof_packetr   rO  r  )r   r  Zwpr&   r&   r'   _check_packet_is_eofq  s    z MySQLResult._check_packet_is_eofc             C   s   |  | _|   |   d S )N)rW  rQ  rX  _read_rowdata_packet)r   rV  r&   r&   r'   rU  }  s    
zMySQLResult._read_result_packetc             C   sP   | j s
d S | j }| |r4d| _ d | _d | _d S | |}d| _|f| _|S )NFr   )r  r  r   r^  r   _read_row_from_packetr   )r   r  rowr&   r&   r'   _read_rowdata_packet_unbuffered  s    


z+MySQLResult._read_rowdata_packet_unbufferedc             C   s0   x*| j r*| j }| |rd| _ d | _qW d S )NF)r  r  r   r^  )r   r  r&   r&   r'   r    s
    

z$MySQLResult._finish_unbuffered_queryc             C   sN   g }x0| j  }| |r"d| _ P || | qW t|| _t|| _dS )z:Read a rowdata packet for each data row in the result set.N)	r  r   r^  appendr`  r  r   tupler   )r   r   r  r&   r&   r'   r_    s    


z MySQLResult._read_rowdata_packetc          	   C   s   g }xx| j D ]n\}}y| }W n tk
r6   P Y nX |d k	rp|d k	rR||}tr`td| |d k	rp||}|| qW t|S )NzDEBUG: DATA = )r   Zread_length_coded_string
IndexErrorr+   r   r   rc  rd  )r   r  ra  r~   	converterr  r&   r&   r'   r`    s    
 
z!MySQLResult._read_row_from_packetc       
      C   s  g | _ g | _| jj}| jj}g }xt| jD ]}| jt}| j 	| |	|
  |j}|r|tjkrp|}q|tkr|jdkrd}q|}qd}nd}| jj|}|tjkrd}trtd|| | j	||f q,W | j }	|	 stdt|| _
dS )z>Read a column descriptor packet for each column in the result.?   Nr0   zDEBUG: field={}, converter={}zProtocol error, expecting EOF)fieldsr   r  r|   r~   r   rQ  r   r   rc  rR  Z	type_coder   ZJSON
TEXT_TYPESZ	charsetnrr   rH   Zthroughr   r   formatr]  AssertionErrorrd  )
r   r|   Zconn_encodingrR  r%   fieldZ
field_typer~   rf  Z
eof_packetr&   r&   r'   rX    s8    


 
zMySQLResult._get_descriptionsN)rH  rI  rJ  r   rM  rh   r  r   rT  r^  rU  rb  r  r_  r`  rX  r&   r&   r&   r'   r     s   	
r   c               @   s   e Zd Zdd Zdd ZdS )r[  c             C   s   || _ || _d S )N)r\  r  )r   r\  r  r&   r&   r'   r     s    zLoadLocalFile.__init__c          	   C   s   | j jstd| j }zvyHt| jd2}t|jd}x||}|sHP |	| q8W W dQ R X W n( t
k
r   tdd| jY nX W d|	d X dS )z3Send data packets from the local file to the serverz(0, '')r   i @  Ni  zCan't find file '{0}'rZ   )r  r   r   r  r   r\  r  r   rh   r  r   r   rj  )r   conn	open_filer  chunkr&   r&   r'   r     s    

zLoadLocalFile.send_dataN)rH  rI  rJ  r   r   r&   r&   r&   r'   r[    s   r[  )O
__future__r   _compatr   r   r   r   r   r	   r   r3   ri   rS   r9   ra   r   r   rD   r
   r{   r   r   	constantsr   r   r   r   r   r   Zcursorsr   Z
optionfiler   protocolr   r   r   r   r   r   utilr   r   r   r   rn   ro   ImportErrorgetpassgetuserrs   rC  r   rb   Z_py_versionranger-   r/   Z	_socketior2   r7   ZBITZBLOBZ	LONG_BLOBZMEDIUM_BLOBSTRINGZ	TINY_BLOBZ
VAR_STRINGZVARCHARZGEOMETRYri  r}   r  r<   rB   objectrC   r   r[  r&   r&   r&   r'   <module>   s     




       . N