Esempio n. 1
0
    def __init__(self,
                 host,
                 port,
                 user,
                 password,
                 autocommit=False,
                 encrypt=True,
                 encrypt_verify=False,
                 currentschema=None,
                 timeout=None):
        self.host = host
        self.port = port
        self.user = user
        self.currentschema = currentschema

        self.autocommit = autocommit
        self.product_version = None
        self.protocol_version = None

        self.encrypt = (encrypt == 'true')
        self.encrypt_verify = (encrypt_verify == 'true')

        self.session_id = -1
        self.packet_count = -1

        self._socket = None
        self._timeout = timeout
        self._auth_manager = AuthManager(self, user, password)
        # It feels like the RLock has a poorer performance
        self._socket_lock = threading.RLock()
        self._packet_count_lock = threading.Lock()
Esempio n. 2
0
def auth_manager():
    manager = AuthManager(None, "TestUser", "secret")
    manager.client_key = b"\xed\xbd\x7c\xc8\xb2\xf2\x64\x89\xd6\x5a\x7c\xd5" \
                         b"\x1e\x27\xf2\xe7\x3f\xca\x22\x7d\x1a\xb6\xaa\xfc" \
                         b"\xac\x0f\x42\x8c\xa4\xd8\xe1\x0c\x19\xe3\xe3\x8f" \
                         b"\x3a\xac\x51\x07\x5e\x67\xbb\xe5\x2f\xdb\x61\x03" \
                         b"\xa7\xc3\x4c\x8a\x70\x90\x8e\xd5\xbe\x0b\x35\x42" \
                         b"\x70\x5f\x73\x8c"
    return manager
Esempio n. 3
0
def auth_manager():
    manager = AuthManager(None, "TestUser", "secret")
    manager.client_key = b"\xed\xbd\x7c\xc8\xb2\xf2\x64\x89\xd6\x5a\x7c\xd5" \
                         b"\x1e\x27\xf2\xe7\x3f\xca\x22\x7d\x1a\xb6\xaa\xfc" \
                         b"\xac\x0f\x42\x8c\xa4\xd8\xe1\x0c\x19\xe3\xe3\x8f" \
                         b"\x3a\xac\x51\x07\x5e\x67\xbb\xe5\x2f\xdb\x61\x03" \
                         b"\xa7\xc3\x4c\x8a\x70\x90\x8e\xd5\xbe\x0b\x35\x42" \
                         b"\x70\x5f\x73\x8c"
    return manager
Esempio n. 4
0
    def __init__(self, host, port, user, password, autocommit=False, reconnect=False, timeout=None):
        self.host = host
        self.port = port
        self.user = user

        self.autocommit = autocommit
        self.product_version = None
        self.protocol_version = None

        self._socket = None
        self._reconnect = reconnect
        self._timeout = timeout
        self._auth_manager = AuthManager(self, user, password)
        # It feels like the RLock has a poorer performance
        self._socket_lock = threading.RLock()
        self._packet_count_lock = threading.Lock()

        self._reset_session()
Esempio n. 5
0
    def __init__(self, host, port, user, password, autocommit=False, timeout=None):
        self.host = host
        self.port = port

        self.autocommit = autocommit
        self.product_version = None
        self.protocol_version = None

        self.session_id = -1
        self.packet_count = -1

        self._socket = None
        self._timeout = timeout
        self._auth_manager = AuthManager(self, user, password)
        # It feels like the RLock has a poorer performance
        self._socket_lock = threading.RLock()
        self._packet_count_lock = threading.Lock()
