]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- mysql uses "DESCRIBE [<schemaname>].<tablename>", catching exceptions
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 29 Apr 2007 20:08:55 +0000 (20:08 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 29 Apr 2007 20:08:55 +0000 (20:08 +0000)
if table doesnt exist, in order to determine if a table exists.
this supports unicode table names as well as schema names. tested
with MySQL5 but should work with 4.1 series as well. (#557)

CHANGES
lib/sqlalchemy/databases/mysql.py
test/sql/unicode.py

diff --git a/CHANGES b/CHANGES
index 441be8b036586f557f6becb49f77a9e086e2fe22..934733d0b1d4e13547690216eb4066f0ad394b10 100644 (file)
--- a/CHANGES
+++ b/CHANGES
 - mysql
     - support for SSL arguments given as inline within URL query string,
       prefixed with "ssl_", courtesy terjeros@gmail.com.
+    - mysql uses "DESCRIBE [<schemaname>].<tablename>", catching exceptions
+      if table doesnt exist, in order to determine if a table exists.
+      this supports unicode table names as well as schema names. tested
+      with MySQL5 but should work with 4.1 series as well. (#557)
 - extensions
     - big fix to AssociationProxy so that multiple AssociationProxy
       objects can be associated with a single association collection. 
index c33b515b58f4eb08998418cf3038da971c3aba03..0903befe9aae093cda6e86a49c5a4e33364a7cbf 100644 (file)
@@ -364,11 +364,24 @@ class MySQLDialect(ansisql.ANSIDialect):
 
     def has_table(self, connection, table_name, schema=None):
         # TODO: this does not work for table names that contain multibyte characters.
-        # i have tried dozens of approaches here with no luck.  statements like
-        # DESCRIBE and SHOW CREATE TABLE work better, but they raise an error when
-        # the table does not exist.
-        cursor = connection.execute("show table status like %s", [table_name])
-        return bool( not not cursor.rowcount )
+
+        # http://dev.mysql.com/doc/refman/5.0/en/error-messages-server.html
+
+        # Error: 1146 SQLSTATE: 42S02 (ER_NO_SUCH_TABLE)
+        # Message: Table '%s.%s' doesn't exist
+
+        # Error: 1046 SQLSTATE: 3D000 (ER_NO_DB_ERROR)
+        # Message: No database selected
+
+        try:
+            name = schema and ("%s.%s" % (schema, table_name)) or table_name
+            connection.execute("DESCRIBE `%s`" % name)
+            return True
+        except exceptions.SQLError, e:
+            if e.orig.args[0] in (1146, 1046): 
+                return False
+            else:
+                raise
 
     def reflecttable(self, connection, table):
         # reference:  http://dev.mysql.com/doc/refman/5.0/en/name-case-sensitivity.html
index 9dfc75059e06c56bcc1ec85830139b27a775e293..65a7cce0d095df386d9a9687a10f2a4e4b45bcb1 100644 (file)
@@ -28,10 +28,6 @@ class UnicodeSchemaTest(testbase.PersistTest):
     def tearDownAll(self):
         metadata.drop_all()
         
-        # has_table() doesnt handle the unicode names on mysql
-        if testbase.db.name == 'mysql':
-            t2.drop()
-        
     def test_insert(self):
         t1.insert().execute({u'méil':1, u'éXXm':5})
         t2.insert().execute({'a':1, 'b':1})