Index: includes/view.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views/includes/view.inc,v
retrieving revision 1.151.2.31
diff -u -p -r1.151.2.31 view.inc
--- includes/view.inc	19 Mar 2010 22:52:36 -0000	1.151.2.31
+++ includes/view.inc	21 Mar 2010 23:53:17 -0000
@@ -494,7 +494,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') {
@@ -514,7 +514,7 @@ class view extends views_db_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);
   }
 
   /**
@@ -548,8 +548,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.
@@ -1041,9 +1039,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.33
diff -u -p -r1.20.2.33 views_plugin_display.inc
--- plugins/views_plugin_display.inc	11 Mar 2010 00:31:53 -0000	1.20.2.33
+++ plugins/views_plugin_display.inc	21 Mar 2010 23:53:17 -0000
@@ -108,6 +108,8 @@ class views_plugin_display extends views
         }
       }
     }
+    $query_options = $this->get_option('query');
+    $this->view->init_query($query_options['options']);
   }
 
   function destroy() {
@@ -250,6 +252,7 @@ class views_plugin_display extends views
       'cache' => array('cache'),
       'title' => array('title'),
       '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'),
@@ -314,6 +317,7 @@ class views_plugin_display extends views
         'default' => array(
           'access' => TRUE,
           'cache' => TRUE,
+          'query' => TRUE,
           'title' => TRUE,
 
           'use_ajax' => TRUE,
@@ -404,6 +408,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
@@ -901,7 +911,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',
@@ -911,6 +922,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.
@@ -1196,6 +1214,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';
@@ -1651,6 +1697,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) {
@@ -1735,6 +1786,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 'link_display':
         $this->set_option($section, $form_state['values'][$section]);
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.7
diff -u -p -r1.1.2.7 views_plugin_query.inc
--- plugins/views_plugin_query.inc	22 Feb 2010 17:41:54 -0000	1.1.2.7
+++ plugins/views_plugin_query.inc	21 Mar 2010 23:53:18 -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.21
diff -u -p -r1.1.2.21 views_plugin_query_default.inc
--- plugins/views_plugin_query_default.inc	12 Mar 2010 01:02:45 -0000	1.1.2.21
+++ plugins/views_plugin_query_default.inc	21 Mar 2010 23:53:18 -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
 
@@ -1053,8 +1078,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);
