From 98fff37ea10df02cfbc28d4ba07deea1026c74be Mon Sep 17 00:00:00 2001
From: Vincent Pelletier <vincent@nexedi.com>
Date: Fri, 3 Aug 2007 13:08:38 +0000
Subject: [PATCH] Make Query and ComplexQuery hashable, so that they can be
 used as dictionnary key. It is valid as long as nobody use direct access to
 properties - there is no setter defined - and that extra care is taken not
 modify variants members which are provided at construction.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@15457 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ZSQLCatalog/SQLCatalog.py | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/product/ZSQLCatalog/SQLCatalog.py b/product/ZSQLCatalog/SQLCatalog.py
index 7c4d88524f..d7130c9bd5 100644
--- a/product/ZSQLCatalog/SQLCatalog.py
+++ b/product/ZSQLCatalog/SQLCatalog.py
@@ -258,6 +258,13 @@ class Query(QueryMixin):
   def __call__(self):
     self.asSQLExpression()
 
+  def __hash__(self):
+    value = self.value
+    if isinstance(value, list):
+      value = tuple(value)
+    return hash((self.format, self.operator, self.range, self.search_mode,
+                 self.table_alias_list, self.key, value, self.type))
+
   def getRange(self):
     return self.range
 
@@ -454,6 +461,10 @@ class ComplexQuery(QueryMixin):
   def __call__(self):
     self.asSQLExpression()
 
+  def __hash__(self):
+    subquery_hash = hash(tuple([hash(x) for x in self.getQueryList()]))
+    return hash((subquery_hash, self.operator))
+
   def getQueryList(self):
     return self.query_list
 
-- 
2.30.9