From d45976bb6255484fd82d1eb0029e896eabe7ac6c Mon Sep 17 00:00:00 2001
From: Jean-Paul Smets <jp@nexedi.com>
Date: Sat, 13 Jan 2007 15:16:04 +0000
Subject: [PATCH] Make the code compatible with Sql naming  (instead of SQL).
 Add the possibility to combine report and domain sections by using table
 aliasing.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@12060 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5Form/Selection.py | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)

diff --git a/product/ERP5Form/Selection.py b/product/ERP5Form/Selection.py
index eb7f3f3bd9..46f08c4bb6 100644
--- a/product/ERP5Form/Selection.py
+++ b/product/ERP5Form/Selection.py
@@ -190,7 +190,7 @@ class Selection(Acquisition.Implicit, Traversable, Persistent):
         if len(sort_on) > 0:
           kw['sort_on'] = sort_on
         elif kw.has_key('sort_on'):
-          del kw.params['sort_on'] # XXX JPS: Should this be really deleted ?
+          del kw['sort_on'] # We should not sort if no sort was defined
         if method is not None:
           if callable(method):
             if self.domain is not None and self.report is not None:
@@ -420,7 +420,8 @@ class DomainSelection(Acquisition.Implicit, Traversable, Persistent):
   security.declarePublic('asSQLExpression')
   def asSQLExpression(self, table_map=None, domain_id=None, 
                       exclude_domain_id=None, strict_membership=0,
-                      join_table="catalog", join_column="uid", base_category=None):
+                      join_table="catalog", join_column="uid", base_category=None,
+                      category_table_alias='category'):
     select_expression = []
     portal = self.getPortalObject()
     for k, d in self.domain_dict.iteritems():
@@ -432,13 +433,13 @@ class DomainSelection(Acquisition.Implicit, Traversable, Persistent):
                                strict_membership=strict_membership))
       elif k is not None:
         if getattr(aq_base(d), 'isPredicate', 0):
-          select_expression.append(d.asSQLExpression(table='%s_category' % k,
+          select_expression.append(d.asSQLExpression(table='%s_%s' % (k, category_table_alias),
                                                      strict_membership=strict_membership))
         else:
           # This is a category, we must join
-          select_expression.append('%s.%s = %s_category.uid' % \
-                                (join_table, join_column, k))
-          select_expression.append(d.asSQLExpression(table='%s_category' % k,
+          select_expression.append('%s.%s = %s_%s.uid' % \
+                                (join_table, join_column, k, category_table_alias))
+          select_expression.append(d.asSQLExpression(table='%s_%s' % (k, category_table_alias),
                                 base_category=k,
                                 strict_membership=strict_membership))
                                 # XXX We should take into account k explicitely
@@ -450,8 +451,12 @@ class DomainSelection(Acquisition.Implicit, Traversable, Persistent):
     #LOG('DomainSelection', 0, 'asSQLExpression returns %r' % (result,))
     return result
 
+  # Compatibility SQL Sql
+  security.declarePublic('asSqlExpression')
+  asSqlExpression = asSQLExpression
+  
   security.declarePublic('asSQLJoinExpression')
-  def asSQLJoinExpression(self, domain_id=None, exclude_domain_id=None):
+  def asSQLJoinExpression(self, domain_id=None, exclude_domain_id=None, category_table_alias='category'):
     join_expression = []
     #LOG('DomainSelection', 0, 'domain_id = %r, exclude_domain_id = %r, self.domain_dict = %r' % (domain_id, exclude_domain_id, self.domain_dict))
     portal = self.getPortalObject()
@@ -462,14 +467,18 @@ class DomainSelection(Acquisition.Implicit, Traversable, Persistent):
         pass
       elif k is not None:
         if getattr(aq_base(d), 'isPredicate', 0):
-          join_expression.append(d.asSQLJoinExpression(table='%s_category' % k))
+          join_expression.append(d.asSQLJoinExpression(table='%s_%s' % (k, category_table_alias)))
         else:
           # This is a category, we must join
-          join_expression.append('category AS %s_category' % k)
+          join_expression.append('category AS %s_%s' % (k, category_table_alias))
     result = "%s" % ' , '.join(join_expression)
     #LOG('DomainSelection', 0, 'asSQLJoinExpression returns %r' % (result,))
     return result
 
+  # Compatibility SQL Sql
+  security.declarePublic('asSqlJoinExpression')
+  asSqlJoinExpression = asSQLJoinExpression
+
   security.declarePublic('asDomainDict')
   def asDomainDict(self, domain_id=None, exclude_domain_id=None):
     return self.domain_dict
-- 
2.30.9