def _emit_update_statements(base_mapper, uowtransaction,
                            cached_connections, mapper, table, update,
                            bookkeeping=True):

    needs_version_id = mapper.version_id_col is not None and \
        mapper.version_id_col in mapper._cols_by_table[table]

    def update_stmt():
        clause = sql.and_()

        for col in mapper._pks_by_table[table]:
            clause.clauses.append(col == sql.bindparam(col._label,
                                                       type_=col.type))

        if needs_version_id:
            clause.clauses.append(
                mapper.version_id_col == sql.bindparam(
                    mapper.version_id_col._label,
                    type_=mapper.version_id_col.type))

        stmt = table.update(clause)
        return stmt

    cached_stmt = base_mapper._memo(('update', table), update_stmt)

    for (connection, paramkeys, hasvalue, has_all_defaults), \
        records in groupby(
            update,
            lambda rec: (
                rec[4],  # connection
                set(rec[2]),  # set of parameter keys
                bool(rec[5]),  # whether or not we have "value" parameters
                rec[6]  # has_all_defaults
            )
    ):
        rows = 0
        records = list(records)

        statement = cached_stmt

        # TODO: would be super-nice to not have to determine this boolean
        # inside the loop here, in the 99.9999% of the time there's only
        # one connection in use
        assert_singlerow = connection.dialect.supports_sane_rowcount
        assert_multirow = assert_singlerow and \
            connection.dialect.supports_sane_multi_rowcount
        allow_multirow = has_all_defaults and not needs_version_id

        if bookkeeping and not has_all_defaults and \
                mapper.base_mapper.eager_defaults:
            statement = statement.return_defaults()
        elif mapper.version_id_col is not None:
            statement = statement.return_defaults(mapper.version_id_col)

        if hasvalue:
            for state, state_dict, params, mapper, \
                    connection, value_params, has_all_defaults in records:
                c = connection.execute(
                    statement.values(value_params),
                    params)
                if bookkeeping:
                    _postfetch(
                        mapper,
                        uowtransaction,
                        table,
                        state,
                        state_dict,
                        c,
                        c.context.compiled_parameters[0],
                        value_params)
                rows += c.rowcount
                check_rowcount = True
        else:
            if not allow_multirow:
                check_rowcount = assert_singlerow
                for state, state_dict, params, mapper, \
                        connection, value_params, has_all_defaults in records:
                    c = cached_connections[connection].\
                        execute(statement, params)

                    # TODO: why with bookkeeping=False?
                    if bookkeeping:
                        _postfetch(
                            mapper,
                            uowtransaction,
                            table,
                            state,
                            state_dict,
                            c,
                            c.context.compiled_parameters[0],
                            value_params)
                    rows += c.rowcount
            else:
                multiparams = [rec[2] for rec in records]

                check_rowcount = assert_multirow or (
                    assert_singlerow and
                    len(multiparams) == 1
                )

                c = cached_connections[connection].\
                    execute(statement, multiparams)

                rows += c.rowcount

                for state, state_dict, params, mapper, \
                        connection, value_params, has_all_defaults in records:
                    if bookkeeping:
                        _postfetch(
                            mapper,
                            uowtransaction,
                            table,
                            state,
                            state_dict,
                            c,
                            c.context.compiled_parameters[0],
                            value_params)

        if check_rowcount:
            if rows != len(records):
                raise orm_exc.StaleDataError(
                    "UPDATE statement on table '%s' expected to "
                    "update %d row(s); %d were matched." %
                    (table.description, len(records), rows))

        elif needs_version_id:
            util.warn("Dialect %s does not support updated rowcount "
                      "- versioning cannot be verified." %
                      c.dialect.dialect_description)
Return the gid for username
dataset/ETHPy150Open zzzeek/sqlalchemy/lib/sqlalchemy/orm/persistence.py/_emit_update_statements--crossover with--dataset/ETHPy150Open NumentaCorp/agamotto/agamotto/user.py/gid