'Run query and return a QueryHistory object in order to see its progress on a Web page.'

def execute_and_watch(self, query, design=None, query_history=None):

    hql_query = query.hql_query
    if (query_history is None):
        query_history = QueryHistory.build(owner=self.client.user, query=hql_query, server_host=('%(server_host)s' % self.client.query_server), server_port=('%(server_port)d' % self.client.query_server), server_name=('%(server_name)s' % self.client.query_server), server_type=self.server_type, last_state=QueryHistory.STATE.submitted.index, design=design, notify=query.query.get('email_notify', False), query_type=query.query['type'], statement_number=0)
        query_history.save()
        LOG.debug(('Made    new    QueryHistory    id    %s    user    %s    query:    %s...' % (query_history.id, self.client.user, query_history.query[:25])))
    try:
        handle = self.client.query(query, query_history.statement_number)
        if (not handle.is_valid()):
            msg = (_('Server    returning    invalid    handle    for    query    id    %(id)d    [%(query)s]...') % {'id': query_history.id, 'query': query[:40]})
            raise QueryServerException(msg)
    except QueryServerException as ex:
        LOG.exception(ex)
        if (hasattr(ex, 'handle') and ex.handle):
            (query_history.server_id, query_history.server_guid) = (ex.handle.id, ex.handle.id)
            query_history.log_context = ex.handle.log_context
        query_history.save_state(QueryHistory.STATE.failed)
        raise ex
    (query_history.server_id, query_history.server_guid) = handle.get()
    query_history.operation_type = handle.operation_type
    query_history.has_results = handle.has_result_set
    query_history.modified_row_count = handle.modified_row_count
    query_history.log_context = handle.log_context
    query_history.query_type = query.query['type']
    query_history.set_to_running()
    query_history.save()
    LOG.debug(('Updated    QueryHistory    id    %s    user    %s    statement_number:    %s' % (query_history.id, self.client.user, query_history.statement_number)))
    return query_history
