Modify address check query to obtain well-defined result v0.6.x
authormartin f. krafft <madduck@madduck.net>
Sat, 07 Apr 2012 00:45:57 +0200
branchv0.6.x
changeset 492 e5c2b3647971
parent 491 320531aa1280
child 493 30365a87650d
Modify address check query to obtain well-defined result The way in which UNION does not yield the desired result, because (a) UNION merges results and (b) the result order is undefined. This patch changes the query to select the counts as columns and hence provides a well-defined order.
VirtualMailManager/domain.py
--- a/VirtualMailManager/domain.py	Fri Apr 06 21:34:23 2012 +0000
+++ b/VirtualMailManager/domain.py	Sat Apr 07 00:45:57 2012 +0200
@@ -96,13 +96,17 @@
         are accounts, aliases and/or relocated users.
         """
         dbc = self._dbh.cursor()
-        dbc.execute('SELECT count(gid) FROM users WHERE gid = %(gid)u '
-                    'UNION SELECT count(gid) FROM alias WHERE gid = %(gid)u '
-                    'UNION SELECT count(gid) FROM relocated WHERE gid = '
-                    '%(gid)u' % {'gid': self._gid})
+        dbc.execute('SELECT '
+                    '(SELECT count(gid) FROM users WHERE gid = %(gid)u)'
+                    '  as account_count, '
+                    '(SELECT count(gid) FROM alias WHERE gid = %(gid)u)'
+                    '  as alias_count, '
+                    '(SELECT count(gid) FROM relocated WHERE gid = %(gid)u)'
+                    '  as relocated_count'
+                    % {'gid': self._gid})
         result = dbc.fetchall()
         dbc.close()
-        result = [count[0] for count in result]
+        result = result[0]
         if any(result):
             keys = ('account_count', 'alias_count', 'relocated_count')
             raise DomErr(_(u'There are %(account_count)u accounts, '