Index: views.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views/views.module,v
retrieving revision 1.166.2.43
diff -u -u -p -r1.166.2.43 views.module
--- views.module	14 Jul 2007 18:54:16 -0000	1.166.2.43
+++ views.module	14 Oct 2008 00:34:58 -0000
@@ -179,7 +179,10 @@ function views_menu_admin_items(&$items,
  * overly intensive.
  */
 function views_get_all_urls() {
-  $cache = cache_get("views_urls", 'cache_views');
+  static $cache;
+  if (!isset($cache)) {
+    $cache = cache_get("views_urls", 'cache_views');
+  }
   if ($cache == 0) {
     $views = array();
     $used = array();
@@ -565,11 +568,13 @@ function views_build_view($type, &$view,
   }
 
   $query = db_rewrite_sql($info['query'], 'node');
+  $query = str_replace('[SUBQUERY]', $info['subquery'], $query);
 
   $items = array();
   if ($query) {
     if ($view->use_pager) {
       $cquery = db_rewrite_sql($info['countquery'], 'node', 'nid', $info['rewrite_args']);
+      $cquery = str_replace('[SUBQUERY]', $info['subquery'], $cquery);
       $result = pager_query($query, $view->pager_limit, $view->use_pager - 1, $cquery, $info['args']);
       $view->total_rows = $GLOBALS['pager_total_items'][$view->use_pager - 1];
     }
@@ -704,6 +709,21 @@ function _views_get_timezone() {
   return $timezone;
 }
 
+/*
+ * Determine whether or not the database supports sub-queries.
+ */
+function _views_is_subquery() {
+  switch ($GLOBALS['db_type']) {
+    case 'mysql':
+      return version_compare(mysql_get_server_info(), '4.1', '>=');
+      
+    // because mysqli is 4.1 or greater
+    case 'mysqli':
+    case 'pgsql':
+      return TRUE;
+  }
+}
+
 /**
  * Figure out what the URL of the view we're currently looking at is.
  */
@@ -910,15 +930,21 @@ function views_load_view($arg) {
  */
 function _views_load_view($arg) {
   static $cache = array();
+  static $cache_null = array();
+  
   $which = is_numeric($arg) ? 'vid' : 'name';
   if (isset($cache[$which][$arg])) {
     return $cache[$which][$arg];
   }
+  if (isset($cache_null[$which][$arg])) {
+    return NULL;
+  }
 
   $where = (is_numeric($arg) ? "v.vid =  %d" : "v.name = '%s'");
   $view = db_fetch_object(db_query("SELECT v.* FROM {view_view} v WHERE $where", $arg));
 
   if (!$view->name) {
+    $cache_null[$which][$arg] = TRUE;
     return NULL;
   }
 
@@ -2143,4 +2169,4 @@ function views_form_alter($form_id, &$fo
 // An implementation of hook_devel_caches() from devel.module. Must be in views.module so it always is included.
 function views_devel_caches() {
   return array('cache_views');
-}
\ No newline at end of file
+}
Index: views_cache.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views/Attic/views_cache.inc,v
retrieving revision 1.2.2.18
diff -u -u -p -r1.2.2.18 views_cache.inc
--- views_cache.inc	14 Jul 2007 19:12:02 -0000	1.2.2.18
+++ views_cache.inc	14 Oct 2008 00:34:58 -0000
@@ -280,7 +280,7 @@ function _views_get_query(&$view, $args,
         'query' => _views_replace_args($info['query'], $info['args']),
         'countquery' => _views_replace_args($info['countquery'], $info['args']),
     );
-    if ($view->is_cacheable) {
+    if ($view->is_cacheable && strpos($info['query'], '[SUBQUERY]') === FALSE) {
       cache_set('views_query:' . $view->name, 'cache_views', serialize($data));
     }
     $info = array_merge($info, $data);
@@ -290,6 +290,7 @@ function _views_get_query(&$view, $args,
   $replacements = module_invoke_all('views_query_substitutions', $view);
   foreach ($replacements as $src => $dest) {
     $info['query'] = str_replace($src, $dest, $info['query']);
+    $info['subquery'] = str_replace($src, $dest, $info['subquery']);
     $info['countquery'] = str_replace($src, $dest, $info['countquery']);
 
     if (is_array($info['args'])) {
@@ -332,4 +333,4 @@ function _views_get_style_plugins($title
     }
   }
   return ($titles ? $views_style_plugins['title'] : $views_style_plugins['base']);
-}
\ No newline at end of file
+}
Index: views_query.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views/Attic/views_query.inc,v
retrieving revision 1.51.2.11
diff -u -u -p -r1.51.2.11 views_query.inc
--- views_query.inc	12 Apr 2007 15:18:59 -0000	1.51.2.11
+++ views_query.inc	14 Oct 2008 00:34:59 -0000
@@ -63,9 +63,10 @@ function _views_build_query(&$view, $arg
 
   $info['query'] = $query->query();
   $info['countquery'] = $query->query(true);
+  $info['subquery'] = $query->subquery();
   $info['summary'] = $summary;
   $info['level'] = $level;
-  $info['args'] = $query->where_args;
+  $info['args'] = array_merge($query->subquery_args, $query->where_args);
 
   return $info;
 
@@ -289,11 +290,13 @@ class _views_query {
     $this->primary_table = $primary_table;
     $this->primary_field = $primary_field;
     $this->joins = array();
+    $this->subquery = array();
     $this->where = array();
     $this->orderby = array();
     $this->groupby = array();
     $this->tables = array();
     $this->where_args = array();
+    $this->subquery_args = array();
     $this->use_alias_prefix = $alias_prefix;
     // Joins care about order, so we put our tables in a queue to make sure
     // the order is correct.
@@ -443,6 +446,32 @@ class _views_query {
   }
 
   /*
+   * Add a subquery as a table join to the query.
+   *
+   * @param $query
+   *   The SQL of the subquery to add.
+   * @param $args
+   *   The subquery's replacable arguments
+   * @param $joininfo
+   *   specify how to join the table
+   * @param $alias
+   *   the alias name associated with the subquery
+   */
+  function add_subquery($query, $args, $joininfo, $alias) {
+    if (_views_is_subquery()) {
+      $this->subquery[$alias] = $query;
+      $this->tables[$alias][0] = 1;
+      $this->joins[$alias][0] = $joininfo;
+      $this->subquery_args = array_merge($this->subquery_args, $args);
+    }
+    else {
+      if (db_query_temporary($query, $args, $alias)) {
+        $this->add_table($alias, FALSE, 1, $join);
+      }
+    }
+  }
+
+  /*
    * This function will add a table to the query.
    *
    * @param $table
@@ -580,6 +609,9 @@ class _views_query {
       $this->no_distinct = TRUE;
     }
 
+    // Add a placeholder for sub-queries
+    $joins = '';
+
     // Add all the tables to the query via joins. We assume all LEFT joins.
     foreach ($this->tablequeue as $tinfo) {
       $table = $tinfo['table'];
@@ -615,6 +647,11 @@ class _views_query {
       }
     }
 
+    // Add a placeholder for sub-queries
+    if (count($this->subquery)) {
+      $joins .= '[SUBQUERY]';
+    }
+
     // If it's not a count query, add our fields
     if (!$getcount) {
       $fields = implode(', ', $this->fields);
@@ -658,6 +695,26 @@ class _views_query {
 
     return $query;
   }
+
+  /*
+   * Return the join clauses for all of the sub-queries.
+   * Do this here instead of in query() because db_rewrite_sql() improperly
+   * rewrites sub-queries.  See http://drupal.org/node/151910
+   */
+  function subquery() {
+    // Add the subqueries to the query via joins.
+    foreach ($this->subquery as $alias => $query) {
+      $joininfo = $this->joins[$alias][0];
+
+      $left_table_alias = isset($joininfo['left']['alias']) ? $joininfo['left']['alias'] : $tinfo['alias_prefix'];
+      $left_table_alias .= $joininfo['left']['table'];
+
+      $join_type = $joininfo['type'] == 'inner' ? 'INNER' : 'LEFT';
+      $joins .= " $join_type JOIN (" . $query . ") $alias ON " . $left_table_alias .".".
+        $joininfo['left']['field'] . " = $alias." . $joininfo['right']['field'];
+    }
+    return $joins;
+  }
 }
 
 /*
