Python sqlalchemy.engine.reflection.Inspector() Examples

The following are 30 code examples of sqlalchemy.engine.reflection.Inspector(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sqlalchemy.engine.reflection , or try the search function .
Example #1
Source File: test_many2one.py    From AnyBlok with Mozilla Public License 2.0 6 votes vote down vote up
def test_add_index_constraint(self):
        def add_in_registry():

            @register(Model)
            class Address:

                id = Integer(primary_key=True)

            @register(Model)
            class Person:

                name = String(primary_key=True)
                address = Many2One(model=Model.Address, index=True)

        registry = self.init_registry(add_in_registry)
        inspector = Inspector(registry.session.connection())
        indexes = inspector.get_indexes(registry.Person.__tablename__)
        assert len(indexes) == 1 
Example #2
Source File: test_many2one.py    From AnyBlok with Mozilla Public License 2.0 6 votes vote down vote up
def test_add_primary_keys_constraint(self):
        def add_in_registry():

            @register(Model)
            class Address:

                id = Integer(primary_key=True)

            @register(Model)
            class Person:

                name = String(primary_key=True)
                address = Many2One(model=Model.Address, primary_key=True)

        registry = self.init_registry(add_in_registry)
        inspector = Inspector(registry.session.connection())
        pks = inspector.get_primary_keys(registry.Person.__tablename__)
        assert 'address_id'in pks 
Example #3
Source File: presto.py    From incubator-superset with Apache License 2.0 6 votes vote down vote up
def _show_columns(
        cls, inspector: Inspector, table_name: str, schema: Optional[str]
    ) -> List[RowProxy]:
        """
        Show presto column names
        :param inspector: object that performs database schema inspection
        :param table_name: table name
        :param schema: schema name
        :return: list of column objects
        """
        quote = inspector.engine.dialect.identifier_preparer.quote_identifier
        full_table = quote(table_name)
        if schema:
            full_table = "{}.{}".format(quote(schema), full_table)
        columns = inspector.bind.execute("SHOW COLUMNS FROM {}".format(full_table))
        return columns 
Example #4
Source File: core.py    From incubator-superset with Apache License 2.0 5 votes vote down vote up
def inspector(self) -> Inspector:
        engine = self.get_sqla_engine()
        return sqla.inspect(engine) 
Example #5
Source File: test_deprecations.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_inspector_from_engine(self):
        with testing.expect_deprecated(
            r"The from_engine\(\) method on Inspector is deprecated and will "
            r"be removed in a future release."
        ):
            i1 = reflection.Inspector.from_engine(testing.db)

        is_(i1.bind, testing.db)
        self.check_usage(i1) 
Example #6
Source File: sqlite.py    From incubator-superset with Apache License 2.0 5 votes vote down vote up
def get_table_names(
        cls, database: "Database", inspector: Inspector, schema: Optional[str]
    ) -> List[str]:
        """Need to disregard the schema for Sqlite"""
        return sorted(inspector.get_table_names()) 
Example #7
Source File: base.py    From incubator-superset with Apache License 2.0 5 votes vote down vote up
def get_schema_names(cls, inspector: Inspector) -> List[str]:
        """
        Get all schemas from database

        :param inspector: SqlAlchemy inspector
        :return: All schemas in the database
        """
        return sorted(inspector.get_schema_names()) 
Example #8
Source File: base.py    From incubator-superset with Apache License 2.0 5 votes vote down vote up
def get_table_names(
        cls, database: "Database", inspector: Inspector, schema: Optional[str]
    ) -> List[str]:
        """
        Get all tables from schema

        :param inspector: SqlAlchemy inspector
        :param schema: Schema to inspect. If omitted, uses default schema for database
        :return: All tables in schema
        """
        tables = inspector.get_table_names(schema)
        if schema and cls.try_remove_schema_from_table_name:
            tables = [re.sub(f"^{schema}\\.", "", table) for table in tables]
        return sorted(tables) 
Example #9
Source File: base.py    From incubator-superset with Apache License 2.0 5 votes vote down vote up
def get_view_names(
        cls, database: "Database", inspector: Inspector, schema: Optional[str]
    ) -> List[str]:
        """
        Get all views from schema

        :param inspector: SqlAlchemy inspector
        :param schema: Schema name. If omitted, uses default schema for database
        :return: All views in schema
        """
        views = inspector.get_view_names(schema)
        if schema and cls.try_remove_schema_from_table_name:
            views = [re.sub(f"^{schema}\\.", "", view) for view in views]
        return sorted(views) 
Example #10
Source File: hive.py    From incubator-superset with Apache License 2.0 5 votes vote down vote up
def get_columns(
        cls, inspector: Inspector, table_name: str, schema: Optional[str]
    ) -> List[Dict[str, Any]]:
        return inspector.get_columns(table_name, schema) 
Example #11
Source File: impala.py    From incubator-superset with Apache License 2.0 5 votes vote down vote up
def get_schema_names(cls, inspector: Inspector) -> List[str]:
        schemas = [
            row[0]
            for row in inspector.engine.execute("SHOW SCHEMAS")
            if not row[0].startswith("_")
        ]
        return schemas 
Example #12
Source File: presto.py    From incubator-superset with Apache License 2.0 5 votes vote down vote up
def get_table_names(
        cls, database: "Database", inspector: Inspector, schema: Optional[str]
    ) -> List[str]:
        tables = super().get_table_names(database, inspector, schema)
        if not is_feature_enabled("PRESTO_SPLIT_VIEWS_FROM_TABLES"):
            return tables

        views = set(cls.get_view_names(database, inspector, schema))
        actual_tables = set(tables) - views
        return list(actual_tables) 
Example #13
Source File: presto.py    From incubator-superset with Apache License 2.0 5 votes vote down vote up
def get_view_names(
        cls, database: "Database", inspector: Inspector, schema: Optional[str]
    ) -> List[str]:
        """Returns an empty list

        get_table_names() function returns all table names and view names,
        and get_view_names() is not implemented in sqlalchemy_presto.py
        https://github.com/dropbox/PyHive/blob/e25fc8440a0686bbb7a5db5de7cb1a77bdb4167a/pyhive/sqlalchemy_presto.py
        """
        if not is_feature_enabled("PRESTO_SPLIT_VIEWS_FROM_TABLES"):
            return []

        if schema:
            sql = (
                "SELECT table_name FROM information_schema.views "
                "WHERE table_schema=%(schema)s"
            )
            params = {"schema": schema}
        else:
            sql = "SELECT table_name FROM information_schema.views"
            params = {}

        engine = cls.get_engine(database, schema=schema)
        with closing(engine.raw_connection()) as conn:
            with closing(conn.cursor()) as cursor:
                cursor.execute(sql, params)
                results = cursor.fetchall()

        return [row[0] for row in results] 
Example #14
Source File: base.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, conn):
        reflection.Inspector.__init__(self, conn) 
Example #15
Source File: test_reflection.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def test_deprecated_get_primary_keys(self):
        meta = self.metadata
        users = self.tables.users
        insp = Inspector(meta.bind)
        assert_raises_message(
            sa_exc.SADeprecationWarning,
            "Call to deprecated method get_primary_keys."
            "  Use get_pk_constraint instead.",
            insp.get_primary_keys, users.name
        ) 
Example #16
Source File: base.py    From moviegrabber with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, conn):
        reflection.Inspector.__init__(self, conn) 
Example #17
Source File: test_reflection.py    From moviegrabber with GNU General Public License v3.0 5 votes vote down vote up
def test_deprecated_get_primary_keys(self):
        meta = self.metadata
        users = self.tables.users
        insp = Inspector(meta.bind)
        assert_raises_message(
            sa_exc.SADeprecationWarning,
            "Call to deprecated method get_primary_keys."
            "  Use get_pk_constraint instead.",
            insp.get_primary_keys, users.name
        ) 
Example #18
Source File: base.py    From android_universal with MIT License 5 votes vote down vote up
def __init__(self, conn):
        reflection.Inspector.__init__(self, conn) 
Example #19
Source File: test_reflection.py    From android_universal with MIT License 5 votes vote down vote up
def test_deprecated_get_primary_keys(self):
        meta = self.metadata
        users = self.tables.users
        insp = Inspector(meta.bind)
        assert_raises_message(
            sa_exc.SADeprecationWarning,
            "Call to deprecated method get_primary_keys."
            "  Use get_pk_constraint instead.",
            insp.get_primary_keys, users.name
        ) 
