Exemplo n.º 1
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()
Exemplo n.º 2
0
def read_table(request, table):
  """View function for select * from table"""
  table_obj = db_utils.meta_client().get_table("default", table)
  hql = "SELECT * FROM `%s` %s" % (table, _get_browse_limit_clause(table_obj))
  query_msg = make_beeswax_query(request, hql)
  try:
    return execute_directly(request, query_msg, tablename=table)
  except BeeswaxException, e:
    # Note that this state is difficult to get to.
    error_message, log = expand_exception(e)
    error = _("Failed to read table. Error: %(error)s") % {'error': error_message}
    raise PopupException(error, title=_("Beeswax Error"), detail=log)
Exemplo n.º 3
0
def read_table(request, table):
    """View function for select * from table"""
    table_obj = db_utils.meta_client().get_table("default", table)
    hql = "SELECT * FROM `%s` %s" % (table,
                                     _get_browse_limit_clause(table_obj))
    query_msg = make_beeswax_query(request, hql)
    try:
        return execute_directly(request, query_msg, tablename=table)
    except BeeswaxException, e:
        # Note that this state is difficult to get to.
        error_message, log = expand_exception(e)
        error = _("Failed to read table. Error: %(error)s") % {
            'error': error_message
        }
        raise PopupException(error, title=_("Beeswax Error"), detail=log)
Exemplo n.º 4
0
def _submit_create_and_load(request, create_hql, table_name, path, do_load):
  """
  Submit the table creation, and setup the load to happen (if ``do_load``).
  """
  on_success_params = { }
  if do_load:
    on_success_params['table'] = table_name
    on_success_params['path'] = path
    on_success_url = urlresolvers.reverse(beeswax.create_table.load_after_create)
  else:
    on_success_url = urlresolvers.reverse(describe_table, kwargs={'table': table_name})

  query_msg = make_beeswax_query(request, create_hql)
  return execute_directly(request, query_msg,
                          on_success_url=on_success_url,
                          on_success_params=on_success_params)
Exemplo n.º 5
0
def _submit_create_and_load(request, create_hql, table_name, path, do_load):
    """
  Submit the table creation, and setup the load to happen (if ``do_load``).
  """
    on_success_params = {}
    if do_load:
        on_success_params['table'] = table_name
        on_success_params['path'] = path
        on_success_url = urlresolvers.reverse(
            beeswax.create_table.load_after_create)
    else:
        on_success_url = urlresolvers.reverse(describe_table,
                                              kwargs={'table': table_name})

    query_msg = make_beeswax_query(request, create_hql)
    return execute_directly(request,
                            query_msg,
                            on_success_url=on_success_url,
                            on_success_params=on_success_params)
Exemplo n.º 6
0
def load_after_create(request):
  """
  Automatically load data into a newly created table.

  We get here from the create's on_success_url, and expect to find
  ``table`` and ``path`` from the parameters.
  """
  tablename = request.REQUEST.get('table')
  path = request.REQUEST.get('path')
  if not tablename or not path:
    msg = 'Internal error: Missing needed parameter to load data into table'
    LOG.error(msg)
    raise PopupException(msg)

  LOG.debug("Auto loading data from %s into table %s" % (path, tablename))
  hql = "LOAD DATA INPATH '%s' INTO TABLE `%s`" % (path, tablename)
  query_msg = make_beeswax_query(request, hql)
  on_success_url = urlresolvers.reverse(describe_table, kwargs={'table': tablename})

  return execute_directly(request, query_msg, on_success_url=on_success_url)
Exemplo n.º 7
0
def load_after_create(request):
    """
  Automatically load data into a newly created table.

  We get here from the create's on_success_url, and expect to find
  ``table`` and ``path`` from the parameters.
  """
    tablename = request.REQUEST.get('table')
    path = request.REQUEST.get('path')
    if not tablename or not path:
        msg = 'Internal error: Missing needed parameter to load data into table'
        LOG.error(msg)
        raise PopupException(msg)

    LOG.debug("Auto loading data from %s into table %s" % (path, tablename))
    hql = "LOAD DATA INPATH '%s' INTO TABLE `%s`" % (path, tablename)
    query_msg = make_beeswax_query(request, hql)
    on_success_url = urlresolvers.reverse(describe_table,
                                          kwargs={'table': tablename})

    return execute_directly(request, query_msg, on_success_url=on_success_url)
Exemplo n.º 8
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()