Exemple #1
0
def execute_directly(request,
                     query_msg,
                     design=None,
                     tablename=None,
                     on_success_url=None,
                     on_success_params=None,
                     **kwargs):
    """
  execute_directly(request, query_msg, tablename, design) -> HTTP response for execution

  This method wraps around db_utils.execute_directly() to take care of the HTTP response
  after the execution.

    query_msg
      The thrift Query object.

    design
      The design associated with the query.

    tablename
      The associated table name for the context.

    on_success_url
      Where to go after the query is done. The URL handler may expect an option "context" GET
      param. (See ``watch_query``.) For advanced usage, on_success_url can be a function, in
      which case the on complete URL is the return of:
        on_success_url(history_obj) -> URL string
      Defaults to the view results page.

    on_success_params
      Optional params to pass to the on_success_url (in additional to "context").

  Note that this may throw a Beeswax exception.
  """
    if design is not None:
        authorized_get_design(request, design.id)
    history_obj = db_utils.execute_directly(request.user, query_msg, design,
                                            **kwargs)
    watch_url = urlresolvers.reverse("hcatalog.views.watch_query",
                                     kwargs=dict(id=history_obj.id))

    # Prepare the GET params for the watch_url
    get_dict = QueryDict(None, mutable=True)
    # (1) context
    if design:
        get_dict['context'] = make_query_context("design", design.id)
    elif tablename:
        get_dict['context'] = make_query_context("table", tablename)

    # (2) on_success_url
    if on_success_url:
        if callable(on_success_url):
            on_success_url = on_success_url(history_obj)
        get_dict['on_success_url'] = on_success_url

    # (3) misc
    if on_success_params:
        get_dict.update(on_success_params)

    return format_preserving_redirect(request, watch_url, get_dict)
def execute_query(request, design_id=None, table=None):
  authorized_get_design(request, design_id)
  error_message, log = None, None
  form = query_form()
  action = request.path
  design = safe_get_design(request, SavedQuery.HQL, design_id)
  on_success_url = request.REQUEST.get('on_success_url')

  for _ in range(1):
    if request.method == 'POST':
      form.bind(request.POST)
      to_explain = request.POST.has_key('button-explain')
      to_submit = request.POST.has_key('button-submit')
      # Always validate the saveform, which will tell us whether it needs explicit saving
      if not form.is_valid():
        break
      to_save = form.saveform.cleaned_data['save']
      to_saveas = form.saveform.cleaned_data['saveas']
      if to_saveas and not design.is_auto:
        # Save As only affects a previously saved query
        design = design.clone()
      if to_submit or to_save or to_saveas or to_explain:
        explicit_save = to_save or to_saveas
        design = save_design(request, form, SavedQuery.HQL, design, explicit_save)
        action = urlresolvers.reverse(execute_query, kwargs=dict(design_id=design.id))

      # We're not going to process the form. Simply re-render it.
      if not to_explain and not to_submit:
        break

      query_str = _strip_trailing_semicolon(form.query.cleaned_data["query"])
      query_server = db_utils.get_query_server(form.query_servers.cleaned_data["server"])
      # (Optional) Parameterization.
      parameterization = get_parameterization(request, query_str, form, design, to_explain)
      if parameterization:
        return parameterization

      query_msg = make_beeswax_query(request, query_str, form)
      try:
        if to_explain:
          return expl_d(request, query_str, query_msg, design, query_server)
        else:
          notify = form.query.cleaned_data.get('email_notify', False)
          return e_d(request, query_msg, design=design,
            on_success_url=on_success_url,
            notify=notify)
      except BeeswaxException, ex:
        error_message, log = expand_exception(ex)
        # Fall through to re-render the execute form.
    else:
      # GET request
      if design.id is not None:
        data = HQLdesign.loads(design.data).get_query_dict()
        form.bind(data)
        form.saveform.set_data(design.name, design.desc)
      else:
        # New design
        form.bind()
