- use Filter to select rows
authorMark Huang <mlhuang@cs.princeton.edu>
Wed, 8 Nov 2006 23:03:05 +0000 (23:03 +0000)
committerMark Huang <mlhuang@cs.princeton.edu>
Wed, 8 Nov 2006 23:03:05 +0000 (23:03 +0000)
PLC/NodeNetworks.py
PLC/Roles.py
PLC/SliceAttributeTypes.py
PLC/SliceAttributes.py

index 47cda57..83cd6e1 100644 (file)
@@ -4,7 +4,7 @@
 # Mark Huang <mlhuang@cs.princeton.edu>
 # Copyright (C) 2006 The Trustees of Princeton University
 #
-# $Id: NodeNetworks.py,v 1.11 2006/10/25 14:29:13 mlhuang Exp $
+# $Id: NodeNetworks.py,v 1.12 2006/11/02 18:32:55 mlhuang Exp $
 #
 
 from types import StringTypes
@@ -13,6 +13,7 @@ import struct
 
 from PLC.Faults import *
 from PLC.Parameter import Parameter
+from PLC.Filter import Filter
 from PLC.Debug import profile
 from PLC.Table import Row, Table
 from PLC.NetworkTypes import NetworkType, NetworkTypes
@@ -191,26 +192,17 @@ class NodeNetworks(Table):
     database.
     """
 
-    def __init__(self, api, nodenetwork_id_or_ip_list = None):
-        self.api = api
+    def __init__(self, api, nodenetwork_filter = None):
+        Table.__init__(self, api, NodeNetwork)
 
-        sql = "SELECT %s FROM nodenetworks" % \
+        sql = "SELECT %s FROM nodenetworks WHERE True" % \
               ", ".join(NodeNetwork.fields)
 
-        if nodenetwork_id_or_ip_list:
-            # Separate the list into integers and strings
-            nodenetwork_ids = filter(lambda nodenetwork_id: isinstance(nodenetwork_id, (int, long)),
-                                     nodenetwork_id_or_ip_list)
-            ips = filter(lambda ip: isinstance(ip, StringTypes),
-                               nodenetwork_id_or_ip_list)
-            sql += " WHERE (False"
-            if nodenetwork_ids:
-                sql += " OR nodenetwork_id IN (%s)" % ", ".join(map(str, nodenetwork_ids))
-            if ips:
-                sql += " OR ip IN (%s)" % ", ".join(api.db.quote(ips)).lower()
-            sql += ")"
-
-        rows = self.api.db.selectall(sql)
-
-        for row in rows:
-            self[row['nodenetwork_id']] = NodeNetwork(api, row)
+        if nodenetwork_filter is not None:
+            if isinstance(nodenetwork_filter, list):
+                nodenetwork_filter = Filter(NodeNetwork.fields, {'nodenetwork_id': nodenetwork_filter})
+            elif isinstance(nodenetwork_filter, dict):
+                nodenetwork_filter = Filter(NodeNetwork.fields, nodenetwork_filter)
+            sql += " AND (%s)" % nodenetwork_filter.sql(api)
+
+        self.selectall(sql)
index 404827b..aad1019 100644 (file)
@@ -4,12 +4,13 @@
 # Mark Huang <mlhuang@cs.princeton.edu>
 # Copyright (C) 2006 The Trustees of Princeton University
 #
-# $Id: Roles.py,v 1.5 2006/10/20 17:53:54 mlhuang Exp $
+# $Id: Roles.py,v 1.6 2006/10/24 20:02:22 mlhuang Exp $
 #
 
 from types import StringTypes
 from PLC.Faults import *
 from PLC.Parameter import Parameter
+from PLC.Filter import Filter
 from PLC.Table import Row, Table
 
 class Role(Row):
@@ -51,24 +52,21 @@ class Roles(Table):
     Representation of the roles table in the database.
     """
 
-    def __init__(self, api, role_id_or_name_list = None):
-        sql = "SELECT %s FROM roles" % \
+    def __init__(self, api, role_filter = None):
+        Table.__init__(self, api, Role)
+
+        sql = "SELECT %s FROM roles WHERE True" % \
               ", ".join(Role.fields)
         
