SQLObject currently assumes that a default database was selected for the co=
nnection.
However I'm using SQLObject in a C++ application which executes python scri=
pts,
and the connection doesn't have a default database selected, on purpose.
With the following naming style I can make SQLObject prepend the database n=
ame
before creating a table or running queries in a table:
class PrependDBStyle(sqlobject.DefaultStyle):
def pythonClassToDBTable(self, className):
return "sqlobjects." + super(PrependDBStyle, self).pythonClassToDBTable(c=
lassName)
def dbTableToPythonClass(self, table):
if table.startsWith( "sqlobjects." ):
table =3D 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 sqlobje=
ct,
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__ =3D mysql.builder().connectionFromURI( "mysql://root:passwor=
d@localhost/test" )
__connection__.style =3D 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 =3D StringCol()
scriptName =3D StringCol()
eventName =3D StringCol()
print EventMapping.sqlmeta.style
I get <sqlobject.styles.MixedCaseUnderscoreStyle...> which is the wrong sty=
le.
Debugging sqlobject.__classinit__, I see that it finds that getattr(cls, '_=
connection', None)
works, and returns <sqlobject.mysql.mysqlconnection.MySQLConnection instanc=
e 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 =3D connection", no chang=
e)
=2D-=20
David Faure -- fa...@kd..., df...@kl...
KDE/KOffice developer, Qt consultancy projects
Klar=C3=A4lvdalens Datakonsult AB, Platform-independent software solutions
|