Beispiel #1
0
def run_query(data_source, parameter_values, query_text, query_id, max_age=0):
    query_parameters = set(collect_query_parameters(query_text))
    missing_params = set(query_parameters) - set(parameter_values.keys())
    if missing_params:
        return {
            'job': {
                'status':
                4,
                'error':
                'Missing parameter value for: {}'.format(
                    ", ".join(missing_params))
            }
        }, 400

    if query_parameters:
        query_text = pystache.render(query_text, parameter_values)

    if max_age == 0:
        query_result = None
    else:
        query_result = models.QueryResult.get_latest(data_source, query_text,
                                                     max_age)

    if query_result:
        return {'query_result': query_result.to_dict()}
    else:
        job = QueryTask.add_task(query_text,
                                 data_source,
                                 metadata={
                                     "Username": current_user.name,
                                     "Query ID": query_id
                                 })
        return {'job': job.to_dict()}
Beispiel #2
0
    def post(self):
        params = request.get_json(force=True)
        data_source = models.DataSource.get_by_id_and_org(params.get('data_source_id'), self.current_org)

        if not has_access(data_source.groups, self.current_user, not_view_only):
            return {'job': {'status': 4, 'error': 'You do not have permission to run queries with this data source.'}}, 403

        self.record_event({
            'action': 'execute_query',
            'timestamp': int(time.time()),
            'object_id': data_source.id,
            'object_type': 'data_source',
            'query': params['query']
        })

        max_age = int(params.get('max_age', -1))

        if max_age == 0:
            query_result = None
        else:
            query_result = models.QueryResult.get_latest(data_source, params['query'], max_age)

        if query_result:
            return {'query_result': query_result.to_dict()}
        else:
            query_id = params.get('query_id', 'adhoc')
            job = QueryTask.add_task(params['query'], data_source,
                                     metadata={"Username": self.current_user.name, "Query ID": query_id})
            return {'job': job.to_dict()}
Beispiel #3
0
    def post(self):
        params = request.get_json(force=True)
        data_source = models.DataSource.get_by_id_and_org(params.get('data_source_id'), self.current_org)

        if not has_access(data_source.groups, self.current_user, not_view_only):
            return {'job': {'status': 4, 'error': 'You do not have permission to run queries with this data source.'}}, 403

        self.record_event({
            'action': 'execute_query',
            'timestamp': int(time.time()),
            'object_id': data_source.id,
            'object_type': 'data_source',
            'query': params['query']
        })

        max_age = int(params.get('max_age', -1))

        if max_age == 0:
            query_result = None
        else:
            query_result = models.QueryResult.get_latest(data_source, params['query'], max_age)

        if query_result:
            return {'query_result': query_result.to_dict()}
        else:
            query_id = params.get('query_id', 'adhoc')
            job = QueryTask.add_task(params['query'], data_source,
                                     metadata={"Username": self.current_user.name, "Query ID": query_id})
            return {'job': job.to_dict()}
Beispiel #4
0
    def post(self):
        params = request.get_json(force=True)

        if settings.FEATURE_TABLES_PERMISSIONS:
            metadata = utils.SQLMetaData(params["query"])

            if metadata.has_non_select_dml_statements or metadata.has_ddl_statements:
                return {"job": {"error": "Only SELECT statements are allowed"}}

            if len(metadata.used_tables - current_user.allowed_tables) > 0 and "*" not in current_user.allowed_tables:
                logging.warning(
                    "Permission denied for user %s to table %s", self.current_user.name, metadata.used_tables
                )
                return {"job": {"error": "Access denied for table(s): %s" % (metadata.used_tables)}}

        models.ActivityLog(
            user=self.current_user, type=models.ActivityLog.QUERY_EXECUTION, activity=params["query"]
        ).save()

        max_age = int(params.get("max_age", -1))

        if max_age == 0:
            query_result = None
        else:
            query_result = models.QueryResult.get_latest(params["data_source_id"], params["query"], max_age)

        if query_result:
            return {"query_result": query_result.to_dict()}
        else:
            data_source = models.DataSource.get_by_id(params["data_source_id"])
            query_id = params.get("query_id", "adhoc")
            job = QueryTask.add_task(
                params["query"], data_source, metadata={"Username": self.current_user.name, "Query ID": query_id}
            )
            return {"job": job.to_dict()}
