def seg_spec_to_sql(spec):
    """Given a string of the form ifo:name:version, ifo:name:* or ifo:name
    constructs a SQL caluse to restrict a search to that segment definer"""
    parts = spec.split(':')
    sql = "(segment_definer.ifos = '%s'" % parts[0]
    if len(parts) > 1 and parts[1] != '*':
        sql += " AND segment_definer.name = '%s'" % parts[1]
        if len(parts) > 2 and parts[2] != '*':
            sql += ' AND segment_definer.version = %s' % parts[2]
    sql += ')'
    return sql
