@@ -1491,24 +1491,24 @@ def _get_default_schema_name(self, connection):
1491
1491
def get_schema_names (self , connection , ** kw ):
1492
1492
return [row [0 ] for row in connection .execute (text ("SHOW DATABASES" ))]
1493
1493
1494
- def _get_table_columns (self , connection , table_name , schema ):
1495
- if schema is None :
1496
- schema = self .default_schema_name
1497
- quote_table_name = self .identifier_preparer .quote_identifier (table_name )
1498
- quote_schema = self .identifier_preparer .quote_identifier (schema )
1499
-
1500
- return connection .execute (
1501
- text (f"DESC { quote_schema } .{ quote_table_name } " )
1502
- ).fetchall ()
1503
-
1504
1494
@reflection .cache
1505
1495
def has_table (self , connection , table_name , schema = None , ** kw ):
1496
+ table_name_query = """
1497
+ select case when exists(
1498
+ select table_name
1499
+ from information_schema.tables
1500
+ where table_schema = :schema_name
1501
+ and table_name = :table_name
1502
+ ) then 1 else 0 end
1503
+ """
1504
+ query = text (table_name_query ).bindparams (
1505
+ bindparam ("schema_name" , type_ = sqltypes .Unicode ),
1506
+ bindparam ("table_name" , type_ = sqltypes .Unicode ),
1507
+ )
1506
1508
if schema is None :
1507
1509
schema = self .default_schema_name
1508
- quote_table_name = self .identifier_preparer .quote_identifier (table_name )
1509
- quote_schema = self .identifier_preparer .quote_identifier (schema )
1510
- query = f"""EXISTS TABLE { quote_schema } .{ quote_table_name } """
1511
- r = connection .scalar (text (query ))
1510
+
1511
+ r = connection .scalar (query , dict (schema_name = schema , table_name = table_name ))
1512
1512
if r == 1 :
1513
1513
return True
1514
1514
return False
@@ -1550,21 +1550,26 @@ def get_columns(self, connection, table_name, schema=None, **kw):
1550
1550
def get_view_definition (self , connection , view_name , schema = None , ** kw ):
1551
1551
if schema is None :
1552
1552
schema = self .default_schema_name
1553
- quote_schema = self .identifier_preparer .quote_identifier (schema )
1554
- quote_view_name = self .identifier_preparer .quote_identifier (view_name )
1555
- full_view_name = f"{ quote_schema } .{ quote_view_name } "
1556
-
1557
- # ToDo : perhaps can be removed if we get `SHOW CREATE VIEW`
1558
- if view_name not in self .get_view_names (connection , schema ):
1559
- raise NoSuchTableError (full_view_name )
1560
-
1561
- query = f"""SHOW CREATE TABLE { full_view_name } """
1562
- try :
1563
- view_def = connection .execute (text (query )).first ()
1564
- return view_def [1 ]
1565
- except DBAPIError as e :
1566
- if "1025" in e .orig .message : # ToDo: The errors need parsing properly
1567
- raise NoSuchTableError (full_view_name ) from e
1553
+ query = text (
1554
+ """
1555
+ select view_query
1556
+ from system.views
1557
+ where name = :view_name
1558
+ and database = :schema_name
1559
+ """
1560
+ ).bindparams (
1561
+ bindparam ("view_name" , type_ = sqltypes .UnicodeText ),
1562
+ bindparam ("schema_name" , type_ = sqltypes .Unicode ),
1563
+ )
1564
+ r = connection .scalar (
1565
+ query , dict (view_name = view_name , schema_name = schema )
1566
+ )
1567
+ if not r :
1568
+ raise NoSuchTableError (
1569
+ f"{ self .identifier_preparer .quote_identifier (schema )} ."
1570
+ f"{ self .identifier_preparer .quote_identifier (view_name )} "
1571
+ )
1572
+ return r
1568
1573
1569
1574
def _get_column_type (self , column_type ):
1570
1575
pattern = r"(?:Nullable)*(?:\()*(\w+)(?:\((.*?)\))?(?:\))*"
0 commit comments