function _drush_sql_get_credentials

6.x sql.drush.inc _drush_sql_get_credentials($db_spec = NULL)
3.x sql.drush.inc _drush_sql_get_credentials($db_spec = NULL)
4.x sql.drush.inc _drush_sql_get_credentials($db_spec = NULL)
5.x sql.drush.inc _drush_sql_get_credentials($db_spec = NULL)

Build a fragment containing credentials and mysql-connection parameters.

Parameters

$db_spec:

Return value

string

3 calls to _drush_sql_get_credentials()
drush_sql_build_dump_command in commands/sql/sql.drush.inc
Build a mysqldump/pg_dump/sqlite statement.
drush_sql_build_exec in commands/sql/sql.drush.inc
_drush_sql_connect in commands/sql/sql.drush.inc
Command callback. Emits a connect string for mysql or pgsql.

File

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

Code

function _drush_sql_get_credentials($db_spec = NULL) {
  if (is_null($db_spec)) {
    $db_spec = _drush_sql_get_db_spec();
  }

  // Build an array of key-value pairs for the parameters.
  $parameters = array();

  switch (_drush_sql_get_scheme($db_spec)) {
    case 'mysql':
      // Some drush commands (e.g. site-install) want to connect to the
      // server, but not the database.  Connect to the built-in database.
      $parameters['database'] = empty($db_spec['database']) ? 'information_schema' : $db_spec['database'];

      // EMPTY host is not the same as NO host, and is valid (see unix_socket).
      if (isset($db_spec['host'])) {
        $parameters['host'] = $db_spec['host'];
      }

      if (!empty($db_spec['port'])) {
        $parameters['port'] = $db_spec['port'];
      }

      // User is required. Drupal calls it 'username'. MySQL calls it 'user'.
      $parameters['user'] = $db_spec['username'];

      // EMPTY password is not the same as NO password, and is valid.
      if (isset($db_spec['password'])) {
        $parameters['password'] = $db_spec['password'];
      }
      break;

    case 'pgsql':
      // Some drush commands (e.g. site-install) want to connect to the
      // server, but not the database.  Connect to the built-in database.
      $parameters['dbname'] = empty($db_spec['database']) ? 'template1' : $db_spec['database'];

      // Host and port are optional but have defaults.
      $parameters['host'] = empty($db_spec['host']) ? 'localhost' : $db_spec['host'];
      $parameters['port'] = empty($db_spec['port']) ? '5432' : $db_spec['port'];

      // Username is required.
      $parameters['username'] = $db_spec['username'];

      // Don't set the password.
      // @see http://drupal.org/node/438828
      break;

    case 'sqlite':
      // SQLite doesn't do user management, instead relying on the filesystem
      // for that. So the only info we really need is the path to the database
      // file, and not as a "--key=value" parameter.
      return ' ' . $db_spec['database'];
      break;
  }

  // Turn each parameter into a valid parameter string.
  $parameter_strings = array();
  foreach ($parameters as $key => $value) {
    // Only escape the values, not the keys or the rest of the string.
    $value = drush_escapeshellarg($value);
    $parameter_strings[] = "--$key=$value";
  }

  // Join the parameters and return.
  return ' ' . implode(' ', $parameter_strings);
}