On Tuesday 21 February 2006 15:32, David Faure wrote:
> SQLObject currently assumes that a default database was selected for the 
> connection.
> However I'm using SQLObject in a C++ application which executes python 
> scripts,
> and the connection doesn't have a default database selected, on purpose.
> 
> With the following naming style I can make SQLObject prepend the database name
> before creating a table or running queries in a table:
> 
> class PrependDBStyle(sqlobject.DefaultStyle):
>       def pythonClassToDBTable(self, className):
>               return "sqlobjects." + super(PrependDBStyle, 
> self).pythonClassToDBTable(className)
>       def dbTableToPythonClass(self, table):
>               if table.startsWith( "sqlobjects." ):
>                       table = table[11:]
>               return super(PrependDBStyle,self).dbTableToPythonClass(table)
> 
> However the tableExists check is currently implemented with "SHOW TABLES", 
> which
> fails when there is no default database selected. The attached patch uses 
> "DESCRIBE %s"
> instead, which works in both cases ("describe foo" or "describe db.foo")

Can the attached patch be considered for inclusion?
"DESCRIBE [db.]table" works in all cases (with or without a default database)
whereas
"SHOW TABLES" needs a default database.

-- 
David Faure -- [EMAIL PROTECTED], [EMAIL PROTECTED]
KDE/KOffice developer, Qt consultancy projects
Klarälvdalens Datakonsult AB, Platform-independent software solutions
Index: sqlobject/mysql/mysqlconnection.py
===================================================================
--- sqlobject/mysql/mysqlconnection.py	(revision 1598)
+++ sqlobject/mysql/mysqlconnection.py	(working copy)
@@ -122,10 +122,13 @@ class MySQLConnection(DBAPI):
         return 'INT NOT NULL'
 
     def tableExists(self, tableName):
-        for (table,) in self.queryAll('SHOW TABLES'):
-            if table.lower() == tableName.lower():
+        try:
+            self.query('DESCRIBE %s' % (tableName))
                 return True
+        except MySQLdb.ProgrammingError, e:
+            if e.args[0] == 1146: # ER_NO_SUCH_TABLE
         return False
+            raise
 
     def addColumn(self, tableName, column):
         self.query('ALTER TABLE %s ADD COLUMN %s' %

Reply via email to