protected function views_data_export_plugin_display_export::initialize_index in Views data export 6.2
Same name and namespace in other branches
- 6.3 plugins/views_data_export_plugin_display_export.inc \views_data_export_plugin_display_export::initialize_index()
- 6 plugins/views_data_export_plugin_display_export.inc \views_data_export_plugin_display_export::initialize_index()
- 7.4 plugins/views_data_export_plugin_display_export.inc \views_data_export_plugin_display_export::initialize_index()
- 7 plugins/views_data_export_plugin_display_export.inc \views_data_export_plugin_display_export::initialize_index()
- 7.3 plugins/views_data_export_plugin_display_export.inc \views_data_export_plugin_display_export::initialize_index()
Called on export initialization.
Modifies the view query to insert the results into a table, which we call the 'index', this means we essentially have a snapshot of the results, which we can then take time over rendering.
This method is essentially all the best bits of the view::execute() method.
1 call to views_data_export_plugin_display_export::initialize_index()
- views_data_export_plugin_display_export::execute_initial in plugins/
views_data_export_plugin_display_export.inc - Initializes the whole export process and starts off the batch process.
File
- plugins/
views_data_export_plugin_display_export.inc, line 512 - Contains the bulk export display plugin.
Class
- views_data_export_plugin_display_export
- The plugin that batches its rendering.
Code
protected function initialize_index() {
$view =& $this->view;
// Get views to build the query.
$view
->build();
// In views 2 there isn't actually an easy way to get the query that has
// been executed, so we'll have to duplicte a lot of the code from the
// view::execute() method.
// Let modules modify the view just prior to executing it.
foreach (module_implements('views_pre_execute') as $module) {
$function = $module . '_views_pre_execute';
$function($view);
}
// only rewrite the query if we have an initial query to begin with.
if ($view->build_info['query']) {
$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);
if ($query) {
$replacements = module_invoke_all('views_query_substitutions', $view);
$query = str_replace(array_keys($replacements), $replacements, $query);
if (is_array($args)) {
foreach ($args as $id => $arg) {
$args[$id] = str_replace(array_keys($replacements), $replacements, $arg);
}
}
// The $query is final and ready to go, we are going to redirect it to
// become an insert into our table, sneaky!
// Our query will look like:
// CREATE TABLE {idx} SELECT @row := @row + 1 AS weight_alias, cl.* FROM
// (-query-) AS cl, (SELECT @row := 0) AS r
// We do some magic to get the row count.
$this->batched_execution_state->sandbox['weight_field_alias'] = $this
->_weight_alias_create($this->view);
// Views can construct queries that have fields with aliases longer than
// 64 characters, which will cause problems when creating the table to
// insert them into. So we hash the aliases down to make sure they are
// unique.
$this->batched_execution_state->sandbox['field_aliases'] = $this
->field_aliases_create($this->view);
$select_aliases = array();
foreach ($this->batched_execution_state->sandbox['field_aliases'] as $hash => $alias) {
$select_aliases[] = "cl.{$alias} AS {$hash}";
}
if ($this
->_get_database_driver() == 'pgsql') {
// Create temporary sequence
$seq_name = $this
->index_tablename() . '_seq';
$create_seq_query = 'CREATE TEMP sequence ' . $seq_name;
// query uses sequence to create row number
$insert_query = 'CREATE TABLE {' . $this
->index_tablename() . "} AS SELECT nextval('" . $seq_name . "') AS " . $this->batched_execution_state->sandbox['weight_field_alias'] . ', ' . implode(', ', $select_aliases) . ' FROM (' . $query . ') AS cl';
}
else {
$insert_query = 'CREATE TABLE {' . $this
->index_tablename() . '} SELECT @row := @row + 1 AS ' . $this->batched_execution_state->sandbox['weight_field_alias'] . ', ' . implode(', ', $select_aliases) . ' FROM (' . $query . ') AS cl, (SELECT @row := 0) AS r';
}
// Allow for a view to query an external database.
if (isset($view->base_database)) {
db_set_active($view->base_database);
$external = TRUE;
}
if ($this
->_get_database_driver() == 'pgsql') {
db_query($create_seq_query);
}
db_query($insert_query, $args);
// Now create an index for the weight field, otherwise the queries on the
// index will take a long time to execute.
$ret = array();
db_add_unique_key($ret, $this
->index_tablename(), $this->batched_execution_state->sandbox['weight_field_alias'], array(
$this->batched_execution_state->sandbox['weight_field_alias'],
));
if (!empty($external)) {
db_set_active();
}
}
}