def inject_where_clause(builder, clause):
    """
    helper function to inject an additional where clause
    :param builder: the current builder
    :param clause: the clause to inject
    :return:
    """
    query = builder.v.query
    if callable(query):
        return builder
    lower = query.lower()
    index = lower.find(' where ')
    if index >= 0:
        index += len(' where ')
        builder.query('{} ({}) AND {}'.format(query[:index], clause, query[
            index:]))
    else:
        before = -1
        for before_q in [' order by', ' group by', ' limit', ' offset']:
            before = lower.find(before_q)
            if before >= 0:
                break
        if before < 0:
            builder.append(' WHERE ').append(clause)
        else:
            builder.query('{} WHERE {} {}'.format(query[:index], clause,
                query[index:]))
    return builder
