<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[14416] twext/branches/users/cdaboo</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.calendarserver.org//changeset/14416">14416</a></dd>
<dt>Author</dt> <dd>cdaboo@apple.com</dd>
<dt>Date</dt> <dd>2015-02-16 12:33:55 -0800 (Mon, 16 Feb 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Branch for pod-to-pod migration work.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#twextbranchesuserscdaboopod2podmigrationtwextenterprisedalrecordpy">twext/branches/users/cdaboo/pod2pod-migration/twext/enterprise/dal/record.py</a></li>
<li><a href="#twextbranchesuserscdaboopod2podmigrationtwextenterprisedaltesttest_recordpy">twext/branches/users/cdaboo/pod2pod-migration/twext/enterprise/dal/test/test_record.py</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li>twext/branches/users/cdaboo/pod2pod-migration/</li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="twextbranchesuserscdaboopod2podmigration"></a>
<div class="propset"><h4>Property changes: twext/branches/users/cdaboo/pod2pod-migration</h4>
<pre class="diff"><span>
</span></pre></div>
<a id="svnignore"></a>
<div class="addfile"><h4>Added: svn:ignore</h4></div>
<span class="cx">.project
</span><span class="cx">.pydevproject
</span><span class="cx">.trial
</span><span class="cx">.trial.lock
</span><span class="cx">dist
</span><span class="cx">twextpy.egg-info
</span><a id="svnmergeinfo"></a>
<div class="addfile"><h4>Added: svn:mergeinfo</h4></div>
<span class="cx">/twext/branches/users/cdaboo/jobs:12742-12780
</span><span class="cx">/twext/branches/users/sagen/recordtypes:13647-13658
</span><span class="cx">/twext/branches/users/sagen/recordtypes-2:13659
</span><a id="twextbranchesuserscdaboopod2podmigrationtwextenterprisedalrecordpy"></a>
<div class="modfile"><h4>Modified: twext/branches/users/cdaboo/pod2pod-migration/twext/enterprise/dal/record.py (14376 => 14416)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/enterprise/dal/record.py        2015-02-05 20:12:23 UTC (rev 14376)
+++ twext/branches/users/cdaboo/pod2pod-migration/twext/enterprise/dal/record.py        2015-02-16 20:33:55 UTC (rev 14416)
</span><span class="lines">@@ -198,7 +198,7 @@
</span><span class="cx">         words = columnName.lower().split(&quot;_&quot;)
</span><span class="cx"> 
</span><span class="cx">         def cap(word):
</span><del>-            if word.lower() == &quot;id&quot;:
</del><ins>+            if word.lower() in (&quot;id&quot;, &quot;uid&quot;,):
</ins><span class="cx">                 return word.upper()
</span><span class="cx">             else:
</span><span class="cx">                 return word.capitalize()
</span><span class="lines">@@ -246,41 +246,42 @@
</span><span class="cx"> 
</span><span class="cx">             MyRecord.create(transaction, column1=1, column2=u&quot;two&quot;)
</span><span class="cx">         &quot;&quot;&quot;
</span><ins>+        self = cls.make(**k)
+        yield self.insert(transaction)
+        returnValue(self)
+
+
+    @classmethod
+    def make(cls, **k):
+        &quot;&quot;&quot;
+        Make a record without creating one in the database - this will not have an
+        associated L{transaction}. When the record is ready to be written to the database
+        use L{SerializeableRecord.insert} to add it. Before it gets written to the DB, the
+        attributes can be changed.
+        &quot;&quot;&quot;
</ins><span class="cx">         self = cls()
</span><del>-        colmap = {}
</del><span class="cx">         attrtocol = cls.__attrmap__
</span><del>-        needsCols = []
-        needsAttrs = []
</del><span class="cx"> 
</span><span class="cx">         for attr in attrtocol:
</span><span class="cx">             col = attrtocol[attr]
</span><span class="cx">             if attr in k:
</span><del>-                setattr(self, attr, k[attr])
-                colmap[col] = k.pop(attr)
</del><ins>+                value = k.pop(attr)
+                setattr(self, attr, value)
</ins><span class="cx">             else:
</span><span class="cx">                 if col.model.needsValue():
</span><span class="cx">                     raise TypeError(
</span><span class="cx">                         &quot;required attribute {0!r} not passed&quot;
</span><span class="cx">                         .format(attr)
</span><span class="cx">                     )
</span><del>-                else:
-                    needsCols.append(col)
-                    needsAttrs.append(attr)
</del><span class="cx"> 
</span><span class="cx">         if k:
</span><span class="cx">             raise TypeError(&quot;received unknown attribute{0}: {1}&quot;.format(
</span><span class="cx">                 &quot;s&quot; if len(k) &gt; 1 else &quot;&quot;, &quot;, &quot;.join(sorted(k))
</span><span class="cx">             ))
</span><del>-        result = yield (Insert(colmap, Return=needsCols if needsCols else None)
-                        .on(transaction))
-        if needsCols:
-            self._attributesFromRow(zip(needsAttrs, result[0]))
</del><span class="cx"> 
</span><del>-        self.transaction = transaction
</del><ins>+        return self
</ins><span class="cx"> 
</span><del>-        returnValue(self)
</del><span class="cx"> 
</span><del>-
</del><span class="cx">     def _attributesFromRow(self, attributeList):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Take some data loaded from a row and apply it to this instance,
</span><span class="lines">@@ -296,6 +297,46 @@
</span><span class="cx">             setattr(self, setAttribute, setValue)
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    @inlineCallbacks
+    def insert(self, transaction):
+        &quot;&quot;&quot;
+        Insert a new a row for an existing record that was not initially created in the database.
+        &quot;&quot;&quot;
+
+        # Cannot do this if a transaction has already been assigned because that means
+        # the record already exists in the DB.
+
+        if self.transaction is not None:
+            raise ReadOnly(self.__class__.__name__, &quot;Cannot insert&quot;)
+
+        colmap = {}
+        attrtocol = self.__attrmap__
+        needsCols = []
+        needsAttrs = []
+
+        for attr in attrtocol:
+            col = attrtocol[attr]
+            v = getattr(self, attr)
+            if not isinstance(v, ColumnSyntax):
+                colmap[col] = v
+            else:
+                if col.model.needsValue():
+                    raise TypeError(
+                        &quot;required attribute {0!r} not passed&quot;
+                        .format(attr)
+                    )
+                else:
+                    needsCols.append(col)
+                    needsAttrs.append(attr)
+
+        result = yield (Insert(colmap, Return=needsCols if needsCols else None)
+                        .on(transaction))
+        if needsCols:
+            self._attributesFromRow(zip(needsAttrs, result[0]))
+
+        self.transaction = transaction
+
+
</ins><span class="cx">     def delete(self):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Delete this row from the database.
</span><span class="lines">@@ -435,6 +476,46 @@
</span><span class="cx">         @param noWait: include NOWAIT with the FOR UPDATE
</span><span class="cx">         @type noWait: L{bool}
</span><span class="cx">         &quot;&quot;&quot;
</span><ins>+        return cls._rowsFromQuery(
+            transaction,
+            cls.queryExpr(
+                expr,
+                order=order,
+                ascending=ascending,
+                group=group,
+                forUpdate=forUpdate,
+                noWait=noWait,
+                limit=limit,
+            ),
+            None
+        )
+
+
+    @classmethod
+    def queryExpr(cls, expr, attributes=None, order=None, ascending=True, group=None, forUpdate=False, noWait=False, limit=None):
+        &quot;&quot;&quot;
+        Query expression that corresponds to C{cls}. Used in cases where a sub-select
+        on this record's table is needed.
+
+        @param expr: An L{ExpressionSyntax} that constraints the results of the
+            query.  This is most easily produced by accessing attributes on the
+            class; for example, C{MyRecordType.query((MyRecordType.col1 &gt;
+            MyRecordType.col2).And(MyRecordType.col3 == 7))}
+
+        @param order: A L{ColumnSyntax} to order the resulting record objects
+            by.
+
+        @param ascending: A boolean; if C{order} is not C{None}, whether to
+            sort in ascending or descending order.
+
+        @param group: a L{ColumnSyntax} to group the resulting record objects
+            by.
+
+        @param forUpdate: do a SELECT ... FOR UPDATE
+        @type forUpdate: L{bool}
+        @param noWait: include NOWAIT with the FOR UPDATE
+        @type noWait: L{bool}
+        &quot;&quot;&quot;
</ins><span class="cx">         kw = {}
</span><span class="cx">         if order is not None:
</span><span class="cx">             kw.update(OrderBy=order, Ascending=ascending)
</span><span class="lines">@@ -446,15 +527,13 @@
</span><span class="cx">                 kw.update(NoWait=True)
</span><span class="cx">         if limit is not None:
</span><span class="cx">             kw.update(Limit=limit)
</span><del>-        return cls._rowsFromQuery(
-            transaction,
-            Select(
-                list(cls.table),
-                From=cls.table,
-                Where=expr,
-                **kw
-            ),
-            None
</del><ins>+        if attributes is None:
+            attributes = list(cls.table)
+        return Select(
+            attributes,
+            From=cls.table,
+            Where=expr,
+            **kw
</ins><span class="cx">         )
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -495,6 +574,21 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     @classmethod
</span><ins>+    def deletematch(cls, transaction, **kw):
+        &quot;&quot;&quot;
+        Delete all rows matching the specified attribute/values from the table that corresponds to C{cls}.
+        &quot;&quot;&quot;
+        where = None
+        for k, v in kw.iteritems():
+            subexpr = (cls.__attrmap__[k] == v)
+            if where is None:
+                where = subexpr
+            else:
+                where = where.And(subexpr)
+        return cls.deletesome(transaction, where)
+
+
+    @classmethod
</ins><span class="cx">     @inlineCallbacks
</span><span class="cx">     def _rowsFromQuery(cls, transaction, qry, rozrc):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="lines">@@ -521,3 +615,45 @@
</span><span class="cx">             self.transaction = transaction
</span><span class="cx">             selves.append(self)
</span><span class="cx">         returnValue(selves)
</span><ins>+
+
+
+class SerializableRecord(Record):
+    &quot;&quot;&quot;
+    An L{Record} that serializes/deserializes its attributes for a text-based
+    transport (e.g., JSON-over-HTTP) to allow records to be transferred from
+    one system to another (with potentially mismatched schemas).
+    &quot;&quot;&quot;
+
+    def serialize(self):
+        &quot;&quot;&quot;
+        Create an L{dict} of each attribute with L{str} values for each attribute
+        value. Sub-classes may need to override this to specialize certain value
+        conversions.
+
+        @return: mapping of attribute to string values
+        @rtype: L{dict} of L{str}:L{str}
+        &quot;&quot;&quot;
+
+        result = dict([(attr, getattr(self, attr),) for attr in self.__attrmap__])
+        return result
+
+
+    @classmethod
+    def deserialize(cls, attrmap):
+        &quot;&quot;&quot;
+        Given an L{dict} mapping attributes to values, create an L{Record} with
+        the specified values. Sub-classes may need to override this to handle special
+        values that need to be converted to specific types. They also need to override
+        this to handle possible schema mismatches (attributes no longer used, new
+        attributes not present in the map).
+
+        @param attrmap: serialized representation of a record
+        @type attrmap: L{dict} of L{str}:L{str}
+
+        @return: a newly created, but not inserted, record
+        @rtype: L{SerializableRecord}
+        &quot;&quot;&quot;
+
+        record = cls.make(**attrmap)
+        return record
</ins></span></pre></div>
<a id="twextbranchesuserscdaboopod2podmigrationtwextenterprisedaltesttest_recordpy"></a>
<div class="modfile"><h4>Modified: twext/branches/users/cdaboo/pod2pod-migration/twext/enterprise/dal/test/test_record.py (14376 => 14416)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/enterprise/dal/test/test_record.py        2015-02-05 20:12:23 UTC (rev 14376)
+++ twext/branches/users/cdaboo/pod2pod-migration/twext/enterprise/dal/test/test_record.py        2015-02-16 20:33:55 UTC (rev 14416)
</span><span class="lines">@@ -24,8 +24,8 @@
</span><span class="cx"> from twisted.trial.unittest import TestCase, SkipTest
</span><span class="cx"> 
</span><span class="cx"> from twext.enterprise.dal.record import (
</span><del>-    Record, fromTable, ReadOnly, NoSuchRecord
-)
</del><ins>+    Record, fromTable, ReadOnly, NoSuchRecord,
+    SerializableRecord)
</ins><span class="cx"> from twext.enterprise.dal.test.test_parseschema import SchemaTestHelper
</span><span class="cx"> from twext.enterprise.dal.syntax import SchemaSyntax
</span><span class="cx"> from twext.enterprise.fixtures import buildConnectionPool
</span><span class="lines">@@ -67,6 +67,13 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+class TestSerializeRecord(SerializableRecord, Alpha):
+    &quot;&quot;&quot;
+    A sample test serializable record with default values specified.
+    &quot;&quot;&quot;
+
+
+
</ins><span class="cx"> class TestAutoRecord(Record, Delta):
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     A sample test record with default values specified.
</span><span class="lines">@@ -335,6 +342,48 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     @inlineCallbacks
</span><ins>+    def test_deletesome(self):
+        &quot;&quot;&quot;
+        L{Record.deletesome} will delete all instances of the matching records.
+        &quot;&quot;&quot;
+        txn = self.pool.connection()
+        data = [(123, u&quot;one&quot;), (456, u&quot;four&quot;), (345, u&quot;three&quot;),
+                (234, u&quot;two&quot;), (356, u&quot;three&quot;)]
+        for beta, gamma in data:
+            yield txn.execSQL(&quot;insert into ALPHA values (:1, :2)&quot;,
+                              [beta, gamma])
+
+        yield TestRecord.deletesome(txn, TestRecord.gamma == u&quot;three&quot;)
+        all = yield TestRecord.all(txn)
+        self.assertEqual(set([record.beta for record in all]), set((123, 456, 234,)))
+
+        yield TestRecord.deletesome(txn, (TestRecord.gamma == u&quot;one&quot;).Or(TestRecord.gamma == u&quot;two&quot;))
+        all = yield TestRecord.all(txn)
+        self.assertEqual(set([record.beta for record in all]), set((456,)))
+
+
+    @inlineCallbacks
+    def test_deletematch(self):
+        &quot;&quot;&quot;
+        L{Record.deletematch} will delete all instances of the matching records.
+        &quot;&quot;&quot;
+        txn = self.pool.connection()
+        data = [(123, u&quot;one&quot;), (456, u&quot;four&quot;), (345, u&quot;three&quot;),
+                (234, u&quot;two&quot;), (356, u&quot;three&quot;)]
+        for beta, gamma in data:
+            yield txn.execSQL(&quot;insert into ALPHA values (:1, :2)&quot;,
+                              [beta, gamma])
+
+        yield TestRecord.deletematch(txn, gamma=u&quot;three&quot;)
+        all = yield TestRecord.all(txn)
+        self.assertEqual(set([record.beta for record in all]), set((123, 456, 234,)))
+
+        yield TestRecord.deletematch(txn, beta=123, gamma=u&quot;one&quot;)
+        all = yield TestRecord.all(txn)
+        self.assertEqual(set([record.beta for record in all]), set((456, 234)))
+
+
+    @inlineCallbacks
</ins><span class="cx">     def test_repr(self):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         The C{repr} of a L{Record} presents all its values.
</span><span class="lines">@@ -458,3 +507,59 @@
</span><span class="cx">         rec = yield TestRecord.load(txn, 234)
</span><span class="cx">         result = yield rec.trylock()
</span><span class="cx">         self.assertTrue(result)
</span><ins>+
+
+    @inlineCallbacks
+    def test_serialize(self):
+        &quot;&quot;&quot;
+        A L{SerializableRecord} may be serialized.
+        &quot;&quot;&quot;
+        txn = self.pool.connection()
+        for beta, gamma in [
+            (123, u&quot;one&quot;),
+            (234, u&quot;two&quot;),
+            (345, u&quot;three&quot;),
+            (356, u&quot;three&quot;),
+            (456, u&quot;four&quot;),
+        ]:
+            yield txn.execSQL(
+                &quot;insert into ALPHA values (:1, :2)&quot;, [beta, gamma]
+            )
+
+        rec = yield TestSerializeRecord.load(txn, 234)
+        result = rec.serialize()
+        self.assertEqual(result, {&quot;beta&quot;: 234, &quot;gamma&quot;: u&quot;two&quot;})
+
+
+    @inlineCallbacks
+    def test_deserialize(self):
+        &quot;&quot;&quot;
+        A L{SerializableRecord} may be deserialized.
+        &quot;&quot;&quot;
+        txn = self.pool.connection()
+
+        rec = yield TestSerializeRecord.deserialize({&quot;beta&quot;: 234, &quot;gamma&quot;: u&quot;two&quot;})
+        yield rec.insert(txn)
+        yield txn.commit()
+
+        txn = self.pool.connection()
+        rec = yield TestSerializeRecord.query(txn, TestSerializeRecord.beta == 234)
+        self.assertEqual(len(rec), 1)
+        self.assertEqual(rec[0].gamma, u&quot;two&quot;)
+        yield txn.commit()
+
+        # Check that attributes can be changed prior to insert, and not after
+        txn = self.pool.connection()
+        rec = yield TestSerializeRecord.deserialize({&quot;beta&quot;: 456, &quot;gamma&quot;: u&quot;one&quot;})
+        rec.gamma = u&quot;four&quot;
+        yield rec.insert(txn)
+        def _raise():
+            rec.gamma = u&quot;five&quot;
+        self.assertRaises(ReadOnly, _raise)
+        yield txn.commit()
+
+        txn = self.pool.connection()
+        rec = yield TestSerializeRecord.query(txn, TestSerializeRecord.beta == 456)
+        self.assertEqual(len(rec), 1)
+        self.assertEqual(rec[0].gamma, u&quot;four&quot;)
+        yield txn.commit()
</ins></span></pre>
</div>
</div>

</body>
</html>