Update of /usr/cvs/Public/pygresql/module
In directory druid.net:/tmp/cvs-serv29377

Modified Files:
        pg.py test_pg.py 
Log Message:
Allow the DB wrapper to be used with an existing _pg, DB or pgdb connection.
To see the diffs for this commit:
   
http://www.druid.net/pygresql/viewcvs.cgi/cvs/pygresql/module/pg.py.diff?r1=1.56&r2=1.57

Index: pg.py
===================================================================
RCS file: /usr/cvs/Public/pygresql/module/pg.py,v
retrieving revision 1.56
retrieving revision 1.57
diff -u -r1.56 -r1.57
--- pg.py       9 Oct 2008 16:28:23 -0000       1.56
+++ pg.py       9 Oct 2008 20:45:29 -0000       1.57
@@ -5,7 +5,7 @@
 # Written by D'Arcy J.M. Cain
 # Improved by Christoph Zwerschke
 #
-# $Id: pg.py,v 1.56 2008/10/09 16:28:23 cito Exp $
+# $Id: pg.py,v 1.57 2008/10/09 20:45:29 cito Exp $
 #
 
 """PyGreSQL classic interface.
@@ -118,8 +118,34 @@
        """Wrapper class for the _pg connection type."""
 
        def __init__(self, *args, **kw):
-               self.db = connect(*args, **kw)
-               self.dbname = self.db.db
+               """Create a new connection.
+
+               You can pass either the connection parameters or an existing
+               _pg or pgdb connection. This allows you to use the methods
+               of the classic pg interface with a DB-API 2 pgdb connection.
+
+               """
+               if not args and len(kw) == 1:
+                       db = kw.get('db')
+               elif not kw and len(args) == 1:
+                       db = args[0]
+               else:
+                       db = None
+               if db:
+                       if isinstance(db, DB):
+                               db = db.db
+                       else:
+                               try:
+                                       db = db._cnx
+                               except AttributeError:
+                                       pass
+               if not db or not hasattr(db, 'db') or not hasattr(db, 'query'):
+                       db = connect(*args, **kw)
+                       self._closeable = 1
+               else:
+                       self._closeable = 0
+               self.db = db
+               self.dbname = db.db
                self._attnames = {}
                self._pkeys = {}
                self._args = args, kw
@@ -152,11 +178,12 @@
        def close(self):
                """Close the database connection."""
                # Wraps shared library function so we can track state.
-               if self.db:
-                       self.db.close()
-                       self.db = None
-               else:
-                       raise InternalError('Connection already closed')
+               if self._closeable:
+                       if self.db:
+                               self.db.close()
+                               self.db = None
+                       else:
+                               raise InternalError('Connection already closed')
 
        def reopen(self):
                """Reopen connection to the database.
@@ -165,13 +192,15 @@
                Note that we can still reopen a database that we have closed.
 
                """
-               if self.db:
-                       self.db.close()
-               try:
-                       self.db = connect(*self._args[0], **self._args[1])
-               except:
-                       self.db = None
-                       raise
+               # There is no such shared library function.
+               if self._closeable:
+                       if self.db:
+                               self.db.close()
+                       try:
+                               self.db = connect(*self._args[0], 
**self._args[1])
+                       except:
+                               self.db = None
+                               raise
 
        def query(self, qstr):
                """Executes a SQL command string.

   
http://www.druid.net/pygresql/viewcvs.cgi/cvs/pygresql/module/test_pg.py.diff?r1=1.12&r2=1.13

Index: test_pg.py
===================================================================
RCS file: /usr/cvs/Public/pygresql/module/test_pg.py,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- test_pg.py  16 Sep 2008 22:29:48 -0000      1.12
+++ test_pg.py  9 Oct 2008 20:45:29 -0000       1.13
@@ -4,7 +4,7 @@
 #
 # Written by Christoph Zwerschke
 #
-# $Id: test_pg.py,v 1.12 2008/09/16 22:29:48 cito Exp $
+# $Id: test_pg.py,v 1.13 2008/10/09 20:45:29 cito Exp $
 #
 
 """Test the classic PyGreSQL interface in the pg module.
@@ -775,6 +775,27 @@
                self.assertRaises(pg.InternalError, self.db.query, 'select 1')
                self.db = pg.DB(self.dbname)
 
+       def testExistingConnection(self):
+               db = pg.DB(self.db.db)
+               self.assertEqual(self.db.db, db.db)
+               self.assert_(db.db)
+               db.close()
+               self.assert_(db.db)
+               db.reopen()
+               self.assert_(db.db)
+               db.close()
+               self.assert_(db.db)
+               db = pg.DB(self.db)
+               self.assertEqual(self.db.db, db.db)
+               db = pg.DB(db=self.db.db)
+               self.assertEqual(self.db.db, db.db)
+               class DB2:
+                       pass
+               db2 = DB2()
+               db2._cnx = self.db.db
+               db = pg.DB(db2)
+               self.assertEqual(self.db.db, db.db)
+
 
 class TestDBClass(unittest.TestCase):
        """"Test the methods of the DB class wrapped pg connection."""

_______________________________________________
PyGreSQL mailing list
[email protected]
http://mailman.vex.net/mailman/listinfo/pygresql

Reply via email to