U ê üe¥@ã@s&dZzddlZdZWndZYnXddlZddlZddlmZddlmZddl m Z m Z m Z dd l mZdd lmZmZdd lmZmZmZdd lmZmZd ZdZGdd„deƒZdd„ZGdd„deƒZGdd„deƒZGdd„deƒZ Gdd„deƒZ!Gdd„de!ƒZ"Gdd„de!ƒZ#dS)z4Implementation of communication for MySQL X servers.éNTF©Úwrapsé)ÚMySQL41AuthPlugin)ÚInterfaceErrorÚOperationalErrorÚProgrammingError©ÚSchema)ÚProtocolÚMessageReaderWriter)ÚResultÚ RowResultÚ DocResult)Ú SqlStatementÚ AddStatementzDROP DATABASE IF EXISTS `{0}`z#CREATE DATABASE IF NOT EXISTS `{0}`c@s@eZdZdd„Zdd„Zdd„Zdd„Zd d „Zifd d „Zd S)Ú SocketStreamcCsd|_d|_dS)NF)Ú_socketÚ_is_ssl©Úself©rúf/opt/hc_python/lib64/python3.8/site-packages/../../../lib/python3.8/site-packages/mysqlx/connection.pyÚ__init__2szSocketStream.__init__cCs6t|tƒrtjntj}t |tj¡|_|j |¡dS©N)Ú isinstanceÚtupleÚsocketÚAF_INETÚAF_UNIXÚ SOCK_STREAMrÚconnect)rÚparamsZs_typerrrr!6szSocketStream.connectcCs\|jdkrtdƒ‚g}|dkrR|j |¡}|dkr:tdƒ‚| |¡|t|ƒ8}qd |¡S)NúMySQLx Connection not availablerózUnexpected connection close)rrÚrecvÚ RuntimeErrorÚappendÚlenÚjoin)rÚcountÚbufÚdatarrrÚread;s   zSocketStream.readcCs"|jdkrtdƒ‚|j |¡dS)Nr#)rrÚsendall©rr,rrrr.Gs zSocketStream.sendallcCs|js dS|j ¡d|_dSr)rÚcloserrrrr0Ls zSocketStream.closec CsLts| ¡tdƒ‚t tj¡}| ¡d|krvz| |d¡tj|_ Wn*t tj fk rt| ¡t dƒ‚YnXd|krÄz| |d¡tj |_Wn*t tj fk rÂ| ¡t dƒ‚YnXd|krz| |d| dd¡¡Wn,t tj fk r| ¡t dƒ‚YnXnd|kr4| ¡t d ƒ‚| |j¡|_d |_dS) Nz'Python installation has no SSL support.zssl-cazInvalid CA certificate.zssl-crlz Invalid CRL.zssl-certzssl-keyzInvalid Client Certificate/Key.z Client Certificate not provided.T)Ú SSL_AVAILABLEr0r&ÚsslÚ SSLContextÚPROTOCOL_SSLv23Úload_default_certsÚload_verify_locationsÚ CERT_REQUIREDÚ verify_modeÚIOErrorÚSSLErrorrÚVERIFY_CRL_CHECK_CHAINÚ verify_flagsÚload_cert_chainÚgetÚ wrap_socketrr)rZssl_optsÚcontextrrrÚset_sslSs@      ÿ zSocketStream.set_sslN) Ú__name__Ú __module__Ú __qualname__rr!r-r.r0rArrrrr1s  rcstˆƒ‡fdd„ƒ}|S)Nc sBzˆ|f|ž|ŽWStjtfk r<| ¡tdƒ‚YnXdS)NzCannot connect to host.)rÚerrorr&Ú disconnectr)rÚargsÚkwargs©ÚfuncrrÚwrapperxs z(catch_network_exception..wrapperr)rJrKrrIrÚcatch_network_exceptionwsrLc@sÚeZdZdd„Zdd„Zdd„Zdd„Zd d „Zd d „Ze d d„ƒZ e dd„ƒZ e dd„ƒZ e dd„ƒZ e dd„ƒZe d+dd„ƒZe dd„ƒZe dd„ƒZe dd„ƒZe d d!„ƒZe d"d#„ƒZd$d%„Zd&d'„Zd(d)„Zd*S),Ú ConnectioncCsH| d¡|_| d¡|_| d¡|_d|_||_tƒ|_d|_d|_ dS)NÚuserÚpasswordÚschema) r>Ú_userÚ _passwordZ_schemaÚ_active_resultÚsettingsrÚstreamÚ reader_writerÚprotocol©rrTrrrrƒs   zConnection.__init__cCs|jdk r|j ¡d|_dSr)rSÚ fetch_allrrrrÚfetch_active_results  zConnection.fetch_active_resultcCs:d|jkr"|jd|j dd¡fSd|jkr6|jdSdS)NÚhostÚporté$r)Ú localhostr])rTr>rrrrÚ_connection_params’s    zConnection._connection_paramscCs<|j | ¡¡t|jƒ|_t|jƒ|_| ¡| ¡dSr) rUr!r_r rVr rWÚ_handle_capabilitiesÚ _authenticaterrrrr!™s   zConnection.connectcCs–|j ¡j}|r$|dj ¡dksJndsJ|j dd¡rF| ¡tdƒ‚dSt j dkrv|j dd¡rr| ¡t dƒ‚dS|jj dd |j  |j¡dS) NrÚtlsFz ssl-enablezSSL not enabled at server.)ééé z=The support for SSL is not available for this Python version.T)rb)rWZget_capabilitesZ capabilitiesÚnameÚlowerrTr>r0rÚsysÚ version_infor&Zset_capabilitiesrUrAr/rrrr` s  zConnection._handle_capabilitiescCsHt|j|jƒ}|j | ¡¡|j ¡}|j | |¡¡|j  ¡dSr) rrQrRrWZsend_auth_startZ auth_nameZread_auth_continueZsend_auth_continueZbuild_authentication_responseZ read_auth_ok)rÚpluginÚ extra_datarrrra²s ÿzConnection._authenticatecGs|j d||¡dS)NÚsql)rWÚsend_execute_statement)rrlrGrrrÚsend_sqlºszConnection.send_sqlcCs*|j |¡d}t|tƒr |j}t||ƒSr)rWÚ send_insertrrZ_idsr )rÚ statementZidsrrrro¾s   zConnection.send_insertcCs"|j |¡|jrt|ƒSt|ƒSr)rWZ send_findZ _doc_basedrr©rrprrrÚfindÆs zConnection.findcCs|j |¡t|ƒSr)rWZ send_deleter rqrrrÚdeleteËs zConnection.deletecCs|j |¡t|ƒSr)rWZ send_updater rqrrrÚupdateÐs zConnection.updateTcGs|j |||¡t|ƒSr)rWrmr )rÚ namespaceÚcmdZ raise_on_failrGrrrÚexecute_nonqueryÕszConnection.execute_nonquerycGs>|j d||¡t|ƒ}| ¡|jdkr2tdƒ‚|ddS)Nrlrz No data found)rWrmrrYr*r)rrlrGÚresultrrrÚexecute_sql_scalarÚs  zConnection.execute_sql_scalarcGs|j d||¡t|ƒS)NZxplugin)rWrmr)rrvrGrrrÚget_row_resultãszConnection.get_row_resultcCs |j |¡Sr)rWÚread_row©rrxrrrr{èszConnection.read_rowcCs|j |¡dSr)rWÚ close_resultr|rrrr}ìszConnection.close_resultcCs |j |¡Sr)rWÚget_column_metadatar|rrrr~ðszConnection.get_column_metadatacCs |jjdk Sr)rUrrrrrÚis_openôszConnection.is_opencCs| ¡s dS|j ¡dSr)rrUr0rrrrrF÷szConnection.disconnectcCsB| ¡s dS|jdk r |j ¡|j ¡|j ¡|j ¡dSr)rrSrYrWZ send_closeÚread_okrUr0rrrrr0üs    zConnection.closeN)T)rBrCrDrrZr_r!r`rarLrnrorrrsrtrwryrzr{r}r~rrFr0rrrrrM‚s>            rMcs\eZdZ‡fdd„Zdd„Z‡fdd„Z‡fdd„Zd d „Z‡fd d „Z‡fd d„Z ‡Z S)Ú XConnectioncs€tt|ƒ |¡g|_| dg¡|_d|krT|drT|j | d¡| dd¡dœ¡d|_d|_|  ¡|jj dd„dd dS) NZroutersr[r\)r[r\éÿÿÿÿTcSs|dS)NÚpriorityr)ÚxrrrÚr$z&XConnection.__init__..)ÚkeyÚreverse) ÚsuperrrÚdependent_connectionsÚpopÚ_routersr'Ú _cur_routerÚ _can_failoverÚ_ensure_prioritiesÚsortrX©Ú __class__rrrs þzXConnection.__init__cCs€d}d}|jD]D}| dd¡}|dkr8|d7}||d<n|dkrJtddƒ‚|d8}qd|krnt|jƒkr|nn tddƒ‚dS) Nrédrƒrz(The priorities must be between 0 and 100i§z\You must either assign no priority to any of the routers or give a priority for every routeri )r‹r>rr()rZpriority_countrƒÚrouterÚprirrrrŽs    ÿ ÿzXConnection._ensure_prioritiescs¸|jsd|_tt|ƒ ¡S|jr*|jdkrJd|_d|_|jD] }d|d<q<|jd7_|j|jd}|j|jd}|jdkr–d|j|jdd<|jt|jƒdkr°d|_||fS) NFr‚TÚ availablerr[r\r)r‹rrˆrr_rŒr()rr“r[r\rrrr_)s    zXConnection._connection_paramsc spd}|jrFztt|ƒ ¡WStjk rB}z|}W5d}~XYqXqt|jƒdkrbtd  |¡ƒ‚tddƒ‚dS)NrúCannot connect to host: {0}z(Failed to connect to any of the routers.i¡) rrˆrr!rrEr(r‹rÚformat)rrEÚerrrrrr!@szXConnection.connectcCs|j |¡dSr)r‰r')rÚ connectionrrrÚbind_connectionMszXConnection.bind_connectioncs(|jr|j ¡ ¡qtt|ƒ ¡dSr)r‰rŠr0rˆrrrrrr0PszXConnection.closecs(|jr|j ¡ ¡qtt|ƒ ¡dSr)r‰rŠrFrˆrrrrrrFUszXConnection.disconnect) rBrCrDrrŽr_r!ršr0rFÚ __classcell__rrrrrs    rcs(eZdZ‡fdd„Z‡fdd„Z‡ZS)ÚNodeConnectioncstt|ƒ |¡dSr)rˆrœrrXrrrr\szNodeConnection.__init__c sJztt|ƒ ¡Wn2tjk rD}ztd |¡ƒ‚W5d}~XYnXdS)Nr–)rˆrœr!rrErr—)rr˜rrrr!_szNodeConnection.connect)rBrCrDrr!r›rrrrrœ[s rœc@s`eZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dd„Z dd„Z dd„Z dS)Ú BaseSessionaÜBase functionality for Session classes through the X Protocol. This class encloses the core functionality to be made available on both the XSession and NodeSession classes, such functionality includes: - Accessing available schemas. - Schema management operations. - Enabling/disabling warning generation. - Retrieval of connection information. Args: settings (dict): Connection data used to connect to the database. cCs ||_dSr)Ú _settingsrXrrrrtszBaseSession.__init__cCs|jjjdk Sr)Ú _connectionrUrrrrrrwszBaseSession.is_opencCs t||ƒS)zöRetrieves a Schema object from the current session by it's name. Args: name (string): The name of the Schema object to be retrieved. Returns: mysqlx.Schema: The Schema object with the given name. r ©rrfrrrÚ get_schemazs zBaseSession.get_schemacCs,|jj d¡r t||jjdƒStdƒ‚dS)aKRetrieves a Schema object from the current session by the schema name configured in the connection settings. Returns: mysqlx.Schema: The Schema object with the given name at connect time. Raises: ProgrammingError: If default schema not provided. rPzDefault schema not providedN)rŸrTr>r rrrrrÚget_default_schema…s zBaseSession.get_default_schemacCs|j dt |¡d¡dS)z‹Drops the schema with the specified name. Args: name (string): The name of the Schema object to be retrieved. rlTN)rŸrwÚ_DROP_DATABASE_QUERYr—r rrrÚ drop_schema”s ÿzBaseSession.drop_schemacCs |j dt |¡d¡t||ƒS)z¬Creates a schema on the database and returns the corresponding object. Args: name (string): A string value indicating the schema name. rlT)rŸrwÚ_CREATE_DATABASE_QUERYr—r r rrrÚ create_schemas ÿzBaseSession.create_schemacCs|j ddd¡dS)z4Starts a transaction context on the server. rlzSTART TRANSACTIONTN©rŸrwrrrrÚstart_transaction¨szBaseSession.start_transactioncCs|j ddd¡dS)zXCommits all the operations executed after a call to startTransaction(). rlZCOMMITTNr§rrrrÚcommit­szBaseSession.commitcCs|j ddd¡dS)zYDiscards all the operations executed after a call to startTransaction(). rlZROLLBACKTNr§rrrrÚrollback³szBaseSession.rollbackcCs|j ¡dSr)rŸr0rrrrr0¹szBaseSession.closeN)rBrCrDÚ__doc__rrr¡r¢r¤r¦r¨r©rªr0rrrrrfs    rcs(eZdZdZ‡fdd„Zdd„Z‡ZS)ÚXSessionaWEnables interaction with a X Protocol enabled MySQL Product. The functionality includes: - Accessing available schemas. - Schema management operations. - Enabling/disabling warning generation. - Retrieval of connection information. Args: settings (dict): Connection data used to connect to the database. cs*tt|ƒ |¡t|jƒ|_|j ¡dSr)rˆr¬rrržrŸr!rXrrrrÊs zXSession.__init__cCs,| ¡stdƒ‚t|jƒ}|j |j¡|S)Nz$XSession is not connected to a farm.)rrÚ NodeSessionržrŸrš)rZnsessrrrÚbind_to_default_shardÏs  zXSession.bind_to_default_shard)rBrCrDr«rr®r›rrrrr¬½s r¬cs(eZdZdZ‡fdd„Zdd„Z‡ZS)r­atEnables interaction with a X Protocol enabled MySQL Server. The functionality includes: - Accessing available schemas. - Schema management operations. - Enabling/disabling warning generation. - Retrieval of connection information. - Includes SQL Execution. Args: settings (dict): Connection data used to connect to the database. cs*tt|ƒ |¡t|jƒ|_|j ¡dSr)rˆr­rrœržrŸr!rXrrrræs zNodeSession.__init__cCs t|j|ƒS)z}Creates a :class:`mysqlx.SqlStatement` object to allow running the SQL statement on the target MySQL Server. )rrŸ)rrlrrrrlëszNodeSession.sql)rBrCrDr«rrlr›rrrrr­Øs r­)$r«r2r1rhrÚ functoolsrZauthenticationrÚerrorsrrrZcrudr rWr r rxr rrrprrr£r¥ÚobjectrrLrMrrœrr¬r­rrrrÚs2    F U W