[Mpuls-commits] r1950 - wasko/branches/2.0/jmdweb/lib

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Mon Mar 15 16:01:16 CET 2010


Author: torsten
Date: 2010-03-15 16:01:16 +0100 (Mon, 15 Mar 2010)
New Revision: 1950

Modified:
   wasko/branches/2.0/jmdweb/lib/search.py
Log:
* jmdweb/lib/search.py: Restructured search to make it better
	refactorable.


Modified: wasko/branches/2.0/jmdweb/lib/search.py
===================================================================
--- wasko/branches/2.0/jmdweb/lib/search.py	2010-03-15 14:37:57 UTC (rev 1949)
+++ wasko/branches/2.0/jmdweb/lib/search.py	2010-03-15 15:01:16 UTC (rev 1950)
@@ -28,6 +28,7 @@
 # Torsten Irländer <torsten.irlaender at intevation.de>
 #
 
+import logging
 import sys
 import re
 from datetime import datetime
@@ -45,19 +46,20 @@
                  'interval_end_field', 'gender', 'phase', 'branch', 'fkz',
                  'inconsistency', 'only_cases_after', 'bad', 'field']
 
-SEARCH_CASE_SQL = \
-""" SELECT
-      m.id,
-      %s,
-      st.status,
-      st.zugriff,
-      st.id as s_id
+SEARCH_QUERY = """
+    SELECT %(fields)s
     FROM master_tbl_view m
     JOIN ka_status_tbl_view st ON
     m.id = st.master_id
     JOIN ka_benutzer_tbl b ON
     m.bearbeiter_id = b.id
-    WHERE
+    WHERE %(where)s
+    %(order)s
+"""
+
+SEARCH_FIELDS = """m.id, %s, st.status, st.zugriff, st.id as s_id """
+
+SEARCH_WHERE = """
       %s
       AND st.status IN (%s)
       AND %s
@@ -71,11 +73,14 @@
       AND %s
       AND %s
       AND %s
-    ORDER BY %s %s"""
+"""
 
+SEARCH_ORDER = """ORDER BY %s %s"""
+
 # FIXME: This should be better a config var
 INCONSISTENCY_CHECK_AFTER = '2008-08-31'
 
+log = logging.getLogger(__name__)
 
 class Search:
 
@@ -117,14 +122,14 @@
                    fieldsdic):
         pass
 
-    def queryDB(self, sql, fields):
+    def queryDB(self, sql):
         '''Does the search on the DB and returns the result of the search'''
         rows = []
         con, cur = None, None
         try:
             con = db.getConnection()
             cur = con.cursor(cursor_factory=psycopg2.extras.DictCursor)
-            cur.execute(sql, fields)
+            cur.execute(sql)
             rows = cur.fetchall()
         finally:
             db.recycleConnection(con, cur)
@@ -134,9 +139,9 @@
         '''Returns the result set of a search based on the search string'''
         search, options, states, bad_types, phases, fields = \
                 self._parse_search_str(search_str)
-        sql, fields = self._build_sql(search, options, states, bad_types,
+        sql = self._build_sql(search, options, states, bad_types,
                                       phases, fields)
-        result = self.queryDB(sql, fields)
+        result = self.queryDB(sql)
         return result
 
 
@@ -147,8 +152,11 @@
         self.retrieve_fields = retrieve_fields
         self.match_fields = match_fields
 
-    def _build_sql(self, search, options, allowed_states, bad_types, phaseslist,
-                   fieldsdic):
+    def __get_fields(self, options):
+        retrieve = ", ".join("m." + field for field in self.retrieve_fields)
+        return SEARCH_FIELDS % retrieve
+
+    def __get_where(self, search, options, allowed_states, bad_types, phaseslist, fieldsdic):
         fields = {'needle': ""}
         ee = "TRUE"
 
@@ -170,8 +178,6 @@
                           and "TRUE" or allowed_viewer)
         editor = (options.has_key('editor')
                   and "m.bearbeiter_id = %s" % options.get('editor') or "TRUE")
-        sort_field = options.get('sort_field', "name")
-        sort_order = options.get('sort_order', "desc")
         branch = (options.get('branch')
                   and "b.filiale = '%s'" % options.get('branch') or "TRUE")
         fkz = (options.get('fkz')
@@ -257,21 +263,38 @@
                              " and (vater_staat != 2 or mutter_staat != 2))")
 
         fields['needle'] = " ".join(search)
-
-        retrieve = ", ".join("m." + field for field in self.retrieve_fields)
         needle_expr = ("((%s) OR (%s))"
-                       % (" OR ".join("m.%s ~* %%(needle)s"
+                       % (" OR ".join("m.%s ~* '%%(needle)s'"
                                       % field
                                       for field in self.match_fields),
                           " AND ".join("m.%s IS NULL"
                                        % field
                                        for field in self.match_fields)))
-        SQL = SEARCH_CASE_SQL % (retrieve, needle_expr,
-                                 ",".join([str(x) for x in allowed_states]),
-                                 ee, allowed_viewer, editor, gender,
-                                 time_interval, phases, branch, fkz, bad,
-                                 only_cases_after, migration, sort_field,
-                                 sort_order)
-        return (SQL, fields)
 
+        needle_expr = needle_expr % fields
+        return SEARCH_WHERE % (needle_expr, ",".join([str(x) for x in allowed_states]),
+                               ee, allowed_viewer, editor, gender, time_interval,
+                               phases, branch, fkz, bad, only_cases_after, migration) 
+
+
+
+    def __get_order(self, options):
+        sort_field = options.get('sort_field', "name")
+        sort_order = options.get('sort_order', "desc")
+        return SEARCH_ORDER % (sort_field, sort_order)
+
+    def _build_sql(self, search, options, allowed_states, bad_types, phaseslist,
+                   fieldsdic):
+
+        sql_fields = {}
+        sql_fields['fields'] = self.__get_fields(options)
+        sql_fields['where'] =  self.__get_where(search, options, allowed_states, 
+                                                bad_types, phaseslist, fieldsdic) 
+        sql_fields['order'] =  self.__get_order(options) 
+
+        log.debug(sql_fields)
+        SQL = SEARCH_QUERY % sql_fields
+        log.debug(SQL)
+        return (SQL)
+
 # vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:



More information about the Mpuls-commits mailing list