def execute_directly(request, query_msg, design=None, tablename=None,
    on_success_url=None, on_success_params=None, **kwargs):
  """
  execute_directly(request, query_msg, tablename, design) -> HTTP response for execution

  This method wraps around db_utils.execute_directly() to take care of the HTTP response
  after the execution.

    query_msg
      The thrift Query object.

    design
      The design associated with the query.

    tablename
      The associated table name for the context.

    on_success_url
      Where to go after the query is done. The URL handler may expect an option "context" GET
      param. (See ``watch_query``.) For advanced usage, on_success_url can be a function, in
      which case the on complete URL is the return of:
        on_success_url(history_obj) -> URL string
      Defaults to the view results page.

    on_success_params
      Optional params to pass to the on_success_url (in additional to "context").

  Note that this may throw a Beeswax exception.
  """
  if design is not None:
    authorized_get_design(request, design.id)
  history_obj = db_utils.execute_directly(request.user, query_msg, design, **kwargs)
  watch_url = urlresolvers.reverse("hcatalog.views.watch_query", kwargs=dict(id=history_obj.id))

  # Prepare the GET params for the watch_url
  get_dict = QueryDict(None, mutable=True)
  # (1) context
  if design:
    get_dict['context'] = make_query_context("design", design.id)
  elif tablename:
    get_dict['context'] = make_query_context("table", tablename)

  # (2) on_success_url
  if on_success_url:
    if callable(on_success_url):
      on_success_url = on_success_url(history_obj)
    get_dict['on_success_url'] = on_success_url

  # (3) misc
  if on_success_params:
    get_dict.update(on_success_params)

  return format_preserving_redirect(request, watch_url, get_dict)
Exemple #4
0
def execute_directly(request, query, design, query_server, tablename=None, **kwargs):
  if design is not None:
    design = authorized_get_design(request, design.id)
  parameters = kwargs.pop('parameters', None)

  db = dbms.get(request.user, query_server)
  database = query.query.get('database', 'default')
  db.use(database)

  history_obj = db.execute_query(query, design)
  watch_url = reverse(get_app_name(request) + ':api_watch_query_refresh_json', kwargs={'id': history_obj.id})

  if parameters is not None:
    history_obj.update_extra('parameters', parameters)
    history_obj.save()

  response = {
    'status': 0,
    'id': history_obj.id,
    'watch_url': watch_url,
    'statement': history_obj.get_current_statement(),
    'is_redacted': history_obj.is_redacted
  }

  return JsonResponse(response)
Exemple #5
0
def execute_directly(request, query, design, query_server, tablename=None, **kwargs):
  if design is not None:
    design = authorized_get_design(request, design.id)
  parameters = kwargs.pop('parameters', None)

  db = dbms.get(request.user, query_server)
  database = query.query.get('database', 'default')
  db.use(database)

  history_obj = db.execute_query(query, design)
  watch_url = reverse(get_app_name(request) + ':api_watch_query_refresh_json', kwargs={'id': history_obj.id})

  if parameters is not None:
    history_obj.update_extra('parameters', parameters)
    history_obj.save()

  response = {
    'status': 0,
    'id': history_obj.id,
    'watch_url': watch_url,
    'statement': history_obj.get_current_statement(),
    'is_redacted': history_obj.is_redacted
  }

  return JsonResponse(response)
Exemple #6
0
def execute_directly(request,
                     query,
                     design,
                     query_server,
                     tablename=None,
                     **kwargs):
    if design is not None:
        design = authorized_get_design(request, design.id)

    db = dbms.get(request.user, query_server)
    database = query.query.get('database', 'default')
    db.use(database)

    history_obj = db.execute_query(query, design)
    watch_url = reverse(get_app_name(request) +
                        ':api_watch_query_refresh_json',
                        kwargs={'id': history_obj.id})

    response = {
        'status': 0,
        'id': history_obj.id,
        'watch_url': watch_url,
        'statement': history_obj.get_current_statement()
    }

    return HttpResponse(json.dumps(response), mimetype="application/json")
Exemple #7
0
def execute_directly(request, query, design, query_server, tablename=None, **kwargs):
  if design is not None:
    design = authorized_get_design(request, design.id)

  db = dbms.get(request.user, query_server)
  database = query.query.get('database', 'default')
  db.use(database)

  history_obj = db.execute_query(query, design)
  watch_url = reverse(get_app_name(request) + ':watch_query_refresh_json', kwargs={'id': history_obj.id})

  response = {
    'status': 0,
    'id': history_obj.id,
    'watch_url': watch_url
  }

  return HttpResponse(json.dumps(response), mimetype="application/json")
