bugfix for when trying to quote enumerate types
[plcapi.git] / PLC / PostgreSQL.py
index 98ef727..076a864 100644 (file)
@@ -6,6 +6,7 @@
 # Copyright (C) 2006 The Trustees of Princeton University
 #
 # $Id$
+# $URL$
 #
 
 import psycopg2
@@ -52,6 +53,7 @@ class PostgreSQL:
     def __init__(self, api):
         self.api = api
         self.debug = False
+#        self.debug = True
         self.connection = None
 
     def cursor(self):
@@ -87,6 +89,12 @@ class PostgreSQL:
             self.connection.close()
             self.connection = None
 
+    # join insists on getting strings
+    @classmethod
+    def quote_string(self, value):
+        return str(PostgreSQL.quote(value))
+
+    @classmethod
     def quote(self, value):
         """
         Returns quoted version of the specified value.
@@ -95,12 +103,11 @@ class PostgreSQL:
         # The pgdb._quote function is good enough for general SQL
         # quoting, except for array types.
         if isinstance(value, (list, tuple, set)):
-            return "ARRAY[%s]" % ", ".join(map, self.quote, value)
+            return "ARRAY[%s]" % ", ".join(map (PostgreSQL.quote_string, value))
         else:
             return pgdb._quote(value)
 
-    quote = classmethod(quote)
-
+    @classmethod
     def param(self, name, value):
         # None is converted to the unquoted string NULL
         if isinstance(value, NoneType):
@@ -117,8 +124,6 @@ class PostgreSQL:
 
         return '%(' + name + ')' + conversion
 
-    param = classmethod(param)
-
     def begin_work(self):
         # Implicit in pgdb.connect()
         pass
@@ -165,8 +170,13 @@ class PostgreSQL:
 
             # psycopg2 requires %()s format for all parameters,
             # regardless of type.
+            # this needs to be done carefully though as with pattern-based filters
+            # we might have percents embedded in the query
+            # so e.g. GetPersons({'email':'*fake*'}) was resulting in .. LIKE '%sake%'
             if psycopg2:
                 query = re.sub(r'(%\([^)]*\)|%)[df]', r'\1s', query)
+            # rewrite wildcards set by Filter.py as '***' into '%'
+            query = query.replace ('***','%')
 
             if not params:
                 if self.debug:
@@ -221,7 +231,7 @@ class PostgreSQL:
         cursor = self.execute(query, params)
         rows = cursor.fetchall()
         cursor.close()
-
+        self.commit()
         if hashref or key_field is not None:
             # Return each row as a dictionary keyed on field name
             # (like DBI selectrow_hashref()).