Add _get_multi_elements2() and get_trunk_port_names_by_switch()

Sounds like a complicated lookup, but it's not!

Change-Id: I4285ca4d4e7dfcc203f35e0bb719c27cff59006e
diff --git a/db/db.py b/db/db.py
index 7b1d010..7e40efb 100644
--- a/db/db.py
+++ b/db/db.py
@@ -278,6 +278,30 @@
         else:
             return None
 
+    # Grab one column from multiple rows of a 2-part query; useful as
+    # a wrapper
+    def _get_multi_elements2(self, select_field, table, compare_field1, value1, compare_field2, value2):
+
+        # We really want to use psycopg's type handling deal with the
+        # (potentially) user-supplied data in the value field, so we
+        # have to pass (sql,data) through to cursor.execute. However,
+        # we can't have psycopg do all the argument substitution here
+        # as it will quote all the params like the table name. That
+        # doesn't work. So, we substitute a "%s" for "%s" here so we
+        # keep it after python's own string substitution.
+        sql = "SELECT %s FROM %s WHERE %s = %s AND %s = %s" % (select_field, table, compare_field1, "%s", compare_field2, "%s")
+
+        data = (value1, value2)
+        self.cursor.execute(sql, data)
+
+        if self.cursor.rowcount > 0:
+            results = []
+            for record in self.cursor:
+                results.append(record[0])
+            return results
+        else:
+            return None
+
     # Simple lookup: look up a switch by ID, and return all the
     # details of that switch.
     #
@@ -313,6 +337,13 @@
     def get_ports_by_switch(self, switch_id):
         return self._get_multi_elements("port_id", "port", "switch_id", switch_id)
 
+    # More complex lookup: look up all the trunk ports on a switch by
+    # ID
+    #
+    # Returns None on failure.
+    def get_trunk_port_names_by_switch(self, switch_id):
+        return self._get_multi_elements2("name", "port", "switch_id", switch_id, "is_trunk", True
+
     # Simple lookup: look up a port by its name and its parent switch
     # by ID, and return the ID of the port.
     #