diff options
author | mike bayer <mike_mp@zzzcomputing.com> | 2023-01-05 17:55:13 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@ci3.zzzcomputing.com> | 2023-01-05 17:55:13 +0000 |
commit | a76f4cbb7d3066f71b035b29e511eb811b810c9c (patch) | |
tree | 49287b164554f928a3c4b82398947f2f226e7180 /lib | |
parent | aafded2fe6f955d23ab974574978f2a6f96234b9 (diff) | |
parent | 7505cc5db44f2d3a84827519d3a7d926a9cdec23 (diff) | |
download | sqlalchemy-a76f4cbb7d3066f71b035b29e511eb811b810c9c.tar.gz |
Merge "revert MySQL to use DESCRIBE for has_table()" into main
Diffstat (limited to 'lib')
-rw-r--r-- | lib/sqlalchemy/dialects/mysql/base.py | 39 | ||||
-rw-r--r-- | lib/sqlalchemy/engine/interfaces.py | 18 | ||||
-rw-r--r-- | lib/sqlalchemy/engine/reflection.py | 19 |
3 files changed, 47 insertions, 29 deletions
diff --git a/lib/sqlalchemy/dialects/mysql/base.py b/lib/sqlalchemy/dialects/mysql/base.py index e89ebb09d..1a0534490 100644 --- a/lib/sqlalchemy/dialects/mysql/base.py +++ b/lib/sqlalchemy/dialects/mysql/base.py @@ -1013,7 +1013,6 @@ from itertools import compress import re from sqlalchemy import literal_column -from sqlalchemy import text from sqlalchemy.sql import visitors from . import reflection as _reflection from .enumerated import ENUM @@ -1070,7 +1069,6 @@ from ...sql import operators from ...sql import roles from ...sql import sqltypes from ...sql import util as sql_util -from ...sql.sqltypes import Unicode from ...types import BINARY from ...types import BLOB from ...types import BOOLEAN @@ -2671,21 +2669,30 @@ class MySQLDialect(default.DefaultDialect): if schema is None: schema = self.default_schema_name - rs = connection.execute( - text( - "SELECT COUNT(*) FROM information_schema.tables WHERE " - "table_schema = :table_schema AND " - "table_name = :table_name" - ).bindparams( - sql.bindparam("table_schema", type_=Unicode), - sql.bindparam("table_name", type_=Unicode), - ), - { - "table_schema": str(schema), - "table_name": str(table_name), - }, + assert schema is not None + + full_name = ".".join( + self.identifier_preparer._quote_free_identifiers( + schema, table_name + ) ) - return bool(rs.scalar()) + + # DESCRIBE *must* be used because there is no information schema + # table that returns information on temp tables that is consistently + # available on MariaDB / MySQL / engine-agnostic etc. + # therefore we have no choice but to use DESCRIBE and an error catch + # to detect "False". See issue #9058 + + try: + with connection.exec_driver_sql( + f"DESCRIBE {full_name}", + execution_options={"skip_user_error_events": True}, + ) as rs: + return rs.fetchone() is not None + except exc.DBAPIError as e: + if self._extract_error_code(e.orig) == 1146: + return False + raise @reflection.cache def has_sequence(self, connection, sequence_name, schema=None, **kw): diff --git a/lib/sqlalchemy/engine/interfaces.py b/lib/sqlalchemy/engine/interfaces.py index c32b1a1fb..c1de13221 100644 --- a/lib/sqlalchemy/engine/interfaces.py +++ b/lib/sqlalchemy/engine/interfaces.py @@ -1790,15 +1790,19 @@ class Dialect(EventTarget): published so that third-party dialects may provide an implementation. It is **not** the public API for checking for table presence. Please use the :meth:`.Inspector.has_table` method. - Alternatively, for legacy cross-compatibility, the - :meth:`_engine.Engine.has_table` method may be used. - .. versionchanged:: 2.0 + .. versionchanged:: 2.0:: :meth:`_engine.Dialect.has_table` now + formally supports checking for additional table-like objects: - The :meth:`_engine.Dialect.has_table` method should also check - for the presence of views. In previous versions this - behavior was dialect specific. New dialect suite tests were added - to ensure that dialects conform with this behavior consistently. + * any type of views (plain or materialized) + * temporary tables of any kind + + Previously, these two checks were not formally specified and + different dialects would vary in their behavior. The dialect + testing suite now includes tests for all of these object types, + and dialects to the degree that the backing database supports views + or temporary tables should seek to support locating these objects + for full compliance. """ diff --git a/lib/sqlalchemy/engine/reflection.py b/lib/sqlalchemy/engine/reflection.py index 7e1fca0e5..050f18f2f 100644 --- a/lib/sqlalchemy/engine/reflection.py +++ b/lib/sqlalchemy/engine/reflection.py @@ -401,7 +401,8 @@ class Inspector(inspection.Inspectable["Inspector"]): def has_table( self, table_name: str, schema: Optional[str] = None, **kw: Any ) -> bool: - r"""Return True if the backend has a table or view of the given name. + r"""Return True if the backend has a table, view, or temporary + table of the given name. :param table_name: name of the table to check :param schema: schema name to query, if not the default schema. @@ -412,11 +413,17 @@ class Inspector(inspection.Inspectable["Inspector"]): .. versionadded:: 1.4 - the :meth:`.Inspector.has_table` method replaces the :meth:`_engine.Engine.has_table` method. - .. versionchanged:: 2.0:: The method checks also for any type of - views (plain or materialized). - In previous version this behaviour was dialect specific. New - dialect suite tests were added to ensure all dialect conform with - this behaviour. + .. versionchanged:: 2.0:: :meth:`.Inspector.has_table` now formally + supports checking for additional table-like objects: + + * any type of views (plain or materialized) + * temporary tables of any kind + + Previously, these two checks were not formally specified and + different dialects would vary in their behavior. The dialect + testing suite now includes tests for all of these object types + and should be supported by all SQLAlchemy-included dialects. + Support among third party dialects may be lagging, however. """ with self._operation_context() as conn: |