U e'Q @sdZddlmZddlmZddlmZmZmZm Z m Z ddl m Z m Z ddlmZdZzDdd lZdd lmZmZmZmZmZmZmZmZmZdd lmZWn4ek rZzed e eW5d Z[XYnXd ZGddde Z!d S)z'Connection class using the C Extension )errors) INT_TYPES) CharacterSet FieldFlag ServerFlag ShutdownType ClientFlag)MySQLConnectionAbstractMySQLCursorAbstract) MySQLProtocolFN) CMySQLCursorCMySQLCursorRawCMySQLCursorBufferedCMySQLCursorBufferedRawCMySQLCursorPreparedCMySQLCursorDictCMySQLCursorBufferedDictCMySQLCursorNamedTupleCMySQLCursorBufferedNamedTuple)MySQLInterfaceErrorz6MySQL Connector/Python C Extension not available ({0})TcseZdZdZfddZddZeddZdYd d Zed d Z e j d d Z eddZ e j ddZ eddZ ddZ ddZeZddZdZddZddZd d!Zed"d#Zd[d%d&Zd\d'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4Zd5d6Zd]d7d8ZeZd^d9d:Z ed;d<Z!ed=d>Z"ed?d@Z#edAdBZ$edCdDZ%dEdFZ&dGdHZ'd_dKdLZ(dMdNZ)dOdPZ*d`dQdRZ+dSdTZ,dUdVZ-dWdXZ.Z/S)aCMySQLConnectionz5Class initiating a MySQL Connection using Connector/Cc sRts tdd|_d|_g|_d|_tt|jf|t |dkrN|j f|dS)ZInitializationz0MySQL Connector/Python C Extension not availableNr ) HAVE_CMYSQL RuntimeError_cmysql_connection_timeout_columns convertersuperr__init__lenconnect)selfkwargs __class__t/opt/hc_python/lib64/python3.8/site-packages/../../../lib/python3.8/site-packages/mysql/connector/connection_cext.pyr =s zCMySQLConnection.__init__c CsF|j|j|jdddd|jd|_||jd|_dS)zr;r<r=r0)r#rFZ cnx_kwargsr?r'r'r(_open_connections@     z!CMySQLConnection._open_connectionc CsX|jrTz|jWn8tk rL}ztj|j|j|jdW5d}~XYnXd|_dS)z Disconnect from the MySQL serverr:N)rcloserrr>r;r<r=)r#r?r'r'r(r]s zCMySQLConnection.closecCs|jr|jSdS)z;Reports whether the connection to MySQL Server is availableF)rpingr/r'r'r( is_connecteds zCMySQLConnection.is_connectedFrr cCsRd}z|j}Wntk r&Yn X|r0dS|rD|j||dn t|dS)aCheck availability of the MySQL server When reconnect is set to True, one or more attempts are made to try to reconnect to the MySQL server using the reconnect()-method. delay is the number of seconds to wait between each retry. When the connection is not available, an InterfaceError is raised. Use the is_connected()-method if you just want to check the connection without raising an error. Raises InterfaceError on errors. z$Connection to MySQL is not availableN)attemptsdelay)rr^AttributeError reconnectrInterfaceError)r#rcr`raerrmsg connectedr'r'r(r^szCMySQLConnection.pingcCs|j|dS)zDSets the default character set name for current connection. N)rZset_character_set)r#r+r'r'r(set_character_set_namesz'CMySQLConnection.set_character_set_namecCsN|j|d}|jjr@|j}|jr@|jtd|j|S)z%Send a query which only returns 1 rowr'z'Query should not return more than 1 row)rqueryhave_result_set fetch_row free_resultrrd)r#rh first_rowr'r'r(r8s     zCMySQLConnection.info_querycCs&z |jWStk r YnXdS)zMySQL connection IDN)rr.rr/r'r'r( connection_ids  zCMySQLConnection.connection_idNc Cs|jr |jstdg}|dk r2|dkr2tdd}z|j}|r|jrt|}t|D]$\}}|j |j |||||<q\t |}| ||d7}|r||krq|j}qBWn@t k r} z"|tj| j| j| jdW5d} ~ XYnX|S)z8Get all or a subset of rows returned by the MySQL serverzNo result set availableNr z$count should be 1 or higher, or Nonerr:)r unread_resultr InternalErrorrbrjrlist enumerateZ to_pythonrtupleappendrrkr>r;r<r=) r#countbinarycolumnsrowscounterrowi_r?r'r'r(get_rowss6       zCMySQLConnection.get_rowscCs2z|jd||ddWStk r,YdSXdS)z.Get the next rows returned by the MySQL serverr)rtrurvr N)r| IndexError)r#rurvr'r'r(get_rowszCMySQLConnection.get_rowcCs|jr|j|jSdS)zReads the next resultN)rconsume_result next_resultr/r'r'r(r$s  zCMySQLConnection.next_resultcCs|jr|jdS)zFrees the resultN)rrkr/r'r'r(rk+szCMySQLConnection.free_resultcCs|jr|jdS)zCommit current transactionN)rcommitr/r'r'r(r0szCMySQLConnection.commitcCs|jr|j|jdS)zRollback current transactionN)rrrollbackr/r'r'r(r5s zCMySQLConnection.rollbackc CsNz|j|Wn8tk rH}ztj|j|j|jdW5d}~XYnXdS)zChange the current databaser:N)rrArrr>r;r<r=)r#r@r?r'r'r( cmd_init_db;s  zCMySQLConnection.cmd_init_dbc Cs|jjstd|j}g|_|D]D}|j|dt|dddddt|dtj @t|dfq&|j |j d|jdS)z Fetch EOF and column information No result setN )Z status_flag warning_count)eofrv) rrirrdZ fetch_fieldsrrsrUrZNOT_NULLr1st_warning_count)r#fieldscolr'r'r(fetch_eof_columnsCs(     z"CMySQLConnection.fetch_eof_columnscCs4|jr0|j|j|j|j|jdSdS)z Fetch EOF and status information)rZ field_count insert_id affected_rowsr,N)rrZst_field_countrrr1r/r'r'r(fetch_eof_status^sz!CMySQLConnection.fetch_eof_statusc Cs|z,t|ts|d}|jj||||dWn~tk rl}ztj|j |j |j dW5d}~XYnHt k r|j r|j }n|jdt|j}tjd|dfdYnXg|_|jjs|S|S) z Send a query to the MySQL serverzutf-8)rErD raw_as_string)r;r=N:izConnection not available.)r<values)handle_unread_result isinstancebytesencoderrhrrr>r<r;r=rbrYrVstrrXOperationalErrorrrirr)r#rhrErDrr?addrr'r'r( cmd_queryks2     zCMySQLConnection.cmd_queryc s||std|dk r>t|ts6td||S|pF|j}|pP|j}d|dkrfdO|dkrvdO|dkrdO|dkrd O|dkrd Ot t t t t ttttd }z||WStk r d td dfddtdDYnXdS)aInstantiates and returns a cursor using C Extension By default, CMySQLCursor is returned. Depending on the options while connecting, a buffered and/or raw cursor is instantiated instead. Also depending upon the cursor options, rows can be returned as dictionary or named tuple. Dictionary and namedtuple based cursors are available with buffered output but not raw. It is possible to also give a custom cursor through the cursor_class parameter, but it needs to be a subclass of mysql.connector.cursor_cext.CMySQLCursor. Raises ProgrammingError when cursor_class is not a subclass of CursorBase. Raises ValueError when cursor is not available. Returns instance of CMySQLCursor or subclass. :param buffered: Return a buffering cursor :param raw: Return a raw cursor :param prepared: Return a cursor which uses prepared statements :param cursor_class: Use a custom cursor class :param dictionary: Rows are returned as dictionary :param named_tuple: Rows are returned as named tuple :return: Subclass of CMySQLCursor :rtype: CMySQLCursor or subclass zMySQL Connection not available.Nz=Cursor class needs be to subclass of cursor_cext.CMySQLCursorr Trrrr) r rrrrrr)rDrE dictionary named_tuplepreparedz*Cursor not available with given criteria: z, cs$g|]}d|>@dkr|qS)rr r').0rzargsZ cursor_typer'r( sz+CMySQLConnection.cursor..r)rr_rr issubclassr ZProgrammingErrorrSrTr rrrrrrrrKeyError ValueErrorjoinrange)r#rDrErZ cursor_classrrtypesr'rr(cursorsN     zCMySQLConnection.cursorcCs|jjstd|jS)z,Returns number of rows of current result setr)rrirrdnum_rowsr/r'r'r(rs zCMySQLConnection.num_rowscCs|js dS|jS)zReturns number of warningsr )rrr/r'r'r(rszCMySQLConnection.warning_countcCs|js dS|jjS)z"Check if a result set is availableF)rrir/r'r'r(result_set_availablesz%CMySQLConnection.result_set_availablecCs|jS)z)Check if there are unread results or rows)rr/r'r'r(rnszCMySQLConnection.unread_resultcCs |jS)zCheck if there are more results)r more_resultsr/r'r'r(rszCMySQLConnection.more_resultscCs^t|ttfr|jj|}n>t|trRi}|D]\}}|j|d||<q2ntd|S)zPrepare parameters for statements This method is use by cursors to prepared parameters found in the list (or tuple) params. Returns dict. r zCould not process parameters)rrprrrZconvert_to_mysqldictitemsr)r#paramsresultrNr6r'r'r(prepare_for_mysqls z"CMySQLConnection.prepare_for_mysqlcCs|jdS)zlConsume the current result This method consume the result by reading (consuming) all rows. N)rrr/r'r'r(consume_resultssz CMySQLConnection.consume_results!c Cs`z|j|||Wn8tk rL}ztj|j|j|jdW5d}~XYnX||_| dS)z!Change the current logged in userr:N) rZ change_userrrr>r;r<r=rPZ_post_connection)r#usernamerIr@r+r?r'r'r(cmd_change_users z CMySQLConnection.cmd_change_userc CsRz|j|Wn8tk rH}ztj|j|j|jdW5d}~XYnX|S)z,Send the Refresh command to the MySQL serverr:N) rrefreshrrr>r;r<r=r)r#optionsr?r'r'r( cmd_refreshs zCMySQLConnection.cmd_refreshcCs |dS)z,Close the current connection with the serverN)r]r/r'r'r(cmd_quit$szCMySQLConnection.cmd_quitc Cs|jstd|r.t|s(td|}ntj}z|j|Wn8tk r|}ztj |j |j |j dW5d}~XYnX| dS)zShut down the MySQL ServerzMySQL Connection not availablezInvalid shutdown typer:N)rrrrrOrdZSHUTDOWN_DEFAULTshutdownrr>r;r<r=r])r#Z shutdown_typelevelr?r'r'r( cmd_shutdown(s    zCMySQLConnection.cmd_shutdownc Csh|z|j}tj|ddWSttjfk rb}ztj|j |j |j dW5d}~XYnXdS)z'Return statistics from the MySQL serverF)Z with_headerr:N) rrstatr Zparse_statisticsrrrdr>r;r<r=)r#rr?r'r'r(cmd_statistics<s  zCMySQLConnection.cmd_statisticscCs&t|tstd|d|dS)zKill a MySQL processzMySQL PID must be intzKILL {0}N)rrrr8format)r#Z mysql_pidr'r'r(cmd_process_killGs z!CMySQLConnection.cmd_process_killcCs$|jr|n|jr tddS)z'Check whether there is an unread resultzUnread result foundN)Zcan_consume_resultsrrnrror/r'r'r(rMs z%CMySQLConnection.handle_unread_result)T)Frr )NFN)FN)FFF)NNNNNN)rrrr)N)0__name__ __module__ __qualname____doc__r r0propertyr1r4r9setterr@rBr\r]Z disconnectr_r^rgr8rmr|r~rrkrrrrrrZ_execute_queryrrrrrnrrrrrrrrrr __classcell__r'r'r%r(r9sz        %      J         r)"rrrZcatch23r constantsrrrrrZ abstractsr r r)r rrQZ cursor_cextr rrrrrrrrr ImportErrorr?rrrr'r'r'r(s$   ,