Esempio n. 6
0
class Connection(object):
    """
    Database connection class
    """
    def __init__(self,
                 host,
                 port,
                 user,
                 password,
                 autocommit=False,
                 timeout=None):
        self.host = host
        self.port = port
        self.user = user

        self.autocommit = autocommit
        self.product_version = None
        self.protocol_version = None

        self.session_id = -1
        self.packet_count = -1

        self._socket = None
        self._timeout = timeout
        self._auth_manager = AuthManager(self, user, password)
        # It feels like the RLock has a poorer performance
        self._socket_lock = threading.RLock()
        self._packet_count_lock = threading.Lock()

    def __repr__(self):
        return '<Hana connection host=%s port=%s user=%s>' % (
            self.host, self.port, self.user)

    def _open_socket_and_init_protocoll(self):
        try:
            self._socket = socket.create_connection((self.host, self.port),
                                                    self._timeout)

            # Initialization Handshake
            self._socket.sendall(INITIALIZATION_BYTES)

            response = self._socket.recv(8)
        except socket.error as e:
            raise Error(e)

        if len(response) != 8:
            raise Error("Connection failed")

        self.product_version = version_struct.unpack(response[0:3])
        self.protocol_version = version_struct.unpack_from(response[3:8])

    def send_request(self, message):
        """Send message request to HANA db and return reply message
        :param message: Instance of Message object containing segments and parts of a HANA db request
        :returns: Instance of reply Message object
        """
        payload = message.pack()  # obtain BytesIO instance
        return self.__send_message_recv_reply(payload.getvalue())

    def __send_message_recv_reply(self, packed_message):
        """
        Private method to send packed message and receive the reply message.
        :param packed_message: a binary string containing the entire message payload
        """
        payload = io.BytesIO()
        try:
            with self._socket_lock:
                self._socket.sendall(packed_message)

                # Read first message header
                raw_header = self._socket.recv(32)
                header = ReplyMessage.header_from_raw_header_data(raw_header)

                # from pyhdb.lib.stringlib import allhexlify
                # print 'Raw msg header:', allhexlify(raw_header)
                msg = 'Message header (32 bytes): sessionid: %d, packetcount: %d, length: %d, size: %d, noofsegm: %d'
                debug(msg, *(header[:5]))

                # Receive complete message payload
                while payload.tell() < header.payload_length:
                    _payload = self._socket.recv(header.payload_length -
                                                 payload.tell())
                    if not _payload:
                        break  # jump out without any warning??
                    payload.write(_payload)

                debug('Read %d bytes payload from socket', payload.tell())

                # Keep session id of connection up to date
                if self.session_id != header.session_id:
                    self.session_id = header.session_id
                    self.packet_count = -1
        except socket.timeout:
            raise ConnectionTimedOutError()
        except (IOError, OSError) as error:
            raise OperationalError("Lost connection to HANA server (%r)" %
                                   error)

        payload.seek(0)  # set pointer position to beginning of buffer
        return ReplyMessage.unpack_reply(header, payload)

    def get_next_packet_count(self):
        with self._packet_count_lock:
            self.packet_count += 1
            return self.packet_count

    def connect(self):
        with self._socket_lock:
            if self._socket is not None:
                # Socket already established
                return

            self._open_socket_and_init_protocoll()

            # Perform the authenication handshake and get the part
            # with the agreed authentication data
            agreed_auth_part = self._auth_manager.perform_handshake()

            request = RequestMessage.new(
                self,
                RequestSegment(message_types.CONNECT,
                               (agreed_auth_part,
                                ClientId("pyhdb-%s@%s" %
                                         (os.getpid(), socket.getfqdn())),
                                ConnectOptions(DEFAULT_CONNECTION_OPTIONS))))
            self.send_request(request)

    def close(self):
        with self._socket_lock:
            if self._socket is None:
                raise Error("Connection already closed")

            try:
                request = RequestMessage.new(
                    self, RequestSegment(message_types.DISCONNECT))
                reply = self.send_request(request)
                if reply.segments[0].function_code != \
                   function_codes.DISCONNECT:
                    raise Error("Connection wasn't closed correctly")
            finally:
                self._socket.close()
                self._socket = None

    @property
    def closed(self):
        return self._socket is None

    def _check_closed(self):
        if self.closed:
            raise Error("Connection closed")

    def cursor(self):
        """Return a new Cursor Object using the connection."""
        self._check_closed()

        return Cursor(self)

    def commit(self):
        self._check_closed()

        request = RequestMessage.new(self,
                                     RequestSegment(message_types.COMMIT))
        self.send_request(request)

    def rollback(self):
        self._check_closed()

        request = RequestMessage.new(self,
                                     RequestSegment(message_types.ROLLBACK))
        self.send_request(request)

    @property
    def timeout(self):
        if self._socket:
            return self._socket.gettimeout()
        return self._timeout

    @timeout.setter
    def timeout(self, value):
        self._timeout = value
        if self._socket:
            self._socket.settimeout(value)

    # Methods for compatibility with hdbclient
    def getautocommit(self):
        return self.autocommit

    def setautocommit(self, auto=True):
        self.autocommit = auto

    def isconnected(self):
        return self._socket is not None
Esempio n. 7
0
class Connection(object):

    def __init__(self, host, port, user, password, autocommit=False,
                 timeout=None):
        self.host = host
        self.port = port

        self.autocommit = autocommit
        self.product_version = None
        self.protocol_version = None

        self.session_id = -1
        self.packet_count = -1

        self._socket = None
        self._timeout = timeout
        self._auth_manager = AuthManager(self, user, password)
        # It feels like the RLock has a poorer performance
        self._socket_lock = threading.RLock()
        self._packet_count_lock = threading.Lock()

    def _open_socket_and_init_protocoll(self):
        self._socket = socket.create_connection(
            (self.host, self.port), self._timeout
        )

        # Initialization Handshake
        self._socket.sendall(INITIALIZATION_BYTES)

        response = self._socket.recv(8)
        if len(response) != 8:
            raise Exception("Connection failed")

        self.product_version = version_struct.unpack(response[0:3])
        self.protocol_version = version_struct.unpack_from(response[3:8])

    def _send_message(self, packed_message):
        """
        Private method to send packed message and receive
        the reply message.
        """
        try:
            with self._socket_lock:
                self._socket.sendall(packed_message)

                # Read first message header
                raw_header = self._socket.recv(32)
                try:
                    header = Message.struct.unpack(raw_header)
                except struct.error:
                    raise Exception("Invalid message header received")

                # Receive complete message payload
                payload = b""
                received = 0
                msg = 'Message header (32 bytes): sessionid: %d, ' \
                      'packetcount: %d, length: %d, size: %d, noofsegm: %d'
                debug(msg, *(header[:5]))
                while received < header[2]:
                    _payload = self._socket.recv(header[2] - received)
                    if not _payload:
                        break
                    payload += _payload
                    received += len(_payload)

                debug('Read %d bytes payload from socket', received)
                payload = BytesIO(payload)

                # Keep session id of connection up to date
                if self.session_id != header[0]:
                    self.session_id = header[0]
                    self.packet_count = -1
        except socket.timeout:
            raise ConnectionTimedOutError()
        except (IOError, OSError) as error:
            raise OperationalError(
                "Lost connection to HANA server (%r)" % error
            )

        return Message.unpack_reply(self, header, payload)

    def Message(self, *args, **kwargs):
        return Message(self, *args, **kwargs)

    def get_next_packet_count(self):
        with self._packet_count_lock:
            self.packet_count += 1
            return self.packet_count

    def connect(self):
        with self._socket_lock:
            if self._socket is not None:
                # Socket already established
                return

            self._open_socket_and_init_protocoll()

            # Perform the authenication handshake and get the part
            # with the agreed authentication data
            agreed_auth_part = self._auth_manager.perform_handshake()

            reply = self.Message(
                RequestSegment(
                    message_types.CONNECT,
                    (
                        agreed_auth_part,
                        ClientId(
                            "pyhdb-%s@%s" % (os.getpid(), socket.getfqdn())
                        ),
                        ConnectOptions(DEFAULT_CONNECTION_OPTIONS)
                    )
                )
            ).send()

    def close(self):
        with self._socket_lock:
            if self._socket is None:
                raise Error("Connection already closed")

            try:
                reply = self.Message(
                    RequestSegment(message_types.DISCONNECT)
                ).send()

                if reply.segments[0].function_code != \
                   function_codes.DISCONNECT:
                    raise Error("Connection wasn't closed correctly")
            finally:
                self._socket.close()
                self._socket = None

    @property
    def closed(self):
        return self._socket is None

    def _check_closed(self):
        if self.closed:
            raise Error("Connection closed")

    def cursor(self):
        """Return a new Cursor Object using the connection."""
        self._check_closed()

        return Cursor(self)

    def commit(self):
        self._check_closed()

        self.Message(
            RequestSegment(message_types.COMMIT)
        ).send()

    def rollback(self):
        self._check_closed()

        self.Message(
            RequestSegment(message_types.ROLLBACK)
        ).send()

    @property
    def timeout(self):
        if self._socket:
            return self._socket.gettimeout()
        return self._timeout

    @timeout.setter
    def timeout(self, value):
        self._timeout = value
        if self._socket:
            self._socket.settimeout(value)

    # Methods for compatibility with hdbclient
    def getautocommit(self):
        return self.autocommit

    def setautocommit(self, auto=True):
        self.autocommit = auto

    def isconnected(self):
        return self._socket is not None
