<s> __author__ = "<STR_LIT>" <EOL> import socket <EOL> import protocol <EOL> import threading <EOL> from errors import * <EOL> def conninfo_parse ( conninfo ) : <EOL> "<STR_LIT>" <EOL> options = { } <EOL> buf = conninfo + "<STR_LIT:U+0020>" <EOL> tmp = pname = "<STR_LIT>" <EOL> quoted_string = False <EOL> cp = <NUM_LIT:0> <EOL> while cp < len ( buf ) : <EOL> c = buf [ cp ] <EOL> if c . isspace ( ) and tmp and not quoted_string and pname : <EOL> options [ pname ] = tmp <EOL> tmp = pname = "<STR_LIT>" <EOL> elif c == "<STR_LIT:'>" : <EOL> quoted_string = not quoted_string <EOL> elif c == '<STR_LIT:\\>' : <EOL> cp += <NUM_LIT:1> <EOL> tmp += buf [ cp ] <EOL> elif c == "<STR_LIT:=>" : <EOL> if not tmp : <EOL> raise RuntimeError ( "<STR_LIT>" % cp ) <EOL> pname = tmp <EOL> tmp = "<STR_LIT>" <EOL> elif not c . isspace ( ) or quoted_string : <EOL> tmp += c <EOL> cp += <NUM_LIT:1> <EOL> if quoted_string : <EOL> raise RuntimeError ( "<STR_LIT>" % cp ) <EOL> return options <EOL> class DataIterator ( object ) : <EOL> def __init__ ( self , obj , func ) : <EOL> self . obj = obj <EOL> self . func = func <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> retval = self . func ( self . obj ) <EOL> if retval == None : <EOL> raise StopIteration ( ) <EOL> return retval <EOL> statement_number_lock = threading . Lock ( ) <EOL> statement_number = <NUM_LIT:0> <EOL> class PreparedStatement ( object ) : <EOL> row_cache_size = <NUM_LIT:100> <EOL> def __init__ ( self , connection , statement , * types , ** kwargs ) : <EOL> global statement_number <EOL> if connection == None or connection . c == None : <EOL> raise InterfaceError ( "<STR_LIT>" ) <EOL> try : <EOL> statement_number_lock . acquire ( ) <EOL> self . _statement_number = statement_number <EOL> statement_number += <NUM_LIT:1> <EOL> finally : <EOL> statement_number_lock . release ( ) <EOL> self . c = connection . c <EOL> self . _portal_name = None <EOL> self . _statement_name = kwargs . get ( "<STR_LIT>" , "<STR_LIT>" % self . _statement_number ) <EOL> self . _row_desc = None <EOL> self . _cached_rows = [ ] <EOL> self . _ongoing_row_count = <NUM_LIT:0> <EOL> self . _command_complete = True <EOL> self . _parse_row_desc = self . c . parse ( self . _statement_name , statement , types ) <EOL> self . _lock = threading . RLock ( ) <EOL> def close ( self ) : <EOL> if self . _statement_name != "<STR_LIT>" : <EOL> self . c . close_statement ( self . _statement_name ) <EOL> if self . _portal_name != None : <EOL> self . c . close_portal ( self . _portal_name ) <EOL> self . _portal_name = None <EOL> row_description = property ( lambda self : self . _getRowDescription ( ) ) <EOL> def _getRowDescription ( self ) : <EOL> if self . _row_desc == None : <EOL> return None <EOL> return self . _row_desc . fields <EOL> def execute ( self , * args , ** kwargs ) : <EOL> self . _lock . acquire ( ) <EOL> try : <EOL> if not self . _command_complete : <EOL> self . _cached_rows = [ ] <EOL> self . _ongoing_row_count = <NUM_LIT:0> <EOL> if self . _portal_name != None : <EOL> self . c . close_portal ( self . _portal_name ) <EOL> self . _command_complete = False <EOL> self . _portal_name = "<STR_LIT>" % self . _statement_number <EOL> self . _row_desc , cmd = self . c . bind ( self . _portal_name , self . _statement_name , args , self . _parse_row_desc , kwargs . get ( "<STR_LIT>" ) ) <EOL> if self . _row_desc : <EOL> self . _fill_cache ( ) <EOL> else : <EOL> self . _command_complete = True <EOL> self . _ongoing_row_count = - <NUM_LIT:1> <EOL> if cmd != None and cmd . rows != None : <EOL> self . _ongoing_row_count = cmd . rows <EOL> finally : <EOL> self . _lock . release ( ) <EOL> def _fill_cache ( self ) : <EOL> self . _lock . acquire ( ) <EOL> try : <EOL> if self . _cached_rows : <EOL> raise InternalError ( "<STR_LIT>" ) <EOL> end_of_data , rows = self . c . fetch_rows ( self . _portal_name , self . row_cache_size , self . _row_desc ) <EOL> self . _cached_rows = rows <EOL> if end_of_data : <EOL> self . _command_complete = True <EOL> finally : <EOL> self . _lock . release ( ) <EOL> def _fetch ( self ) : <EOL> if not self . _row_desc : <EOL> raise ProgrammingError ( "<STR_LIT>" ) <EOL> self . _lock . acquire ( ) <EOL> try : <EOL> if not self . _cached_rows : <EOL> if self . _command_complete : <EOL> return None <EOL> self . _fill_cache ( ) <EOL> if self . _command_complete and not self . _cached_rows : <EOL> return None <EOL> row = self . _cached_rows . pop ( <NUM_LIT:0> ) <EOL> self . _ongoing_row_count += <NUM_LIT:1> <EOL> return tuple ( row ) <EOL> finally : <EOL> self . _lock . release ( ) <EOL> row_count = property ( lambda self : self . _get_row_count ( ) ) <EOL> def _get_row_count ( self ) : <EOL> self . _lock . acquire ( ) <EOL> try : <EOL> if not self . _command_complete : <EOL> end_of_data , rows = self . c . fetch_rows ( self . _portal_name , <NUM_LIT:0> , self . _row_desc ) <EOL> self . _cached_rows += rows <EOL> if end_of_data : <EOL> self . _command_complete = True <EOL> else : <EOL> raise InternalError ( "<STR_LIT>" ) <EOL> return self . _ongoing_row_count + len ( self . _cached_rows ) <EOL> finally : <EOL> self . _lock . release ( ) <EOL> def read_dict ( self ) : <EOL> row = self . _fetch ( ) <EOL> if row == None : <EOL> return row <EOL> retval = { } <EOL> for i in range ( len ( self . _row_desc . fields ) ) : <EOL> col_name = self . _row_desc . fields [ i ] [ '<STR_LIT:name>' ] <EOL> if retval . has_key ( col_name ) : <EOL> raise InterfaceError ( "<STR_LIT>" % ( col_name , ) ) <EOL> retval [ col_name ] = row [ i ] <EOL> return retval <EOL> def read_tuple ( self ) : <EOL> return self . _fetch ( ) <EOL> def iterate_tuple ( self ) : <EOL> return DataIterator ( self , PreparedStatement . read_tuple ) <EOL> def iterate_dict ( self ) : <EOL> return DataIterator ( self , PreparedStatement . read_dict ) <EOL> class SimpleStatement ( PreparedStatement ) : <EOL> "<STR_LIT>" <EOL> row_cache_size = None <EOL> def __init__ ( self , connection , statement ) : <EOL> if connection == None or connection . c == None : <EOL> raise InterfaceError ( "<STR_LIT>" ) <EOL> self . c = connection . c <EOL> self . _row_desc = None <EOL> self . _cached_rows = [ ] <EOL> self . _ongoing_row_count = - <NUM_LIT:1> <EOL> self . _command_complete = True <EOL> self . statement = statement <EOL> self . _lock = threading . RLock ( ) <EOL> def close ( self ) : <EOL> pass <EOL> def execute ( self , * args , ** kwargs ) : <EOL> "<STR_LIT>" <EOL> self . _lock . acquire ( ) <EOL> try : <EOL> self . _row_desc , cmd_complete , self . _cached_rows = self . c . send_simple_query ( self . statement , kwargs . get ( "<STR_LIT>" ) ) <EOL> self . _command_complete = True <EOL> self . _ongoing_row_count = - <NUM_LIT:1> <EOL> if cmd_complete is not None and cmd_complete . rows is not None : <EOL> self . _ongoing_row_count = cmd_complete . rows <EOL> finally : <EOL> self . _lock . release ( ) <EOL> def _fill_cache ( self ) : <EOL> pass <EOL> def _fetch ( self ) : <EOL> if not self . _row_desc : <EOL> raise ProgrammingError ( "<STR_LIT>" ) <EOL> self . _lock . acquire ( ) <EOL> try : <EOL> if not self . _cached_rows : <EOL> return None <EOL> row = self . _cached_rows . pop ( <NUM_LIT:0> ) <EOL> return tuple ( row ) <EOL> finally : <EOL> self . _lock . release ( ) <EOL> def _get_row_count ( self ) : <EOL> return self . _ongoing_row_count <EOL> class Cursor ( object ) : <EOL> def __init__ ( self , connection ) : <EOL> self . connection = connection <EOL> self . _stmt = None <EOL> def require_stmt ( func ) : <EOL> def retval ( self , * args , ** kwargs ) : <EOL> if self . _stmt == None : <EOL> raise ProgrammingError ( "<STR_LIT>" ) <EOL> return func ( self , * args , ** kwargs ) <EOL> return retval <EOL> row_description = property ( lambda self : self . _getRowDescription ( ) ) <EOL> def _getRowDescription ( self ) : <EOL> if self . _stmt == None : <EOL> return None <EOL> return self . _stmt . row_description <EOL> def execute ( self , query , * args , ** kwargs ) : <EOL> if self . connection . is_closed : <EOL> raise ConnectionClosedError ( ) <EOL> self . connection . _unnamed_prepared_statement_lock . acquire ( ) <EOL> try : <EOL> if kwargs . get ( "<STR_LIT>" ) : <EOL> self . _stmt = SimpleStatement ( self . connection , query ) <EOL> else : <EOL> self . _stmt = PreparedStatement ( self . connection , query , statement_name = "<STR_LIT>" , * [ { "<STR_LIT:type>" : type ( x ) , "<STR_LIT:value>" : x } for x in args ] ) <EOL> self . _stmt . execute ( * args , ** kwargs ) <EOL> finally : <EOL> self . connection . _unnamed_prepared_statement_lock . release ( ) <EOL> row_count = property ( lambda self : self . _get_row_count ( ) ) <EOL> @ require_stmt <EOL> def _get_row_count ( self ) : <EOL> return self . _stmt . row_count <EOL> @ require_stmt <EOL> def read_dict ( self ) : <EOL> return self . _stmt . read_dict ( ) <EOL> @ require_stmt <EOL> def read_tuple ( self ) : <EOL> return self . _stmt . read_tuple ( ) <EOL> @ require_stmt <EOL> def iterate_tuple ( self ) : <EOL> return self . _stmt . iterate_tuple ( ) <EOL> @ require_stmt <EOL> def iterate_dict ( self ) : <EOL> return self . _stmt . iterate_dict ( ) <EOL> def close ( self ) : <EOL> if self . _stmt != None : <EOL> self . _stmt . close ( ) <EOL> self . _stmt = None <EOL> def fileno ( self ) : <EOL> return self . connection . fileno ( ) <EOL> def isready ( self ) : <EOL> return self . connection . isready ( ) <EOL> class Connection ( Cursor ) : <EOL> def __init__ ( self , dsn = "<STR_LIT>" , user = None , host = None , unix_sock = None , port = <NUM_LIT> , database = None , password = None , socket_timeout = <NUM_LIT> , ssl = False ) : <EOL> self . _row_desc = None <EOL> if dsn : <EOL> opts = conninfo_parse ( dsn ) <EOL> database = opts . get ( "<STR_LIT>" , database ) <EOL> user = opts . get ( "<STR_LIT:user>" , user ) <EOL> password = opts . get ( "<STR_LIT:password>" , user ) <EOL> host = opts . get ( "<STR_LIT:host>" , host ) <EOL> port = int ( opts . get ( "<STR_LIT:port>" , port ) ) <EOL> ssl = opts . get ( "<STR_LIT>" , '<STR_LIT>' ) != '<STR_LIT>' <EOL> try : <EOL> self . c = protocol . Connection ( unix_sock = unix_sock , host = host , port = port , socket_timeout = socket_timeout , ssl = ssl ) <EOL> self . c . authenticate ( user , password = password , database = database ) <EOL> except socket . error , e : <EOL> raise InterfaceError ( "<STR_LIT>" , e ) <EOL> Cursor . __init__ ( self , self ) <EOL> self . _begin = PreparedStatement ( self , "<STR_LIT>" ) <EOL> self . _commit = PreparedStatement ( self , "<STR_LIT>" ) <EOL> self . _rollback = PreparedStatement ( self , "<STR_LIT>" ) <EOL> self . _unnamed_prepared_statement_lock = threading . RLock ( ) <EOL> self . in_transaction = False <EOL> self . autocommit = False <EOL> NotificationReceived = property ( <EOL> lambda self : getattr ( self . c , "<STR_LIT>" ) , <EOL> lambda self , value : setattr ( self . c , "<STR_LIT>" , value ) <EOL> ) <EOL> NoticeReceived = property ( <EOL> lambda self : getattr ( self . c , "<STR_LIT>" ) , <EOL> lambda self , value : setattr ( self . c , "<STR_LIT>" , value ) <EOL> ) <EOL> ParameterStatusReceived = property ( <EOL> lambda self : getattr ( self . c , "<STR_LIT>" ) , <EOL> lambda self , value : setattr ( self . c , "<STR_LIT>" , value ) <EOL> ) <EOL> def begin ( self ) : <EOL> if self . is_closed : <EOL> raise ConnectionClosedError ( ) <EOL> if self . autocommit : <EOL> return <EOL> self . _begin . execute ( ) <EOL> self . in_transaction = True <EOL> def commit ( self ) : <EOL> if self . is_closed : <EOL> raise ConnectionClosedError ( ) <EOL> self . _commit . execute ( ) <EOL> self . in_transaction = False <EOL> def rollback ( self ) : <EOL> if self . is_closed : <EOL> raise ConnectionClosedError ( ) <EOL> self . _rollback . execute ( ) <EOL> self . in_transaction = False <EOL> def close ( self ) : <EOL> if self . is_closed : <EOL> raise ConnectionClosedError ( ) <EOL> self . c . close ( ) <EOL> self . c = None <EOL> is_closed = property ( lambda self : self . c == None ) <EOL> def fileno ( self ) : <EOL> return self . c . fileno ( ) <EOL> def isready ( self ) : <EOL> return self . c . isready ( ) <EOL> def server_version ( self ) : <EOL> return self . c . server_version ( ) <EOL> def encoding ( self , encoding = None ) : <EOL> "<STR_LIT>" <EOL> return self . c . encoding ( ) </s>
