function _drush_sql_get_db_spec

6.x _drush_sql_get_db_spec()
3.x _drush_sql_get_db_spec()
4.x _drush_sql_get_db_spec()
5.x _drush_sql_get_db_spec()

Get a database specification for the active DB connection. Honors the 'database' and 'target command' line options. If not in a Drupal site, honors a --db-url option.

Return value

An info array describing a database target.

11 calls to _drush_sql_get_db_spec()
drush_core_pre_site_install in commands/core/
Perform setup tasks for installation.
drush_core_site_install_version in commands/core/drupal/
Install Drupal 7+
drush_db_select in includes/
A db_select() that works for any version of Drupal.
drush_sql_build_dump_command in commands/sql/
Build a mysqldump/pg_dump/sqlite statement.
drush_sql_conf in commands/sql/
Command callback. Displays the Drupal site's database connection string.

... See full list


commands/sql/, line 642
Drush sql commands


function _drush_sql_get_db_spec() {
  $database = drush_get_option('database', 'default');
  $target = drush_get_option('target', 'default');

  // Use --db-url if present.
  if ($url = drush_get_option('db-url')) {
    $url = is_array($url) ? $url[$database] : $url;
    $db_spec = drush_convert_db_from_db_url($url);
    $db_spec['db_prefix'] = drush_get_option('db-prefix');
    return $db_spec;
  elseif (($databases = drush_get_option('databases')) && (array_key_exists($database, $databases)) && (array_key_exists($target, $databases[$database]))) {
    return $databases[$database][$target];
  elseif (drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION)) {
    switch (drush_drupal_major_version()) {
      case 5:
      case 6:
        $url = isset($GLOBALS['db_url']) ? $GLOBALS['db_url'] : drush_get_option('db-url', NULL);
        $url = is_array($url) ? $url[$database] : $url;
        $db_spec = drush_convert_db_from_db_url($url);
        $db_spec['db_prefix'] = isset($GLOBALS['db_prefix']) ? $GLOBALS['db_prefix'] : drush_get_option('db-prefix', NULL);
        return $db_spec;
        // We don't use DB API here `sql-sync` would have to messily addConnection.
        if (!isset($GLOBALS['databases']) || !array_key_exists($database, $GLOBALS['databases']) || !array_key_exists($target, $GLOBALS['databases'][$database])) {
          return NULL;
        return $GLOBALS['databases'][$database][$target];