Esempio n. 1
0
 def start_transaction(self):
     request = prestodb.client.PrestoRequest(
         self.host,
         self.port,
         self.user,
         self.source,
         self.catalog,
         self.schema,
         self.session_properties,
         self.http_headers,
         NO_TRANSACTION,
         self.http_scheme,
         self.auth,
         self.redirect_handler,
         self.max_attempts,
         self.request_timeout,
     )
     self._transaction = Transaction(request)
     self._transaction.begin()
     return self._transaction
Esempio n. 2
0
class Connection(object):
    """Presto supports transactions and the ability to either commit or rollback
    a sequence of SQL statements. A single query i.e. the execution of a SQL
    statement, can also be cancelled. Transactions are not supported by this
    client implementation yet.

    """
    def __init__(
        self,
        host,
        port=constants.DEFAULT_PORT,
        user=None,
        password=None,
        resource_group="default",
        source=constants.DEFAULT_SOURCE,
        catalog=constants.DEFAULT_CATALOG,
        schema=constants.DEFAULT_SCHEMA,
        session_properties=None,
        http_headers=None,
        http_scheme=constants.HTTP,
        auth=constants.DEFAULT_AUTH,
        redirect_handler=prestodb.redirect.GatewayRedirectHandler(),
        max_attempts=constants.DEFAULT_MAX_ATTEMPTS,
        request_timeout=constants.DEFAULT_REQUEST_TIMEOUT,
        isolation_level=IsolationLevel.AUTOCOMMIT,
    ):
        self.host = host
        self.port = port
        self.user = user
        self.password = password
        self.resource_group = resource_group
        self.source = source
        self.catalog = catalog
        self.schema = schema
        self.session_properties = session_properties
        # mypy cannot follow module import
        self._http_session = prestodb.client.PrestoRequest.http.Session()
        self.http_headers = http_headers
        self.http_scheme = http_scheme
        self.auth = auth
        self.redirect_handler = redirect_handler
        self.max_attempts = max_attempts
        self.request_timeout = request_timeout

        self._isolation_level = isolation_level
        self._request = None
        self._transaction = None

    @property
    def isolation_level(self):
        return self._isolation_level

    @property
    def transaction(self):
        return self._transaction

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        try:
            self.commit()
        except Exception:
            self.rollback()
        else:
            self.close()

    def close(self):
        """Presto does not have anything to close"""
        # TODO cancel outstanding queries?
        pass

    def start_transaction(self):
        self._transaction = Transaction(self._create_request())
        self._transaction.begin()
        return self._transaction

    def commit(self):
        if self.transaction is None:
            return
        self._transaction.commit()
        self._transaction = None

    def rollback(self):
        if self.transaction is None:
            raise RuntimeError('no transaction was started')
        self._transaction.rollback()
        self._transaction = None

    def _create_request(self):
        return prestodb.client.PrestoRequest(
            self.host,
            self.port,
            self.user,
            self.password,
            self.resource_group,
            self.source,
            self.catalog,
            self.schema,
            self.session_properties,
            self._http_session,
            self.http_headers,
            NO_TRANSACTION,
            self.http_scheme,
            self.auth,
            self.redirect_handler,
            self.max_attempts,
            self.request_timeout,
        )

    def cursor(self):
        """Return a new :py:class:`Cursor` object using the connection."""
        if self.isolation_level != IsolationLevel.AUTOCOMMIT:
            if self.transaction is None:
                self.start_transaction()
            request = self.transaction._request
        else:
            request = self._create_request()
        return Cursor(self, request)
Esempio n. 3
0
 def start_transaction(self):
     self._transaction = Transaction(self._create_request())
     self._transaction.begin()
     return self._transaction