Example #20
Source File: test_deprecations.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_reflecttable(self):
        inspector = inspect(testing.db)
        metadata = self.metadata
        table = Table("user", metadata)
        with testing.expect_deprecated_20(
            r"The Inspector.reflecttable\(\) function/method is considered "
        ):
            res = inspector.reflecttable(table, None)
        exp = inspector.reflect_table(table, None)

        eq_(res, exp) 
Example #21
Source File: base.py    From jbox with MIT License 5 votes vote down vote up
def __init__(self, conn):
        reflection.Inspector.__init__(self, conn) 
Example #22
Source File: test_deprecations.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_inspector_constructor_connection(self):
        with testing.db.connect() as conn:
            with testing.expect_deprecated(
                r"The __init__\(\) method on Inspector is deprecated and "
                r"will be removed in a future release."
            ):
                i1 = reflection.Inspector(conn)

            is_(i1.bind, conn)
            is_(i1.engine, testing.db)
            self.check_usage(i1) 
Example #23
Source File: test_deprecations.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_inspector_constructor_engine(self):
        with testing.expect_deprecated(
            r"The __init__\(\) method on Inspector is deprecated and will "
            r"be removed in a future release."
        ):
            i1 = reflection.Inspector(testing.db)

        is_(i1.bind, testing.db)
        self.check_usage(i1) 
Example #24
Source File: base.py    From sqlalchemy with MIT License 5 votes vote down vote up
def __init__(self, conn):
        reflection.Inspector.__init__(self, conn) 
Example #25
Source File: test_reflection.py    From stdm with GNU General Public License v2.0 5 votes vote down vote up
def test_deprecated_get_primary_keys(self):
        meta = self.metadata
        users = self.tables.users
        insp = Inspector(meta.bind)
        assert_raises_message(
            sa_exc.SADeprecationWarning,
            "Call to deprecated method get_primary_keys."
            "  Use get_pk_constraint instead.",
            insp.get_primary_keys, users.name
        ) 
Example #26
Source File: base.py    From stdm with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, conn):
        reflection.Inspector.__init__(self, conn) 
Example #27
Source File: test_reflection.py    From pyRevit with GNU General Public License v3.0 5 votes vote down vote up
def test_deprecated_get_primary_keys(self):
        meta = self.metadata
        users = self.tables.users
        insp = Inspector(meta.bind)
        assert_raises_message(
            sa_exc.SADeprecationWarning,
            "Call to deprecated method get_primary_keys."
            "  Use get_pk_constraint instead.",
            insp.get_primary_keys, users.name
        ) 
Example #28
Source File: base.py    From pyRevit with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, conn):
        reflection.Inspector.__init__(self, conn) 
Example #29
Source File: test_reflection.py    From planespotter with MIT License 5 votes vote down vote up
def test_deprecated_get_primary_keys(self):
        meta = self.metadata
        users = self.tables.users
        insp = Inspector(meta.bind)
        assert_raises_message(
            sa_exc.SADeprecationWarning,
            "Call to deprecated method get_primary_keys."
            "  Use get_pk_constraint instead.",
            insp.get_primary_keys, users.name
        ) 
Example #30
Source File: base.py    From planespotter with MIT License 5 votes vote down vote up
def __init__(self, conn):
        reflection.Inspector.__init__(self, conn)