def fetch_results(request, id, first_row=0): """ Returns the results of the QueryHistory with the given id. The query results MUST be ready. If ``first_row`` is 0, restarts (if necessary) the query read. Otherwise, just spits out a warning if first_row doesn't match the servers conception. Multiple readers will produce a confusing interaction here, and that's known. """ first_row = long(first_row) results = type('Result', (object,), { 'rows': 0, 'columns': [], 'has_more': False, 'start_row': 0, }) fetch_error = False error_message = '' query_history = authorized_get_history(request, id, must_exist=True) query_server = query_history.get_query_server_config() design = SQLdesign.loads(query_history.design.data) db = dbms.get(request.user, query_server) try: database = design.query.get('database', 'default') db.use(database) datatable = db.execute_and_wait(design) results = db.client.create_result(datatable) status = 0 except Exception, e: fetch_error = True error_message = str(e) status = -1
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:
def design_to_dict(design): sql_design = SQLdesign.loads(design.data) return { 'id': design.id, 'query': sql_design.sql_query, 'name': design.name, 'desc': design.desc, 'server': sql_design.server, 'database': sql_design.database }
def design_to_dict(design): sql_design = SQLdesign.loads(design.data) return { "id": design.id, "query": sql_design.sql_query, "name": design.name, "desc": design.desc, "server": sql_design.server, "database": sql_design.database, }
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: