Example #1
0
    def describe_sqla_col(column: Column, sql_dialect: str = None) \
            -> Dict[str, Any]:
        """
        Describes a single SQLAlchemy :class:`Column` in the :ref:`NLPRP
        <nlprp>` format, which follows ``INFORMATION_SCHEMA.COLUMNS`` closely.

        Args:
            column:
                the :class:`Column`
            sql_dialect:
                preferred SQL dialect for response, or ``None`` for a default
        """
        sql_dialect = sql_dialect or DEFAULT_NLPRP_SQL_DIALECT
        assert sql_dialect in ALL_SQL_DIALECTS, (
            f"Unknown SQL dialect {sql_dialect!r}; must be one of "
            f"{ALL_SQL_DIALECTS}")
        dialect = registry.load(sql_dialect)()  # type: Dialect
        # log.critical(f"dialect: {dialect}")
        # dialect = MSDialect()
        column_type = column.type.compile(dialect)
        data_type = column_type.partition("(")[0]
        # ... https://stackoverflow.com/questions/27387415/how-would-i-get-everything-before-a-in-a-string-python  # noqa
        return {
            NlprpKeys.COLUMN_NAME: column.name,
            NlprpKeys.COLUMN_TYPE: column_type,
            NlprpKeys.DATA_TYPE: data_type,
            NlprpKeys.IS_NULLABLE: column.nullable,
            NlprpKeys.COLUMN_COMMENT: column.comment,
        }
Example #2
0
    def get_dialect(self):
        """Return the SQLAlchemy database dialect class corresponding
        to this URL's driver name.
        """

        if "+" not in self.drivername:
            name = self.drivername
        else:
            name = self.drivername.replace("+", ".")
        from sqlalchemy.dialects import registry

        cls = registry.load(name)
        # check for legacy dialects that
        # would return a module with 'dialect' as the
        # actual class
        if hasattr(cls, "dialect") and isinstance(cls.dialect, type) and issubclass(cls.dialect, Dialect):
            return cls.dialect
        else:
            return cls
Example #3
0
    def get_dialect(self):
        """Return the SQLAlchemy database dialect class corresponding
        to this URL's driver name.
        """

        if '+' not in self.drivername:
            name = self.drivername
        else:
            name = self.drivername.replace('+', '.')
        from sqlalchemy.dialects import registry
        cls = registry.load(name)
        # check for legacy dialects that
        # would return a module with 'dialect' as the
        # actual class
        if hasattr(cls, 'dialect') and \
            isinstance(cls.dialect, type) and \
            issubclass(cls.dialect, Dialect):
            return cls.dialect
        else:
            return cls
Example #4
0
def mock_sqlalchemy_engine(dialect):
    """
    Create a sqlalchemy.engine.Engine without it connecting to a database.

    Examples
    --------

    ::
        from siuba.sql import LazyTbl
        from siuba import _, mutate, show_query

        engine = mock_sqlalchemy_engine('postgresql')
        tbl = LazyTbl(engine, 'some_table', ['x'])

        query = mutate(tbl, y = _.x + _.x)
        show_query(query)

    """
    from sqlalchemy.engine import Engine
    from sqlalchemy.dialects import registry

    dialect_cls = registry.load('postgresql')
    return Engine(None, dialect_cls(), '')
 def test_dialect_registered(self):
     from sqlalchemy.dialects import registry
     assert registry.load(
         'mssql.pyodbc_mssql') is dialect.MssqlDialect_pyodbc_quoted
Example #6
0
def is_dialect_available(dialect: str) -> bool:
    try:
        sqlalchemy_registry.load(dialect)
        return True
    except (NoSuchModuleError, ImportError):
        return False