-        if role_id_or_name_list:
-            # Separate the list into integers and strings
-            role_ids = filter(lambda role_id: isinstance(role_id, (int, long)),
-                                   role_id_or_name_list)
-            names = filter(lambda name: isinstance(name, StringTypes),
-                           role_id_or_name_list)
-            sql += " WHERE (False"
-            if role_ids:
-                sql += " OR role_id IN (%s)" % ", ".join(map(str, role_ids))
-            if names:
-                sql += " OR name IN (%s)" % ", ".join(api.db.quote(names))
-            sql += ")"
-
-        rows = api.db.selectall(sql)
+        if role_filter is not None:
+            if isinstance(role_filter, list):
+                # Separate the list into integers and strings
+                ints = filter(lambda x: isinstance(x, (int, long)), role_filter)
+                strs = filter(lambda x: isinstance(x, StringTypes), role_filter)
+                role_filter = Filter(Role.fields, {'role_id': ints, 'name': strs})
+                sql += " AND (%s)" % role_filter.sql(api, "OR")
+            elif isinstance(role_filter, dict):
+                role_filter = Filter(Role.fields, role_filter)
+                sql += " AND (%s)" % role_filter.sql(api, "AND")
 
-        for row in rows:
-            self[row['role_id']] = Role(api, row)
+        self.selectall(sql)
index 2f6132c..a3ed881 100644 (file)
@@ -2,6 +2,7 @@ from types import StringTypes
 
 from PLC.Faults import *
 from PLC.Parameter import Parameter
+from PLC.Filter import Filter
 from PLC.Table import Row, Table
 from PLC.Roles import Role, Roles
 
@@ -45,26 +46,21 @@ class SliceAttributeTypes(Table):
     database.
     """
 
-    def __init__(self, api, attribute_type_id_or_name_list = None):
-       self.api = api
+    def __init__(self, api, attribute_type_filter = None):
+        Table.__init__(self, api, SliceAttributeType)
 
-        sql = "SELECT %s FROM slice_attribute_types" % \
+        sql = "SELECT %s FROM slice_attribute_types WHERE True" % \
               ", ".join(SliceAttributeType.fields)
 
-        if attribute_type_id_or_name_list:
-            # Separate the list into integers and strings
-            attribute_type_ids = filter(lambda attribute_type_id: isinstance(attribute_type_id, (int, long)),
-                                   attribute_type_id_or_name_list)
-            names = filter(lambda name: isinstance(name, StringTypes),
-                           attribute_type_id_or_name_list)
-            sql += " WHERE (False"
-            if attribute_type_ids:
-                sql += " OR attribute_type_id IN (%s)" % ", ".join(map(str, attribute_type_ids))
-            if names:
-                sql += " OR name IN (%s)" % ", ".join(api.db.quote(names))
-            sql += ")"
+        if attribute_type_filter is not None:
+            if isinstance(attribute_type_filter, list):
+                # Separate the list into integers and strings
+                ints = filter(lambda x: isinstance(x, (int, long)), attribute_type_filter)
+                strs = filter(lambda x: isinstance(x, StringTypes), attribute_type_filter)
+                attribute_type_filter = Filter(SliceAttributeType.fields, {'attribute_type_id': ints, 'name': strs})
+                sql += " AND (%s)" % attribute_type_filter.sql(api, "OR")
+            elif isinstance(attribute_type_filter, dict):
+                attribute_type_filter = Filter(SliceAttributeType.fields, attribute_type_filter)
+                sql += " AND (%s)" % attribute_type_filter.sql(api, "AND")
 
-        rows = self.api.db.selectall(sql)
-        for row in rows:
-            self[row['attribute_type_id']] = SliceAttributeType(api, row)
+        self.selectall(sql)
index f7c3499..1354444 100644 (file)
@@ -1,7 +1,6 @@
-from types import StringTypes
-
 from PLC.Faults import *
 from PLC.Parameter import Parameter
+from PLC.Filter import Filter
 from PLC.Table import Row, Table
 from PLC.SliceAttributeTypes import SliceAttributeType, SliceAttributeTypes
 
@@ -31,16 +30,17 @@ class SliceAttributes(Table):
     database.
     """
 
-    def __init__(self, api, slice_attribute_id_list = None):
-       self.api = api
+    def __init__(self, api, slice_attribute_filter = None):
+        Table.__init__(self, api, SliceAttribute)
 
-        sql = "SELECT %s FROM view_slice_attributes" % \
+        sql = "SELECT %s FROM view_slice_attributes WHERE True" % \
               ", ".join(SliceAttribute.fields)
 
-        if slice_attribute_id_list:
-            sql += " WHERE slice_attribute_id IN (%s)" % ", ".join(map(str, slice_attribute_id_list))
+        if slice_attribute_filter is not None:
+            if isinstance(slice_attribute_filter, list):
+                slice_attribute_filter = Filter(SliceAttribute.fields, {'slice_attribute_id': slice_attribute_filter})
+            elif isinstance(slice_attribute_filter, dict):
+                slice_attribute_filter = Filter(SliceAttribute.fields, slice_attribute_filter)
+            sql += " AND (%s)" % slice_attribute_filter.sql(api)
 
-        rows = self.api.db.selectall(sql)
-        for row in rows:
-            self[row['slice_attribute_id']] = SliceAttribute(api, row)
+        self.selectall(sql)