Ejemplo n.º 1
0
    def open(cls, connection: Connection) -> Connection:
        if connection.state == "open":
            logger.debug("Connection is already open, skipping open.")
            return connection

        try:
            creds: AthenaCredentials = connection.credentials

            handle = AthenaConnection(s3_staging_dir=creds.s3_staging_dir,
                                      region_name=creds.region_name,
                                      schema_name=creds.schema,
                                      work_group=creds.work_group,
                                      cursor_class=AthenaCursor,
                                      formatter=AthenaParameterFormatter(),
                                      poll_interval=creds.poll_interval,
                                      profile_name=creds.aws_profile_name)

            connection.state = "open"
            connection.handle = handle

        except Exception as e:
            logger.debug("Got an error when attempting to open a Athena "
                         "connection: '{}'".format(e))
            connection.handle = None
            connection.state = "fail"

            raise FailedToConnectException(str(e))

        return connection
Ejemplo n.º 2
0
    def open(cls, connection: Connection) -> Connection:
        if connection.state == 'open':
            logger.debug('Connection is already open, skipping open.')
            return connection

        credentials = connection.credentials

        try:
            connection_string = '{}:{}/{}'
            handle = oracle.connect(
                credentials.username,
                credentials.password,
                connection_string.format(credentials.host, credentials.port, credentials.database),
                mode=oracle.SYSDBA if credentials.as_sysdba else oracle.DEFAULT_AUTH
            )
            connection.state = 'open'
            connection.handle = handle

            if credentials.nls_date_format is not None:
                handle.cursor().execute(f"ALTER SESSION SET NLS_DATE_FORMAT = '{credentials.nls_date_format}'")
                handle.cursor().execute(f"ALTER SESSION SET NLS_TIMESTAMP_FORMAT = '{credentials.nls_date_format}XFF'")
                handle.cursor().execute(f"ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT = '{credentials.nls_date_format}XFF TZR'")


        except Exception as e:
            logger.debug(
                f"Got an error when attempting to open a oracle connection. Connection string = {credentials.connection_string}. Error: {e}"
            )

            connection.handle = None
            connection.state = 'fail'

            raise FailedToConnectException(f"{str(e)} using connection string {credentials.connection_string}")

        return connection
Ejemplo n.º 3
0
 def open(cls, connection: Connection) -> Connection:
     if connection.state == "open":
         logger.debug("Connection is already open, skipping open")
         return connection
     credentials = connection.credentials
     kwargs = {
         "host": credentials.host,
         "port": credentials.port,
         "user": credentials.username,
         "password": credentials.password,
     }
     try:
         connection.handle = MySQLdb.connect(**kwargs)
         connection.state = ConnectionState.OPEN
     except MySQLdb.Error as e:
         logger.debug(f"Error connecting to database: {e}")
         connection.handle = None
         connection.state = ConnectionState.FAIL
         raise exceptions.FailedToConnectException(str(e))
     return connection
Ejemplo n.º 4
0
    def open(cls, connection: Connection):
        if connection.state == ConnectionState.OPEN:
            logger.debug("Connection is already open, skipping open.")
            return connection

        try:
            connection.handle = pyodbc.connect(
                connection.credentials.connection_string(),
                autocommit=True,
            )
            connection.state = ConnectionState.OPEN
        except pyodbc.OperationalError as e:
            logger.debug(
                f"Got an error when attempting to open an odbc connection: '{e}'"
            )

            connection.handle = None
            connection.state = ConnectionState.FAIL

            raise dbt.exceptions.FailedToConnectException(str(e)) from e

        return connection
Ejemplo n.º 5
0
    def close(cls, connection: Connection) -> Connection:
        if dbt.flags.STRICT_MODE:
            if not isinstance(connection, Connection):
                raise dbt.exceptions.CompilerException(
                    f'In close, got {connection} - not a Connection!')

        # if the connection is in closed or init, there's nothing to do
        if connection.state in {ConnectionState.CLOSED, ConnectionState.INIT}:
            return connection

        if connection.transaction_open and connection.handle:
            cls._rollback_handle(connection)
        connection.transaction_open = False

        cls._close_handle(connection)
        connection.state = ConnectionState.CLOSED

        return connection
Ejemplo n.º 6
0
    def open(cls, connection: Connection):
        if connection.state == "open":
            logger.debug("Connection is already open, skipping open.")
            return connection

        credentials: SQLiteCredentials = connection.credentials

        schemas_and_paths = {}
        for schema, path in credentials.schemas_and_paths.items():
            # Make .db file path absolute
            schemas_and_paths[schema] = os.path.abspath(path)

        try:
            if 'main' in schemas_and_paths:
                handle: sqlite3.Connection = sqlite3.connect(
                    schemas_and_paths['main'])
            else:
                raise FailedToConnectException(
                    "at least one schema must be called 'main'")

            if len(credentials.extensions) > 0:
                handle.enable_load_extension(True)

            for ext_path in credentials.extensions:
                handle.load_extension(ext_path)

            cursor = handle.cursor()

            attached = []
            for schema in set(schemas_and_paths.keys()) - set(['main']):
                path = schemas_and_paths[schema]
                cursor.execute(f"attach '{path}' as '{schema}'")
                attached.append(schema)

            for path in glob.glob(
                    os.path.join(credentials.schema_directory, "*.db")):
                abs_path = os.path.abspath(path)

                # if file was already attached from being defined in schemas_and_paths, ignore it
                if not abs_path in schemas_and_paths.values():
                    schema = os.path.basename(path)[:-3]

                    # has schema name been used already?
                    if schema not in attached:
                        cursor.execute(f"attach '{path}' as '{schema}'")
                    else:
                        raise FailedToConnectException(
                            f"found {path} while scanning schema_directory, but cannot attach it as '{schema}' "
                            +
                            f"because that schema name is already defined in schemas_and_paths. "
                            + f"fix your ~/.dbt/profiles.yml file")

            # # uncomment these lines to print out SQL: this only happens if statement is successful
            # handle.set_trace_callback(print)
            # sqlite3.enable_callback_tracebacks(True)

            connection.state = "open"
            connection.handle = handle

            return connection
        except sqlite3.Error as e:
            logger.debug(
                "Got an error when attempting to open a sqlite3 connection: '%s'",
                e)
            connection.handle = None
            connection.state = "fail"

            raise FailedToConnectException(str(e))
        except Exception as e:
            print(f"Unknown error opening SQLite connection: {e}")
            raise e