[Zope-CVS] CVS: Products/Ape/lib/apelib/core - interfaces.py:1.2 schemas.py:1.2

Shane Hathaway shane@zope.com
Fri, 11 Apr 2003 02:18:20 -0400


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

Modified Files:
	interfaces.py schemas.py 
Log Message:
- Added a SQL query generator and converted all SQL gateways to use
it.  The current generator is designed for PostgreSQL, but it should
be simple to adapt for other databases.  This also involved
backward-incompatible changes to sqlbase.py--I hope no one minds.

- Added the IRelationalSchema interface, which declares the
getColumnDefs() method.  getColumnDefs() makes it easier to generate
SQL.  The basic schema types now support this interface.



=== Products/Ape/lib/apelib/core/interfaces.py 1.1 => 1.2 ===
--- Products/Ape/lib/apelib/core/interfaces.py:1.1	Wed Apr  9 23:09:55 2003
+++ Products/Ape/lib/apelib/core/interfaces.py	Fri Apr 11 02:17:49 2003
@@ -31,6 +31,16 @@
         """
 
 
+class IRelationalSchema(ISchema):
+    """Schema oriented for relational storage.
+
+    Designed to assist integration with relational databases."""
+
+    def getColumnDefs():
+        """Returns a sequence of (column_name, column_type, unique_flag)"""
+
+
+
 class IKeyedObjectSystem (Interface):
     """A collection of objects identifiable by keychain.
 
@@ -41,9 +51,9 @@
     def loadStub(keychain, hints=None):
         """Returns a class instance, possibly ghosted.
 
-        hints, a mapping, may be provided as an optimization.
-        Without it, implementations of this method may have to load a
-        full object rather than a ghosted object.
+        The hints argument, a mapping, may be provided as an
+        optimization.  Without it, implementations of this method may
+        have to load a full object rather than a ghosted object.
 
         Some hints may be:
 


=== Products/Ape/lib/apelib/core/schemas.py 1.1 => 1.2 ===
--- Products/Ape/lib/apelib/core/schemas.py:1.1	Wed Apr  9 23:09:55 2003
+++ Products/Ape/lib/apelib/core/schemas.py	Fri Apr 11 02:17:49 2003
@@ -18,10 +18,10 @@
 
 from types import StringType
 
-from interfaces import ISchema
+from interfaces import IRelationalSchema
 
 ok_types = ['unicode', 'string', 'int', 'float', 'bool', 'object',
-            'classification', 'keychain', 'string:list']
+            'classification', 'keychain', 'string:list', 'blob']
 
 
 def addFieldType(t):
@@ -33,7 +33,7 @@
 
 class FieldSchema:
     """Defines the schema of one field."""
-    __implements__ = ISchema
+    __implements__ = IRelationalSchema
 
     def __init__(self, name, type='string', unique=0):
         assert type in ok_types, type
@@ -41,6 +41,9 @@
         self.type = type
         self.unique = not not unique
 
+    def getColumnDefs(self):
+        return [(self.name, self.type, self.unique)]
+
     def __eq__(self, other):
         if isinstance(other, self.__class__):
             if (other.name == self.name) and (other.type == self.type) and (
@@ -49,14 +52,14 @@
         return 0  # Different
 
     def __repr__(self):
-        return 'FieldSchema(name=%s, type=%s, unique=%s)' % (
+        return 'FieldSchema(%s, %s, %s)' % (
             repr(self.name), repr(self.type), repr(self.unique))
 
 
 class RowSchema:
     """Defines an ordered set of fields for exactly one row.
     """
-    __implements__ = ISchema
+    __implements__ = IRelationalSchema
 
     def __init__(self, fields=()):
         self.fields = []
@@ -64,6 +67,12 @@
         for c in fields:
             self._add(c)
 
+    def getColumnDefs(self):
+        res = []
+        for f in self.fields:
+            res.extend(f.getColumnDefs())
+        return res
+
     def _add(self, c):
         if self.field_names.has_key(c.name):
             raise KeyError, 'Duplicate field name: %s' % c.name
@@ -86,7 +95,7 @@
 class RowSequenceSchema (RowSchema):
     """Defines a schema for a sequence of rows, including row count limits.
     """
-    __implements__ = ISchema
+    __implements__ = IRelationalSchema
 
     def __init__(self, fields=(), min_rows=0, max_rows=0):
         # max_rows == 0 means unlimited.