<s> from __future__ import with_statement <EOL> """<STR_LIT>""" <EOL> import sys <EOL> from . . import exc , util , log , interfaces <EOL> from . . sql import util as sql_util <EOL> from . interfaces import Connectable , ExceptionContext <EOL> from . util import _distill_params <EOL> import contextlib <EOL> class Connection ( Connectable ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , engine , connection = None , close_with_result = False , <EOL> _branch = False , _execution_options = None , <EOL> _dispatch = None , <EOL> _has_events = None ) : <EOL> """<STR_LIT>""" <EOL> self . engine = engine <EOL> self . dialect = engine . dialect <EOL> self . __connection = connection or engine . raw_connection ( ) <EOL> self . __transaction = None <EOL> self . should_close_with_result = close_with_result <EOL> self . __savepoint_seq = <NUM_LIT:0> <EOL> self . __branch = _branch <EOL> self . __invalid = False <EOL> self . __can_reconnect = True <EOL> if _dispatch : <EOL> self . dispatch = _dispatch <EOL> elif _has_events is None : <EOL> self . dispatch = self . dispatch . _join ( engine . dispatch ) <EOL> self . _has_events = _has_events or ( <EOL> _has_events is None and engine . _has_events ) <EOL> self . _echo = self . engine . _should_log_info ( ) <EOL> if _execution_options : <EOL> self . _execution_options = engine . _execution_options . union ( _execution_options ) <EOL> else : <EOL> self . _execution_options = engine . _execution_options <EOL> if self . _has_events or self . engine . _has_events : <EOL> self . dispatch . engine_connect ( self , _branch ) <EOL> def _branch ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . engine . _connection_cls ( <EOL> self . engine , <EOL> self . __connection , <EOL> _branch = True , <EOL> _has_events = self . _has_events , <EOL> _dispatch = self . dispatch ) <EOL> def _clone ( self ) : <EOL> """<STR_LIT>""" <EOL> c = self . __class__ . __new__ ( self . __class__ ) <EOL> c . __dict__ = self . __dict__ . copy ( ) <EOL> return c <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , type , value , traceback ) : <EOL> self . close ( ) <EOL> def execution_options ( self , ** opt ) : <EOL> """<STR_LIT>""" <EOL> c = self . _clone ( ) <EOL> c . _execution_options = c . _execution_options . union ( opt ) <EOL> if self . _has_events or self . engine . _has_events : <EOL> self . dispatch . set_connection_execution_options ( c , opt ) <EOL> self . dialect . set_connection_execution_options ( c , opt ) <EOL> return c <EOL> @ property <EOL> def closed ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' not in self . __dict__ and not self . __can_reconnect <EOL> @ property <EOL> def invalidated ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __invalid <EOL> @ property <EOL> def connection ( self ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> return self . __connection <EOL> except AttributeError : <EOL> return self . _revalidate_connection ( ) <EOL> def _revalidate_connection ( self ) : <EOL> if self . __can_reconnect and self . __invalid : <EOL> if self . __transaction is not None : <EOL> raise exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . __connection = self . engine . raw_connection ( ) <EOL> self . __invalid = False <EOL> return self . __connection <EOL> raise exc . ResourceClosedError ( "<STR_LIT>" ) <EOL> @ property <EOL> def _connection_is_valid ( self ) : <EOL> return getattr ( self . __connection , '<STR_LIT>' , False ) <EOL> @ property <EOL> def _still_open_and_connection_is_valid ( self ) : <EOL> return not self . closed and not self . invalidated and getattr ( self . __connection , '<STR_LIT>' , False ) <EOL> @ property <EOL> def info ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . connection . info <EOL> def connect ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _branch ( ) <EOL> def contextual_connect ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . _branch ( ) <EOL> def invalidate ( self , exception = None ) : <EOL> """<STR_LIT>""" <EOL> if self . invalidated : <EOL> return <EOL> if self . closed : <EOL> raise exc . ResourceClosedError ( "<STR_LIT>" ) <EOL> if self . _connection_is_valid : <EOL> self . __connection . invalidate ( exception ) <EOL> del self . __connection <EOL> self . __invalid = True <EOL> def detach ( self ) : <EOL> """<STR_LIT>""" <EOL> self . __connection . detach ( ) <EOL> def begin ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . __transaction is None : <EOL> self . __transaction = RootTransaction ( self ) <EOL> return self . __transaction <EOL> else : <EOL> return Transaction ( self , self . __transaction ) <EOL> def begin_nested ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . __transaction is None : <EOL> self . __transaction = RootTransaction ( self ) <EOL> else : <EOL> self . __transaction = NestedTransaction ( self , self . __transaction ) <EOL> return self . __transaction <EOL> def begin_twophase ( self , xid = None ) : <EOL> """<STR_LIT>""" <EOL> if self . __transaction is not None : <EOL> raise exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if xid is None : <EOL> xid = self . engine . dialect . create_xid ( ) <EOL> self . __transaction = TwoPhaseTransaction ( self , xid ) <EOL> return self . __transaction <EOL> def recover_twophase ( self ) : <EOL> return self . engine . dialect . do_recover_twophase ( self ) <EOL> def rollback_prepared ( self , xid , recover = False ) : <EOL> self . engine . dialect . do_rollback_twophase ( self , xid , recover = recover ) <EOL> def commit_prepared ( self , xid , recover = False ) : <EOL> self . engine . dialect . do_commit_twophase ( self , xid , recover = recover ) <EOL> def in_transaction ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __transaction is not None <EOL> def _begin_impl ( self , transaction ) : <EOL> if self . _echo : <EOL> self . engine . logger . info ( "<STR_LIT>" ) <EOL> if self . _has_events or self . engine . _has_events : <EOL> self . dispatch . begin ( self ) <EOL> try : <EOL> self . engine . dialect . do_begin ( self . connection ) <EOL> if self . connection . _reset_agent is None : <EOL> self . connection . _reset_agent = transaction <EOL> except Exception as e : <EOL> self . _handle_dbapi_exception ( e , None , None , None , None ) <EOL> def _rollback_impl ( self ) : <EOL> if self . _has_events or self . engine . _has_events : <EOL> self . dispatch . rollback ( self ) <EOL> if self . _still_open_and_connection_is_valid : <EOL> if self . _echo : <EOL> self . engine . logger . info ( "<STR_LIT>" ) <EOL> try : <EOL> self . engine . dialect . do_rollback ( self . connection ) <EOL> except Exception as e : <EOL> self . _handle_dbapi_exception ( e , None , None , None , None ) <EOL> finally : <EOL> if not self . __invalid and self . connection . _reset_agent is self . __transaction : <EOL> self . connection . _reset_agent = None <EOL> self . __transaction = None <EOL> else : <EOL> self . __transaction = None <EOL> def _commit_impl ( self , autocommit = False ) : <EOL> if self . _has_events or self . engine . _has_events : <EOL> self . dispatch . commit ( self ) <EOL> if self . _echo : <EOL> self . engine . logger . info ( "<STR_LIT>" ) <EOL> try : <EOL> self . engine . dialect . do_commit ( self . connection ) <EOL> except Exception as e : <EOL> self . _handle_dbapi_exception ( e , None , None , None , None ) <EOL> finally : <EOL> if not self . __invalid and self . connection . _reset_agent is self . __transaction : <EOL> self . connection . _reset_agent = None <EOL> self . __transaction = None <EOL> def _savepoint_impl ( self , name = None ) : <EOL> if self . _has_events or self . engine . _has_events : <EOL> self . dispatch . savepoint ( self , name ) <EOL> if name is None : <EOL> self . __savepoint_seq += <NUM_LIT:1> <EOL> name = '<STR_LIT>' % self . __savepoint_seq <EOL> if self . _still_open_and_connection_is_valid : <EOL> self . engine . dialect . do_savepoint ( self , name ) <EOL> return name <EOL> def _rollback_to_savepoint_impl ( self , name , context ) : <EOL> if self . _has_events or self . engine . _has_events : <EOL> self . dispatch . rollback_savepoint ( self , name , context ) <EOL> if self . _still_open_and_connection_is_valid : <EOL> self . engine . dialect . do_rollback_to_savepoint ( self , name ) <EOL> self . __transaction = context <EOL> def _release_savepoint_impl ( self , name , context ) : <EOL> if self . _has_events or self . engine . _has_events : <EOL> self . dispatch . release_savepoint ( self , name , context ) <EOL> if self . _still_open_and_connection_is_valid : <EOL> self . engine . dialect . do_release_savepoint ( self , name ) <EOL> self . __transaction = context <EOL> def _begin_twophase_impl ( self , transaction ) : <EOL> if self . _echo : <EOL> self . engine . logger . info ( "<STR_LIT>" ) <EOL> if self . _has_events or self . engine . _has_events : <EOL> self . dispatch . begin_twophase ( self , transaction . xid ) <EOL> if self . _still_open_and_connection_is_valid : <EOL> self . engine . dialect . do_begin_twophase ( self , transaction . xid ) <EOL> if self . connection . _reset_agent is None : <EOL> self . connection . _reset_agent = transaction <EOL> def _prepare_twophase_impl ( self , xid ) : <EOL> if self . _has_events or self . engine . _has_events : <EOL> self . dispatch . prepare_twophase ( self , xid ) <EOL> if self . _still_open_and_connection_is_valid : <EOL> assert isinstance ( self . __transaction , TwoPhaseTransaction ) <EOL> self . engine . dialect . do_prepare_twophase ( self , xid ) <EOL> def _rollback_twophase_impl ( self , xid , is_prepared ) : <EOL> if self . _has_events or self . engine . _has_events : <EOL> self . dispatch . rollback_twophase ( self , xid , is_prepared ) <EOL> if self . _still_open_and_connection_is_valid : <EOL> assert isinstance ( self . __transaction , TwoPhaseTransaction ) <EOL> try : <EOL> self . engine . dialect . do_rollback_twophase ( <EOL> self , xid , is_prepared ) <EOL> finally : <EOL> if self . connection . _reset_agent is self . __transaction : <EOL> self . connection . _reset_agent = None <EOL> self . __transaction = None <EOL> else : <EOL> self . __transaction = None <EOL> def _commit_twophase_impl ( self , xid , is_prepared ) : <EOL> if self . _has_events or self . engine . _has_events : <EOL> self . dispatch . commit_twophase ( self , xid , is_prepared ) <EOL> if self . _still_open_and_connection_is_valid : <EOL> assert isinstance ( self . __transaction , TwoPhaseTransaction ) <EOL> try : <EOL> self . engine . dialect . do_commit_twophase ( self , xid , is_prepared ) <EOL> finally : <EOL> if self . connection . _reset_agent is self . __transaction : <EOL> self . connection . _reset_agent = None <EOL> self . __transaction = None <EOL> else : <EOL> self . __transaction = None <EOL> def _autorollback ( self ) : <EOL> if not self . in_transaction ( ) : <EOL> self . _rollback_impl ( ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> conn = self . __connection <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> if not self . __branch : <EOL> conn . close ( ) <EOL> if conn . _reset_agent is self . __transaction : <EOL> conn . _reset_agent = None <EOL> if not self . __invalid : <EOL> del self . __connection <EOL> self . __can_reconnect = False <EOL> self . __transaction = None <EOL> def scalar ( self , object , * multiparams , ** params ) : <EOL> """<STR_LIT>""" <EOL> return self . execute ( object , * multiparams , ** params ) . scalar ( ) <EOL> def execute ( self , object , * multiparams , ** params ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( object , util . string_types [ <NUM_LIT:0> ] ) : <EOL> return self . _execute_text ( object , multiparams , params ) <EOL> try : <EOL> meth = object . _execute_on_connection <EOL> except AttributeError : <EOL> raise exc . InvalidRequestError ( <EOL> "<STR_LIT>" % <EOL> type ( object ) ) <EOL> else : <EOL> return meth ( self , multiparams , params ) <EOL> def _execute_function ( self , func , multiparams , params ) : <EOL> """<STR_LIT>""" <EOL> return self . _execute_clauseelement ( func . select ( ) , <EOL> multiparams , params ) <EOL> def _execute_default ( self , default , multiparams , params ) : <EOL> """<STR_LIT>""" <EOL> if self . _has_events or self . engine . _has_events : <EOL> for fn in self . dispatch . before_execute : <EOL> default , multiparams , params = fn ( self , default , multiparams , params ) <EOL> try : <EOL> try : <EOL> conn = self . __connection <EOL> except AttributeError : <EOL> conn = self . _revalidate_connection ( ) <EOL> dialect = self . dialect <EOL> ctx = dialect . execution_ctx_cls . _init_default ( <EOL> dialect , self , conn ) <EOL> except Exception as e : <EOL> self . _handle_dbapi_exception ( e , None , None , None , None ) <EOL> ret = ctx . _exec_default ( default , None ) <EOL> if self . should_close_with_result : <EOL> self . close ( ) <EOL> if self . _has_events or self . engine . _has_events : <EOL> self . dispatch . after_execute ( self , <EOL> default , multiparams , params , ret ) <EOL> return ret <EOL> def _execute_ddl ( self , ddl , multiparams , params ) : <EOL> """<STR_LIT>""" <EOL> if self . _has_events or self . engine . _has_events : <EOL> for fn in self . dispatch . before_execute : <EOL> ddl , multiparams , params = fn ( self , ddl , multiparams , params ) <EOL> dialect = self . dialect <EOL> compiled = ddl . compile ( dialect = dialect ) <EOL> ret = self . _execute_context ( <EOL> dialect , <EOL> dialect . execution_ctx_cls . _init_ddl , <EOL> compiled , <EOL> None , <EOL> compiled <EOL> ) <EOL> if self . _has_events or self . engine . _has_events : <EOL> self . dispatch . after_execute ( self , <EOL> ddl , multiparams , params , ret ) <EOL> return ret <EOL> def _execute_clauseelement ( self , elem , multiparams , params ) : <EOL> """<STR_LIT>""" <EOL> if self . _has_events or self . engine . _has_events : <EOL> for fn in self . dispatch . before_execute : <EOL> elem , multiparams , params = fn ( self , elem , multiparams , params ) <EOL> distilled_params = _distill_params ( multiparams , params ) <EOL> if distilled_params : <EOL> keys = distilled_params [ <NUM_LIT:0> ] . keys ( ) <EOL> else : <EOL> keys = [ ] <EOL> dialect = self . dialect <EOL> if '<STR_LIT>' in self . _execution_options : <EOL> key = dialect , elem , tuple ( sorted ( keys ) ) , len ( distilled_params ) > <NUM_LIT:1> <EOL> if key in self . _execution_options [ '<STR_LIT>' ] : <EOL> compiled_sql = self . _execution_options [ '<STR_LIT>' ] [ key ] <EOL> else : <EOL> compiled_sql = elem . compile ( <EOL> dialect = dialect , column_keys = keys , <EOL> inline = len ( distilled_params ) > <NUM_LIT:1> ) <EOL> self . _execution_options [ '<STR_LIT>' ] [ key ] = compiled_sql <EOL> else : <EOL> compiled_sql = elem . compile ( <EOL> dialect = dialect , column_keys = keys , <EOL> inline = len ( distilled_params ) > <NUM_LIT:1> ) <EOL> ret = self . _execute_context ( <EOL> dialect , <EOL> dialect . execution_ctx_cls . _init_compiled , <EOL> compiled_sql , <EOL> distilled_params , <EOL> compiled_sql , distilled_params <EOL> ) <EOL> if self . _has_events or self . engine . _has_events : <EOL> self . dispatch . after_execute ( self , <EOL> elem , multiparams , params , ret ) <EOL> return ret <EOL> def _execute_compiled ( self , compiled , multiparams , params ) : <EOL> """<STR_LIT>""" <EOL> if self . _has_events or self . engine . _has_events : <EOL> for fn in self . dispatch . before_execute : <EOL> compiled , multiparams , params = fn ( self , compiled , multiparams , params ) <EOL> dialect = self . dialect <EOL> parameters = _distill_params ( multiparams , params ) <EOL> ret = self . _execute_context ( <EOL> dialect , <EOL> dialect . execution_ctx_cls . _init_compiled , <EOL> compiled , <EOL> parameters , <EOL> compiled , parameters <EOL> ) <EOL> if self . _has_events or self . engine . _has_events : <EOL> self . dispatch . after_execute ( self , <EOL> compiled , multiparams , params , ret ) <EOL> return ret <EOL> def _execute_text ( self , statement , multiparams , params ) : <EOL> """<STR_LIT>""" <EOL> if self . _has_events or self . engine . _has_events : <EOL> for fn in self . dispatch . before_execute : <EOL> statement , multiparams , params = fn ( self , statement , multiparams , params ) <EOL> dialect = self . dialect <EOL> parameters = _distill_params ( multiparams , params ) <EOL> ret = self . _execute_context ( <EOL> dialect , <EOL> dialect . execution_ctx_cls . _init_statement , <EOL> statement , <EOL> parameters , <EOL> statement , parameters <EOL> ) <EOL> if self . _has_events or self . engine . _has_events : <EOL> self . dispatch . after_execute ( self , <EOL> statement , multiparams , params , ret ) <EOL> return ret <EOL> def _execute_context ( self , dialect , constructor , <EOL> statement , parameters , <EOL> * args ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> try : <EOL> conn = self . __connection <EOL> except AttributeError : <EOL> conn = self . _revalidate_connection ( ) <EOL> context = constructor ( dialect , self , conn , * args ) <EOL> except Exception as e : <EOL> self . _handle_dbapi_exception ( e , <EOL> util . text_type ( statement ) , parameters , <EOL> None , None ) <EOL> if context . compiled : <EOL> context . pre_exec ( ) <EOL> cursor , statement , parameters = context . cursor , context . statement , context . parameters <EOL> if not context . executemany : <EOL> parameters = parameters [ <NUM_LIT:0> ] <EOL> if self . _has_events or self . engine . _has_events : <EOL> for fn in self . dispatch . before_cursor_execute : <EOL> statement , parameters = fn ( self , cursor , statement , parameters , <EOL> context , context . executemany ) <EOL> if self . _echo : <EOL> self . engine . logger . info ( statement ) <EOL> self . engine . logger . info ( <EOL> "<STR_LIT>" , <EOL> sql_util . _repr_params ( parameters , batches = <NUM_LIT:10> ) <EOL> ) <EOL> try : <EOL> if context . executemany : <EOL> for fn in ( ) if not self . dialect . _has_events else self . dialect . dispatch . do_executemany : <EOL> if fn ( cursor , statement , parameters , context ) : <EOL> break <EOL> else : <EOL> self . dialect . do_executemany ( <EOL> cursor , <EOL> statement , <EOL> parameters , <EOL> context ) <EOL> elif not parameters and context . no_parameters : <EOL> for fn in ( ) if not self . dialect . _has_events else self . dialect . dispatch . do_execute_no_params : <EOL> if fn ( cursor , statement , context ) : <EOL> break <EOL> else : <EOL> self . dialect . do_execute_no_params ( <EOL> cursor , <EOL> statement , <EOL> context ) <EOL> else : <EOL> for fn in ( ) if not self . dialect . _has_events else self . dialect . dispatch . do_execute : <EOL> if fn ( cursor , statement , parameters , context ) : <EOL> break <EOL> else : <EOL> self . dialect . do_execute ( <EOL> cursor , <EOL> statement , <EOL> parameters , <EOL> context ) <EOL> except Exception as e : <EOL> self . _handle_dbapi_exception ( <EOL> e , <EOL> statement , <EOL> parameters , <EOL> cursor , <EOL> context ) <EOL> if self . _has_events or self . engine . _has_events : <EOL> self . dispatch . after_cursor_execute ( self , cursor , <EOL> statement , <EOL> parameters , <EOL> context , <EOL> context . executemany ) <EOL> if context . compiled : <EOL> context . post_exec ( ) <EOL> if context . isinsert and not context . executemany : <EOL> context . post_insert ( ) <EOL> result = context . get_result_proxy ( ) <EOL> if context . isinsert : <EOL> if context . _is_implicit_returning : <EOL> context . _fetch_implicit_returning ( result ) <EOL> result . close ( _autoclose_connection = False ) <EOL> result . _metadata = None <EOL> elif not context . _is_explicit_returning : <EOL> result . close ( _autoclose_connection = False ) <EOL> result . _metadata = None <EOL> elif context . isupdate and context . _is_implicit_returning : <EOL> context . _fetch_implicit_update_returning ( result ) <EOL> result . close ( _autoclose_connection = False ) <EOL> result . _metadata = None <EOL> elif result . _metadata is None : <EOL> result . rowcount <EOL> result . close ( _autoclose_connection = False ) <EOL> if self . __transaction is None and context . should_autocommit : <EOL> self . _commit_impl ( autocommit = True ) <EOL> if result . closed and self . should_close_with_result : <EOL> self . close ( ) <EOL> return result <EOL> def _cursor_execute ( self , cursor , statement , parameters , context = None ) : <EOL> """<STR_LIT>""" <EOL> if self . _has_events or self . engine . _has_events : <EOL> for fn in self . dispatch . before_cursor_execute : <EOL> statement , parameters = fn ( self , cursor , statement , parameters , <EOL> context , <EOL> False ) <EOL> if self . _echo : <EOL> self . engine . logger . info ( statement ) <EOL> self . engine . logger . info ( "<STR_LIT>" , parameters ) <EOL> try : <EOL> for fn in ( ) if not self . dialect . _has_events else self . dialect . dispatch . do_execute : <EOL> if fn ( cursor , statement , parameters , context ) : <EOL> break <EOL> else : <EOL> self . dialect . do_execute ( <EOL> cursor , <EOL> statement , <EOL> parameters , <EOL> context ) <EOL> except Exception as e : <EOL> self . _handle_dbapi_exception ( <EOL> e , <EOL> statement , <EOL> parameters , <EOL> cursor , <EOL> context ) <EOL> if self . _has_events or self . engine . _has_events : <EOL> self . dispatch . after_cursor_execute ( self , cursor , <EOL> statement , <EOL> parameters , <EOL> context , <EOL> False ) <EOL> def _safe_close_cursor ( self , cursor ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> cursor . close ( ) <EOL> except ( SystemExit , KeyboardInterrupt ) : <EOL> raise <EOL> except Exception : <EOL> self . engine . pool . logger . error ( <EOL> "<STR_LIT>" , exc_info = True ) <EOL> _reentrant_error = False <EOL> _is_disconnect = False <EOL> def _handle_dbapi_exception ( self , <EOL> e , <EOL> statement , <EOL> parameters , <EOL> cursor , <EOL> context ) : <EOL> exc_info = sys . exc_info ( ) <EOL> if context and context . exception is None : <EOL> context . exception = e <EOL> if not self . _is_disconnect : <EOL> self . _is_disconnect = isinstance ( e , self . dialect . dbapi . Error ) and not self . closed and self . dialect . is_disconnect ( e , self . __connection , cursor ) <EOL> if context : <EOL> context . is_disconnect = self . _is_disconnect <EOL> if self . _reentrant_error : <EOL> util . raise_from_cause ( <EOL> exc . DBAPIError . instance ( statement , <EOL> parameters , <EOL> e , <EOL> self . dialect . dbapi . Error ) , <EOL> exc_info <EOL> ) <EOL> self . _reentrant_error = True <EOL> try : <EOL> should_wrap = isinstance ( e , self . dialect . dbapi . Error ) or ( statement is not None and context is None ) <EOL> if should_wrap : <EOL> sqlalchemy_exception = exc . DBAPIError . instance ( <EOL> statement , <EOL> parameters , <EOL> e , <EOL> self . dialect . dbapi . Error , <EOL> connection_invalidated = self . _is_disconnect ) <EOL> else : <EOL> sqlalchemy_exception = None <EOL> newraise = None <EOL> if self . _has_events or self . engine . _has_events : <EOL> if should_wrap and context : <EOL> self . dispatch . dbapi_error ( self , <EOL> cursor , <EOL> statement , <EOL> parameters , <EOL> context , <EOL> e ) <EOL> ctx = ExceptionContextImpl ( <EOL> e , sqlalchemy_exception , self , cursor , statement , <EOL> parameters , context , self . _is_disconnect ) <EOL> for fn in self . dispatch . handle_error : <EOL> try : <EOL> per_fn = fn ( ctx ) <EOL> if per_fn is not None : <EOL> ctx . chained_exception = newraise = per_fn <EOL> except Exception as _raised : <EOL> newraise = _raised <EOL> break <EOL> if sqlalchemy_exception and self . _is_disconnect != ctx . is_disconnect : <EOL> sqlalchemy_exception . connection_invalidated = self . _is_disconnect = ctx . is_disconnect <EOL> if should_wrap and context : <EOL> context . handle_dbapi_exception ( e ) <EOL> if not self . _is_disconnect : <EOL> if cursor : <EOL> self . _safe_close_cursor ( cursor ) <EOL> self . _autorollback ( ) <EOL> if newraise : <EOL> util . raise_from_cause ( newraise , exc_info ) <EOL> elif should_wrap : <EOL> util . raise_from_cause ( <EOL> sqlalchemy_exception , <EOL> exc_info <EOL> ) <EOL> else : <EOL> util . reraise ( * exc_info ) <EOL> finally : <EOL> del self . _reentrant_error <EOL> if self . _is_disconnect : <EOL> del self . _is_disconnect <EOL> dbapi_conn_wrapper = self . connection <EOL> self . engine . pool . _invalidate ( dbapi_conn_wrapper , e ) <EOL> self . invalidate ( e ) <EOL> if self . should_close_with_result : <EOL> self . close ( ) <EOL> def default_schema_name ( self ) : <EOL> return self . engine . dialect . get_default_schema_name ( self ) <EOL> def transaction ( self , callable_ , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> trans = self . begin ( ) <EOL> try : <EOL> ret = self . run_callable ( callable_ , * args , ** kwargs ) <EOL> trans . commit ( ) <EOL> return ret <EOL> except : <EOL> with util . safe_reraise ( ) : <EOL> trans . rollback ( ) <EOL> def run_callable ( self , callable_ , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return callable_ ( self , * args , ** kwargs ) <EOL> def _run_visitor ( self , visitorcallable , element , ** kwargs ) : <EOL> visitorcallable ( self . dialect , self , <EOL> ** kwargs ) . traverse_single ( element ) <EOL> class ExceptionContextImpl ( ExceptionContext ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , exception , sqlalchemy_exception , <EOL> connection , cursor , statement , parameters , <EOL> context , is_disconnect ) : <EOL> self . connection = connection <EOL> self . sqlalchemy_exception = sqlalchemy_exception <EOL> self . original_exception = exception <EOL> self . execution_context = context <EOL> self . statement = statement <EOL> self . parameters = parameters <EOL> self . is_disconnect = is_disconnect <EOL> class Transaction ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , connection , parent ) : <EOL> self . connection = connection <EOL> self . _parent = parent or self <EOL> self . is_active = True <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _parent . is_active : <EOL> return <EOL> if self . _parent is self : <EOL> self . rollback ( ) <EOL> def rollback ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _parent . is_active : <EOL> return <EOL> self . _do_rollback ( ) <EOL> self . is_active = False <EOL> def _do_rollback ( self ) : <EOL> self . _parent . rollback ( ) <EOL> def commit ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _parent . is_active : <EOL> raise exc . InvalidRequestError ( "<STR_LIT>" ) <EOL> self . _do_commit ( ) <EOL> self . is_active = False <EOL> def _do_commit ( self ) : <EOL> pass <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , type , value , traceback ) : <EOL> if type is None and self . is_active : <EOL> try : <EOL> self . commit ( ) <EOL> except : <EOL> with util . safe_reraise ( ) : <EOL> self . rollback ( ) <EOL> else : <EOL> self . rollback ( ) <EOL> class RootTransaction ( Transaction ) : <EOL> def __init__ ( self , connection ) : <EOL> super ( RootTransaction , self ) . __init__ ( connection , None ) <EOL> self . connection . _begin_impl ( self ) <EOL> def _do_rollback ( self ) : <EOL> if self . is_active : <EOL> self . connection . _rollback_impl ( ) <EOL> def _do_commit ( self ) : <EOL> if self . is_active : <EOL> self . connection . _commit_impl ( ) <EOL> class NestedTransaction ( Transaction ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , connection , parent ) : <EOL> super ( NestedTransaction , self ) . __init__ ( connection , parent ) <EOL> self . _savepoint = self . connection . _savepoint_impl ( ) <EOL> def _do_rollback ( self ) : <EOL> if self . is_active : <EOL> self . connection . _rollback_to_savepoint_impl ( <EOL> self . _savepoint , self . _parent ) <EOL> def _do_commit ( self ) : <EOL> if self . is_active : <EOL> self . connection . _release_savepoint_impl ( <EOL> self . _savepoint , self . _parent ) <EOL> class TwoPhaseTransaction ( Transaction ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , connection , xid ) : <EOL> super ( TwoPhaseTransaction , self ) . __init__ ( connection , None ) <EOL> self . _is_prepared = False <EOL> self . xid = xid <EOL> self . connection . _begin_twophase_impl ( self ) <EOL> def prepare ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _parent . is_active : <EOL> raise exc . InvalidRequestError ( "<STR_LIT>" ) <EOL> self . connection . _prepare_twophase_impl ( self . xid ) <EOL> self . _is_prepared = True <EOL> def _do_rollback ( self ) : <EOL> self . connection . _rollback_twophase_impl ( self . xid , self . _is_prepared ) <EOL> def _do_commit ( self ) : <EOL> self . connection . _commit_twophase_impl ( self . xid , self . _is_prepared ) <EOL> class Engine ( Connectable , log . Identified ) : <EOL> """<STR_LIT>""" <EOL> _execution_options = util . immutabledict ( ) <EOL> _has_events = False <EOL> _connection_cls = Connection <EOL> def __init__ ( self , pool , dialect , url , <EOL> logging_name = None , echo = None , proxy = None , <EOL> execution_options = None <EOL> ) : <EOL> self . pool = pool <EOL> self . url = url <EOL> self . dialect = dialect <EOL> self . pool . _dialect = dialect <EOL> if logging_name : <EOL> self . logging_name = logging_name <EOL> self . echo = echo <EOL> self . engine = self <EOL> log . instance_logger ( self , echoflag = echo ) <EOL> if proxy : <EOL> interfaces . ConnectionProxy . _adapt_listener ( self , proxy ) <EOL> if execution_options : <EOL> self . update_execution_options ( ** execution_options ) <EOL> def update_execution_options ( self , ** opt ) : <EOL> """<STR_LIT>""" <EOL> self . _execution_options = self . _execution_options . union ( opt ) <EOL> self . dispatch . set_engine_execution_options ( self , opt ) <EOL> self . dialect . set_engine_execution_options ( self , opt ) <EOL> def execution_options ( self , ** opt ) : <EOL> """<STR_LIT>""" <EOL> return OptionEngine ( self , opt ) <EOL> @ property <EOL> def name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . dialect . name <EOL> @ property <EOL> def driver ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . dialect . driver <EOL> echo = log . echo_property ( ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . url <EOL> def dispose ( self ) : <EOL> """<STR_LIT>""" <EOL> self . pool . dispose ( ) <EOL> self . pool = self . pool . recreate ( ) <EOL> def _execute_default ( self , default ) : <EOL> with self . contextual_connect ( ) as conn : <EOL> return conn . _execute_default ( default , ( ) , { } ) <EOL> @ contextlib . contextmanager <EOL> def _optional_conn_ctx_manager ( self , connection = None ) : <EOL> if connection is None : <EOL> with self . contextual_connect ( ) as conn : <EOL> yield conn <EOL> else : <EOL> yield connection <EOL> def _run_visitor ( self , visitorcallable , element , <EOL> connection = None , ** kwargs ) : <EOL> with self . _optional_conn_ctx_manager ( connection ) as conn : <EOL> conn . _run_visitor ( visitorcallable , element , ** kwargs ) <EOL> class _trans_ctx ( object ) : <EOL> def __init__ ( self , conn , transaction , close_with_result ) : <EOL> self . conn = conn <EOL> self . transaction = transaction <EOL> self . close_with_result = close_with_result <EOL> def __enter__ ( self ) : <EOL> return self . conn <EOL> def __exit__ ( self , type , value , traceback ) : <EOL> if type is not None : <EOL> self . transaction . rollback ( ) <EOL> else : <EOL> self . transaction . commit ( ) <EOL> if not self . close_with_result : <EOL> self . conn . close ( ) <EOL> def begin ( self , close_with_result = False ) : <EOL> """<STR_LIT>""" <EOL> conn = self . contextual_connect ( close_with_result = close_with_result ) <EOL> try : <EOL> trans = conn . begin ( ) <EOL> except : <EOL> with util . safe_reraise ( ) : <EOL> conn . close ( ) <EOL> return Engine . _trans_ctx ( conn , trans , close_with_result ) <EOL> def transaction ( self , callable_ , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> with self . contextual_connect ( ) as conn : <EOL> return conn . transaction ( callable_ , * args , ** kwargs ) <EOL> def run_callable ( self , callable_ , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> with self . contextual_connect ( ) as conn : <EOL> return conn . run_callable ( callable_ , * args , ** kwargs ) <EOL> def execute ( self , statement , * multiparams , ** params ) : <EOL> """<STR_LIT>""" <EOL> connection = self . contextual_connect ( close_with_result = True ) <EOL> return connection . execute ( statement , * multiparams , ** params ) <EOL> def scalar ( self , statement , * multiparams , ** params ) : <EOL> return self . execute ( statement , * multiparams , ** params ) . scalar ( ) <EOL> def _execute_clauseelement ( self , elem , multiparams = None , params = None ) : <EOL> connection = self . contextual_connect ( close_with_result = True ) <EOL> return connection . _execute_clauseelement ( elem , multiparams , params ) <EOL> def _execute_compiled ( self , compiled , multiparams , params ) : <EOL> connection = self . contextual_connect ( close_with_result = True ) <EOL> return connection . _execute_compiled ( compiled , multiparams , params ) <EOL> def connect ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . _connection_cls ( self , ** kwargs ) <EOL> def contextual_connect ( self , close_with_result = False , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . _connection_cls ( self , <EOL> self . pool . connect ( ) , <EOL> close_with_result = close_with_result , <EOL> ** kwargs ) <EOL> def table_names ( self , schema = None , connection = None ) : <EOL> """<STR_LIT>""" <EOL> with self . _optional_conn_ctx_manager ( connection ) as conn : <EOL> if not schema : <EOL> schema = self . dialect . default_schema_name <EOL> return self . dialect . get_table_names ( conn , schema ) <EOL> def has_table ( self , table_name , schema = None ) : <EOL> """<STR_LIT>""" <EOL> return self . run_callable ( self . dialect . has_table , table_name , schema ) <EOL> def raw_connection ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . pool . unique_connection ( ) <EOL> class OptionEngine ( Engine ) : <EOL> def __init__ ( self , proxied , execution_options ) : <EOL> self . _proxied = proxied <EOL> self . url = proxied . url <EOL> self . dialect = proxied . dialect <EOL> self . logging_name = proxied . logging_name <EOL> self . echo = proxied . echo <EOL> log . instance_logger ( self , echoflag = self . echo ) <EOL> self . dispatch = self . dispatch . _join ( proxied . dispatch ) <EOL> self . _execution_options = proxied . _execution_options <EOL> self . update_execution_options ( ** execution_options ) <EOL> def _get_pool ( self ) : <EOL> return self . _proxied . pool <EOL> def _set_pool ( self , pool ) : <EOL> self . _proxied . pool = pool <EOL> pool = property ( _get_pool , _set_pool ) <EOL> def _get_has_events ( self ) : <EOL> return self . _proxied . _has_events or self . __dict__ . get ( '<STR_LIT>' , False ) <EOL> def _set_has_events ( self , value ) : <EOL> self . __dict__ [ '<STR_LIT>' ] = value <EOL> _has_events = property ( _get_has_events , _set_has_events ) </s>
