示例#1
0
    def _run_sql_query(self, sql):
        if not self.utils.sql_url:
            Log.error("This test requires a `sql_url` parameter in the settings file")

        test = Data(data=simple_test_data)
        self.utils.fill_container(test)
        sql = sql.replace(TEST_TABLE, test.query['from'])

        url = URL(self.utils.sql_url)
        response = self.utils.post_till_response(str(url), json={"meta": {"testing": True}, "sql": sql})
        self.assertEqual(response.status_code, 200)
        return json2value(utf82unicode(response.all_content))
示例#2
0
文件: sql.py 项目: mars-f/ActiveData
def sql_query(path):
    with RegisterThread():
        query_timer = Timer("total duration")
        request_body = None
        try:
            with query_timer:
                preamble_timer = Timer("preamble", silent=True)
                with preamble_timer:
                    if flask.request.headers.get("content-length",
                                                 "") in ["", "0"]:
                        # ASSUME A BROWSER HIT THIS POINT, SEND text/html RESPONSE BACK
                        return Response(BLANK,
                                        status=400,
                                        headers={"Content-Type": "text/html"})
                    elif int(flask.request.headers["content-length"]
                             ) > QUERY_SIZE_LIMIT:
                        Log.error("Query is too large")

                    request_body = flask.request.get_data().strip()
                    text = utf82unicode(request_body)
                    data = json2value(text)
                    record_request(flask.request, data, None, None)

                translate_timer = Timer("translate", silent=True)
                with translate_timer:
                    if not data.sql:
                        Log.error("Expecting a `sql` parameter")
                    jx_query = parse_sql(data.sql)
                    frum = find_container(jx_query['from'])
                    if data.meta.testing:
                        test_mode_wait(jx_query)
                    result = jx.run(jx_query, container=frum)
                    if isinstance(
                            result, Container
                    ):  # TODO: REMOVE THIS CHECK, jx SHOULD ALWAYS RETURN Containers
                        result = result.format(jx_query.format)
                    result.meta.jx_query = jx_query

                save_timer = Timer("save")
                with save_timer:
                    if data.meta.save:
                        try:
                            result.meta.saved_as = save_query.query_finder.save(
                                data)
                        except Exception as e:
                            Log.warning("Unexpected save problem", cause=e)

                result.meta.timing.preamble = mo_math.round(
                    preamble_timer.duration.seconds, digits=4)
                result.meta.timing.translate = mo_math.round(
                    translate_timer.duration.seconds, digits=4)
                result.meta.timing.save = mo_math.round(
                    save_timer.duration.seconds, digits=4)
                result.meta.timing.total = "{{TOTAL_TIME}}"  # TIMING PLACEHOLDER

                with Timer("jsonification", silent=True) as json_timer:
                    response_data = unicode2utf8(value2json(result))

            with Timer("post timer", silent=True):
                # IMPORTANT: WE WANT TO TIME OF THE JSON SERIALIZATION, AND HAVE IT IN THE JSON ITSELF.
                # WE CHEAT BY DOING A (HOPEFULLY FAST) STRING REPLACEMENT AT THE VERY END
                timing_replacement = b'"total": ' + str(mo_math.round(query_timer.duration.seconds, digits=4)) +\
                                     b', "jsonification": ' + str(mo_math.round(json_timer.duration.seconds, digits=4))
                response_data = response_data.replace(
                    b'"total":"{{TOTAL_TIME}}"', timing_replacement)
                Log.note("Response is {{num}} bytes in {{duration}}",
                         num=len(response_data),
                         duration=query_timer.duration)

                return Response(
                    response_data,
                    status=200,
                    headers={"Content-Type": result.meta.content_type})
        except Exception as e:
            e = Except.wrap(e)
            return send_error(query_timer, request_body, e)