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)
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_empty_db in commands/sql/sql.drush.inc
drush_sql_query in commands/sql/sql.drush.inc
Command callback. Executes the given SQL query on the Drupal database.
_drush_sql_drop in commands/sql/sql.drush.inc

File

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

Code

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

  // Inject table prefixes as needed.
  if (drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_DATABASE)) {
    if ($filename) {
      $query = file_get_contents($filename);
    }
    if (drush_drupal_major_version() >= 7) {
      $query = Database::getConnection()->prefixTables($query);
    }
    else {
      $query = db_prefix_tables($query);
    }
  }

  // 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);
  }
  $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 full disclosure.
  if (drush_get_context('DRUSH_SIMULATE')) {
    drush_print('sql-query: ' . $query);
  }
  $return = drush_op_system($exec) == 0;
  return $return;
}