def search_by(cur, field, query, extent='whole', regexp=False,
    case_sensitive=False, frequent=False):
    """Main search function.  Return list of ent_seqs.

    Field in ('kanji', 'reading', 'gloss').
    Extent in ('whole', 'beginning', 'word', 'partial').
    """
    if regexp:
        operator = 'REGEXP ?'
        if extent == 'whole':
            query = '^' + query + '$'
        elif extent == 'beginning':
            query = '^' + query
        elif extent == 'word':
            query = '\\b' + query + '\\b'
    elif extent == 'word':
        operator = 'MATCH ?'
    else:
        operator = "LIKE ? ESCAPE '\\'"
        query = query.replace('\\', '\\\\')
        query = query.replace('%', '\\%')
        query = query.replace('_', '\\_')
        if extent == 'beginning':
            query = query + '%'
        elif extent == 'partial':
            query = '%' + query + '%'
    if field == 'kanji':
        table = 'kanjis'
        join = 'JOIN kanjis ON entries.ent_seq = kanjis.ent_seq'
    elif field == 'reading':
        table = 'readings'
        join = 'JOIN readings ON entries.ent_seq = readings.ent_seq'
    elif field == 'gloss':
        table = 'glosses'
        join = 'JOIN glosses ON entries.ent_seq = glosses.ent_seq'
    where_extra = ''
    if frequent:
        where_extra += 'AND entries.frequent = 1'
    cur.execute(
        '\nSELECT DISTINCT entries.ent_seq\nFROM entries\n  %s\nWHERE %s.%s %s\n%s\n;'
         % (join, table, field, operator, where_extra), [query])
    res = []
    for row in cur.fetchall():
        res.append(row[0])
    return res
