# HG changeset patch
# User martin f. krafft <madduck@madduck.net>
# Date 1333752357 -7200
# Node ID e5c2b3647971287139093df8722f5a38d7128fcd
# Parent  320531aa1280fd8cbd5a464b37c44a1be55e20ff
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.

diff -r 320531aa1280 -r e5c2b3647971 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, '