Пример #1
0
def forward_references(row, table, keys, aliases):
    foreign_keys = table.foreign_keys()
    alias = aliases.get(table.name, table.name)

    refs = []
    for key in keys:
        k = key.replace("{0}_".format(alias), "", 1)
        if key in foreign_keys:
            # Key is a foreign key column
            fk = foreign_keys[key]
            autocomplete = fk.b.table.autocomplete(fk.b.name, row[tostring(key)])
        elif table.column(k).primary_key:
            # Key is a simple column, but primary key
            autocomplete = table.autocomplete(k, row[tostring(key)], OPTION_URI_SINGLE_ROW_FORMAT)
        else:
            # Key is a simple column
            autocomplete = table.autocomplete(k, row[tostring(key)], OPTION_URI_MULTIPLE_ROWS_FORMAT)
        f = foreign_key_or_column(table, k)
        kind = KIND_VALUE
        if f.__class__.__name__ == "ForeignKey":
            kind = KIND_FOREIGN_KEY
        refs.append(Value(row[tostring(key)], str(f), autocomplete, True, kind))

    return refs
Пример #2
0
def create_values(connection, table, filter_):
    """Creates row values according to the given filter"""

    builder = QueryBuilder(connection, table, filter_=filter_.filter, limit=1, simplify=filter_.simplify)

    mapper = None
    keys = None
    if filter_.simplify:
        comment = create_comment(table, connection.comment(table.name), builder.counter, builder.aliases, None)

        keys = comment.display

        mapper = SimplifyMapper(table, comment=comment)

    result = connection.queryone(builder.build(), mapper)

    row = Row(table, result)

    logger.debug("Keys: %s", keys)
    if keys is None:
        keys = sorted(row.row.keys(), key=lambda key: "" if key == COMMENT_TITLE else tostring(key))
    logger.debug("Keys: %s", keys)

    values = forward_references(row, table, keys, builder.aliases)
    values += back_references(row, table, builder.aliases)

    return values