Index: includes/view.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views/includes/view.inc,v
retrieving revision 1.151.2.33
diff -u -p -r1.151.2.33 view.inc
--- includes/view.inc	29 Apr 2010 18:39:31 -0000	1.151.2.33
+++ includes/view.inc	25 May 2010 17:57:25 -0000
@@ -536,7 +536,7 @@ class view extends views_db_object {
   /**
    * Do some common building initialization.
    */
-  function init_query() {
+  function init_query($query_options) {
     if (!empty($this->query)) {
       $class = get_class($this->query);
       if ($class && $class != 'stdClass') {
@@ -555,8 +555,7 @@ class view extends views_db_object {
     // Create and initialize the query object.
     $plugin = !empty($views_data['table']['base']['query class']) ? $views_data['table']['base']['query class'] : 'views_query';
     $this->query = views_get_plugin('query', $plugin);
-
-    $this->query->init($this->base_table, $this->base_field);
+    $this->query->init($this->base_table, $this->base_field, $query_options);
   }
 
   /**
@@ -590,8 +589,6 @@ class view extends views_db_object {
       'query_args' => array(),
     );
 
-    $this->init_query();
-
     // Call a module hook and see if it wants to present us with a
     // pre-built query or instruct us not to build the query for
     // some reason.
@@ -1083,9 +1080,7 @@ class view extends views_db_object {
    */
   function build_title() {
     $this->init_display();
-    if (empty($this->built)) {
-      $this->init_query();
-    }
+
     $this->init_handlers();
 
     $this->_build_arguments();
Index: plugins/views_plugin_display.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views/plugins/views_plugin_display.inc,v
retrieving revision 1.20.2.39
diff -u -p -r1.20.2.39 views_plugin_display.inc
--- plugins/views_plugin_display.inc	8 Apr 2010 22:18:06 -0000	1.20.2.39
+++ plugins/views_plugin_display.inc	25 May 2010 17:57:25 -0000
@@ -114,6 +114,12 @@ class views_plugin_display extends views
         }
       }
     }
+    $query_options = $this->get_option('query');
+    $views_data = views_fetch_data($this->view->base_table);
+    $query_plugin = !empty($views_data['table']['base']['query class']) ? $views_data['table']['base']['query class'] : 'views_query';
+    $query_options['type'] = $query_plugin;
+    $this->view->init_query($query_options['options']);
+    $this->set_option('query', $query_options);
   }
 
   function destroy() {
@@ -257,6 +263,7 @@ class views_plugin_display extends views
       'title' => array('title'),
       'css_class' => array('css_class'),
       'use_ajax' => array('use_ajax'),
+      'query' => array('query'),
       'items_per_page' => array('items_per_page', 'offset', 'use_pager', 'pager_element'),
       'pager' => array('pager'),
       'use_more' => array('use_more', 'use_more_always', 'use_more_text'),
@@ -321,6 +328,7 @@ class views_plugin_display extends views
         'default' => array(
           'access' => TRUE,
           'cache' => TRUE,
+          'query' => TRUE,
           'title' => TRUE,
           'css_class' => TRUE,
 
@@ -416,6 +424,12 @@ class views_plugin_display extends views
           'type' => array('default' => 'none', 'export' => 'export_plugin'),
          ),
       ),
+      'query' => array(
+        'contains' => array(
+          'type' => array('default' => 'views_query', 'export' => 'export_plugin'),
+          'options' => array('default' => array(), 'export' => FALSE),
+         ),
+      ),
       // Note that exposed_form plugin has options in a separate array,
       // while access and cache do not. access and cache are legacy and
       // that pattern should not be repeated, but it is left as is to
@@ -638,13 +652,16 @@ class views_plugin_display extends views
             $options = $options['options'];
           }
       }
-
       $plugin = views_get_plugin($type, $name);
       if (!$plugin) {
         return;
       }
-
-      $plugin->init($this->view, $this->display, $options);
+      if ($type != 'query') {
+        $plugin->init($this->view, $this->display, $options);
+      }
+      else {
+        $plugin->init($this->view->base_table, $this->view->base_field, $options);
+      }
       $cache[$type][$name] = $plugin;
     }
 
@@ -687,7 +704,7 @@ class views_plugin_display extends views
         $override = NULL;
         if ($this->use_group_by() && !empty($info['group_type'])) {
           if (empty($this->view->query)) {
-            $this->view->init_query();
+            $this->view->init_query($this->get_option('query'));
           }
           $aggregate = $this->view->query->get_aggregation_info();
           if (!empty($aggregate[$info['group_type']]['handler'][$type])) {
@@ -917,7 +934,8 @@ class views_plugin_display extends views
       'desc' => t('Display only distinct items, without duplicates.'),
     );
 
-    $this->view->init_query();
+    $query_options = $this->get_option('query');
+    $this->view->init_query($query_options['options']);
     if ($this->view->query->get_aggregation_info()) {
       $options['group_by'] = array(
         'category' => 'advanced',
@@ -927,6 +945,13 @@ class views_plugin_display extends views
       );
     }
 
+    $options['query'] = array(
+      'category' => 'advanced',
+      'title' => t('Query settings'),
+      'value' => t('Settings'),
+      'desc' => t('Allow to set some advanced settings for the query plugin'),
+    );
+
     $access_plugin = $this->get_plugin('access');
     if (!$access_plugin) {
       // default to the no access control plugin.
@@ -1232,6 +1257,34 @@ class views_plugin_display extends views
           $plugin->options_form($form['cache_options'], $form_state);
         }
         break;
+      case 'query':
+        $query_options = $this->get_option('query');
+        $plugin_name = $query_options['type'];
+
+        $form['#title'] .= t('Query options');
+        if ($this->view->query) {
+          if (isset($this->view->query->definition['help topic'])) {
+            $form['#help_topic'] = $this->view->query->definition['help topic'];
+          }
+
+          if (isset($this->view->query->definition['module'])) {
+            $form['#help_module'] = $this->view->query->definition['module'];
+          }
+
+          $form['query'] = array(
+            '#tree' => TRUE,
+            'type' => array(
+              '#type' => 'value',
+              '#value' => $plugin_name,
+            ),
+            'options' => array(
+              '#tree' => TRUE,
+            ),
+          );
+          
+          $this->view->query->options_form($form['query']['options'], $form_state);
+        }
+        break;
       case 'style_plugin':
         $form['#title'] .= t('How should this view be styled');
         $form['#help_topic'] = 'style';
@@ -1693,6 +1746,11 @@ class views_plugin_display extends views
           $plugin->options_validate($form['access_options'], $form_state);
         }
         break;
