where clause generator for gacksdb, filtering for get_invoices
smbaker [Tue, 24 Jul 2012 05:22:32 +0000 (22:22 -0700)]
apps/gacks/gacksaccountmanager.py
apps/gacks/gacksdb.py

index 46c18f9..d4965a4 100644 (file)
@@ -184,7 +184,8 @@ class GacksAccountManager(GacksDB):
     def get_accounts(self, filter={}):
         # XXX filter is currently unimplemented
         accounts = []
-        records = self.execute_dict("SELECT *,UNIX_TIMESTAMP(lastInTime) FROM `" + self.tablename + "`;")
+        (sql, params) = self.sql_filter(filter, sql="SELECT *,UNIX_TIMESTAMP(lastInTime) FROM `" + self.tablename + "`")
+        records = self.execute_dict(sql, params)
         for record in records:
             acct = GacksAccount(record["name"], record["kind"], manager=self)
             acct.id = record["id"]
index 0a7772b..c286c4d 100644 (file)
@@ -104,3 +104,30 @@ class GacksDB:
        else:
            self.execute("INSERT INTO `" + self.infoTableName + "` (name, version) VALUES('%s','%s');" % (name, str(version)))
 
+    def where_filter(self, filter, where=[], params=[], sql=None):
+       where=where[:]
+       params=params[:]
+
+       for (k, v) in filter.items():
+           if isinstance(v, list):
+               ors=[]
+               for item in v:
+                   ors.append(k + " = %s")
+                   params.append(str(item))
+               where.append( "(" + str(" OR ".join(ors)) + ")")
+           else:
+               where.append(k + " = %s")
+               params.append(str(v))
+
+       return (where, params)
+
+    def sql_filter(self, filter, where=[], params=[], sql=None):
+       (where, params) = self.where_filter(filter, where, params)
+       if where:
+           if sql:
+               sql = sql + " WHERE " + " AND ".join(where)
+           else:
+               sql = " WHERE " + " AND ".join(where)
+
+       return (sql, params)
+