U e(m@sdZddlZddlZddlmZddlmZmZmZm Z m Z ddl m Z m Z ddlmZddlmZmZdd l mZGd d d eZdS) z,Implements the MySQL Client/Server protocol N)Decimal) FieldFlag ServerCmd FieldType ClientFlagMAX_MYSQL_TABLE_COLUMNS)errorsutils)get_auth_plugin)PY2 struct_unpack) get_exceptionc@seZdZdZddZddZdBd d ZdCd dZdDddZdEddZ ddZ ddZ ddZ dFddZ ddZdGdd ZdHd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-ZdId.d/Zd0d1Zd2d3Zd4d5Zd6d7Zd8d9ZdJdd?Zd@dAZdS)K MySQLProtocolzRImplements MySQL client/server protocol Create and parses MySQL packets. cCs |tj@r|r|ddSdS)z.Prepare database string for handshake responseutf8)rZCONNECT_WITH_DBencode)self client_flagsdatabaserm/opt/hc_python/lib64/python3.8/site-packages/../../../lib/python3.8/site-packages/mysql/connector/protocol.py_connect_with_db-szMySQLProtocol._connect_with_dbc Cs|sdSz"t||||||d}|} Wn<ttjfk rf} ztdt| W5d} ~ XYnX|tj @rt | } t d| | } n| d} | S)z#Prepare the authentication responser)usernamepasswordr ssl_enabledzFailed authentication: {0}Nz%sz2MySQLProtocol.parse_statistics..:rdrJz{0} ({1}:{2}).)splitr rrMintrr ) rr7Z with_headererrmsgrPpairspairZlblvalrrrparse_statisticss*   zMySQLProtocol.parse_statisticsrc CsVg}d}d}d}|dk}|s$||kr(qN|} | dr| ddg} |} | drv| | dd|} qP| | ddttd| }nj| ddkr| ddkr|| }d}nB|r| ddkr| dd kr|| }d}nd}t| dd}|dkr(|dk r(||n|dkrD|dkrDt | |d 7}q||fS) zRead MySQL text result Reads all or given number of rows from the socket. Returns a tuple with 2 elements: a list with all rows and the EOF packet. Nr)rBrBsrSr.rbrurcr) recvrOappendr Zread_lc_string_list bytearrayjoinrhrYr) rsockversionr[rowseofZrowdataiZeof57r7Zdatasrrrread_text_result5s<        zMySQLProtocol.read_text_resultcCs|dtjkrd}d}nL|dtjkr0d}d}n4|dtjtjfkrNd}d}n|dtjkrdd}d}|d tj@rz|}||d t ||d |d fS) z%Parse an integer from a binary packetrbhrdr~rSqrHruNr) rTINYSHORTINT24LONGLONGLONGrZUNSIGNEDupperr )rr7fieldformat_lengthrrr_parse_binary_integer_sz#MySQLProtocol._parse_binary_integercCsB|dtjkrd}d}nd}d}||dt||d|dfS)z)Parse a float/double from a binary packetrrHdrSfNr)rDOUBLEr )rr7rrrrrr_parse_binary_floatss z!MySQLProtocol._parse_binary_floatc Cs|d}d}|dkr@tjtd|ddd|d|dd}nl|dkrd}|d krntd |d |dd}tjtd|ddd|d|d|d |d |d|d}||dd|fS)z&Parse a timestamp from a binary packetrNrSHrre)yearmonthdayru IrHrB)rrrhourminutesecond microsecond)datetimedater )rr7rrvaluemcsrrr_parse_binary_timestamp~s, z%MySQLProtocol._parse_binary_timestampcCs|d}|d|d}d}|dkr:td|ddd}td|ddd}|ddkrd|d9}tj||d||d |dd }||dd|fS) z'Parse a time value from a binary packetrrrHrNrBrIrur)daysseconds microsecondsminuteshours)r r timedelta)rr7rrr>rrtmprrr_parse_binary_times z MySQLProtocol._parse_binary_timec Csnt|ddd}dd|d|D}||d}g}t|D] \}}|t|ddd|dd>@r~|dqBqB|dtjtjtjtjtj fkr| ||\}}||qB|dtj tj fkr| ||\}}||qB|dtjtjtjfkr |||\}}||qB|dtjkrL|||\}}||qBt|\}}||qBt|S) z(Parse values from a binary result packetrurdrHcSsg|] }t|qSr)ro)rjr~rrrrlsz6MySQLProtocol._parse_binary_values..rNr)r# enumeraterorwrrrrrrrrFLOATrDATETIMEDATE TIMESTAMPrTIMErr rWtuple) rfieldsr7Znull_bitmap_length null_bitmapvaluesposrrrrr_parse_binary_valuess: $      z"MySQLProtocol._parse_binary_valuesc Csg}d}d}d}|dk rq||kr$q|}|ddkrH||}d}n$|ddkrld}|||dd}|dkr|dk r||n|dkr|dkrt||d7}q||fS)zwRead MySQL binary protocol result Reads all or given number of binary resultset rows from the socket. NrrSrbrBr)rvrhrrwr) rrzcolumnsr[r|r}rr~r7rrrread_binary_results*     z MySQLProtocol.read_binary_resultcCs|ddkstdi}zht|ddd\}|d<t|d\}|d<t|d\}|d <|d d}t|d\}|d <Wntk rtdYnX|S) z'Parse a MySQL Binary Protocol OK packetrSrzFailed parsing Binary OK packetrBN statement_idrdZ num_columnsZ num_paramsrrU)r rr Zread_intrX)rr7Zok_pktrrrparse_binary_prepare_oks   z%MySQLProtocol.parse_binary_prepare_okcCsd}d}|dkrX|dkr$d}tj}q|dkr8d}tj}q|dkrLd}tj}qd }tj}nJd }|d krpd }tj}n2|d krd}tj}n|dkrd}tj}n tj}d}t||||fS)z0Prepare an integer for the MySQL binary protocolNriririr~rBirlrQ)rrrrrr$r%)rrr_r`rrrr_prepare_binary_integers6z%MySQLProtocol._prepare_binary_integercCst|tjrtj}nt|tjr(tj}ntdt|j t |j t |j }t|tjr|t |j t |jt |j}|jdkr|t|j7}t t||}||fS)aPrepare a timestamp object for the MySQL binary protocol This method prepares a timestamp of type datetime.datetime or datetime.date for sending over the MySQL binary protocol. A tuple is returned with the prepared value and field type as elements. Raises ValueError when the argument value is of invalid type. Returns a tuple. z2Argument must a datetime.datetime or datetime.dater) isinstancerrrrrrXr int2storerr:rrrrrrr9r#)rrr_packedrrr_prepare_binary_timestamps,         z'MySQLProtocol._prepare_binary_timestampc Cst|tjtjfstdtj}d}d}d}t|tjr|jdkrFd}t|j d\}}t|d\}} |t t |jt |t |t | 7}|j}n8|t dt |jt |jt |j7}|j}|r|t |7}t ||}t t||}||fS)aPrepare a time object for the MySQL binary protocol This method prepares a time object of type datetime.timedelta or datetime.time for sending over the MySQL binary protocol. A tuple is returned with the prepared value and field type as elements. Raises ValueError when the argument value is of invalid type. Returns a tuple. z3Argument must a datetime.timedelta or datetime.timerNr.ri<)rrrtimerXrrrdivmodrr r9absr:rrrrrr#) rrr_negativerrr remainderZminsZsecsrrr_prepare_binary_time8sD       z"MySQLProtocol._prepare_binary_timecCst|t||}|S)zMPrepare long data for prepared statements Returns a string. )r r9r)rZ statementparamr>r7rrr_prepare_stmt_send_long_dataesz*MySQLProtocol._prepare_stmt_send_long_datarrc Cs*d}dgt|dd}g} g} d} |dkr2i}|r|rt|t|krXtdt|D]^\} } || }d}|dkr|| dd| d>O<| ttjt|q`n| |kr|| drtj }ntj }nt |t r | |\} }}| | nt |trdtr8| tt||n"||}| tt||tj}nBt |tr| tt||tj }ntrt |tr||}| tt||tj}nt |tr| ttt||t||tj}nt |tr4| td|tj}nrt |tjtjfrb||\} }| | nDt |tjtjfr| |\} }| | nt!d j"|j#j$d | t|t|q`t%|t|t%|d&d d |Dtd}| D]}||7}q| D]}||7}q|S) z6Make a MySQL packet with the Statement Execute commandrrrurHr.NzTFailed executing prepared statement: data values does not match number of parametersrz:MySQL binary protocol can not handle '{classname}' objects) classnamecSsg|]}td|qS)r)r$r%)rjbitrrrrlsz3MySQLProtocol.make_stmt_execute..)'r#r rrrwr r:rZNULLZBLOBSTRINGrrorr!r Zlc_intrZVARCHARbytesunicoderZDECIMALfloatr$r%rrrrrrrrr __class____name__r9ry)rrr> parametersr`Zlong_data_usedr4Ziteration_countrrtypesrrr]rr_r7a_typeZa_valuerrrmake_stmt_executeps                  zMySQLProtocol.make_stmt_executecCsZ|ddkstdtj|dddd\}}|rL|dd krL|dd}|d |fS) z&Parse a MySQL AuthSwitchRequest-packetrSrbz'Failed parsing AuthSwitchRequest packetrBNrrDrIrr)r rr rLrM)rr7Z plugin_namerrrparse_auth_switch_requests  z'MySQLProtocol.parse_auth_switch_requestcCs"|ddkstd|ddS)z!Parse a MySQL AuthMoreData-packetrSrz"Failed parsing AuthMoreData packetrBN)r r)rr7rrrparse_auth_more_datas  z"MySQLProtocol.parse_auth_more_data)NNNr+rr,FN)r+rr,)N)NNNr+rFN)rJ)T)r)r)rrrNr)r __module__ __qualname____doc__rr*r8r;r?r@rRrYr\rarhrtrrrrrrrrrrrrrrrrrrrr's\ %   $,   *  !"-  Z r)rr$rdecimalr constantsrrrrrr r Zauthenticationr Zcatch23r r robjectrrrrrs