<s> """<STR_LIT>""" <EOL> import re <EOL> import sys <EOL> try : <EOL> from types import ListType , TupleType , UnicodeType <EOL> except ImportError : <EOL> ListType = list <EOL> TupleType = tuple <EOL> UnicodeType = str <EOL> restr = r"""<STR_LIT>""" <EOL> insert_values = re . compile ( restr , re . S | re . I | re . X ) <EOL> from _mysql_exceptions import Warning , Error , InterfaceError , DataError , DatabaseError , OperationalError , IntegrityError , InternalError , NotSupportedError , ProgrammingError <EOL> class BaseCursor ( object ) : <EOL> """<STR_LIT>""" <EOL> from _mysql_exceptions import MySQLError , Warning , Error , InterfaceError , DatabaseError , DataError , OperationalError , IntegrityError , InternalError , ProgrammingError , NotSupportedError <EOL> _defer_warnings = False <EOL> def __init__ ( self , connection ) : <EOL> from weakref import proxy <EOL> self . connection = proxy ( connection ) <EOL> self . description = None <EOL> self . description_flags = None <EOL> self . rowcount = - <NUM_LIT:1> <EOL> self . arraysize = <NUM_LIT:1> <EOL> self . _executed = None <EOL> self . lastrowid = None <EOL> self . messages = [ ] <EOL> self . errorhandler = connection . errorhandler <EOL> self . _result = None <EOL> self . _warnings = <NUM_LIT:0> <EOL> self . _info = None <EOL> self . rownumber = None <EOL> def __del__ ( self ) : <EOL> self . close ( ) <EOL> self . errorhandler = None <EOL> self . _result = None <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . connection : return <EOL> while self . nextset ( ) : pass <EOL> self . connection = None <EOL> def _check_executed ( self ) : <EOL> if not self . _executed : <EOL> self . errorhandler ( self , ProgrammingError , "<STR_LIT>" ) <EOL> def _warning_check ( self ) : <EOL> from warnings import warn <EOL> if self . _warnings : <EOL> warnings = self . _get_db ( ) . show_warnings ( ) <EOL> if warnings : <EOL> for w in warnings : <EOL> self . messages . append ( ( self . Warning , w ) ) <EOL> for w in warnings : <EOL> warn ( w [ - <NUM_LIT:1> ] , self . Warning , <NUM_LIT:3> ) <EOL> elif self . _info : <EOL> self . messages . append ( ( self . Warning , self . _info ) ) <EOL> warn ( self . _info , self . Warning , <NUM_LIT:3> ) <EOL> def nextset ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _executed : <EOL> self . fetchall ( ) <EOL> del self . messages [ : ] <EOL> db = self . _get_db ( ) <EOL> nr = db . next_result ( ) <EOL> if nr == - <NUM_LIT:1> : <EOL> return None <EOL> self . _do_get_result ( ) <EOL> self . _post_get_result ( ) <EOL> self . _warning_check ( ) <EOL> return <NUM_LIT:1> <EOL> def _post_get_result ( self ) : pass <EOL> def _do_get_result ( self ) : <EOL> db = self . _get_db ( ) <EOL> self . _result = self . _get_result ( ) <EOL> self . rowcount = db . affected_rows ( ) <EOL> self . rownumber = <NUM_LIT:0> <EOL> self . description = self . _result and self . _result . describe ( ) or None <EOL> self . description_flags = self . _result and self . _result . field_flags ( ) or None <EOL> self . lastrowid = db . insert_id ( ) <EOL> self . _warnings = db . warning_count ( ) <EOL> self . _info = db . info ( ) <EOL> def setinputsizes ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> def setoutputsizes ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> def _get_db ( self ) : <EOL> if not self . connection : <EOL> self . errorhandler ( self , ProgrammingError , "<STR_LIT>" ) <EOL> return self . connection <EOL> def execute ( self , query , args = None ) : <EOL> """<STR_LIT>""" <EOL> del self . messages [ : ] <EOL> db = self . _get_db ( ) <EOL> if isinstance ( query , unicode ) : <EOL> query = query . encode ( db . unicode_literal . charset ) <EOL> if args is not None : <EOL> if isinstance ( args , dict ) : <EOL> query = query % dict ( ( key , db . literal ( item ) ) <EOL> for key , item in args . iteritems ( ) ) <EOL> else : <EOL> query = query % tuple ( [ db . literal ( item ) for item in args ] ) <EOL> try : <EOL> r = None <EOL> r = self . _query ( query ) <EOL> except TypeError , m : <EOL> if m . args [ <NUM_LIT:0> ] in ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) : <EOL> self . messages . append ( ( ProgrammingError , m . args [ <NUM_LIT:0> ] ) ) <EOL> self . errorhandler ( self , ProgrammingError , m . args [ <NUM_LIT:0> ] ) <EOL> else : <EOL> self . messages . append ( ( TypeError , m ) ) <EOL> self . errorhandler ( self , TypeError , m ) <EOL> except ( SystemExit , KeyboardInterrupt ) : <EOL> raise <EOL> except : <EOL> exc , value , tb = sys . exc_info ( ) <EOL> del tb <EOL> self . messages . append ( ( exc , value ) ) <EOL> self . errorhandler ( self , exc , value ) <EOL> self . _executed = query <EOL> if not self . _defer_warnings : self . _warning_check ( ) <EOL> return r <EOL> def executemany ( self , query , args ) : <EOL> """<STR_LIT>""" <EOL> del self . messages [ : ] <EOL> db = self . _get_db ( ) <EOL> if not args : return <EOL> if isinstance ( query , unicode ) : <EOL> query = query . encode ( db . unicode_literal . charset ) <EOL> m = insert_values . search ( query ) <EOL> if not m : <EOL> r = <NUM_LIT:0> <EOL> for a in args : <EOL> r = r + self . execute ( query , a ) <EOL> return r <EOL> p = m . start ( <NUM_LIT:1> ) <EOL> e = m . end ( <NUM_LIT:1> ) <EOL> qv = m . group ( <NUM_LIT:1> ) <EOL> try : <EOL> q = [ ] <EOL> for a in args : <EOL> if isinstance ( a , dict ) : <EOL> q . append ( qv % dict ( ( key , db . literal ( item ) ) <EOL> for key , item in a . iteritems ( ) ) ) <EOL> else : <EOL> q . append ( qv % tuple ( [ db . literal ( item ) for item in a ] ) ) <EOL> except TypeError , msg : <EOL> if msg . args [ <NUM_LIT:0> ] in ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) : <EOL> self . errorhandler ( self , ProgrammingError , msg . args [ <NUM_LIT:0> ] ) <EOL> else : <EOL> self . errorhandler ( self , TypeError , msg ) <EOL> except ( SystemExit , KeyboardInterrupt ) : <EOL> raise <EOL> except : <EOL> exc , value , tb = sys . exc_info ( ) <EOL> del tb <EOL> self . errorhandler ( self , exc , value ) <EOL> r = self . _query ( '<STR_LIT:\n>' . join ( [ query [ : p ] , '<STR_LIT>' . join ( q ) , query [ e : ] ] ) ) <EOL> if not self . _defer_warnings : self . _warning_check ( ) <EOL> return r <EOL> def callproc ( self , procname , args = ( ) ) : <EOL> """<STR_LIT>""" <EOL> db = self . _get_db ( ) <EOL> for index , arg in enumerate ( args ) : <EOL> q = "<STR_LIT>" % ( procname , index , <EOL> db . literal ( arg ) ) <EOL> if isinstance ( q , unicode ) : <EOL> q = q . encode ( db . unicode_literal . charset ) <EOL> self . _query ( q ) <EOL> self . nextset ( ) <EOL> q = "<STR_LIT>" % ( procname , <EOL> '<STR_LIT:U+002C>' . join ( [ '<STR_LIT>' % ( procname , i ) <EOL> for i in range ( len ( args ) ) ] ) ) <EOL> if type ( q ) is UnicodeType : <EOL> q = q . encode ( db . unicode_literal . charset ) <EOL> self . _query ( q ) <EOL> self . _executed = q <EOL> if not self . _defer_warnings : self . _warning_check ( ) <EOL> return args <EOL> def _do_query ( self , q ) : <EOL> db = self . _get_db ( ) <EOL> self . _last_executed = q <EOL> db . query ( q ) <EOL> self . _do_get_result ( ) <EOL> return self . rowcount <EOL> def _query ( self , q ) : return self . _do_query ( q ) <EOL> def _fetch_row ( self , size = <NUM_LIT:1> ) : <EOL> if not self . _result : <EOL> return ( ) <EOL> return self . _result . fetch_row ( size , self . _fetch_type ) <EOL> def __iter__ ( self ) : <EOL> return iter ( self . fetchone , None ) <EOL> Warning = Warning <EOL> Error = Error <EOL> InterfaceError = InterfaceError <EOL> DatabaseError = DatabaseError <EOL> DataError = DataError <EOL> OperationalError = OperationalError <EOL> IntegrityError = IntegrityError <EOL> InternalError = InternalError <EOL> ProgrammingError = ProgrammingError <EOL> NotSupportedError = NotSupportedError <EOL> class CursorStoreResultMixIn ( object ) : <EOL> """<STR_LIT>""" <EOL> def _get_result ( self ) : return self . _get_db ( ) . store_result ( ) <EOL> def _query ( self , q ) : <EOL> rowcount = self . _do_query ( q ) <EOL> self . _post_get_result ( ) <EOL> return rowcount <EOL> def _post_get_result ( self ) : <EOL> self . _rows = self . _fetch_row ( <NUM_LIT:0> ) <EOL> self . _result = None <EOL> def fetchone ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _check_executed ( ) <EOL> if self . rownumber >= len ( self . _rows ) : return None <EOL> result = self . _rows [ self . rownumber ] <EOL> self . rownumber = self . rownumber + <NUM_LIT:1> <EOL> return result <EOL> def fetchmany ( self , size = None ) : <EOL> """<STR_LIT>""" <EOL> self . _check_executed ( ) <EOL> end = self . rownumber + ( size or self . arraysize ) <EOL> result = self . _rows [ self . rownumber : end ] <EOL> self . rownumber = min ( end , len ( self . _rows ) ) <EOL> return result <EOL> def fetchall ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _check_executed ( ) <EOL> if self . rownumber : <EOL> result = self . _rows [ self . rownumber : ] <EOL> else : <EOL> result = self . _rows <EOL> self . rownumber = len ( self . _rows ) <EOL> return result <EOL> def scroll ( self , value , mode = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> self . _check_executed ( ) <EOL> if mode == '<STR_LIT>' : <EOL> r = self . rownumber + value <EOL> elif mode == '<STR_LIT>' : <EOL> r = value <EOL> else : <EOL> self . errorhandler ( self , ProgrammingError , <EOL> "<STR_LIT>" % repr ( mode ) ) <EOL> if r < <NUM_LIT:0> or r >= len ( self . _rows ) : <EOL> self . errorhandler ( self , IndexError , "<STR_LIT>" ) <EOL> self . rownumber = r <EOL> def __iter__ ( self ) : <EOL> self . _check_executed ( ) <EOL> result = self . rownumber and self . _rows [ self . rownumber : ] or self . _rows <EOL> return iter ( result ) <EOL> class CursorUseResultMixIn ( object ) : <EOL> """<STR_LIT>""" <EOL> _defer_warnings = True <EOL> def _get_result ( self ) : return self . _get_db ( ) . use_result ( ) <EOL> def fetchone ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _check_executed ( ) <EOL> r = self . _fetch_row ( <NUM_LIT:1> ) <EOL> if not r : <EOL> self . _warning_check ( ) <EOL> return None <EOL> self . rownumber = self . rownumber + <NUM_LIT:1> <EOL> return r [ <NUM_LIT:0> ] <EOL> def fetchmany ( self , size = None ) : <EOL> """<STR_LIT>""" <EOL> self . _check_executed ( ) <EOL> r = self . _fetch_row ( size or self . arraysize ) <EOL> self . rownumber = self . rownumber + len ( r ) <EOL> if not r : <EOL> self . _warning_check ( ) <EOL> return r <EOL> def fetchall ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _check_executed ( ) <EOL> r = self . _fetch_row ( <NUM_LIT:0> ) <EOL> self . rownumber = self . rownumber + len ( r ) <EOL> self . _warning_check ( ) <EOL> return r <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> row = self . fetchone ( ) <EOL> if row is None : <EOL> raise StopIteration <EOL> return row <EOL> class CursorTupleRowsMixIn ( object ) : <EOL> """<STR_LIT>""" <EOL> _fetch_type = <NUM_LIT:0> <EOL> class CursorDictRowsMixIn ( object ) : <EOL> """<STR_LIT>""" <EOL> _fetch_type = <NUM_LIT:1> <EOL> def fetchoneDict ( self ) : <EOL> """<STR_LIT>""" <EOL> from warnings import warn <EOL> warn ( "<STR_LIT>" , <EOL> DeprecationWarning , <NUM_LIT:2> ) <EOL> return self . fetchone ( ) <EOL> def fetchmanyDict ( self , size = None ) : <EOL> """<STR_LIT>""" <EOL> from warnings import warn <EOL> warn ( "<STR_LIT>" , <EOL> DeprecationWarning , <NUM_LIT:2> ) <EOL> return self . fetchmany ( size ) <EOL> def fetchallDict ( self ) : <EOL> """<STR_LIT>""" <EOL> from warnings import warn <EOL> warn ( "<STR_LIT>" , <EOL> DeprecationWarning , <NUM_LIT:2> ) <EOL> return self . fetchall ( ) <EOL> class CursorOldDictRowsMixIn ( CursorDictRowsMixIn ) : <EOL> """<STR_LIT>""" <EOL> _fetch_type = <NUM_LIT:2> <EOL> class Cursor ( CursorStoreResultMixIn , CursorTupleRowsMixIn , <EOL> BaseCursor ) : <EOL> """<STR_LIT>""" <EOL> class DictCursor ( CursorStoreResultMixIn , CursorDictRowsMixIn , <EOL> BaseCursor ) : <EOL> """<STR_LIT>""" <EOL> class SSCursor ( CursorUseResultMixIn , CursorTupleRowsMixIn , <EOL> BaseCursor ) : <EOL> """<STR_LIT>""" <EOL> class SSDictCursor ( CursorUseResultMixIn , CursorDictRowsMixIn , <EOL> BaseCursor ) : <EOL> """<STR_LIT>""" </s>
