function _drush_sql_query

6.x sql.drush.inc _drush_sql_query($query, $db_spec = NULL, $filename = NULL)
3.x sql.drush.inc _drush_sql_query($query, $db_spec = NULL)
4.x sql.drush.inc _drush_sql_query($query, $db_spec = NULL, $filename = NULL)
5.x sql.drush.inc _drush_sql_query($query, $db_spec = NULL, $filename = NULL)

Execute a SQL query.

Parameters

string $query: The SQL to be executed. Should be NULL if $file is provided.

array $db_spec: A database target.

string $filename: A path to a file containing the SQL to be executed.

4 calls to _drush_sql_query()
drush_archive_restore in commands/core/archive.drush.inc
Command callback. Restore web site(s) from a site archive file.
drush_sql_query in commands/sql/sql.drush.inc
Command callback. Executes the given SQL query on the Drupal database.
_drush_sql_create in commands/sql/sql.drush.inc
Generate CREATE DATABASE sql plus additonal preparations.
_drush_sql_drop in commands/sql/sql.drush.inc

File

commands/sql/sql.drush.inc, line 887
Drush sql commands

Code

function _drush_sql_query($query, $db_spec = NULL, $filename = NULL) {
  $suffix = '';
  $scheme = _drush_sql_get_scheme($db_spec);

  // Handle case when $filename is a tarball. Needed for compat with Drush7's sql-sync.
  if ($filename && drush_file_is_tarball($filename)) {
    if (drush_shell_exec('gunzip %s', $filename)) {
      $filename = trim($filename, '.gz');
    }
    else {
      return drush_set_error(dt('Failed to gunzip input file.'));
    }
  }

  // Inject table prefixes as needed.
  if (drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_DATABASE)) {
    if ($filename) {
      $query = file_get_contents($filename);
    }
    // Enable prefix processing which can be dangerous so off by default. See http://drupal.org/node/1219850.
    if (drush_get_option('db-prefix')) {
      if (drush_drupal_major_version() >= 7) {
        $query = Database::getConnection()->prefixTables($query);
      }
      else {
        $query = db_prefix_tables($query);
      }
    }
  }

  // Is this an Oracle query?
  if ($scheme == 'oracle') {
    $query = drush_sql_format_oracle($query);
    $suffix = '.sql';
  }

  // Convert mysql 'show tables;' query into something pgsql understands
  if (($scheme == 'pgsql') && ($query == 'show tables;')) {
    $query = drush_sql_show_tables_pgsql();
  }

  // Save $query to a tmp file if needed. We will redirect it in.
  if (!$filename) {
    $filename = drush_save_data_to_temp_file($query, $suffix);
  }
  $exec = drush_sql_build_exec($db_spec, $filename);
  if ($output_file = drush_get_option('result-file')) {
    $exec .= ' > ' . drush_escapeshellarg($output_file);
  }
  // In --simulate mode, drush_op will show the call to mysql or psql,
  // but the sql query itself is stored in a temp file and not displayed.
  // We will therefore show the query explicitly in the interest of debugging.
  if (drush_get_context('DRUSH_SIMULATE')) {
    drush_print('sql-query: ' . $query);
    if (!empty($exec)) {
      drush_print('exec: ' . $exec);
    }
    return TRUE;
  }
  if (empty($scheme)) {
    return drush_set_error('DRUSH_SQL_NO_DATABASE', dt("No database to operate on."));
  }
  if (empty($exec)) {
    return drush_set_error('DRUSH_SQL_NO_QUERY', 'No query provided');
  }
  return (drush_op_system($exec) == 0);
}