\c @sIddlmZddlmZmZmZmZmZmZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZddlmZddlmZmZddlmZmZmZmZmZddlmZdd lmZdd lm Z dd l!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'dd l(m)Z)m*Z*dd lm+Z+m,Z,yddl-Z-e.Z/Wne0k re1Z-e2Z/nXyddl3Z3e3j4Z5[3Wne0e6fk re1Z5nXe2Z7ej8d Z9ernhe9ddfkrfge:dD].Z;e;dkrDe<e;n e<e;d^q&Z=dZ>n dZ>eoye rddl?m@Z@dZAn dZAejBejCejDejEejFejGejHejIejJh ZKdZLdddZMdZNdZOdePfdYZQdePfd YZRd!ePfd"YZSdS(#i(tprint_functioni(tPY2t range_typet text_typetstr_typetJYTHONt IRONPYTHONN(t_auth(tcharset_by_namet charset_by_id(tCLIENTtCOMMANDtCRt FIELD_TYPEt SERVER_STATUS(t converters(tCursor(tParser(t dump_packett MysqlPackettFieldDescriptorPackettOKPacketWrappertEOFPacketWrappertLoadLocalPacketWrapper(tbyte2inttint2byte(terrtVERSION_STRINGiiiiiicCs|jdjtS(Ntlatin1(tdecodet translatet_surrogateescape_table(ts((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyt_fast_surrogateescape8scCs|jddS(Ntasciitsurrogateescape(R(R ((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyR!;s(tSocketIOcCstjt||S(N(tiotBufferedReaderR$(tsocktmode((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyt _makefileFscCs |j|S(N(tmakefile(R'R(((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyR)Jstutf8mb4icCstjd|d S(NsZ>e:j?Z?e:j@Z@e:jAZAe:jBZBe:jCZCe:jDZDRS(5s 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 `_ in the specification. tii iic!$s| dkr(tjddkr(t} n|dk rI|dkrI|}n|dk re| re|}n|sq|rtdnt||_|jr| tjO} nr| rtj j drd} qd} n| rsdnt j t jj| fd}!|!d |}|!d |}|!d |}|!d |}|!d |}t|!d|}|!d|}|!d|}|si}nt|trxOdddddgD]5}"|!d|"|j|"}#|#r|#||"| |_?id#d$6t@t jAd%6tBd&6|_C|r||jCd'0 and <=31536000sread_timeout should be >= 0swrite_timeout should be >= 0s Not connectedcSs1i|]'\}}t|tk r||qS((ttypetint(t.0tktv((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pys 0s cSs1i|]'\}}t|tkr||qS((RIRJ(RKRLRM((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pys 1s tpymysqlt _client_namet_pidt_client_versiont program_name(FtNonetsyst version_infotTruetNotImplementedErrortboolt _local_infileR t LOCAL_FILEStplatformt startswithRtreadtostpatht expanduserRJt isinstancetdictR7tFalsetsslt SSL_ENABLEDtSSLt_create_ssl_ctxtctxR@RCt DEFAULT_USERR>R?Rtencodetdbt unix_sockett bind_addressR0tconnect_timeoutt _read_timeoutt_write_timeouttcharsett use_unicodetDEFAULT_CHARSETRtencodingt CAPABILITIEStCONNECT_WITH_DBt client_flagt cursorclasst_resultt_affected_rowst host_infotautocommit_modeRt conversionstitemstencoderstdecoderstsql_modet init_commandtmax_allowed_packett_auth_plugin_mapt_binary_prefixtserver_public_keytstrtgetpidRt_connect_attrst_socktconnect($tselfR@R>R?RARCRlRqRtread_default_filetconvRrRwRxRRnRdR<tcompresst named_pipet autocommitRktpasswdt local_infileRt defer_connecttauth_plugin_mapt read_timeoutt write_timeoutRmt binary_prefixRRRR=R9tvalue((R;R<s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyt__init__s                                           cCst|tjr|S|jd}|jd}|dkoI|dk}tjd|d|}| oz|jdt|_|rtjntj |_ d|kr|j |dd|jdnd|kr|j |dn|j tjO_ |j tjO_ |S( NRDREtcafiletcheck_hostnameRFtkeyfileR9RG(RaRdt SSLContextR7RStcreate_default_contextRVRt CERT_NONEt CERT_REQUIREDt verify_modetload_cert_chaint set_cipherstoptionst OP_NO_SSLv2t OP_NO_SSLv3(RtsslpRDREthasnocaRh((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyRgGs # cCs|jrtjdnt|_|jdkr7dStjddtj }z)y|j |Wnt k rvnXWd|j XdS(s Send the quit message and close the socket. See `Connection.close() `_ in the specification. :raise Error: If the connection is already closed. sAlready closedNs`_ in the specification. tCOMMITN(RR RR(R((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pytcommitscCs!|jtjd|jdS(s Roll back the current transaction. See `Connection.rollback() `_ in the specification. tROLLBACKN(RR RR(R((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pytrollbackscCs0|jtjdt|}|j|jS(s%Send the "SHOW WARNINGS" SQL command.s SHOW WARNINGS(RR Rt MySQLResultR]trows(Rtresult((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyt show_warningss  cCs!|jtj||jdS(sI Set current db. :param db: The name of the db. N(RR t COM_INIT_DBR(RRk((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyt select_dbscCs{t|tr$d|j|dSt|ttfrb|j|}|jr^d|}n|Stj||j d|S(s}Escape whatever value you pass to it. Non-standard, for internal use; do not use this in your applications. t't_binarytmapping( RaRt escape_stringtbytest bytearrayt _quote_bytesRRt escape_itemRq(RtobjRtret((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyRs  cCs|j||jS(sjAlias for escape() Non-standard, for internal use; do not use this in your applications. (RR(RR((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pytliteralscCs-|jtj@r |jddStj|S(NRs''(RRt"SERVER_STATUS_NO_BACKSLASH_ESCAPEStreplaceRR(RR ((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyRs cCs:|jtj@r-dt|jddfStj|S(Ns'%s'Rs''(RRRR!RRt escape_bytes(RR ((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyRs cCs|r||S|j|S(s  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. (Rx(Rtcursor((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyRs cCstjdt|jS(s%Context manager that returns a CursorsHContext manager API of Connection object is deprecated; Use conn.begin()(twarningstwarntDeprecationWarningR(R((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyt __enter__scCs!|r|jn |jdS(s2On successful exit, commit. On exception, rollbackN(RR(RtexcRt traceback((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyt__exit__s cCs~t|trOtpt rOtr7|j|j}qO|j|jd}n|jtj ||j d||_ |j S(NR#t unbuffered( RaRRRRRjRtRR Rt_read_query_resultRz(RtsqlR((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pytqueryscCs|jd||_|jS(NR(RRz(RR((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyt next_resultscCs|jS(N(Rz(R((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyt affected_rows scCs/tjd|}|jtj||jS(Ns}||jkr|j|dkrtjtj dntj d||jfn|jdd |_|j|}t rt |n||7}|d krq n|t kr Pq q W|||j} | j| S( s!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. R4isRSR0RRqtidRaRRjRtR,R-RRdtserver_capabilitiesRfRRht wrap_socketRR@R)RRVRt_auth_plugin_nameRtscramble_native_passwordR?tsaltRRtscramble_caching_sha2tPLUGIN_AUTH_LENENC_CLIENT_DATAR2R tSECURE_CONNECTIONRkRvt PLUGIN_AUTHt CONNECT_ATTRSRR~Rtis_auth_switch_requestt read_uint8t read_stringt _process_authtscramble_old_passwordtis_extra_auth_datatcaching_sha2_password_authtsha256_password_authRR( Rt charset_idt data_initRtauthrespt plugin_namet connect_attrsRLRMt auth_packet((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyRs% !      # $#         c Cs|j|}|rny|j|SWqntk rj|dkrktjdd|t|fqkqnXn|dkrtj||S|dkrtj||S|dkrtj |j |j }n|dkrtj |j |j d}n|d kr|j d}nt|dkrx|}xEt rs|j}|d @d k}|d @d k}|j } | d kr|j|j dn|r(d} y'|j|| } |j| dWqDtk rtjdd||fqDtk r$tjdd||| | fqDXntjdd||f|j}|j|jsl|r/Pq/q/W|Stjdd||j||j}|j|S(Ntdialogi sGAuthentication plugin '%s' not loaded: - %r missing authenticate methodR1R2R0tmysql_old_passwordstmysql_clear_passwordiiis Password: s3no response - TypeError within plugin.prompt methodsAAuthentication plugin '%s' not loaded: - %r missing prompt methodi sTAuthentication plugin '%s' %r didn't respond with string. Returned '%r' to prompt %rs.Authentication plugin '%s' (%r) not configureds)Authentication plugin '%s' not configured(t_get_auth_plugin_handlert authenticatetAttributeErrorRRRIRRHRIR;R?tread_allRFRVRCRtpromptt TypeErrorRRR( RRMROthandlerRRtflagtechotlastRWtresp((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyREzsb        "               cCs|jj|}| rFt|trF|jj|jd}n|ry||}Wqtk rtjdd||fqXnd}|S(NR"i sXAuthentication plugin '%s' not loaded: - %r cannot be constructed with connection object( RR7RaRRRXRRRS(RRMt plugin_classRY((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyRSs  cCs |jdS(Ni(tserver_thread_id(R((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyRscCs|jS(N(Rq(R((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pytcharacter_set_namescCs|jS(N(R{(R((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyt get_host_infoscCs|jS(N(tprotocol_version(R((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pytget_proto_infosc Csd}|j}|j}t|||d!|_|d7}|jd|}|||!jd|_|d}tjd|||d!|_ |d7}|||d!|_ |d7}tjd |||d !d|_ |d 7}t ||d krtjd |||d !\}}}}|d 7}||_ yt|j|_Wntk rqd|_nX||_trtd |n|j |d>O_ trtd|ntd|d}n|d7}t |||kr|j ||||!7_ ||7}n|d7}|j tj@rt ||kr|jd|}|dkr{||jd|_q|||!jd|_ndS(NiisRss          cCsXt|}|j|_|j|_|j|_|j|_|j|_|j|_dS(N(RRR&RR|R}R)(RRt ok_packet((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyRWs      cCs|jjstdnt|}t|j|j}y|jWn|jjnX|jj}|jst j ddn|j |dS(NsF**WARN**: Received LOAD_LOCAL packet but local_infile option is false.isCommands Out of Sync( R%RYt RuntimeErrorRt LoadLocalFiletfilenameRRRRRR(RRt load_packettsenderR((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyR`s     cCs8|jstSt|}|j|_|j|_tS(N(t is_eof_packetRcRR|R)RV(RRtwp((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyt_check_packet_is_eofqs     cCs'|j|_|j|jdS(N(RR~Rt_read_rowdata_packet(RR((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyR}s cCsr|js dS|jj}|j|rJt|_d|_d|_dS|j|}d|_|f|_|S(Ni( R$R%RRRcRSRt_read_row_from_packetR(RRtrow((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyt_read_rowdata_packet_unbuffereds      cCsGx@|jrB|jj}|j|rt|_d|_qqWdS(N(R$R%RRRcRS(RR((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyR(s   cCsvg}xKtrS|jj}|j|r:d|_Pn|j|j|q Wt||_t ||_ dS(s:Read a rowdata packet for each data row in the result set.N( RVR%RRRStappendRR RttupleR(RRR((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyRs  cCsg}x|jD]\}}y|j}Wntk r@PnX|dk r|dk rk|j|}ntrtd|n|dk r||}qn|j|qWt|S(NsDEBUG: DATA = ( Rtread_length_coded_stringt IndexErrorRSRRRRR(RRRRtt converterR((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyRs    c Cs~g|_g|_|jj}|jj}g}xt|jD]}|jjt}|jj ||j |j |j }|r|t j kr|}q|tkr|jdkrd}q|}qd}nd}|jjj|}|tjkr d}ntr*tdj||n|jj ||fq@W|jj} | jsktdt||_ dS(s>Read a column descriptor packet for each column in the result.i?R"sDEBUG: field={}, converter={}sProtocol error, expecting EOFN(tfieldsRR%RrRtRR~RRRRt type_codeR tJSONt TEXT_TYPESt charsetnrRSRR7tthroughRRtformatRtAssertionErrorR( RRrt conn_encodingRR1tfieldt field_typeRtRt eof_packet((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyRs8           (RqRrRRuR]R#RRRRRR(RRR(((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyRs       RcBseZdZdZRS(cCs||_||_dS(N(RR%(RRR%((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyRs cCs|jjstjdn|j}zydt|jdL}t|jd }x0tr|j |}|svPn|j |qWWWdQXWn/t k rtj ddj |jnXWd|j dXdS( s3Send data packets from the local file to the servers(0, '')RiiNisCan't find file '{0}'R4i@(R%RRR'RRR*RRVR]RRRR(Rtconnt open_fileR,tchunk((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyRs    &(RqRrRR(((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pyRs (Tt __future__Rt_compatRRRRRRRR%R^RBR,RTRRR4RRqRR t constantsR R R R RRtcursorsRt optionfileRtprotocolRRRRRRtutilRRRRRdRVRet ImportErrorRSRctgetpasstgetuserRiRiRRUt _py_versiontrangeR1tchrRR!t _socketioR$R)tBITtBLOBt LONG_BLOBt MEDIUM_BLOBtSTRINGt TINY_BLOBt VAR_STRINGtVARCHARtGEOMETRYRRsRR/R2tobjectR3RR(((s4/tmp/pip-build-7KIPgS/PyMySQL/pymysql/connections.pytsz.        (.        A