+      case 'query':
+        if ($this->view->query) {
+          $this->view->query->options_validate($form['query'], $form_state);
+        }
+        break;
       case 'cache_options':
         $plugin = $this->get_plugin('cache');
         if ($plugin) {
@@ -1777,6 +1835,14 @@ class views_plugin_display extends views
           $this->set_option('cache', $form_state['values'][$section]);
         }
         break;
+      case 'query':
+        $plugin = $this->get_plugin('query');
+        if ($plugin) {
+          $plugin->options_submit($form['query']['options'], $form_state);
+          $this->set_option('query', $form_state['values'][$section]);
+        }
+        break;
+
       case 'title':
       case 'css_class':
       case 'link_display':
Index: plugins/views_plugin_query.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views/plugins/Attic/views_plugin_query.inc,v
retrieving revision 1.1.2.8
diff -u -p -r1.1.2.8 views_plugin_query.inc
--- plugins/views_plugin_query.inc	29 Apr 2010 18:39:31 -0000	1.1.2.8
+++ plugins/views_plugin_query.inc	25 May 2010 17:57:25 -0000
@@ -17,7 +17,9 @@ class views_plugin_query extends views_p
   /**
    * Constructor; Create the basic query object and fill with default values.
    */
-  function init($base_table, $base_field) {  }
+  function init($base_table, $base_field, $options) {
+    $this->options = $options;
+  }
 
   /**
    * Generate a query and a countquery from all of the information supplied
@@ -63,6 +65,19 @@ class views_plugin_query extends views_p
   function get_aggregation_info() { }
 
   /**
+   * Add settings for the ui.
+   */
+  function options_form(&$form, &$form_state) { }
+
+  function options_validate(&$form, &$form_state) { }
+
+  function options_submit(&$form, &$form_state) { }
+
+  function summary_title() {
+    return t('Settings');
+  }
+
+  /**
    * Set a LIMIT on the query, specifying a maximum number of results.
    */
   function set_limit($limit) {
Index: plugins/views_plugin_query_default.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views/plugins/Attic/views_plugin_query_default.inc,v
retrieving revision 1.1.2.22
diff -u -p -r1.1.2.22 views_plugin_query_default.inc
--- plugins/views_plugin_query_default.inc	29 Apr 2010 18:39:31 -0000	1.1.2.22
+++ plugins/views_plugin_query_default.inc	25 May 2010 17:57:25 -0000
@@ -71,7 +71,8 @@ class views_plugin_query_default extends
   /**
    * Constructor; Create the basic query object and fill with default values.
    */
-  function init($base_table = 'node', $base_field = 'nid') {
+  function init($base_table = 'node', $base_field = 'nid', $options) {
+    parent::init($base_table, $base_field, $options);
     $this->base_table = $base_table;  // Predefine these above, for clarity.
     $this->base_field = $base_field;
     $this->relationships[$base_table] = array(
@@ -149,6 +150,30 @@ class views_plugin_query_default extends
     $this->header = $header;
   }
 
+  function option_definition() {
+    $options = parent::option_definition();
+    $options['disable_sql_rewrite'] = array(
+      'default' => FALSE,
+      'translatable' => FALSE,
+      'bool' => TRUE,
+    );
+
+    return $options;
+  }
+
+  /**
+   * Add settings for the ui.
+   */
+  function options_form(&$form, &$form_state) {
+    $form['disable_sql_rewrite'] = array(
+      '#title' => t('Disable SQL rewritting'),
+      '#description' => t('Disabling SQL rewritting will disable node_access checks as well as other modules that implement hook_db_rewrite_sql().'),
+      '#type' => 'checkbox',
+      '#default_value' => !empty($this->options['disable_sql_rewrite']),
+      '#prefix' =>  '<div class="messages warning">' . t('You have to know what you are doing here, be sure that you really want to turn off SQL rewriting') . '</div>',
+    );
+  }
+
   // ----------------------------------------------------------------
   // Table/join adding
 
@@ -1050,8 +1075,12 @@ class views_plugin_query_default extends
    */
   function execute(&$view) {
     $external = FALSE; // Whether this query will run against an external database.
-    $query = db_rewrite_sql($view->build_info['query'], $view->base_table, $view->base_field, array('view' => &$view));
-    $count_query = db_rewrite_sql($view->build_info['count_query'], $view->base_table, $view->base_field, array('view' => &$view));
+    $query = $view->build_info['query'];
+    $count_query = $view->build_info['count_query'];
+    if (!$this->options['disable_sql_rewrite']) {
+      $query = db_rewrite_sql($view->build_info['query'], $view->base_table, $view->base_field, array('view' => &$view));
+      $count_query = db_rewrite_sql($view->build_info['count_query'], $view->base_table, $view->base_field, array('view' => &$view));
+    }
     $args = $view->build_info['query_args'];
 
     vpr($query);
