diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2007-04-29 20:08:55 +0000 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2007-04-29 20:08:55 +0000 |
commit | 8d2c9ae434a9026e9a198120c9a7b6faf0346cd7 (patch) | |
tree | 457e9b5a04c209a673bd1c769a2d042a2593d921 /lib/sqlalchemy | |
parent | 22278d02b943c005af1d416fd7a7016da535f4a8 (diff) | |
download | sqlalchemy-8d2c9ae434a9026e9a198120c9a7b6faf0346cd7.tar.gz |
- 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)
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r-- | lib/sqlalchemy/databases/mysql.py | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/lib/sqlalchemy/databases/mysql.py b/lib/sqlalchemy/databases/mysql.py index c33b515b5..0903befe9 100644 --- a/lib/sqlalchemy/databases/mysql.py +++ b/lib/sqlalchemy/databases/mysql.py @@ -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 |