function drush_sql_query

8.0.x sql.drush.inc drush_sql_query($query = NULL)
6.x sql.drush.inc drush_sql_query($query = NULL)
7.x sql.drush.inc drush_sql_query($query = NULL)
3.x sql.drush.inc drush_sql_query($query)
4.x sql.drush.inc drush_sql_query($query, $filename = NULL)
5.x sql.drush.inc drush_sql_query($query = NULL)
master sql.drush.inc drush_sql_query($query = NULL)

Command callback. Executes the given SQL query on the Drupal database.

File

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

Code

function drush_sql_query($query = NULL) {
  drush_sql_bootstrap_further();
  $filename = drush_get_option('file', NULL);
  // Enable prefix processing when db-prefix option is used.
  if (drush_get_option('db-prefix')) {
    drush_bootstrap_max(DRUSH_BOOTSTRAP_DRUPAL_DATABASE);
  }
  if (drush_get_context('DRUSH_SIMULATE')) {
    if ($query) {
      drush_print(dt('Simulating sql-query: !q', array('!q' => $query)));
    }
    else {
      drush_print(dt('Simulating sql-import from !f', array('!f' => drush_get_option('file'))));
    }
  }
  else {
    $sql = drush_sql_get_class(drush_get_option('db-spec'));
    $result = $sql->query($query, $filename, drush_get_option('result-file'));
    if (!$result) {
      return drush_set_error('DRUSH_SQL_NO_QUERY', dt('Query failed.'));
    }
    drush_print(implode("\n", drush_shell_exec_output()));
  }
  return TRUE;
}