Exemple #8
0
def execute_directly(request, query, query_server=None, design=None, tablename=None,
                     on_success_url=None, on_success_params=None, download_format=None, **kwargs):
    """
    execute_directly(request, query_msg, tablename, design) -> HTTP response for execution

    This method wraps around dbms.execute_query() to take care of the HTTP response
    after the execution.

      query
        The HQL model Query object.

      query_server
        To which Query Server to submit the query.
        Dictionary with keys: ['server_name', 'server_host', 'server_port'].

      design
        The design associated with the query.

      tablename
        The associated table name for the context.

      on_success_url
        Where to go after the query is done. The URL handler may expect an option "context" GET
        param. (See ``watch_query``.) For advanced usage, on_success_url can be a function, in
        which case the on complete URL is the return of:
          on_success_url(history_obj) -> URL string
        Defaults to the view results page.

      on_success_params
        Optional params to pass to the on_success_url (in additional to "context").

    Note that this may throw a Beeswax exception.
    """
    if design is not None:
        authorized_get_design(request, design.id)

    db = dbms.get(request.user, query_server)
    database = query.query.get('database')
    if database is None:
        database = _get_last_database(request)
    db.use(database)

    history_obj = db.execute_query(query, design)

    watch_url = urlresolvers.reverse(get_app_name(request) + ':watch_query', kwargs={'id': history_obj.id, 'download_format':download_format})
    if 'download' in kwargs and kwargs['download']:
        watch_url += '?download=true'

    # Prepare the GET params for the watch_url
    get_dict = QueryDict(None, mutable=True)
    # (1) context
    if design:
        get_dict['context'] = make_query_context('design', design.id)
    elif tablename:
        get_dict['context'] = make_query_context('table', '%s:%s' % (tablename, database))

    # (2) on_success_url
    if on_success_url:
        if callable(on_success_url):
            on_success_url = on_success_url(history_obj)
        get_dict['on_success_url'] = on_success_url

    # (3) misc
    if on_success_params:
        get_dict.update(on_success_params)

    return format_preserving_redirect(request, watch_url, get_dict)
Exemple #9
0
def execute_query(request, design_id=None):
  """
  View function for executing an arbitrary query.
  It understands the optional GET/POST params:

    on_success_url
      If given, it will be displayed when the query is successfully finished.
      Otherwise, it will display the view query results page by default.
  """
  authorized_get_design(request, design_id)

  request.session['start_time'] = time.time()  # FIXME: add job id to not intersect simultaneous jobs
  error_message = None
  form = QueryForm()
  action = request.path
  log = None
  app_name = get_app_name(request)
  query_type = SavedQuery.TYPES_MAPPING['beeswax']
  design = safe_get_design(request, query_type, design_id)
  on_success_url = request.REQUEST.get('on_success_url')

  query_server = get_query_server_config(app_name)
  db = dbms.get(request.user, query_server)
  databases = _get_db_choices(request)


  if request.method == 'POST':
    form.bind(request.POST)
    form.query.fields['database'].choices =  databases # Could not do it in the form

    to_explain = request.POST.has_key('button-explain')
    to_submit = request.POST.has_key('button-submit')

    # Always validate the saveform, which will tell us whether it needs explicit saving
    if form.is_valid():
      to_save = form.saveform.cleaned_data['save']
      to_saveas = form.saveform.cleaned_data['saveas']

      if to_save or to_saveas:
        if 'beeswax-autosave' in request.session:
          del request.session['beeswax-autosave']

      if to_saveas and not design.is_auto:
        # Save As only affects a previously saved query
        design = design.clone()

      if to_submit or to_save or to_saveas or to_explain:
        explicit_save = to_save or to_saveas
        design = save_design(request, form, query_type, design, explicit_save)
        action = urlresolvers.reverse(app_name + ':execute_query', kwargs=dict(design_id=design.id))

      if to_explain or to_submit:
        query_str = form.query.cleaned_data["query"]

        if conf.CHECK_PARTITION_CLAUSE_IN_QUERY.get():
          query_str = _strip_trailing_semicolon(query_str)
          # check query. if a select query on partitioned table without partition keys,
          # intercept it and raise a PopupException.
          _check_partition_clause_in_query(form.query.cleaned_data.get('database', None), query_str, db)

        # (Optional) Parameterization.
        parameterization = get_parameterization(request, query_str, form, design, to_explain)
        if parameterization:
          return parameterization

        try:
          query = HQLdesign(form, query_type=query_type)
          if to_explain:
            return explain_directly(request, query, design, query_server)
          else:
            download = request.POST.has_key('download')

            download_format = form.query.cleaned_data.get('download_format', None)
            if not download_format: download_format = None
            if download_format in common.DL_FORMATS:
              request.session['dl_status'] = True

            return execute_directly(request, query, query_server, design, on_success_url=on_success_url, download_format=download_format, download=download)
        except QueryServerException, ex:
          error_message, log = expand_exception(ex, db)
