#NEWLINE# def get_bind( self , mapper = None , clause = None ) : #NEWLINE# #INDENT# 'Return a "bind" to which this :class:`.Session` is bound.\n\n        The "bind" is usually an instance of :class:`.Engine`,\n        except in the case where the :class:`.Session` has been\n        explicitly bound directly to a :class:`.Connection`.\n\n        For a multiply-bound or unbound :class:`.Session`, the\n        ``mapper`` or ``clause`` arguments are used to determine the\n        appropriate bind to return.\n\n        Note that the "mapper" argument is usually present\n        when :meth:`.Session.get_bind` is called via an ORM\n        operation such as a :meth:`.Session.query`, each\n        individual INSERT/UPDATE/DELETE operation within a\n        :meth:`.Session.flush`, call, etc.\n\n        The order of resolution is:\n\n        1. if mapper given and session.binds is present,\n           locate a bind based on mapper.\n        2. if clause given and session.binds is present,\n           locate a bind based on :class:`.Table` objects\n           found in the given clause present in session.binds.\n        3. if session.bind is present, return that.\n        4. if clause given, attempt to return a bind\n           linked to the :class:`.MetaData` ultimately\n           associated with the clause.\n        5. if mapper given, attempt to return a bind\n           linked to the :class:`.MetaData` ultimately\n           associated with the :class:`.Table` or other\n           selectable to which the mapper is mapped.\n        6. No bind can be found, :exc:`~sqlalchemy.exc.UnboundExecutionError`\n           is raised.\n\n        :param mapper:\n          Optional :func:`.mapper` mapped class or instance of\n          :class:`.Mapper`.   The bind can be derived from a :class:`.Mapper`\n          first by consulting the "binds" map associated with this\n          :class:`.Session`, and secondly by consulting the :class:`.MetaData`\n          associated with the :class:`.Table` to which the :class:`.Mapper`\n          is mapped for a bind.\n\n        :param clause:\n            A :class:`.ClauseElement` (i.e. :func:`~.sql.expression.select`,\n            :func:`~.sql.expression.text`,\n            etc.).  If the ``mapper`` argument is not present or could not\n            produce a bind, the given expression construct will be searched\n            for a bound element, typically a :class:`.Table` associated with\n            bound :class:`.MetaData`.\n\n        ' #NEWLINE# if ( mapper is clause is None ) : #NEWLINE# #INDENT# if self . bind : #NEWLINE# #INDENT# return self . bind #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise sa_exc . UnboundExecutionError ( 'This session is not bound to a single Engine or Connection, and no context was provided to locate a binding.' ) #NEWLINE# #UNINDENT# #UNINDENT# c_mapper = ( ( ( mapper is not None ) and _class_to_mapper ( mapper ) ) or None ) #NEWLINE# if self . __binds : #NEWLINE# #INDENT# if c_mapper : #NEWLINE# #INDENT# if ( c_mapper . base_mapper in self . __binds ) : #NEWLINE# #INDENT# return self . __binds [ c_mapper . base_mapper ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( c_mapper . mapped_table in self . __binds ) : #NEWLINE# #INDENT# return self . __binds [ c_mapper . mapped_table ] #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( clause is not None ) : #NEWLINE# #INDENT# for t in sql_util . find_tables ( clause , include_crud = True ) : #NEWLINE# #INDENT# if ( t in self . __binds ) : #NEWLINE# #INDENT# return self . __binds [ t ] #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if self . bind : #NEWLINE# #INDENT# return self . bind #NEWLINE# #UNINDENT# if ( isinstance ( clause , sql . expression . ClauseElement ) and clause . bind ) : #NEWLINE# #INDENT# return clause . bind #NEWLINE# #UNINDENT# if ( c_mapper and c_mapper . mapped_table . bind ) : #NEWLINE# #INDENT# return c_mapper . mapped_table . bind #NEWLINE# #UNINDENT# context = [ ] #NEWLINE# if ( mapper is not None ) : #NEWLINE# #INDENT# context . append ( ( 'mapper %s' % c_mapper ) ) #NEWLINE# #UNINDENT# if ( clause is not None ) : #NEWLINE# #INDENT# context . append ( 'SQL expression' ) #NEWLINE# #UNINDENT# raise sa_exc . UnboundExecutionError ( ( 'Could not locate a bind configured on %s or this Session' % ', ' . join ( context ) ) )