def runWorkflowQuery(config, vistrail=None, version=None, fromTime=None,
        toTime=None, user=None, offset=0, limit=100, modules=[], thumbs=None):
    # returns list of workflows:
    #         (vistrail name, vistrail id, id, name, date, user, thumb)
    result = []
    db = open_db_connection(config)
    select_part = \
    """SELECT DISTINCT v.name, v.id, w.parent_id, a1.value,
              action.date, action.user"""
    from_part = \
    """FROM workflow w"""
    # "tag name" exist in workflow table but may have been changed
    # so we use value from the vistrail __tag__ annotation
    where_part = \
    """WHERE w.entity_type='workflow'"""
    limit_part = 'LIMIT %s, %s' % (int(offset), int(limit))

    if vistrail:
        try:
            where_part += " AND v.id=%s" % int(vistrail)
        except ValueError:
            where_part += " AND v.name=%s" % \
                   db.escape(vistrail, get_db_lib().converters.conversions)
    if version:
        try:
            where_part += " AND w.parent_id=%s" % int(version)
        except __HOLE__:
            where_part += " AND a1.value=%s" % \
                   db.escape(version, get_db_lib().converters.conversions)
    if fromTime:
        where_part += " AND w.last_modified>%s" % \
               db.escape(fromTime, get_db_lib().converters.conversions)
    if toTime:
        where_part += " AND w.last_modified<%s" % \
               db.escape(toTime, get_db_lib().converters.conversions)
    if user:
        where_part += " AND action.user=%s" % \
               db.escape(user, get_db_lib().converters.conversions)
    next_port = 1
    old_alias = None
    for i, module, connected in zip(range(1,len(modules)+1), *zip(*modules)):
        module = module.lower()
        alias = "m%s"%i
        from_part += \
        """ JOIN module {0} ON
                ({0}.parent_id=w.id AND {0}.entity_type=w.entity_type AND
                 {0}.name={1})
        """.format(alias,
                   db.escape(module, get_db_lib().converters.conversions))
        if connected:
            p1_alias, p2_alias=("port%s"%next_port), ("port%s"%(next_port+1))
            next_port += 2
            from_part += \
            """ JOIN port {0} ON
                ({0}.entity_id=w.id AND {0}.entity_type=w.entity_type AND
                 {0}.moduleId={1}.id AND {0}.type='source')""".format(
                 p1_alias, old_alias)
            from_part += \
            """ JOIN port {0} ON
                ({0}.entity_id=w.id AND {0}.entity_type=w.entity_type AND
                 {0}.moduleId={1}.id AND {0}.type='destination' AND
                 {0}.parent_id = {2}.parent_id)""".format(
                 p2_alias, alias, p1_alias)
        old_alias = alias
    from_part += \
    """ JOIN vistrail v ON w.vistrail_id = v.id JOIN
            action ON action.entity_id=w.vistrail_id AND
                       action.id=w.parent_id LEFT JOIN
            action_annotation a1 ON
                a1.entity_id=w.vistrail_id AND
                a1.action_id=w.parent_id AND
                (a1.akey='__tag__' OR a1.akey IS NULL)"""
    if thumbs:
        select_part += ', t.image_bytes'
        from_part += """ LEFT JOIN action_annotation a2 ON
                              (a2.entity_id=w.vistrail_id AND
                               a2.action_id=w.parent_id AND
                               (a2.akey='__thumb__' OR
                                a2.akey IS NULL)) LEFT JOIN
                         thumbnail t ON a2.value=t.file_name"""
    else:
        select_part += ', NULL'

    command = ' '.join([select_part, from_part, where_part, limit_part]) + ';'
    #print command
    try:
        c = db.cursor()
        c.execute(command)
        rows = c.fetchall()
        result = rows
        c.close()
    except get_db_lib().Error, e:
        msg = "Couldn't perform query on db (%d : %s)" % \
            (e.args[0], e.args[1])
        raise VistrailsDBException(msg)

    # count all rows when offset = 0
    if 0 == offset:
        select_part = 'SELECT count(0)'
        command = ' '.join([select_part,from_part,where_part]) +';'
        #print command
        try:
            c = db.cursor()
            c.execute(command)
            res = c.fetchall()
            result= (result, res[0][0])
            c.close()
        except get_db_lib().Error, e:
            msg = "Couldn't perform query on db (%d : %s)" % \
                (e.args[0], e.args[1])
            raise VistrailsDBException(msg)

    close_db_connection(db)
    return result
dataset/ETHPy150Open VisTrails/VisTrails/vistrails/db/services/query.py/runWorkflowQuery