'Update the values of an existing field according to the mappings given through the mapping_function
- currently unused
@param db               : database instance
@param tablename        : name of the original table in which the new unique field id added
@param field_to_update  : name of the field to be updated according to the mapping
@param mapping_function : class instance containing the mapping functions'

def update_field_by_mapping(db, tablename, field_to_update, mapping_function):

    fields = mapping_function.fields(db)
    table = db[tablename]
    if (table['id'] not in fields):
        fields.append(table['id'])
    rows = db(mapping_function.query(db)).select(*fields)
    if rows:
        try:
            rows[0][tablename]['id']
            row_single_layer = False
        except KeyError:
            row_single_layer = True
    dict_update = {}
    for row in rows:
        if (not row_single_layer):
            row_id = row[tablename]['id']
        else:
            row_id = row['id']
        changed_value = mapping_function.mapping(row)
        dict_update[field_to_update] = changed_value
        db((table['id'] == row_id)).update(**dict_update)
