Exemple #1
0
    def execute(self, script, params={}, isolate=True, transaction=True, pretty=False):
        """
        executes the given gremlin script with the provided parameters

        :param script: the gremlin script to isolate
        :type script: string
        :param params: the parameters to execute the script with
        :type params: dictionary
        :param isolate: wraps the script in a closure so any variables set aren't persisted for the next execute call
        :type isolate: bool
        :param transaction: query will be wrapped in a transaction if set to True (default)
        :type transaction: bool
        :param pretty: will dedent the script if set to True
        :type pretty: bool

        :rtype: list
        """
        self._conn.send_message(
            messages.ScriptRequest(
                script=script,
                params=params,
                session_key=self._session_key,
                isolate=isolate,
                in_transaction=transaction
            )
        )
        response = self._conn.get_response()

        if isinstance(response, messages.ErrorResponse):
            raise exceptions.RexProScriptException(response.message)

        return response.results
Exemple #2
0
    def execute(self, script, params={}, isolate=True, transaction=True):
        """
        executes the given gremlin script with the provided parameters

        :param script: the gremlin script to isolate
        :type script: str
        :param params: the parameters to execute the script with
        :type params: dictionary
        :param isolate: wraps the script in a closure so any variables set aren't persisted for the next execute call
        :type isolate: bool
        :param transaction: query will be wrapped in a transaction if set to True (default)
        :type transaction: bool

        :rtype: list
        """
        for i in range(CONNECTION_ATTEMPTS):
            try:
                if self._in_transaction:
                    transaction = False

                import traceback

                self._conn.send_message(
                    messages.ScriptRequest(
                        script=script,
                        params=params,
                        session_key=self._session_key,
                        isolate=isolate,
                        in_transaction=transaction,
                    )
                )
                response = self._conn.get_response()

                if isinstance(response, messages.ErrorResponse):
                    response.raise_exception()
                return response.results

            except Exception, e:
                self.connection_failed()

                # RexProInvalidSessionException happens frequently when a session is idle for too long so not logging an error
                log_handler = logger.info if isinstance(e, RexProInvalidSessionException) else logger.error
                log_handler("Received an exception executing query! {}".format(e))

                if i >= CONNECTION_ATTEMPTS - 1:
                    raise
                else:
                    self.close()
                    self.open()
Exemple #3
0
    def execute(self,
                script,
                params=None,
                isolate=True,
                transaction=True,
                language=messages.ScriptRequest.Language.GROOVY):
        """
        executes the given gremlin script with the provided parameters

        :param script: the gremlin script to isolate
        :type script: str
        :param params: the parameters to execute the script with
        :type params: dictionary
        :param isolate: wraps the script in a closure so any variables set aren't persisted for the next execute call
        :type isolate: bool
        :param transaction: query will be wrapped in a transaction if set to True (default)
        :type transaction: bool
        :param language: the script language that should be used (defaults to groovy)
        :type language: str

        :rtype: list
        """
        if self._in_transaction:
            transaction = False

        self._conn.send_message(
            messages.ScriptRequest(
                script=script,
                params=params or {},
                in_session=False if self.session_less else True,
                session_key=None if self.session_less else self._session_key,
                isolate=isolate,
                in_transaction=transaction,
                language=language,
                graph_name=self.graph_name if self.session_less else None,
                graph_obj_name=self.graph_obj_name
                if self.session_less else None))
        response = self._conn.get_response()

        if isinstance(response, messages.ErrorResponse):
            response.raise_exception()

        return response.results