Exemple #10
0
def execute_query(request, design_id=None, table=None):
    authorized_get_design(request, design_id)
    error_message, log = None, None
    form = query_form()
    action = request.path
    design = safe_get_design(request, SavedQuery.HQL, design_id)
    on_success_url = request.REQUEST.get('on_success_url')

    for _ in range(1):
        if request.method == 'POST':
            form.bind(request.POST)
            to_explain = request.POST.has_key('button-explain')
            to_submit = request.POST.has_key('button-submit')
            # Always validate the saveform, which will tell us whether it needs explicit saving
            if not form.is_valid():
                break
            to_save = form.saveform.cleaned_data['save']
            to_saveas = form.saveform.cleaned_data['saveas']
            if to_saveas and not design.is_auto:
                # Save As only affects a previously saved query
                design = design.clone()
            if to_submit or to_save or to_saveas or to_explain:
                explicit_save = to_save or to_saveas
                design = save_design(request, form, SavedQuery.HQL, design,
                                     explicit_save)
                action = urlresolvers.reverse(execute_query,
                                              kwargs=dict(design_id=design.id))

            # We're not going to process the form. Simply re-render it.
            if not to_explain and not to_submit:
                break

            query_str = _strip_trailing_semicolon(
                form.query.cleaned_data["query"])
            query_server = db_utils.get_query_server(
                form.query_servers.cleaned_data["server"])
            # (Optional) Parameterization.
            parameterization = get_parameterization(request, query_str, form,
                                                    design, to_explain)
            if parameterization:
                return parameterization

            query_msg = make_beeswax_query(request, query_str, form)
            try:
                if to_explain:
                    return expl_d(request, query_str, query_msg, design,
                                  query_server)
                else:
                    notify = form.query.cleaned_data.get('email_notify', False)
                    return e_d(request,
                               query_msg,
                               design=design,
                               on_success_url=on_success_url,
                               notify=notify)
            except BeeswaxException, ex:
                error_message, log = expand_exception(ex)
                # Fall through to re-render the execute form.
        else:
            # GET request
            if design.id is not None:
                data = HQLdesign.loads(design.data).get_query_dict()
                form.bind(data)
                form.saveform.set_data(design.name, design.desc)
            else:
                # New design
                form.bind()
