[Zope-CVS] CVS: Products/Ape/lib/apelib/zodb3 - storage.py:1.4

Shane Hathaway shane@zope.com
Mon, 19 May 2003 15:33:06 -0400


Update of /cvs-repository/Products/Ape/lib/apelib/zodb3
In directory cvs.zope.org:/tmp/cvs-serv1802/zodb3

Modified Files:
	storage.py 
Log Message:
Provided new machinery for initializing databases, especially creating
tables.

- Added two new interfaces, IDatabaseInitializer and
IDatabaseInitEvent.  The initializer.init(event) gets called when new
connections are made to the database.

- All gateways in the sql subpackage now implement IDatabaseInitializer.

- Added methods to mappers for configuring and using initializers.

- Renamed setUp() to init(), since it's easier to spell.

- Avoided calling initializers more than once.

- Renamed 'object' to 'obj' in some places.



=== Products/Ape/lib/apelib/zodb3/storage.py 1.3 => 1.4 ===
--- Products/Ape/lib/apelib/zodb3/storage.py:1.3	Sun May 18 00:16:30 2003
+++ Products/Ape/lib/apelib/zodb3/storage.py	Mon May 19 15:32:35 2003
@@ -22,7 +22,8 @@
 
 from ZODB import POSException, BaseStorage
 
-from apelib.core.events import MapperEvent, GatewayEvent, LoadEvent, StoreEvent
+from apelib.core.events \
+     import MapperEvent, GatewayEvent, LoadEvent, StoreEvent, DatabaseInitEvent
 from apelib.core.interfaces import ITPCConnection
 from apelib.core.exceptions import NoStateFoundError, ConfigurationError
 from consts import HASH0, HASH1, DEBUG
@@ -61,7 +62,7 @@
                 c.connect()
                 opened.append((sort_key, c))
                 names.append(c.getName())
-            self.setUpGateways(clear_all)
+            self.initDatabases(clear_all)
         except:
             for sort_key, c in opened:
                 c.close()
@@ -91,19 +92,26 @@
     def getMapperResource(self):
         return self._mapper_resource
 
-    def setUpGateways(self, clear_all=0):
-        """Calls a method of all gateways, passing a GatewayEvent.
+    def initDatabases(self, clear_all=0):
+        """Creates tables, etc.
         """
         root_mapper = self._mapper_resource.access(self)
+        # Find all initializers, eliminating duplicates.
+        initializers = {}  # obj -> 1
         todo = [root_mapper]
         while todo:
             mapper = todo.pop()
-            gw = mapper.getGateway()
-            event = GatewayEvent(mapper, None, self._conn_map)
-            gw.setUp(event, clear_all)
-            for name in mapper.listSubMappers():
-                m = mapper.getSubMapper(name)
-                todo.append(m)
+            for obj in mapper.getInitializers():
+                initializers[obj] = 1
+            sub = mapper.listSubMapperNames()
+            if sub:
+                for name in sub:
+                    m = mapper.getSubMapper(name)
+                    todo.append(m)
+        # Now call them.
+        for initializer in initializers.keys():
+            event = DatabaseInitEvent(self._conn_map, clear_all)
+            initializer.init(event)
 
     def hash64(self, value):
         """Returns an 8-byte hash value.