Beispiel #1
0
    def autocomplete(self,
                     snippet,
                     database=None,
                     table=None,
                     column=None,
                     nested=None):
        from search.conf import SOLR_URL
        api = NativeSolrApi(SOLR_URL.get(), self.user.username)
        assist = Assist(self, self.user, api)
        response = {'status': -1}

        if database is None:
            response['databases'] = [
                self.options.get('collection') or snippet.get('database')
                or 'default'
            ]
        elif table is None:
            response['tables_meta'] = assist.get_tables(database)
        else:
            columns = assist.get_columns(database, table)
            response['columns'] = [col['name'] for col in columns]
            response['extended_columns'] = columns

        response['status'] = 0
        return response
Beispiel #2
0
    def get_sample_data(self,
                        snippet,
                        database=None,
                        table=None,
                        column=None,
                        is_async=False,
                        operation=None):
        from search.conf import SOLR_URL
        db = NativeSolrApi(SOLR_URL.get(), self.user)

        assist = Assist(self, self.user, db)
        response = {'status': -1}

        if snippet.get('source') == 'sql':
            sample_data = assist.get_sample_data_sql(database, table, column)
        else:
            sample_data = assist.get_sample_data(database, table, column)

        if sample_data:
            response['status'] = 0
            response['headers'] = sample_data['headers']
            response['full_headers'] = sample_data.get('full_headers')
            response['rows'] = sample_data['rows']
        else:
            response['message'] = _('Failed to get sample data.')

        return response
Beispiel #3
0
    def execute(self, notebook, snippet):
        from search.conf import SOLR_URL

        api = NativeSolrApi(SOLR_URL.get(), self.user.username)

        collection = self.options.get('collection') or snippet.get('database')
        if not collection or collection == 'default':
            collection = api.collections2()[0]

        response = api.sql(collection, snippet['statement'])

        info = response['result-set']['docs'].pop(
            -1)  # EOF, RESPONSE_TIME, EXCEPTION
        if info.get('EXCEPTION'):
            raise QueryError(info['EXCEPTION'])

        headers = []
        for row in response['result-set']['docs']:
            for col in list(row.keys()):
                if col not in headers:
                    headers.append(col)

        data = [[doc.get(col) for col in headers]
                for doc in response['result-set']['docs']]
        has_result_set = bool(data)

        return {
            'sync': True,
            'has_result_set': has_result_set,
            'modified_row_count': 0,
            'result': {
                'has_more':
                False,
                'data':
                data if has_result_set else [],
                'meta': [{
                    'name': col,
                    'type': '',
                    'comment': ''
                } for col in headers] if has_result_set else [],
                'type':
                'table'
            },
            'statement_id': 0,
            'has_more_statements': False,
            'statements_count': 1
        }
Beispiel #4
0
    def get_sample_data(self, snippet, database=None, table=None, column=None):
        from search.conf import SOLR_URL
        db = NativeSolrApi(SOLR_URL.get(), self.user)

        assist = Assist(self, self.user, db)
        response = {'status': -1}

        sample_data = assist.get_sample_data(database, table, column)

        if sample_data:
            response['status'] = 0
            response['headers'] = sample_data['headers']
            response['rows'] = sample_data['rows']
        else:
            response['message'] = _('Failed to get sample data.')

        return response
Beispiel #5
0
            response['columns'] = [col['name'] for col in columns]
            response['extended_columns'] = columns

        response['status'] = 0
        return response

    @query_error_handler
    def get_sample_data(self,
                        snippet,
                        database=None,
                        table=None,
                        column=None,
                        async=False,
                        operation=None):
        from search.conf import SOLR_URL
        db = NativeSolrApi(SOLR_URL.get(), self.user)

        assist = Assist(self, self.user, db)
        response = {'status': -1}

        if snippet.get('source') == 'sql':
            sample_data = assist.get_sample_data_sql(database, table, column)
        else:
            sample_data = assist.get_sample_data(database, table, column)

        if sample_data:
            response['status'] = 0
            response['headers'] = sample_data['headers']
            response['full_headers'] = sample_data.get('full_headers')
            response['rows'] = sample_data['rows']
        else:
