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")
This all works when setting the style in "class sqlmeta:" for every sqlobject,
but how can I set a naming style by default for all sqlobjects?
The documentation says to set __connection__.style but I can't seem to make
that work.
I do:
from sqlobject import mysql
__connection__ = mysql.builder().connectionFromURI( "mysql://root:[EMAIL
PROTECTED]/test" )
__connection__.style = PrependDBStyle()
print "__connection__ is " + str(__connection__)
print "__connection__.style is " + str(__connection__.style)
and it shows:
__connection__ is <sqlobject.mysql.mysqlconnection.MySQLConnection instance at
0x110b6e8>
__connection__.style is <__main__.PrependDBStyle object at 0x1104db0>
No problem there, the style is the one I have set, but when doing:
class EventMapping(SQLObject):
strategy = StringCol()
scriptName = StringCol()
eventName = StringCol()
print EventMapping.sqlmeta.style
I get <sqlobject.styles.MixedCaseUnderscoreStyle...> which is the wrong style.
Debugging sqlobject.__classinit__, I see that it finds that getattr(cls,
'_connection', None)
works, and returns <sqlobject.mysql.mysqlconnection.MySQLConnection instance at
0x110b6e8>,
which is indeed the connection I have set... It just got the default style back
again, somehow...
(I also tried "sqlobject.sqlhub.processConnection = connection", no change)
--
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' %