--- hs_taxonomy.module	Sat Jun 16 12:03:34 2012
+++ hs_taxonomy.module	Mon Nov 12 16:17:51 2012
@@ -1010,7 +1010,16 @@ function _hs_taxonomy_hierarchical_selec
     $children[$vid] = array();
 
     // TODO Please convert this statement to the D7 database API syntax.
-    $result = db_query('SELECT t.tid, t.*, parent FROM {taxonomy_term_data} t INNER JOIN  {taxonomy_term_hierarchy} h ON t.tid = h.tid WHERE t.vid = :vid ORDER BY weight, name', array(':vid' => $vid));
+    $query = db_select('taxonomy_term_data', 't');
+    $query->join('taxonomy_term_hierarchy', 'h', 't.tid = h.tid');
+    $query->fields('t')
+      ->fields('h', array('parent'))
+      ->condition('t.vid', $vid)
+      ->orderBy('weight, name', 'ASC')
+      ->addTag('hs_taxonomy_tree');
+
+    $result = $query->execute();
+
     foreach ($result as $term) {
       $children[$vid][$term->parent][] = $term->tid;
       $parents[$vid][$term->tid][] = $term->parent;
@@ -1055,14 +1064,21 @@ function hs_taxonomy_term_count_nodes($t
   }
 
   if (!isset($count[$type][$tid])) {
-    if (is_numeric($type)) {
-      // TODO Please convert this statement to the D7 database API syntax.
-      $count[$type][$tid] = db_query(db_rewrite_sql("SELECT COUNT(DISTINCT(n.nid)) AS count FROM {taxonomy_term_node} t INNER JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 AND t.tid IN (%s)"), implode(',', $tids))->fetchField();
-    }
-    else {
-      // TODO Please convert this statement to the D7 database API syntax.
-      $count[$type][$tid] = db_query(db_rewrite_sql("SELECT COUNT(DISTINCT(n.nid)) AS count FROM {taxonomy_term_node} t INNER JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 AND n.type = '%s' AND t.tid IN (%s)"), $type, implode(',', $tids))->fetchField();
+    $query = db_select('taxonomy_term_node','t');
+    $query->join('node', 'n', 't.nid = n.nid');
+    $query->addExpression('COUNT(DISTINCT(n.nid))', 'count')
+      ->condition('n.status', 1)
+      ->condition('t,tid', $tids);
+
+    if (!is_numeric($type)) {
+      $query->condition('n.type', $type);
     }
+
+    $query->addTag('hs_taxonomy_term_count_nodes');
+    $result = $query->execute();
+
+    $count[$type][$tid] = $result->fetchField();
+
   }
   return $count[$type][$tid];
 }
