[CalendarServer-changes] [9563] CalendarServer/branches/users/glyph/q

source_changes at macosforge.org source_changes at macosforge.org
Sat Aug 11 01:54:10 PDT 2012


Revision: 9563
          http://trac.macosforge.org/projects/calendarserver/changeset/9563
Author:   glyph at apple.com
Date:     2012-08-11 01:54:10 -0700 (Sat, 11 Aug 2012)
Log Message:
-----------
Simple row loading.

Modified Paths:
--------------
    CalendarServer/branches/users/glyph/q/twext/enterprise/dal/record.py
    CalendarServer/branches/users/glyph/q/twext/enterprise/dal/syntax.py
    CalendarServer/branches/users/glyph/q/twext/enterprise/dal/test/test_record.py

Property Changed:
----------------
    CalendarServer/branches/users/glyph/q/

Modified: CalendarServer/branches/users/glyph/q/twext/enterprise/dal/record.py
===================================================================
--- CalendarServer/branches/users/glyph/q/twext/enterprise/dal/record.py	2012-08-11 08:54:09 UTC (rev 9562)
+++ CalendarServer/branches/users/glyph/q/twext/enterprise/dal/record.py	2012-08-11 08:54:10 UTC (rev 9563)
@@ -22,10 +22,26 @@
 L{twext.enterprise.dal.syntax}.
 """
 
+from twisted.internet.defer import inlineCallbacks, returnValue
+from twext.enterprise.dal.syntax import Select, Tuple, Constant, ColumnSyntax
+
+
+
 class _RecordBase(object):
     @classmethod
-    def load(cls, txn, primaryKey):
-        return cls()
+    @inlineCallbacks
+    def load(cls, txn, *primaryKey):
+        tbl = cls.__tbl__
+        pkey = Tuple([ColumnSyntax(c) for c in tbl.model.primaryKey])
+        allColumns = list(tbl)
+        slct = Select(allColumns, From=tbl, Where=pkey == Tuple(map(Constant, primaryKey)))
+        rows = yield slct.on(txn)
+        row = rows[0]
+        self = cls()
+        for (column, value) in zip(allColumns, row):
+            attrname = column.model.name.lower()
+            setattr(self, attrname, value)
+        returnValue(self)
 
 
 
@@ -33,9 +49,12 @@
     """
     Create a L{type} that maps the columns from a particular table.
 
-    A L{type} created in this manner will have instances with attributes that are mapp.
+    A L{type} created in this manner will have instances with attributes that
+    are mapped according to a naming convention like 'FOO_BAR' => 'fooBar'.
 
     @param table: The table.
+    @type table: L{twext.enterprise.dal.syntax.TableSyntax}
     """
-    return type(table.model.name, tuple([_RecordBase]), {})
+    return type(table.model.name, tuple([_RecordBase]),
+                dict(__tbl__=table))
 

Modified: CalendarServer/branches/users/glyph/q/twext/enterprise/dal/syntax.py
===================================================================
--- CalendarServer/branches/users/glyph/q/twext/enterprise/dal/syntax.py	2012-08-11 08:54:09 UTC (rev 9562)
+++ CalendarServer/branches/users/glyph/q/twext/enterprise/dal/syntax.py	2012-08-11 08:54:10 UTC (rev 9563)
@@ -956,7 +956,7 @@
     return True
 
 
-class Tuple(object):
+class Tuple(ExpressionSyntax):
 
     def __init__(self, columns):
         self.columns = columns

Modified: CalendarServer/branches/users/glyph/q/twext/enterprise/dal/test/test_record.py
===================================================================
--- CalendarServer/branches/users/glyph/q/twext/enterprise/dal/test/test_record.py	2012-08-11 08:54:09 UTC (rev 9562)
+++ CalendarServer/branches/users/glyph/q/twext/enterprise/dal/test/test_record.py	2012-08-11 08:54:10 UTC (rev 9563)
@@ -62,13 +62,18 @@
         con = connectionFactory()
         con.execute(schemaString)
         con.commit()
-        self.pool = ConnectionPool(connectionFactory)
+        self.pool = ConnectionPool(connectionFactory, paramstyle='numeric')
         self.pool.startService()
         self.addCleanup(self.pool.stopService)
 
 
     @inlineCallbacks
-    def test_simpleCreate(self):
+    def test_simpleLoad(self):
+        """
+        Loading an existing row from the database by its primary key will
+        populate its attributes from columns of the corresponding row in the
+        database.
+        """
         txn = self.pool.connection()
         yield txn.execSQL("insert into ALPHA values (:1, :2)", [234, "one"])
         yield txn.execSQL("insert into ALPHA values (:1, :2)", [456, "two"])
@@ -76,6 +81,10 @@
         self.assertIsInstance(rec, TestRecord)
         self.assertEquals(rec.beta, 456)
         self.assertEquals(rec.gamma, "two")
+        rec2 = yield TestRecord.load(txn, 234)
+        self.assertIsInstance(rec2, TestRecord)
+        self.assertEqual(rec2.beta, 234)
+        self.assertEqual(rec2.gamma, "one")
 
 
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20120811/a95cdadc/attachment-0001.html>


More information about the calendarserver-changes mailing list