Exemple #11
0
def execute_directly(request,
                     query,
                     query_server=None,
                     design=None,
                     tablename=None,
                     on_success_url=None,
                     on_success_params=None,
                     download_format=None,
                     **kwargs):
    """
    execute_directly(request, query_msg, tablename, design) -> HTTP response for execution

    This method wraps around dbms.execute_query() to take care of the HTTP response
    after the execution.

      query
        The HQL model Query object.

      query_server
        To which Query Server to submit the query.
        Dictionary with keys: ['server_name', 'server_host', 'server_port'].

      design
        The design associated with the query.

      tablename
        The associated table name for the context.

      on_success_url
        Where to go after the query is done. The URL handler may expect an option "context" GET
        param. (See ``watch_query``.) For advanced usage, on_success_url can be a function, in
        which case the on complete URL is the return of:
          on_success_url(history_obj) -> URL string
        Defaults to the view results page.

      on_success_params
        Optional params to pass to the on_success_url (in additional to "context").

    Note that this may throw a Beeswax exception.
    """
    if design is not None:
        authorized_get_design(request, design.id)

    db = dbms.get(request.user, query_server)
    database = query.query.get('database')
    if database is None:
        database = _get_last_database(request)
    db.use(database)

    history_obj = db.execute_query(query, design)

    watch_url = urlresolvers.reverse(get_app_name(request) + ':watch_query',
                                     kwargs={
                                         'id': history_obj.id,
                                         'download_format': download_format
                                     })
    if 'download' in kwargs and kwargs['download']:
        watch_url += '?download=true'

    # Prepare the GET params for the watch_url
    get_dict = QueryDict(None, mutable=True)
    # (1) context
    if design:
        get_dict['context'] = make_query_context('design', design.id)
    elif tablename:
        get_dict['context'] = make_query_context(
            'table', '%s:%s' % (tablename, database))

    # (2) on_success_url
    if on_success_url:
        if callable(on_success_url):
            on_success_url = on_success_url(history_obj)
        get_dict['on_success_url'] = on_success_url

    # (3) misc
    if on_success_params:
        get_dict.update(on_success_params)

    return format_preserving_redirect(request, watch_url, get_dict)
Exemple #12
0
def execute_query(request, design_id=None):
    """
  View function for executing an arbitrary query.
  It understands the optional GET/POST params:

    on_success_url
      If given, it will be displayed when the query is successfully finished.
      Otherwise, it will display the view query results page by default.
  """
    authorized_get_design(request, design_id)

    request.session['start_time'] = time.time(
    )  # FIXME: add job id to not intersect simultaneous jobs
    error_message = None
    form = QueryForm()
    action = request.path
    log = None
    app_name = get_app_name(request)
    query_type = SavedQuery.TYPES_MAPPING['beeswax']
    design = safe_get_design(request, query_type, design_id)
    on_success_url = request.REQUEST.get('on_success_url')

    query_server = get_query_server_config(app_name)
    db = dbms.get(request.user, query_server)
    databases = _get_db_choices(request)

    if request.method == 'POST':
        form.bind(request.POST)
        form.query.fields[
            'database'].choices = databases  # Could not do it in the form

        to_explain = request.POST.has_key('button-explain')
        to_submit = request.POST.has_key('button-submit')

        # Always validate the saveform, which will tell us whether it needs explicit saving
        if form.is_valid():
            to_save = form.saveform.cleaned_data['save']
            to_saveas = form.saveform.cleaned_data['saveas']

            if to_save or to_saveas:
                if 'beeswax-autosave' in request.session:
                    del request.session['beeswax-autosave']

            if to_saveas and not design.is_auto:
                # Save As only affects a previously saved query
                design = design.clone()

            if to_submit or to_save or to_saveas or to_explain:
                explicit_save = to_save or to_saveas
                design = save_design(request, form, query_type, design,
                                     explicit_save)
                action = urlresolvers.reverse(app_name + ':execute_query',
                                              kwargs=dict(design_id=design.id))

            if to_explain or to_submit:
                query_str = form.query.cleaned_data["query"]

                if conf.CHECK_PARTITION_CLAUSE_IN_QUERY.get():
                    query_str = _strip_trailing_semicolon(query_str)
                    # check query. if a select query on partitioned table without partition keys,
                    # intercept it and raise a PopupException.
                    _check_partition_clause_in_query(
                        form.query.cleaned_data.get('database', None),
                        query_str, db)

                # (Optional) Parameterization.
                parameterization = get_parameterization(
                    request, query_str, form, design, to_explain)
                if parameterization:
                    return parameterization

                try:
                    query = HQLdesign(form, query_type=query_type)
                    if to_explain:
                        return explain_directly(request, query, design,
                                                query_server)
                    else:
                        download = request.POST.has_key('download')

                        download_format = form.query.cleaned_data.get(
                            'download_format', None)
                        if not download_format: download_format = None
                        if download_format in common.DL_FORMATS:
                            request.session['dl_status'] = True

                        return execute_directly(
                            request,
                            query,
                            query_server,
                            design,
                            on_success_url=on_success_url,
                            download_format=download_format,
                            download=download)
                except QueryServerException, ex:
                    error_message, log = expand_exception(ex, db)