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)
1 call to _drush_sql_query()
drush_sql_query in commands/sql/sql.drush.inc
Command callback. Executes the given SQL query on the Drupal database.

File

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

Code

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

  // Inject table prefixes as needed.
  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 = "select tablename from pg_tables where schemaname='public';";
  }

  // Save $query to a tmp file. We will redirect it in.
  if ($file = drush_save_data_to_temp_file($query)) {
    switch ($scheme) {
      case 'mysql':
        $exec = 'mysql' . (drush_get_context('DRUSH_VERBOSE') ? ' -v' : '');
        $exec .= _drush_sql_get_credentials($db_spec);
        $exec .= ' ' . drush_get_option('extra');
        $exec .= " < $file";

        break;
      case 'pgsql':
        $exec = 'psql';
        $exec .= _drush_sql_get_credentials($db_spec);
        $exec .= (drush_get_context('DRUSH_VERBOSE') ? '' : ' -q');
        $exec .= ' ' . (drush_get_option('extra') ? drush_get_option('extra') : "--no-align --field-separator=$'\t' --pset footer=off");
        $exec .= " --file $file";
        break;
    }

    $return = drush_op('system', $exec) !== FALSE;
    return $return;
  }
}