示例#1
0
    def nextset(self):
        """Advance to the next result set.

    Returns:
      True if there was an available set to advance to, otherwise, None.

    Raises:
      InternalError: The cursor has been closed, or no statement has been
        executed yet.
      DatabaseError: A SQL exception occurred.
      OperationalError: RPC problem.
    """
        self._CheckOpen()
        self._CheckExecuted('nextset() called before execute')

        self._rows = collections.deque()
        self._rowcount = -1
        if not self._more_results:
            return None

        request = sql_pb2.ExecOpRequest()
        request.op.type = client_pb2.OpProto.NEXT_RESULT
        request.op.statement_id = self._statement_id
        self._HandleResult(self._conn.MakeRequest('ExecOp', request).result)
        return True
示例#2
0
  def ping(self, reconnect=False):
    """Checks whether or not the connection to the server is working.

    If it has gone down, an automatic reconnection is attempted.

    This function can be used by clients that remain idle for a long while, to
    check whether or not the server has closed the connection and reconnect if
    necessary.

    Non-standard. You should assume that ping() performs an implicit rollback;
    use only when starting a new transaction.  You have been warned.

    Args:
      reconnect: Whether to perform an automatic reconnection.

    Raises:
      DatabaseError: The connection to the server is not working.
    """
    self.CheckOpen()
    request = sql_pb2.ExecOpRequest()
    request.op.type = client_pb2.OpProto.PING
    try:
      self.MakeRequest('ExecOp', request)
    except DatabaseError:
      if not reconnect:
        raise
      self.OpenConnection()
示例#3
0
  def rollback(self):
    """Rolls back any pending transaction to the database.

    Raises:
      DatabaseError: A SQL exception occurred.
      OperationalError: RPC problem.
    """
    self.CheckOpen()
    request = sql_pb2.ExecOpRequest()
    request.op.type = client_pb2.OpProto.ROLLBACK
    self.MakeRequest('ExecOp', request)
示例#4
0
  def commit(self):
    """Commits any pending transaction to the database.

    Raises:
      DatabaseError: A SQL exception occurred.
      OperationalError: RPC problem.
    """
    self.CheckOpen()
    request = sql_pb2.ExecOpRequest()
    request.op.type = client_pb2.OpProto.COMMIT
    self.MakeRequest('ExecOp', request)
示例#5
0
  def autocommit(self, value):
    """Changes whether there is an implicit commit after each statement.

    By default, transactions must be explicitly committed.

    Args:
      value: A boolean.

    Raises:
      DatabaseError: A SQL exception occurred.
      OperationalError: RPC problem.
    """
    self.CheckOpen()
    request = sql_pb2.ExecOpRequest()
    request.op.type = client_pb2.OpProto.SET_AUTO_COMMIT
    request.op.auto_commit = value
    self.MakeRequest('ExecOp', request)
示例#6
0
    def _Retry(self, stub_method, request_id, absolute_deadline_seconds):
        """Retries request with the given request id.

    Continues to retry until either the deadline has expired or the response
    has been received.

    Args:
      stub_method: A string, the name of the original method that triggered the
                   retry.
      request_id: An integer, the request id used in the original request
      absolute_deadline_seconds: An integer, absolute deadline in seconds.

    Returns:
      A protobuf.

    Raises:
      DatabaseError: If the ExecOpResponse contains a SqlException that it not
                     related to retry.
      InternalError: If the ExceOpResponse is not valid.
    """
        request = sql_pb2.ExecOpRequest()
        request.op.type = client_pb2.OpProto.RETRY
        request.op.request_id = request_id
        request.connection_id = self._connection_id
        request.instance = self._instance
        while True:
            seconds_remaining = absolute_deadline_seconds - time.clock()
            if seconds_remaining <= 0:
                raise InternalError('Request [%d] timed out' % (request_id))
            time.sleep(min(self._retry_interval_seconds, seconds_remaining))
            self._idempotent_request_id += 1
            request.request_id = self._idempotent_request_id
            response = self.MakeRequestImpl('ExecOp', request)
            if not response.HasField('sql_exception'):
                return self._ConvertCachedResponse(stub_method, response)
            sql_exception = response.sql_exception
            if (sql_exception.application_error_code != client_error_code_pb2.
                    SqlServiceClientError.ERROR_RESPONSE_PENDING):
                raise DatabaseError('%d: %s' %
                                    (response.sql_exception.code,
                                     response.sql_exception.message))
示例#7
0
    def OpenConnection(self):
        """Opens an ApiProxy connection to speckle."""
        request = sql_pb2.OpenConnectionRequest()
        prop = request.property.add()
        prop.key = 'autoCommit'
        prop.value = 'false'
        if self._user:
            prop = request.property.add()
            prop.key = 'user'
            prop.value = self._user
        if self._password:
            prop = request.property.add()
            prop.key = 'password'
            prop.value = self._password

        response = self.MakeRequest('OpenConnection', request)

        self._connection_id = response.connection_id

        if self._database:
            request = sql_pb2.ExecOpRequest()
            request.op.type = client_pb2.OpProto.SET_CATALOG
            request.op.catalog = self._database
            self.MakeRequest('ExecOp', request)