コード例 #1
0
def explain_query(request):
  response = {'status': -1, 'message': ''}

  if request.method != 'POST':
    response['message'] = _('A POST request is required.')
  
  app_name = get_app_name(request)
  query_type = beeswax_models.SavedQuery.TYPES_MAPPING[app_name]

  try:
    form = get_query_form(request)

    if form.is_valid():
      query = SQLdesign(form, query_type=query_type)
      query_server = dbms.get_query_server_config(app_name)
      db = dbms.get(request.user, query_server)

      try:
        db.use(form.cleaned_data['database'])
        datatable = db.explain(query)
        results = db.client.create_result(datatable)

        response['status'] = 0
        response['results'] = results_to_dict(results)
      except Exception, e:
        response['status'] = -1
        response['message'] = str(e)

    else:
コード例 #2
0
ファイル: api.py プロジェクト: devinshields/hue
def databases(request, server):
  query_server = dbms.get_query_server_config(server)

  if not query_server:
    raise Http404
  
  db = dbms.get(request.user, query_server)

  response = {
    'databases': db.get_databases()
  }

  return HttpResponse(json.dumps(response), mimetype="application/json")
コード例 #3
0
def tables(request, server, database):
  query_server = dbms.get_query_server_config(server)

  if not query_server:
    raise Http404

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

  response = {
    'tables': db.get_tables(database)
  }

  return HttpResponse(json.dumps(response), mimetype="application/json")
コード例 #4
0
def databases(request, server):
  query_server = dbms.get_query_server_config(server)

  if not query_server:
    raise Http404

  db = dbms.get(request.user, query_server)

  response = {
    'status': 0,
    'databases': db.get_databases()
  }

  return HttpResponse(json.dumps(response), mimetype="application/json")
コード例 #5
0
ファイル: api.py プロジェクト: gitluochao/hue
def columns(request, server, database, table):
  query_server = dbms.get_query_server_config(server)

  if not query_server:
    raise Http404

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

  response = {
    'columns': db.get_columns(database, table)
  }

  return HttpResponse(json.dumps(response), mimetype="application/json")
コード例 #6
0
def get_query_form(request, design_id=None):
  servers = conf.get_server_choices()

  # Get database choices
  query_server = dbms.get_query_server_config(request.POST.get('server'))

  if not query_server:
    raise RuntimeError(_("Server specified doesn't exist."))
  
  db = dbms.get(request.user, query_server)
  databases = [(database, database) for database in db.get_databases()]

  if not databases:
    raise RuntimeError(_("No databases are available. Permissions could be missing."))

  form = SQLForm(request.POST)
  form.fields['server'].choices = servers # Could not do it in the form
  form.fields['database'].choices = databases # Could not do it in the form

  return form
コード例 #7
0
def execute_query(request, design_id=None):
  response = {'status': -1, 'message': ''}

  if request.method != 'POST':
    response['message'] = _('A POST request is required.')
  
  app_name = get_app_name(request)
  query_type = beeswax_models.SavedQuery.TYPES_MAPPING[app_name]
  design = safe_get_design(request, query_type, design_id)

  try:
    form = get_query_form(request, design_id)

    if form.is_valid():
      design = save_design(request, SaveForm(), form, query_type, design)

      query = SQLdesign(form, query_type=query_type)
      query_server = dbms.get_query_server_config(request.POST.get('server'))
      db = dbms.get(request.user, query_server)
      query_history = db.execute_query(query, design)
      query_history.last_state = beeswax_models.QueryHistory.STATE.expired.index
      query_history.save()

      try:
        db.use(form.cleaned_data['database'])
        datatable = db.execute_and_wait(query)
        results = db.client.create_result(datatable)

        response['status'] = 0
        response['results'] = results_to_dict(results)
        response['design'] = design.id
      except Exception, e:
        response['status'] = -1
        response['message'] = str(e)

    else: