<s> """<STR_LIT>""" <EOL> from . . import exc , util <EOL> from . . sql import expression , sqltypes , util as sql_util <EOL> import collections <EOL> import operator <EOL> try : <EOL> from sqlalchemy . cresultproxy import safe_rowproxy_reconstructor <EOL> def rowproxy_reconstructor ( cls , state ) : <EOL> return safe_rowproxy_reconstructor ( cls , state ) <EOL> except ImportError : <EOL> def rowproxy_reconstructor ( cls , state ) : <EOL> obj = cls . __new__ ( cls ) <EOL> obj . __setstate__ ( state ) <EOL> return obj <EOL> try : <EOL> from sqlalchemy . cresultproxy import BaseRowProxy <EOL> _baserowproxy_usecext = True <EOL> except ImportError : <EOL> _baserowproxy_usecext = False <EOL> class BaseRowProxy ( object ) : <EOL> __slots__ = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self , parent , row , processors , keymap ) : <EOL> """<STR_LIT>""" <EOL> self . _parent = parent <EOL> self . _row = row <EOL> self . _processors = processors <EOL> self . _keymap = keymap <EOL> def __reduce__ ( self ) : <EOL> return ( rowproxy_reconstructor , <EOL> ( self . __class__ , self . __getstate__ ( ) ) ) <EOL> def values ( self ) : <EOL> """<STR_LIT>""" <EOL> return list ( self ) <EOL> def __iter__ ( self ) : <EOL> for processor , value in zip ( self . _processors , self . _row ) : <EOL> if processor is None : <EOL> yield value <EOL> else : <EOL> yield processor ( value ) <EOL> def __len__ ( self ) : <EOL> return len ( self . _row ) <EOL> def __getitem__ ( self , key ) : <EOL> try : <EOL> processor , obj , index = self . _keymap [ key ] <EOL> except KeyError : <EOL> processor , obj , index = self . _parent . _key_fallback ( key ) <EOL> except TypeError : <EOL> if isinstance ( key , slice ) : <EOL> l = [ ] <EOL> for processor , value in zip ( self . _processors [ key ] , <EOL> self . _row [ key ] ) : <EOL> if processor is None : <EOL> l . append ( value ) <EOL> else : <EOL> l . append ( processor ( value ) ) <EOL> return tuple ( l ) <EOL> else : <EOL> raise <EOL> if index is None : <EOL> raise exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % obj ) <EOL> if processor is not None : <EOL> return processor ( self . _row [ index ] ) <EOL> else : <EOL> return self . _row [ index ] <EOL> def __getattr__ ( self , name ) : <EOL> try : <EOL> return self [ name ] <EOL> except KeyError as e : <EOL> raise AttributeError ( e . args [ <NUM_LIT:0> ] ) <EOL> class RowProxy ( BaseRowProxy ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = ( ) <EOL> def __contains__ ( self , key ) : <EOL> return self . _parent . _has_key ( key ) <EOL> def __getstate__ ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . _parent , <EOL> '<STR_LIT>' : tuple ( self ) <EOL> } <EOL> def __setstate__ ( self , state ) : <EOL> self . _parent = parent = state [ '<STR_LIT>' ] <EOL> self . _row = state [ '<STR_LIT>' ] <EOL> self . _processors = parent . _processors <EOL> self . _keymap = parent . _keymap <EOL> __hash__ = None <EOL> def _op ( self , other , op ) : <EOL> return op ( tuple ( self ) , tuple ( other ) ) if isinstance ( other , RowProxy ) else op ( tuple ( self ) , other ) <EOL> def __lt__ ( self , other ) : <EOL> return self . _op ( other , operator . lt ) <EOL> def __le__ ( self , other ) : <EOL> return self . _op ( other , operator . le ) <EOL> def __ge__ ( self , other ) : <EOL> return self . _op ( other , operator . ge ) <EOL> def __gt__ ( self , other ) : <EOL> return self . _op ( other , operator . gt ) <EOL> def __eq__ ( self , other ) : <EOL> return self . _op ( other , operator . eq ) <EOL> def __ne__ ( self , other ) : <EOL> return self . _op ( other , operator . ne ) <EOL> def __repr__ ( self ) : <EOL> return repr ( sql_util . _repr_row ( self ) ) <EOL> def has_key ( self , key ) : <EOL> """<STR_LIT>""" <EOL> return self . _parent . _has_key ( key ) <EOL> def items ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ ( key , self [ key ] ) for key in self . keys ( ) ] <EOL> def keys ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _parent . keys <EOL> def iterkeys ( self ) : <EOL> return iter ( self . _parent . keys ) <EOL> def itervalues ( self ) : <EOL> return iter ( self ) <EOL> try : <EOL> from collections import Sequence <EOL> Sequence . register ( RowProxy ) <EOL> except ImportError : <EOL> pass <EOL> class ResultMetaData ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self , parent , cursor_description ) : <EOL> context = parent . context <EOL> dialect = context . dialect <EOL> self . case_sensitive = dialect . case_sensitive <EOL> self . matched_on_name = False <EOL> self . _orig_processors = None <EOL> if context . result_column_struct : <EOL> result_columns , cols_are_ordered , textual_ordered = context . result_column_struct <EOL> num_ctx_cols = len ( result_columns ) <EOL> else : <EOL> result_columns = cols_are_ordered = num_ctx_cols = textual_ordered = False <EOL> raw = self . _merge_cursor_description ( <EOL> context , cursor_description , result_columns , <EOL> num_ctx_cols , cols_are_ordered , textual_ordered ) <EOL> self . _keymap = { } <EOL> if not _baserowproxy_usecext : <EOL> len_raw = len ( raw ) <EOL> self . _keymap . update ( [ <EOL> ( elem [ <NUM_LIT:0> ] , ( elem [ <NUM_LIT:3> ] , elem [ <NUM_LIT:4> ] , elem [ <NUM_LIT:0> ] ) ) <EOL> for elem in raw <EOL> ] + [ <EOL> ( elem [ <NUM_LIT:0> ] - len_raw , ( elem [ <NUM_LIT:3> ] , elem [ <NUM_LIT:4> ] , elem [ <NUM_LIT:0> ] ) ) <EOL> for elem in raw <EOL> ] ) <EOL> self . _processors = [ elem [ <NUM_LIT:3> ] for elem in raw ] <EOL> by_key = dict ( [ <EOL> ( elem [ <NUM_LIT:2> ] , ( elem [ <NUM_LIT:3> ] , elem [ <NUM_LIT:4> ] , elem [ <NUM_LIT:0> ] ) ) <EOL> for elem in raw <EOL> ] ) <EOL> if num_ctx_cols : <EOL> if len ( by_key ) != num_ctx_cols : <EOL> seen = set ( ) <EOL> for rec in raw : <EOL> key = rec [ <NUM_LIT:1> ] <EOL> if key in seen : <EOL> key = key . lower ( ) if not self . case_sensitive else key <EOL> by_key [ key ] = ( None , key , None ) <EOL> seen . add ( key ) <EOL> self . _keymap . update ( [ <EOL> ( obj_elem , by_key [ elem [ <NUM_LIT:2> ] ] ) <EOL> for elem in raw if elem [ <NUM_LIT:4> ] <EOL> for obj_elem in elem [ <NUM_LIT:4> ] <EOL> ] ) <EOL> if not self . matched_on_name : <EOL> self . _keymap . update ( [ <EOL> ( elem [ <NUM_LIT:4> ] [ <NUM_LIT:0> ] , ( elem [ <NUM_LIT:3> ] , elem [ <NUM_LIT:4> ] , elem [ <NUM_LIT:0> ] ) ) <EOL> for elem in raw if elem [ <NUM_LIT:4> ] <EOL> ] ) <EOL> else : <EOL> self . _keymap . update ( [ <EOL> ( obj_elem , ( elem [ <NUM_LIT:3> ] , elem [ <NUM_LIT:4> ] , elem [ <NUM_LIT:0> ] ) ) <EOL> for elem in raw if elem [ <NUM_LIT:4> ] <EOL> for obj_elem in elem [ <NUM_LIT:4> ] <EOL> ] ) <EOL> self . _keymap . update ( by_key ) <EOL> if not num_ctx_cols and context . _translate_colname : <EOL> self . _keymap . update ( [ <EOL> ( elem [ <NUM_LIT:5> ] , self . _keymap [ elem [ <NUM_LIT:2> ] ] ) <EOL> for elem in raw if elem [ <NUM_LIT:5> ] <EOL> ] ) <EOL> def _merge_cursor_description ( <EOL> self , context , cursor_description , result_columns , <EOL> num_ctx_cols , cols_are_ordered , textual_ordered ) : <EOL> """<STR_LIT>""" <EOL> case_sensitive = context . dialect . case_sensitive <EOL> if num_ctx_cols and cols_are_ordered and not textual_ordered and num_ctx_cols == len ( cursor_description ) : <EOL> self . keys = [ elem [ <NUM_LIT:0> ] for elem in result_columns ] <EOL> return [ <EOL> ( <EOL> idx , <EOL> key , <EOL> name . lower ( ) if not case_sensitive else name , <EOL> context . get_result_processor ( <EOL> type_ , key , cursor_description [ idx ] [ <NUM_LIT:1> ] <EOL> ) , <EOL> obj , <EOL> None <EOL> ) for idx , ( key , name , obj , type_ ) <EOL> in enumerate ( result_columns ) <EOL> ] <EOL> else : <EOL> if textual_ordered : <EOL> raw_iterator = self . _merge_textual_cols_by_position ( <EOL> context , cursor_description , result_columns ) <EOL> elif num_ctx_cols : <EOL> raw_iterator = self . _merge_cols_by_name ( <EOL> context , cursor_description , result_columns ) <EOL> else : <EOL> raw_iterator = self . _merge_cols_by_none ( <EOL> context , cursor_description ) <EOL> return [ <EOL> ( <EOL> idx , colname , colname , <EOL> context . get_result_processor ( <EOL> mapped_type , colname , coltype ) , <EOL> obj , untranslated ) <EOL> for idx , colname , mapped_type , coltype , obj , untranslated <EOL> in raw_iterator <EOL> ] <EOL> def _colnames_from_description ( self , context , cursor_description ) : <EOL> """<STR_LIT>""" <EOL> dialect = context . dialect <EOL> case_sensitive = dialect . case_sensitive <EOL> translate_colname = context . _translate_colname <EOL> description_decoder = dialect . _description_decoder if dialect . description_encoding else None <EOL> normalize_name = dialect . normalize_name if dialect . requires_name_normalize else None <EOL> untranslated = None <EOL> self . keys = [ ] <EOL> for idx , rec in enumerate ( cursor_description ) : <EOL> colname = rec [ <NUM_LIT:0> ] <EOL> coltype = rec [ <NUM_LIT:1> ] <EOL> if description_decoder : <EOL> colname = description_decoder ( colname ) <EOL> if translate_colname : <EOL> colname , untranslated = translate_colname ( colname ) <EOL> if normalize_name : <EOL> colname = normalize_name ( colname ) <EOL> self . keys . append ( colname ) <EOL> if not case_sensitive : <EOL> colname = colname . lower ( ) <EOL> yield idx , colname , untranslated , coltype <EOL> def _merge_textual_cols_by_position ( <EOL> self , context , cursor_description , result_columns ) : <EOL> dialect = context . dialect <EOL> typemap = dialect . dbapi_type_map <EOL> num_ctx_cols = len ( result_columns ) if result_columns else None <EOL> if num_ctx_cols > len ( cursor_description ) : <EOL> util . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( <EOL> num_ctx_cols , len ( cursor_description ) <EOL> ) ) <EOL> seen = set ( ) <EOL> for idx , colname , untranslated , coltype in self . _colnames_from_description ( context , cursor_description ) : <EOL> if idx < num_ctx_cols : <EOL> ctx_rec = result_columns [ idx ] <EOL> obj = ctx_rec [ <NUM_LIT:2> ] <EOL> mapped_type = ctx_rec [ <NUM_LIT:3> ] <EOL> if obj [ <NUM_LIT:0> ] in seen : <EOL> raise exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % obj [ <NUM_LIT:0> ] ) <EOL> seen . add ( obj [ <NUM_LIT:0> ] ) <EOL> else : <EOL> mapped_type = typemap . get ( coltype , sqltypes . NULLTYPE ) <EOL> obj = None <EOL> yield idx , colname , mapped_type , coltype , obj , untranslated <EOL> def _merge_cols_by_name ( self , context , cursor_description , result_columns ) : <EOL> dialect = context . dialect <EOL> typemap = dialect . dbapi_type_map <EOL> case_sensitive = dialect . case_sensitive <EOL> result_map = self . _create_result_map ( result_columns , case_sensitive ) <EOL> self . matched_on_name = True <EOL> for idx , colname , untranslated , coltype in self . _colnames_from_description ( context , cursor_description ) : <EOL> try : <EOL> ctx_rec = result_map [ colname ] <EOL> except KeyError : <EOL> mapped_type = typemap . get ( coltype , sqltypes . NULLTYPE ) <EOL> obj = None <EOL> else : <EOL> obj = ctx_rec [ <NUM_LIT:1> ] <EOL> mapped_type = ctx_rec [ <NUM_LIT:2> ] <EOL> yield idx , colname , mapped_type , coltype , obj , untranslated <EOL> def _merge_cols_by_none ( self , context , cursor_description ) : <EOL> dialect = context . dialect <EOL> typemap = dialect . dbapi_type_map <EOL> for idx , colname , untranslated , coltype in self . _colnames_from_description ( context , cursor_description ) : <EOL> mapped_type = typemap . get ( coltype , sqltypes . NULLTYPE ) <EOL> yield idx , colname , mapped_type , coltype , None , untranslated <EOL> @ classmethod <EOL> def _create_result_map ( cls , result_columns , case_sensitive = True ) : <EOL> d = { } <EOL> for elem in result_columns : <EOL> key , rec = elem [ <NUM_LIT:0> ] , elem [ <NUM_LIT:1> : ] <EOL> if not case_sensitive : <EOL> key = key . lower ( ) <EOL> if key in d : <EOL> e_name , e_obj , e_type = d [ key ] <EOL> d [ key ] = e_name , e_obj + rec [ <NUM_LIT:1> ] , e_type <EOL> else : <EOL> d [ key ] = rec <EOL> return d <EOL> def _key_fallback ( self , key , raiseerr = True ) : <EOL> map = self . _keymap <EOL> result = None <EOL> if isinstance ( key , util . string_types ) : <EOL> result = map . get ( key if self . case_sensitive else key . lower ( ) ) <EOL> elif isinstance ( key , expression . ColumnElement ) : <EOL> if key . _label and ( <EOL> key . _label <EOL> if self . case_sensitive <EOL> else key . _label . lower ( ) ) in map : <EOL> result = map [ key . _label <EOL> if self . case_sensitive <EOL> else key . _label . lower ( ) ] <EOL> elif hasattr ( key , '<STR_LIT:name>' ) and ( <EOL> key . name <EOL> if self . case_sensitive <EOL> else key . name . lower ( ) ) in map : <EOL> result = map [ key . name <EOL> if self . case_sensitive <EOL> else key . name . lower ( ) ] <EOL> if result is not None and result [ <NUM_LIT:1> ] is not None : <EOL> for obj in result [ <NUM_LIT:1> ] : <EOL> if key . _compare_name_for_result ( obj ) : <EOL> break <EOL> else : <EOL> result = None <EOL> if result is None : <EOL> if raiseerr : <EOL> raise exc . NoSuchColumnError ( <EOL> "<STR_LIT>" % <EOL> expression . _string_or_unprintable ( key ) ) <EOL> else : <EOL> return None <EOL> else : <EOL> map [ key ] = result <EOL> return result <EOL> def _has_key ( self , key ) : <EOL> if key in self . _keymap : <EOL> return True <EOL> else : <EOL> return self . _key_fallback ( key , False ) is not None <EOL> def _getter ( self , key , raiseerr = True ) : <EOL> if key in self . _keymap : <EOL> processor , obj , index = self . _keymap [ key ] <EOL> else : <EOL> ret = self . _key_fallback ( key , raiseerr ) <EOL> if ret is None : <EOL> return None <EOL> processor , obj , index = ret <EOL> if index is None : <EOL> raise exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % obj ) <EOL> return operator . itemgetter ( index ) <EOL> def __getstate__ ( self ) : <EOL> return { <EOL> '<STR_LIT>' : dict ( <EOL> ( key , index ) <EOL> for key , ( processor , obj , index ) in self . _keymap . items ( ) <EOL> if isinstance ( key , util . string_types + util . int_types ) <EOL> ) , <EOL> '<STR_LIT>' : self . keys , <EOL> "<STR_LIT>" : self . case_sensitive , <EOL> "<STR_LIT>" : self . matched_on_name <EOL> } <EOL> def __setstate__ ( self , state ) : <EOL> self . _processors = [ None for _ in range ( len ( state [ '<STR_LIT>' ] ) ) ] <EOL> self . _keymap = keymap = { } <EOL> for key , index in state [ '<STR_LIT>' ] . items ( ) : <EOL> keymap [ key ] = ( None , None , index ) <EOL> self . keys = state [ '<STR_LIT>' ] <EOL> self . case_sensitive = state [ '<STR_LIT>' ] <EOL> self . matched_on_name = state [ '<STR_LIT>' ] <EOL> class ResultProxy ( object ) : <EOL> """<STR_LIT>""" <EOL> _process_row = RowProxy <EOL> out_parameters = None <EOL> _can_close_connection = False <EOL> _metadata = None <EOL> _soft_closed = False <EOL> closed = False <EOL> def __init__ ( self , context ) : <EOL> self . context = context <EOL> self . dialect = context . dialect <EOL> self . cursor = self . _saved_cursor = context . cursor <EOL> self . connection = context . root_connection <EOL> self . _echo = self . connection . _echo and context . engine . _should_log_debug ( ) <EOL> self . _init_metadata ( ) <EOL> def _getter ( self , key , raiseerr = True ) : <EOL> try : <EOL> getter = self . _metadata . _getter <EOL> except AttributeError : <EOL> return self . _non_result ( None ) <EOL> else : <EOL> return getter ( key , raiseerr ) <EOL> def _has_key ( self , key ) : <EOL> try : <EOL> has_key = self . _metadata . _has_key <EOL> except AttributeError : <EOL> return self . _non_result ( None ) <EOL> else : <EOL> return has_key ( key ) <EOL> def _init_metadata ( self ) : <EOL> cursor_description = self . _cursor_description ( ) <EOL> if cursor_description is not None : <EOL> if self . context . compiled and '<STR_LIT>' in self . context . execution_options : <EOL> if self . context . compiled . _cached_metadata : <EOL> self . _metadata = self . context . compiled . _cached_metadata <EOL> else : <EOL> self . _metadata = self . context . compiled . _cached_metadata = ResultMetaData ( self , cursor_description ) <EOL> else : <EOL> self . _metadata = ResultMetaData ( self , cursor_description ) <EOL> if self . _echo : <EOL> self . context . engine . logger . debug ( <EOL> "<STR_LIT>" , tuple ( x [ <NUM_LIT:0> ] for x in cursor_description ) ) <EOL> def keys ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _metadata : <EOL> return self . _metadata . keys <EOL> else : <EOL> return [ ] <EOL> @ util . memoized_property <EOL> def rowcount ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . context . rowcount <EOL> except Exception as e : <EOL> self . connection . _handle_dbapi_exception ( <EOL> e , None , None , self . cursor , self . context ) <EOL> @ property <EOL> def lastrowid ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _saved_cursor . lastrowid <EOL> except Exception as e : <EOL> self . connection . _handle_dbapi_exception ( <EOL> e , None , None , <EOL> self . _saved_cursor , self . context ) <EOL> @ property <EOL> def returns_rows ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _metadata is not None <EOL> @ property <EOL> def is_insert ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . context . isinsert <EOL> def _cursor_description ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _saved_cursor . description <EOL> def _soft_close ( self , _autoclose_connection = True ) : <EOL> """<STR_LIT>""" <EOL> if self . _soft_closed : <EOL> return <EOL> self . _soft_closed = True <EOL> cursor = self . cursor <EOL> self . connection . _safe_close_cursor ( cursor ) <EOL> if _autoclose_connection and self . connection . should_close_with_result : <EOL> self . connection . close ( ) <EOL> self . cursor = None <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . closed : <EOL> self . _soft_close ( ) <EOL> self . closed = True <EOL> def __iter__ ( self ) : <EOL> while True : <EOL> row = self . fetchone ( ) <EOL> if row is None : <EOL> return <EOL> else : <EOL> yield row <EOL> @ util . memoized_property <EOL> def inserted_primary_key ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . context . compiled : <EOL> raise exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> elif not self . context . isinsert : <EOL> raise exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> elif self . context . _is_explicit_returning : <EOL> raise exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return self . context . inserted_primary_key <EOL> def last_updated_params ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . context . compiled : <EOL> raise exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> elif not self . context . isupdate : <EOL> raise exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> elif self . context . executemany : <EOL> return self . context . compiled_parameters <EOL> else : <EOL> return self . context . compiled_parameters [ <NUM_LIT:0> ] <EOL> def last_inserted_params ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . context . compiled : <EOL> raise exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> elif not self . context . isinsert : <EOL> raise exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> elif self . context . executemany : <EOL> return self . context . compiled_parameters <EOL> else : <EOL> return self . context . compiled_parameters [ <NUM_LIT:0> ] <EOL> @ property <EOL> def returned_defaults ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . context . returned_defaults <EOL> def lastrow_has_defaults ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . context . lastrow_has_defaults ( ) <EOL> def postfetch_cols ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . context . compiled : <EOL> raise exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> elif not self . context . isinsert and not self . context . isupdate : <EOL> raise exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return self . context . postfetch_cols <EOL> def prefetch_cols ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . context . compiled : <EOL> raise exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> elif not self . context . isinsert and not self . context . isupdate : <EOL> raise exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return self . context . prefetch_cols <EOL> def supports_sane_rowcount ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . dialect . supports_sane_rowcount <EOL> def supports_sane_multi_rowcount ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . dialect . supports_sane_multi_rowcount <EOL> def _fetchone_impl ( self ) : <EOL> try : <EOL> return self . cursor . fetchone ( ) <EOL> except AttributeError : <EOL> return self . _non_result ( None ) <EOL> def _fetchmany_impl ( self , size = None ) : <EOL> try : <EOL> if size is None : <EOL> return self . cursor . fetchmany ( ) <EOL> else : <EOL> return self . cursor . fetchmany ( size ) <EOL> except AttributeError : <EOL> return self . _non_result ( [ ] ) <EOL> def _fetchall_impl ( self ) : <EOL> try : <EOL> return self . cursor . fetchall ( ) <EOL> except AttributeError : <EOL> return self . _non_result ( [ ] ) <EOL> def _non_result ( self , default ) : <EOL> if self . _metadata is None : <EOL> raise exc . ResourceClosedError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> ) <EOL> elif self . closed : <EOL> raise exc . ResourceClosedError ( "<STR_LIT>" ) <EOL> else : <EOL> return default <EOL> def process_rows ( self , rows ) : <EOL> process_row = self . _process_row <EOL> metadata = self . _metadata <EOL> keymap = metadata . _keymap <EOL> processors = metadata . _processors <EOL> if self . _echo : <EOL> log = self . context . engine . logger . debug <EOL> l = [ ] <EOL> for row in rows : <EOL> log ( "<STR_LIT>" , sql_util . _repr_row ( row ) ) <EOL> l . append ( process_row ( metadata , row , processors , keymap ) ) <EOL> return l <EOL> else : <EOL> return [ process_row ( metadata , row , processors , keymap ) <EOL> for row in rows ] <EOL> def fetchall ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> l = self . process_rows ( self . _fetchall_impl ( ) ) <EOL> self . _soft_close ( ) <EOL> return l <EOL> except Exception as e : <EOL> self . connection . _handle_dbapi_exception ( <EOL> e , None , None , <EOL> self . cursor , self . context ) <EOL> def fetchmany ( self , size = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> l = self . process_rows ( self . _fetchmany_impl ( size ) ) <EOL> if len ( l ) == <NUM_LIT:0> : <EOL> self . _soft_close ( ) <EOL> return l <EOL> except Exception as e : <EOL> self . connection . _handle_dbapi_exception ( <EOL> e , None , None , <EOL> self . cursor , self . context ) <EOL> def fetchone ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> row = self . _fetchone_impl ( ) <EOL> if row is not None : <EOL> return self . process_rows ( [ row ] ) [ <NUM_LIT:0> ] <EOL> else : <EOL> self . _soft_close ( ) <EOL> return None <EOL> except Exception as e : <EOL> self . connection . _handle_dbapi_exception ( <EOL> e , None , None , <EOL> self . cursor , self . context ) <EOL> def first ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _metadata is None : <EOL> return self . _non_result ( None ) <EOL> try : <EOL> row = self . _fetchone_impl ( ) <EOL> except Exception as e : <EOL> self . connection . _handle_dbapi_exception ( <EOL> e , None , None , <EOL> self . cursor , self . context ) <EOL> try : <EOL> if row is not None : <EOL> return self . process_rows ( [ row ] ) [ <NUM_LIT:0> ] <EOL> else : <EOL> return None <EOL> finally : <EOL> self . close ( ) <EOL> def scalar ( self ) : <EOL> """<STR_LIT>""" <EOL> row = self . first ( ) <EOL> if row is not None : <EOL> return row [ <NUM_LIT:0> ] <EOL> else : <EOL> return None <EOL> class BufferedRowResultProxy ( ResultProxy ) : <EOL> """<STR_LIT>""" <EOL> def _init_metadata ( self ) : <EOL> self . _max_row_buffer = self . context . execution_options . get ( <EOL> '<STR_LIT>' , None ) <EOL> self . __buffer_rows ( ) <EOL> super ( BufferedRowResultProxy , self ) . _init_metadata ( ) <EOL> size_growth = { <EOL> <NUM_LIT:1> : <NUM_LIT:5> , <EOL> <NUM_LIT:5> : <NUM_LIT:10> , <EOL> <NUM_LIT:10> : <NUM_LIT:20> , <EOL> <NUM_LIT:20> : <NUM_LIT:50> , <EOL> <NUM_LIT:50> : <NUM_LIT:100> , <EOL> <NUM_LIT:100> : <NUM_LIT> , <EOL> <NUM_LIT> : <NUM_LIT> , <EOL> <NUM_LIT> : <NUM_LIT:1000> <EOL> } <EOL> def __buffer_rows ( self ) : <EOL> if self . cursor is None : <EOL> return <EOL> size = getattr ( self , '<STR_LIT>' , <NUM_LIT:1> ) <EOL> self . __rowbuffer = collections . deque ( self . cursor . fetchmany ( size ) ) <EOL> self . _bufsize = self . size_growth . get ( size , size ) <EOL> if self . _max_row_buffer is not None : <EOL> self . _bufsize = min ( self . _max_row_buffer , self . _bufsize ) <EOL> def _soft_close ( self , ** kw ) : <EOL> self . __rowbuffer . clear ( ) <EOL> super ( BufferedRowResultProxy , self ) . _soft_close ( ** kw ) <EOL> def _fetchone_impl ( self ) : <EOL> if self . cursor is None : <EOL> return self . _non_result ( None ) <EOL> if not self . __rowbuffer : <EOL> self . __buffer_rows ( ) <EOL> if not self . __rowbuffer : <EOL> return None <EOL> return self . __rowbuffer . popleft ( ) <EOL> def _fetchmany_impl ( self , size = None ) : <EOL> if size is None : <EOL> return self . _fetchall_impl ( ) <EOL> result = [ ] <EOL> for x in range ( <NUM_LIT:0> , size ) : <EOL> row = self . _fetchone_impl ( ) <EOL> if row is None : <EOL> break <EOL> result . append ( row ) <EOL> return result <EOL> def _fetchall_impl ( self ) : <EOL> if self . cursor is None : <EOL> return self . _non_result ( [ ] ) <EOL> self . __rowbuffer . extend ( self . cursor . fetchall ( ) ) <EOL> ret = self . __rowbuffer <EOL> self . __rowbuffer = collections . deque ( ) <EOL> return ret <EOL> class FullyBufferedResultProxy ( ResultProxy ) : <EOL> """<STR_LIT>""" <EOL> def _init_metadata ( self ) : <EOL> super ( FullyBufferedResultProxy , self ) . _init_metadata ( ) <EOL> self . __rowbuffer = self . _buffer_rows ( ) <EOL> def _buffer_rows ( self ) : <EOL> return collections . deque ( self . cursor . fetchall ( ) ) <EOL> def _soft_close ( self , ** kw ) : <EOL> self . __rowbuffer . clear ( ) <EOL> super ( FullyBufferedResultProxy , self ) . _soft_close ( ** kw ) <EOL> def _fetchone_impl ( self ) : <EOL> if self . __rowbuffer : <EOL> return self . __rowbuffer . popleft ( ) <EOL> else : <EOL> return self . _non_result ( None ) <EOL> def _fetchmany_impl ( self , size = None ) : <EOL> if size is None : <EOL> return self . _fetchall_impl ( ) <EOL> result = [ ] <EOL> for x in range ( <NUM_LIT:0> , size ) : <EOL> row = self . _fetchone_impl ( ) <EOL> if row is None : <EOL> break <EOL> result . append ( row ) <EOL> return result <EOL> def _fetchall_impl ( self ) : <EOL> if not self . cursor : <EOL> return self . _non_result ( [ ] ) <EOL> ret = self . __rowbuffer <EOL> self . __rowbuffer = collections . deque ( ) <EOL> return ret <EOL> class BufferedColumnRow ( RowProxy ) : <EOL> def __init__ ( self , parent , row , processors , keymap ) : <EOL> row = list ( row ) <EOL> index = <NUM_LIT:0> <EOL> for processor in parent . _orig_processors : <EOL> if processor is not None : <EOL> row [ index ] = processor ( row [ index ] ) <EOL> index += <NUM_LIT:1> <EOL> row = tuple ( row ) <EOL> super ( BufferedColumnRow , self ) . __init__ ( parent , row , <EOL> processors , keymap ) <EOL> class BufferedColumnResultProxy ( ResultProxy ) : <EOL> """<STR_LIT>""" <EOL> _process_row = BufferedColumnRow <EOL> def _init_metadata ( self ) : <EOL> super ( BufferedColumnResultProxy , self ) . _init_metadata ( ) <EOL> metadata = self . _metadata <EOL> if metadata . _orig_processors is None : <EOL> metadata . _orig_processors = metadata . _processors <EOL> metadata . _processors = [ None for _ in range ( len ( metadata . keys ) ) ] <EOL> keymap = { } <EOL> for k , ( func , obj , index ) in metadata . _keymap . items ( ) : <EOL> keymap [ k ] = ( None , obj , index ) <EOL> metadata . _keymap = keymap <EOL> def fetchall ( self ) : <EOL> l = [ ] <EOL> while True : <EOL> row = self . fetchone ( ) <EOL> if row is None : <EOL> break <EOL> l . append ( row ) <EOL> return l <EOL> def fetchmany ( self , size = None ) : <EOL> if size is None : <EOL> return self . fetchall ( ) <EOL> l = [ ] <EOL> for i in range ( size ) : <EOL> row = self . fetchone ( ) <EOL> if row is None : <EOL> break <EOL> l . append ( row ) <EOL> return l </s>