Beispiel #6
0
class SolrApi(Api):
    def __init__(self, user, interpreter=None):
        Api.__init__(self, user, interpreter=interpreter)
        self.options = interpreter['options']

    @query_error_handler
    def execute(self, notebook, snippet):
        from search.conf import SOLR_URL

        api = NativeSolrApi(SOLR_URL.get(), self.user.username)

        collection = self.options.get('collection') or snippet.get('database')
        if not collection or collection == 'default':
            collection = api.collections2()[0]

        response = api.sql(collection, snippet['statement'])

        info = response['result-set']['docs'].pop(
            -1)  # EOF, RESPONSE_TIME, EXCEPTION
        if info.get('EXCEPTION'):
            raise QueryError(info['EXCEPTION'])

        headers = []
        for row in response['result-set']['docs']:
            for col in row.keys():
                if col not in headers:
                    headers.append(col)

        data = [[doc.get(col) for col in headers]
                for doc in response['result-set']['docs']]
        has_result_set = bool(data)

        return {
            'sync': True,
            'has_result_set': has_result_set,
            'modified_row_count': 0,
            'result': {
                'has_more':
                False,
                'data':
                data if has_result_set else [],
                'meta': [{
                    'name': col,
                    'type': '',
                    'comment': ''
                } for col in headers] if has_result_set else [],
                'type':
                'table'
            },
            'statement_id': 0,
            'has_more_statements': False,
            'statements_count': 1
        }

    @query_error_handler
    def check_status(self, notebook, snippet):
        return {'status': 'available'}

    @query_error_handler
    def fetch_result(self, notebook, snippet, rows, start_over):
        return {'has_more': False, 'data': [], 'meta': [], 'type': 'table'}

    @query_error_handler
    def fetch_result_metadata(self):
        pass

    @query_error_handler
    def cancel(self, notebook, snippet):
        return {'status': 0}

    @query_error_handler
    def get_log(self, notebook, snippet, startFrom=None, size=None):
        return 'No logs'

    def download(self, notebook, snippet, format):
        raise PopupException('Downloading is not supported yet')

    @query_error_handler
    def close_statement(self, snippet):
        return {'status': -1}

    @query_error_handler
    def autocomplete(self,
                     snippet,
                     database=None,
                     table=None,
                     column=None,
                     nested=None):
        from search.conf import SOLR_URL
        api = NativeSolrApi(SOLR_URL.get(), self.user.username)
        assist = Assist(self, self.user, api)
        response = {'status': -1}

        if database is None:
            response['databases'] = [
                self.options.get('collection') or snippet.get('database')
                or 'default'
            ]
        elif table is None:
            response['tables_meta'] = assist.get_tables(database)
        else:
            columns = assist.get_columns(database, table)
            response['columns'] = [col['name'] for col in columns]
            response['extended_columns'] = columns

        response['status'] = 0
        return response

    @query_error_handler
    def get_sample_data(self,
                        snippet,
                        database=None,
                        table=None,
                        column=None,
                        async=False):
        from search.conf import SOLR_URL
        db = NativeSolrApi(SOLR_URL.get(), self.user)

        assist = Assist(self, self.user, db)
        response = {'status': -1}

        if snippet.get('source') == 'sql':
            sample_data = assist.get_sample_data_sql(database, table, column)
        else:
            sample_data = assist.get_sample_data(database, table, column)

        if sample_data:
            response['status'] = 0
            response['headers'] = sample_data['headers']
            response['full_headers'] = sample_data.get('full_headers')
            response['rows'] = sample_data['rows']
        else:
            response['message'] = _('Failed to get sample data.')

        return response