~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/canonical/database/sqlbase.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2008-02-05 12:50:30 UTC
  • mfrom: (5398.8.31 testsuite)
  • Revision ID: launchpad@pqm.canonical.com-20080205125030-iremwgzs3myyft5b
[r=jtv,jamesh][!log] Initial mockdb work

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
           'clear_current_connection_cache', 'expire_from_cache']
30
30
 
31
31
# As per badly documented psycopg 1 constants
32
 
AUTOCOMMIT_ISOLATION=0
33
 
READ_COMMITTED_ISOLATION=1
34
 
SERIALIZABLE_ISOLATION=3
 
32
AUTOCOMMIT_ISOLATION = 0
 
33
READ_COMMITTED_ISOLATION = 1
 
34
SERIALIZABLE_ISOLATION = 3
35
35
# Default we want for scripts, and the PostgreSQL default. Note psycopg1 will
36
36
# use SERIALIZABLE unless we override, but psycopg2 will not.
37
 
DEFAULT_ISOLATION=READ_COMMITTED_ISOLATION
 
37
DEFAULT_ISOLATION = READ_COMMITTED_ISOLATION
38
38
 
39
39
# First, let's monkey-patch SQLObject a little:
40
40
import zope.security.proxy
76
76
 
77
77
    # dsilvers: 20050322: If you take this method out; then RelativeJoin
78
78
    # instances in our SQLObject classes cause the following error:
79
 
    # AttributeError: 'LaunchpadStyle' object has no attribute 'tableReference'
 
79
    # AttributeError: 'LaunchpadStyle' object has no attribute
 
80
    # 'tableReference'
80
81
    def tableReference(self, table):
81
82
        """Return the tablename mapped for use in RelativeJoin statements."""
82
83
        return table.__str__()
151
152
                    pass
152
153
                conn = None
153
154
 
154
 
            # Make a connection, and a cursor.  If this fails, just loop and try
155
 
            # again.
 
155
            # Make a connection, and a cursor.  If this fails, just loop and
 
156
            # try again.
156
157
            try:
157
158
                conn = connectionForURI(self.connectionURI).makeConnection()
158
159
                cur = conn.cursor()
199
200
        """Deactivate SQLBase._connection for the current thread."""
200
201
        tid = thread.get_ident()
201
202
        assert tid in self.transactions, (
202
 
            "Deactivating a non-active connection descriptor for this thread.")
 
203
            "Deactivating a non-active connection descriptor for this "
 
204
            "thread.")
203
205
        self.transactions[tid]._connection.close()
204
206
        self.transactions[tid]._makeObsolete()
205
207
        del self.transactions[tid]
206
208
 
207
209
    def __get__(self, inst, cls=None):
208
 
        """Return Transaction object for this thread (if it exists) or None."""
 
210
        """Return Transaction object for this thread (if it exists) or None.
 
211
        """
209
212
        tid = thread.get_ident()
210
213
        if self.implicitActivate and tid not in self.transactions:
211
214
            self._activate()
247
250
            trans._dbConnection._connection.close()
248
251
 
249
252
        # Remove the _connection descriptor.  This assumes there was no
250
 
        # _connection in this particular class to start with (which is true for
251
 
        # SQLBase, but wouldn't be true for SQLOS)
 
253
        # _connection in this particular class to start with (which is true
 
254
        # for SQLBase, but wouldn't be true for SQLOS)
252
255
        del cls.sqlClass._connection
253
256
 
254
257
 
287
290
                        "A ZopelessTransactionManager with different "
288
291
                        "settings is already installed"
289
292
                )
290
 
            # There's an identical ZopelessTransactionManager already installed,
291
 
            # so return that one, but also emit a warning.
 
293
            # There's an identical ZopelessTransactionManager already
 
294
            # installed, so return that one, but also emit a warning.
292
295
            warnings.warn(alreadyInstalledMsg, stacklevel=2)
293
296
            return cls._installed
294
297
        cls._installed = object.__new__(cls, connectionURI, sqlClass, debug,
339
342
        con.set_isolation_level(level)
340
343
        # Make the isolation level stick
341
344
        self.desc.isolation = level
342
 
        cur = con.cursor()
343
 
        cur.execute('SHOW transaction_isolation')
344
 
        isolation_str = cur.fetchone()[0]
345
 
        if level == AUTOCOMMIT_ISOLATION:
346
 
            # psycopg implements autocommit using read committed and commits.
347
 
            assert isolation_str == 'read committed', 'Got ' + isolation_str
348
 
        elif level == READ_COMMITTED_ISOLATION:
349
 
            assert isolation_str == 'read committed', 'Got ' + isolation_str
350
 
        elif level == SERIALIZABLE_ISOLATION:
351
 
            assert isolation_str == 'serializable', 'Got ' + isolation_str
352
 
        else:
353
 
            raise AssertionError("Unknown transaction isolation level")
354
345
 
355
346
    def conn(self):
356
347
        return self.sqlClass._connection._connection
703
694
        ZopelessTransactionManager._installed = None
704
695
 
705
696
    # XXX Andrew Bennetts 2005-07-12:
706
 
    #      Ideally I'd be able to re-use some of the ZopelessTransactionManager
707
 
    #      implementation of begin, commit and abort.
 
697
    #      Ideally I'd be able to re-use some of the
 
698
    #      ZopelessTransactionManager implementation of begin, commit
 
699
    #      and abort.
708
700
    def begin(self):
709
701
        if not self.implicitBegin:
710
702
            self.desc._activate()