Esempio n. 1
0
    def connect(cls, connection, password):
        '''Establishes a connection to the server and stores the connection object in the connections pool.

        It first looks for a connection with the given connection parameters in the connections pool to
        reuse existent connections. If such connection is found it queries the server to ensure that the
        connection is alive and reestablishes it if is dead. If no suitable connection is found in the
        connections pool, a new one is created and stored in the pool.

        Parameters:
        ===========
            connection:  an object of the class db_mgmt_Connection storing the parameters
                         for the connection.
            password:    a string with the password to use for the connection (ignored for SQLite).
        '''
        con = None
        try:
            con = cls.get_connection(connection)
            try:
                if not con.cursor().execute('SELECT 1'):
                    raise Exception('connection error')
            except Exception, exc:
                grt.send_info(
                    'Connection to %s apparently lost, reconnecting...' %
                    connection.hostIdentifier)
                raise NotConnectedError('Connection error')
        except NotConnectedError, exc:
            grt.send_info('Connecting to %s...' % connection.hostIdentifier)
            if connection.driver.driverLibraryName == 'sqlanydb':
                import sqlanydbwrapper as sqlanydb  # Replace this to a direct sqlanydb import when it complies with PEP 249
                connstr = replace_string_parameters(
                    connection.driver.connectionStringTemplate,
                    dict(connection.parameterValues))
                import ast
                try:
                    all_params_dict = ast.literal_eval(connstr)
                except Exception, exc:
                    grt.send_error(
                        'The given connection string is not a valid python dict: %s'
                        % connstr)
                    raise
                # Remove unreplaced parameters:
                params = dict(
                    (key, value) for key, value in all_params_dict.iteritems()
                    if not (value.startswith('%') and value.endswith('%')))
                params['password'] = password
                conn_params = dict(params)
                conn_params['password'] = '******'
                connection.parameterValues[
                    'wbcopytables_connection_string'] = repr(conn_params)

                con = sqlanydb.connect(**params)
    def connect(cls, connection, password):
        """Establishes a connection to the server and stores the connection object in the connections pool.

        It first looks for a connection with the given connection parameters in the connections pool to
        reuse existent connections. If such connection is found it queries the server to ensure that the
        connection is alive and reestablishes it if is dead. If no suitable connection is found in the
        connections pool, a new one is created and stored in the pool.

        Parameters:
        ===========
            connection:  an object of the class db_mgmt_Connection storing the parameters
                         for the connection.
            password:    a string with the password to use for the connection (ignored for SQLite).
        """
        con = None
        try:
            con = cls.get_connection(connection)
            try:
                if not con.cursor().execute("SELECT 1"):
                    raise Exception("connection error")
            except Exception, exc:
                grt.send_info("Connection to %s apparently lost, reconnecting..." % connection.hostIdentifier)
                raise NotConnectedError("Connection error")
        except NotConnectedError, exc:
            grt.send_info("Connecting to %s..." % connection.hostIdentifier)
            if connection.driver.driverLibraryName == "sqlanydb":
                import sqlanydbwrapper as sqlanydb  # Replace this to a direct sqlanydb import when it complies with PEP 249

                connstr = replace_string_parameters(
                    connection.driver.connectionStringTemplate, dict(connection.parameterValues)
                )
                import ast

                try:
                    all_params_dict = ast.literal_eval(connstr)
                except Exception, exc:
                    grt.send_error("The given connection string is not a valid python dict: %s" % connstr)
                    raise
                # Remove unreplaced parameters:
                params = dict(
                    (key, value)
                    for key, value in all_params_dict.iteritems()
                    if not (value.startswith("%") and value.endswith("%"))
                )
                params["password"] = password
                conn_params = dict(params)
                conn_params["password"] = "******"
                connection.parameterValues["wbcopytables_connection_string"] = repr(conn_params)

                con = sqlanydb.connect(**params)
Esempio n. 3
0
    def connect(cls, connection, password):
        '''Establishes a connection to the server and stores the connection object in the connections pool.

        It first looks for a connection with the given connection parameters in the connections pool to
        reuse existent connections. If such connection is found it queries the server to ensure that the
        connection is alive and reestablishes it if is dead. If no suitable connection is found in the
        connections pool, a new one is created and stored in the pool.

        Parameters:
        ===========
            connection:  an object of the class db_mgmt_Connection storing the parameters
                         for the connection.
            password:    a string with the password to use for the connection (ignored for SQLite).
        '''
        con = None
        try:
            con = cls.get_connection(connection)
            try:
                if not con.cursor().execute('SELECT 1'):
                    raise Exception('connection error')
            except Exception as exc:
                grt.send_info(
                    'Connection to %s apparently lost, reconnecting...' %
                    connection.hostIdentifier)
                raise NotConnectedError('Connection error')
        except NotConnectedError as exc:
            grt.send_info('Connecting to %s...' % connection.hostIdentifier)
            if connection.driver.driverLibraryName == 'sqlanydb':
                import sqlanydbwrapper as sqlanydb  # Replace this to a direct sqlanydb import when it complies with PEP 249
                connstr = replace_string_parameters(
                    connection.driver.connectionStringTemplate,
                    dict(connection.parameterValues))
                import ast
                try:
                    all_params_dict = ast.literal_eval(connstr)
                except Exception as exc:
                    grt.send_error(
                        'The given connection string is not a valid python dict: %s'
                        % connstr)
                    raise
                # Remove unreplaced parameters:
                params = dict(
                    (key, value)
                    for key, value in list(all_params_dict.items())
                    if not (value.startswith('%') and value.endswith('%')))
                params['password'] = password
                conn_params = dict(params)
                conn_params['password'] = '******'
                connection.parameterValues[
                    'wbcopytables_connection_string'] = repr(conn_params)

                con = sqlanydb.connect(**params)
            else:
                con = db_driver.connect(connection, password)
            if not con:
                grt.send_error('Connection failed', str(exc))
                raise
            grt.send_info('Connected')
            cls._connections[connection.__id__] = {'connection': con}
        if con:
            ver = cls.execute_query(connection,
                                    "SELECT @@version").fetchone()[0]
            grt.log_info("SQLAnywhere RE",
                         "Connected to %s, %s\n" % (connection.name, ver))
            ver_parts = server_version_str2tuple(ver) + (0, 0, 0, 0)
            version = grt.classes.GrtVersion()
            version.majorNumber, version.minorNumber, version.releaseNumber, version.buildNumber = ver_parts[:
                                                                                                             4]
            cls._connections[connection.__id__]["version"] = version

        return 1