'Custom lookup method for site rows, does a
left join with any instance_types found. Parameters
key and fields are not used, but are kept for API
compatibility reasons.
@param values: the site IDs'

def custom_lookup_rows(self, key, values, fields=[]):

    db = current.db
    s3db = current.s3db
    stable = s3db.stats_source
    qty = len(values)
    if (qty == 1):
        query = (stable.id == values[0])
        limitby = (0, 1)
    else:
        query = stable.id.belongs(values)
        limitby = (0, qty)
    if self.show_link:
        rows = db(query).select(stable.instance_type, limitby=limitby)
        instance_types = []
        for row in rows:
            if (row.instance_type not in instance_types):
                instance_types.append(row.instance_type)
        fields = [stable.source_id, stable.name]
        left = []
        for instance_type in instance_types:
            table = s3db[instance_type]
            fields.append(table.id)
            left.append(table.on((table.source_id == stable.source_id)))
            if (instance_type == 'doc_document'):
                fields.append(table.url)
        rows = db(query).select(left=left, limitby=limitby, *fields)
    else:
        rows = db(query).select(stable.source_id, stable.name, limitby=limitby)
    self.queries += 1
    return rows
