[Zope-CVS] CVS: Products/Ape/apelib/sql - structure.py:1.2

Shane Hathaway shane@zope.com
Sat, 29 Mar 2003 17:28:20 -0500


Update of /cvs-repository/Products/Ape/apelib/sql
In directory cvs.zope.org:/tmp/cvs-serv10503/apelib/sql

Modified Files:
	structure.py 
Log Message:
Made _p_mtime work.  The modification time comes through a gateway and
gets installed by a serializer.

Note that the strategy we're using for SQL is starting to become
burdensome.  There is a lot of information we could collect with a
single query rather than several (currently a minimum of four queries
per object).  CompositeGateway is perhaps too simple.

Also updated the style of imports in sqlmapper and fsmapper.  By
importing modules rather than classes, the import statements are
simpler.  I think this is nice but not applicable everywhere.


=== Products/Ape/apelib/sql/structure.py 1.1.1.1 => 1.2 ===
--- Products/Ape/apelib/sql/structure.py:1.1.1.1	Sat Mar 15 18:44:38 2003
+++ Products/Ape/apelib/sql/structure.py	Sat Mar 29 17:27:50 2003
@@ -16,6 +16,8 @@
 $Id$
 """
 
+import time
+
 from apelib.core.interfaces import IGateway
 from apelib.core.schemas import FieldSchema, RowSequenceSchema
 from sqlbase import SQLGatewayBase
@@ -211,4 +213,60 @@
     insert_sql = '''INSERT INTO %(table)s
     (key, pickle)
     VALUES (%(key)s, %(data)s)'''
+
+
+class SQLModTime (SQLGatewayBase):
+    """SQL object mod time gateway"""
+    # This class stores integers but the state is returned as a float.
+    # Integers avoid conflicts arising from rounding errors in the database.
+
+    __implements__ = IGateway
+
+    schema = FieldSchema('mtime', 'float')
+
+    table_base_name = 'mtime'
+
+    checkexist_sql = '''SELECT key FROM %(table)s WHERE key = 0'''
+
+    create_sql = '''CREATE TABLE %(table)s (
+    key int PRIMARY KEY,
+    mtime bigint
+    )'''
+
+    read_sql = '''SELECT mtime from %(table)s
+    WHERE key = %(key)s'''
+
+    update_sql = '''UPDATE %(table)s
+    SET mtime = %(mtime)d
+    WHERE key = %(key)s'''
+
+    insert_sql = '''INSERT INTO %(table)s
+    (key, mtime)
+    VALUES (%(key)s, %(mtime)d)'''
+
+
+    def getSchema(self):
+        return self.schema
+
+    def load(self, event):
+        key = long(event.getKey())
+        items = self.execute(self.read_sql, 1, key=key)
+        if items:
+            state = long(items[0][0])
+        else:
+            state = long(time.time())
+        return float(state), state
+
+    def store(self, event, state):
+        key = long(event.getKey())
+        state = long(state)
+        items = self.execute(self.read_sql, 1, key=key)
+        kw = {'key': key, 'mtime': state}
+        if items:
+            # update.
+            self.execute(self.update_sql, **kw)
+        else:
+            # insert.
+            self.execute(self.insert_sql, **kw)
+        return state