'Return a "bind" to which this :class:`.Session` is bound.
The "bind" is usually an instance of :class:`.Engine`,
except in the case where the :class:`.Session` has been
explicitly bound directly to a :class:`.Connection`.
For a multiply-bound or unbound :class:`.Session`, the
``mapper`` or ``clause`` arguments are used to determine the
appropriate bind to return.
Note that the "mapper" argument is usually present
when :meth:`.Session.get_bind` is called via an ORM
operation such as a :meth:`.Session.query`, each
individual INSERT/UPDATE/DELETE operation within a
:meth:`.Session.flush`, call, etc.
The order of resolution is:
1. if mapper given and session.binds is present,
locate a bind based on mapper.
2. if clause given and session.binds is present,
locate a bind based on :class:`.Table` objects
found in the given clause present in session.binds.
3. if session.bind is present, return that.
4. if clause given, attempt to return a bind
linked to the :class:`.MetaData` ultimately
associated with the clause.
5. if mapper given, attempt to return a bind
linked to the :class:`.MetaData` ultimately
associated with the :class:`.Table` or other
selectable to which the mapper is mapped.
6. No bind can be found, :exc:`~sqlalchemy.exc.UnboundExecutionError`
is raised.
:param mapper:
Optional :func:`.mapper` mapped class or instance of
:class:`.Mapper`.   The bind can be derived from a :class:`.Mapper`
first by consulting the "binds" map associated with this
:class:`.Session`, and secondly by consulting the :class:`.MetaData`
associated with the :class:`.Table` to which the :class:`.Mapper`
is mapped for a bind.
:param clause:
A :class:`.ClauseElement` (i.e. :func:`~.sql.expression.select`,
:func:`~.sql.expression.text`,
etc.).  If the ``mapper`` argument is not present or could not
produce a bind, the given expression construct will be searched
for a bound element, typically a :class:`.Table` associated with
bound :class:`.MetaData`.'

def get_bind(self, mapper=None, clause=None):

    if (mapper is clause is None):
        if self.bind:
            return self.bind
        else:
            raise sa_exc.UnboundExecutionError('This    session    is    not    bound    to    a    single    Engine    or    Connection,    and    no    context    was    provided    to    locate    a    binding.')
    c_mapper = (((mapper is not None) and _class_to_mapper(mapper)) or None)
    if self.__binds:
        if c_mapper:
            if (c_mapper.base_mapper in self.__binds):
                return self.__binds[c_mapper.base_mapper]
            elif (c_mapper.mapped_table in self.__binds):
                return self.__binds[c_mapper.mapped_table]
        if (clause is not None):
            for t in sql_util.find_tables(clause, include_crud=True):
                if (t in self.__binds):
                    return self.__binds[t]
    if self.bind:
        return self.bind
    if (isinstance(clause, sql.expression.ClauseElement) and clause.bind):
        return clause.bind
    if (c_mapper and c_mapper.mapped_table.bind):
        return c_mapper.mapped_table.bind
    context = []
    if (mapper is not None):
        context.append(('mapper    %s' % c_mapper))
    if (clause is not None):
        context.append('SQL    expression')
    raise sa_exc.UnboundExecutionError(('Could    not    locate    a    bind    configured    on    %s    or    this    Session' % ',    '.join(context)))
