[CalendarServer-changes] [14418] twext/branches/users/cdaboo/pod2pod-migration/twext/enterprise/dal
source_changes at macosforge.org
source_changes at macosforge.org
Mon Feb 16 16:04:44 PST 2015
Revision: 14418
http://trac.calendarserver.org//changeset/14418
Author: cdaboo at apple.com
Date: 2015-02-16 16:04:44 -0800 (Mon, 16 Feb 2015)
Log Message:
-----------
New simple query option, other bits.
Modified Paths:
--------------
twext/branches/users/cdaboo/pod2pod-migration/twext/enterprise/dal/record.py
twext/branches/users/cdaboo/pod2pod-migration/twext/enterprise/dal/test/test_record.py
Modified: twext/branches/users/cdaboo/pod2pod-migration/twext/enterprise/dal/record.py
===================================================================
--- twext/branches/users/cdaboo/pod2pod-migration/twext/enterprise/dal/record.py 2015-02-16 20:56:21 UTC (rev 14417)
+++ twext/branches/users/cdaboo/pod2pod-migration/twext/enterprise/dal/record.py 2015-02-17 00:04:44 UTC (rev 14418)
@@ -171,6 +171,14 @@
return r
+ def __eq__(self, other):
+ if type(self) != type(other):
+ return False
+ attrs = dict([(attr, getattr(self, attr),) for attr in self.__attrmap__.keys()])
+ otherattrs = dict([(attr, getattr(other, attr),) for attr in other.__attrmap__.keys()])
+ return attrs == otherattrs
+
+
@classmethod
def fromTable(cls, table):
"""
@@ -282,6 +290,14 @@
return self
+ def duplicate(self):
+ return self.make(**dict([(attr, getattr(self, attr),) for attr in self.__attrmap__.keys()]))
+
+
+ def isnew(self):
+ return self.transaction is None
+
+
def _attributesFromRow(self, attributeList):
"""
Take some data loaded from a row and apply it to this instance,
@@ -538,6 +554,22 @@
@classmethod
+ def querysimple(cls, transaction, **kw):
+ """
+ Match all rows matching the specified attribute/values from the table that corresponds to C{cls}.
+ All attributes are logically AND'ed.
+ """
+ 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.query(transaction, where)
+
+
+ @classmethod
def all(cls, transaction):
"""
Load all rows from the table that corresponds to C{cls} and return
@@ -574,9 +606,10 @@
@classmethod
- def deletematch(cls, transaction, **kw):
+ def deletesimple(cls, transaction, **kw):
"""
Delete all rows matching the specified attribute/values from the table that corresponds to C{cls}.
+ All attributes are logically AND'ed.
"""
where = None
for k, v in kw.iteritems():
Modified: twext/branches/users/cdaboo/pod2pod-migration/twext/enterprise/dal/test/test_record.py
===================================================================
--- twext/branches/users/cdaboo/pod2pod-migration/twext/enterprise/dal/test/test_record.py 2015-02-16 20:56:21 UTC (rev 14417)
+++ twext/branches/users/cdaboo/pod2pod-migration/twext/enterprise/dal/test/test_record.py 2015-02-17 00:04:44 UTC (rev 14418)
@@ -307,6 +307,44 @@
@inlineCallbacks
+ def test_querySimple(self):
+ """
+ L{Record.querysimple} will allow you to query for a record by its class
+ attributes as columns.
+ """
+ txn = self.pool.connection()
+ for beta, gamma in [(123, u"one"), (234, u"two"), (345, u"three"),
+ (356, u"three"), (456, u"four")]:
+ yield txn.execSQL("insert into ALPHA values (:1, :2)",
+ [beta, gamma])
+ records = yield TestRecord.querysimple(txn, gamma=u"three")
+ self.assertEqual(len(records), 2)
+ records.sort(key=lambda x: x.beta)
+ self.assertEqual(records[0].beta, 345)
+ self.assertEqual(records[1].beta, 356)
+
+
+ @inlineCallbacks
+ def test_eq(self):
+ """
+ L{Record.__eq__} works.
+ """
+ txn = self.pool.connection()
+ data = [(123, u"one"), (456, u"four"), (345, u"three"),
+ (234, u"two"), (356, u"three")]
+ for beta, gamma in data:
+ yield txn.execSQL("insert into ALPHA values (:1, :2)",
+ [beta, gamma])
+
+ one = yield TestRecord.load(txn, 123)
+ one_copy = yield TestRecord.load(txn, 123)
+ two = yield TestRecord.load(txn, 234)
+
+ self.assertTrue(one == one_copy)
+ self.assertFalse(one == two)
+
+
+ @inlineCallbacks
def test_all(self):
"""
L{Record.all} will return all instances of the record, sorted by
@@ -363,9 +401,9 @@
@inlineCallbacks
- def test_deletematch(self):
+ def test_deletesimple(self):
"""
- L{Record.deletematch} will delete all instances of the matching records.
+ L{Record.deletesimple} will delete all instances of the matching records.
"""
txn = self.pool.connection()
data = [(123, u"one"), (456, u"four"), (345, u"three"),
@@ -374,11 +412,11 @@
yield txn.execSQL("insert into ALPHA values (:1, :2)",
[beta, gamma])
- yield TestRecord.deletematch(txn, gamma=u"three")
+ yield TestRecord.deletesimple(txn, gamma=u"three")
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"one")
+ yield TestRecord.deletesimple(txn, beta=123, gamma=u"one")
all = yield TestRecord.all(txn)
self.assertEqual(set([record.beta for record in all]), set((456, 234)))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20150216/a343c72c/attachment.html>
More information about the calendarserver-changes
mailing list