Beispiel #5
0
    def post(self):
        params = request.get_json(force=True)

        if settings.FEATURE_TABLES_PERMISSIONS:
            metadata = utils.SQLMetaData(params['query'])

            if metadata.has_non_select_dml_statements or metadata.has_ddl_statements:
                return {'job': {'error': 'Only SELECT statements are allowed'}}

            if len(metadata.used_tables - current_user.allowed_tables
                   ) > 0 and '*' not in current_user.allowed_tables:
                logging.warning('Permission denied for user %s to table %s',
                                self.current_user.name, metadata.used_tables)
                return {
                    'job': {
                        'error':
                        'Access denied for table(s): %s' %
                        (metadata.used_tables)
                    }
                }

        models.ActivityLog(user=self.current_user,
                           type=models.ActivityLog.QUERY_EXECUTION,
                           activity=params['query']).save()

        max_age = int(params.get('max_age', -1))

        if max_age == 0:
            query_result = None
        else:
            query_result = models.QueryResult.get_latest(
                params['data_source_id'], params['query'], max_age)

        if query_result:
            return {'query_result': query_result.to_dict()}
        else:
            data_source = models.DataSource.get_by_id(params['data_source_id'])
            query_id = params.get('query_id', 'adhoc')
            job = QueryTask.add_task(params['query'],
                                     data_source,
                                     metadata={
                                         "Username": self.current_user.name,
                                         "Query ID": query_id
                                     })
            return {'job': job.to_dict()}
Beispiel #6
0
    def post(self):
        params = request.json

        if settings.FEATURE_TABLES_PERMISSIONS:
            metadata = utils.SQLMetaData(params['query'])

            if metadata.has_non_select_dml_statements or metadata.has_ddl_statements:
                return {
                    'job': {
                        'error': 'Only SELECT statements are allowed'
                    }
                }

            if len(metadata.used_tables - current_user.allowed_tables) > 0 and '*' not in current_user.allowed_tables:
                logging.warning('Permission denied for user %s to table %s', self.current_user.name, metadata.used_tables)
                return {
                    'job': {
                        'error': 'Access denied for table(s): %s' % (metadata.used_tables)
                    }
                }

        models.ActivityLog(
            user=self.current_user,
            type=models.ActivityLog.QUERY_EXECUTION,
            activity=params['query']
        ).save()

        max_age = int(params['max_age'])

        if max_age == 0:
            query_result = None
        else:
            query_result = models.QueryResult.get_latest(params['data_source_id'], params['query'], max_age)

        if query_result:
            return {'query_result': query_result.to_dict()}
        else:
            data_source = models.DataSource.get_by_id(params['data_source_id'])
            query_id = params.get('query_id', 'adhoc')
            job = QueryTask.add_task(params['query'], data_source, metadata={"Username": self.current_user.name, "Query ID": query_id})
            return {'job': job.to_dict()}
def run_query(data_source, parameter_values, query_text, query_id, max_age=0):
    query_parameters = set(collect_query_parameters(query_text))
    missing_params = set(query_parameters) - set(parameter_values.keys())
    if missing_params:
        return {'job': {'status': 4,
                        'error': 'Missing parameter value for: {}'.format(", ".join(missing_params))}}, 400

    if query_parameters:
        query_text = pystache.render(query_text, parameter_values)

    if max_age == 0:
        query_result = None
    else:
        query_result = models.QueryResult.get_latest(data_source, query_text, max_age)

    if query_result:
        return {'query_result': query_result.to_dict()}
    else:
        job = QueryTask.add_task(query_text, data_source,
                                 metadata={"Username": current_user.name, "Query ID": query_id})
        return {'job': job.to_dict()}