Esempio n. 8
0
class Connection(object):
    """
    Database connection class
    """

    def __init__(self, host, port, user, password, autocommit=False, timeout=None):
        self.host = host
        self.port = port
        self.user = user

        self.autocommit = autocommit
        self.product_version = None
        self.protocol_version = None

        self.session_id = -1
        self.packet_count = -1

        self._socket = None
        self._timeout = timeout
        self._auth_manager = AuthManager(self, user, password)
        # It feels like the RLock has a poorer performance
        self._socket_lock = threading.RLock()
        self._packet_count_lock = threading.Lock()

    def __repr__(self):
        return '<Hana connection host=%s port=%s user=%s>' % (self.host, self.port, self.user)

    def _open_socket_and_init_protocoll(self):
        try:
            self._socket = socket.create_connection((self.host, self.port), self._timeout)

            # Initialization Handshake
            self._socket.sendall(INITIALIZATION_BYTES)

            response = self._socket.recv(8)
        except socket.error as e:
            raise Error(e)

        if len(response) != 8:
            raise Error("Connection failed")

        self.product_version = version_struct.unpack(response[0:3])
        self.protocol_version = version_struct.unpack_from(response[3:8])

    def send_request(self, message):
        """Send message request to HANA db and return reply message
        :param message: Instance of Message object containing segments and parts of a HANA db request
        :returns: Instance of reply Message object
        """
        payload = message.pack()  # obtain BytesIO instance
        return self.__send_message_recv_reply(payload.getvalue())

    def __send_message_recv_reply(self, packed_message):
        """
        Private method to send packed message and receive the reply message.
        :param packed_message: a binary string containing the entire message payload
        """
        payload = io.BytesIO()
        try:
            with self._socket_lock:
                self._socket.sendall(packed_message)

                # Read first message header
                raw_header = self._socket.recv(32)
                header = ReplyMessage.header_from_raw_header_data(raw_header)

                # from pyhdb.lib.stringlib import allhexlify
                # print 'Raw msg header:', allhexlify(raw_header)
                msg = 'Message header (32 bytes): sessionid: %d, packetcount: %d, length: %d, size: %d, noofsegm: %d'
                debug(msg, *(header[:5]))

                # Receive complete message payload
                while payload.tell() < header.payload_length:
                    _payload = self._socket.recv(header.payload_length - payload.tell())
                    if not _payload:
                        break   # jump out without any warning??
                    payload.write(_payload)

                debug('Read %d bytes payload from socket', payload.tell())

                # Keep session id of connection up to date
                if self.session_id != header.session_id:
                    self.session_id = header.session_id
                    self.packet_count = -1
        except socket.timeout:
            raise ConnectionTimedOutError()
        except (IOError, OSError) as error:
            raise OperationalError("Lost connection to HANA server (%r)" % error)

        payload.seek(0)  # set pointer position to beginning of buffer
        return ReplyMessage.unpack_reply(header, payload)

    def get_next_packet_count(self):
        with self._packet_count_lock:
            self.packet_count += 1
            return self.packet_count

    def connect(self):
        with self._socket_lock:
            if self._socket is not None:
                # Socket already established
                return

            self._open_socket_and_init_protocoll()

            # Perform the authenication handshake and get the part
            # with the agreed authentication data
            agreed_auth_part = self._auth_manager.perform_handshake()

            request = RequestMessage.new(
                self,
                RequestSegment(
                    message_types.CONNECT,
                    (
                        agreed_auth_part,
                        ClientId(
                            "pyhdb-%s@%s" % (os.getpid(), socket.getfqdn())
                        ),
                        ConnectOptions(DEFAULT_CONNECTION_OPTIONS)
                    )
                )
            )
            self.send_request(request)

    def close(self):
        with self._socket_lock:
            if self._socket is None:
                raise Error("Connection already closed")

            try:
                request = RequestMessage.new(
                    self,
                    RequestSegment(message_types.DISCONNECT)
                )
                reply = self.send_request(request)
                if reply.segments[0].function_code != \
                   function_codes.DISCONNECT:
                    raise Error("Connection wasn't closed correctly")
            finally:
                self._socket.close()
                self._socket = None

    @property
    def closed(self):
        return self._socket is None

    def _check_closed(self):
        if self.closed:
            raise Error("Connection closed")

    def cursor(self):
        """Return a new Cursor Object using the connection."""
        self._check_closed()

        return Cursor(self)

    def commit(self):
        self._check_closed()

        request = RequestMessage.new(
            self,
            RequestSegment(message_types.COMMIT)
        )
        self.send_request(request)

    def rollback(self):
        self._check_closed()

        request = RequestMessage.new(
            self,
            RequestSegment(message_types.ROLLBACK)
        )
        self.send_request(request)

    @property
    def timeout(self):
        if self._socket:
            return self._socket.gettimeout()
        return self._timeout

    @timeout.setter
    def timeout(self, value):
        self._timeout = value
        if self._socket:
            self._socket.settimeout(value)

    # Methods for compatibility with hdbclient
    def getautocommit(self):
        return self.autocommit

    def setautocommit(self, auto=True):
        self.autocommit = auto

    def isconnected(self):
        return self._socket is not None