예제 #1
0
    def set_connection_name(self, name: Optional[str] = None) -> Connection:
        conn_name: str
        if name is None:
            # if a name isn't specified, we'll re-use a single handle
            # named 'master'
            conn_name = 'master'
        else:
            if not isinstance(name, str):
                raise dbt.exceptions.CompilerException(
                    f'For connection name, got {name} - not a string!'
                )
            assert isinstance(name, str)
            conn_name = name

        conn = self.get_if_exists()
        if conn is None:
            conn = Connection(
                type=Identifier(self.TYPE),
                name=None,
                state=ConnectionState.INIT,
                transaction_open=False,
                handle=None,
                credentials=self.profile.credentials
            )
            self.set_thread_connection(conn)

        if conn.name == conn_name and conn.state == 'open':
            return conn

        logger.debug(
            'Acquiring new {} connection "{}".'.format(self.TYPE, conn_name))

        if conn.state == 'open':
            logger.debug(
                'Re-using an available connection from the pool (formerly {}).'
                .format(conn.name)
            )
        else:
            conn.handle = LazyHandle(self.open)

        conn.name = conn_name
        return conn
예제 #2
0
    def set_connection_name(self, name=None):
        if name is None:
            # if a name isn't specified, we'll re-use a single handle
            # named 'master'
            name = 'master'

        conn = self.get_if_exists()
        thread_id_key = self.get_thread_identifier()

        if conn is None:
            conn = Connection(type=self.TYPE,
                              name=None,
                              state='init',
                              transaction_open=False,
                              handle=None,
                              credentials=self.profile.credentials)
            self.thread_connections[thread_id_key] = conn

        if conn.name == name and conn.state == 'open':
            return conn

        logger.debug('Acquiring new {} connection "{}".'.format(
            self.TYPE, name))

        if conn.state == 'open':
            logger.debug(
                'Re-using an available connection from the pool (formerly {}).'
                .format(conn.name))
        else:
            logger.debug(
                'Opening a new connection, currently in state {}'.format(
                    conn.state))
            self.open(conn)

        conn.name = name
        return conn