#246 _connection ignored if sqlhub.processConnection set before

closed-fixed
General (125)
5
2009-05-18
2008-12-11
No

If sqlhub.processConnection is set before a subclass of SQLObject is defined, then the _connection attribute of the new class will be ignored. This is, as far as I could find out, because of:

sqlobject/main.py:768
if connection and not hasattr(cls, '_connection'):
cls.setConnection(connection)

This creates a lot of confusion and, if I understood correctly, is not the intended behaviour either (citing from the documentation):
"If you have defined sqlhub.processConnection then this attribute can be omitted from your class and the sqlhub will be used instead. If you have several classes using the same connection that might be an advantage, besides saving a lot of typing."

Discussion

  • Oleg Broytman

    Oleg Broytman - 2008-12-11
    • assigned_to: nobody --> phd
     
  • Oleg Broytman

    Oleg Broytman - 2008-12-11

    if connection and not hasattr(cls, '_connection')

    means "if there is a global connection and there is no _connection attribute". So I don't see how SQLObject can ignore _connection - there is no one.

    Can you write a short program that demonstrates the problem?

     
  • Luci Stanescu

    Luci Stanescu - 2008-12-11

    testsqlobject.py:
    from sqlobject import *

    a_connection = connectionForURI('mysql://user:pass@other_host/db')
    sqlhub.processConnection = connectionForURI('mysql://user:pass@host/db')

    class Foo(SQLObject):
    _connection = a_connection
    bar = StringCol(length=16)

    print Foo._connection.uri()

    luci@lucifer:~/sandbox$ python testsqlobject.py
    mysql://user:pass@host/db

    It was supposed to print `mysql://user:pass@other_host/db', which it does, if I move the line
    sqlhub.processConnection = connectionForURI('mysql://user:pass@host/db')
    after the class is defined.

     
  • Oleg Broytman

    Oleg Broytman - 2009-04-29

    The problem is hasattr(cls, '_connection') - even if there is the code del cls._connection a few lines above hasattr(cls, '_connection') return True in your case because there is a connection in SQLObject class. To fix this I propose to replace hasattr(cls, '_connection') with the test '_connection' not in cls.__dict__:

    @@ -768,7 +768,7 @@
    if hasattr(mod, '__connection__'):
    connection = mod.__connection__

    - if connection and not hasattr(cls, '_connection'):
    + if connection and ('_connection' not in cls.__dict__):
    cls.setConnection(connection)

    What do you think?

     
  • Oleg Broytman

    Oleg Broytman - 2009-05-18

    ixed in the revisions 3889:3891.

     
  • Oleg Broytman

    Oleg Broytman - 2009-05-18
    • status: open --> closed-fixed
     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks