function drush_sql_build_createdb_sql

6.x sql.drush.inc drush_sql_build_createdb_sql($db_spec, $quoted = FALSE)
4.x sql.drush.inc drush_sql_build_createdb_sql($db_spec, $quoted = FALSE)
5.x sql.drush.inc drush_sql_build_createdb_sql($db_spec, $quoted = FALSE)

Build a SQL string for dropping and creating a database.

Parameters

array $db_spec: A database specification array.

boolean $quoted: Quote the database name. Mysql uses backticks to quote which can cause problems in a Windows shell. Set TRUE if the CREATE is not running on the bash command line.

2 calls to drush_sql_build_createdb_sql()
drush_sql_sync in commands/sql/sync.sql.inc
_drush_sql_create in commands/sql/sql.drush.inc
Generate CREATE DATABASE sql plus additonal preparations.

File

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

Code

function drush_sql_build_createdb_sql($db_spec, $quoted = FALSE) {
  $sql = array();
  $schema = _drush_sql_get_scheme($db_spec);
  switch ($schema) {
    case 'mysql':
      $dbname = $quoted ? '`' . $db_spec['database'] . '`' : $db_spec['database'];
      $sql[] = sprintf('DROP DATABASE IF EXISTS %s;', $dbname);
      $sql[] = sprintf('CREATE DATABASE %s /*!40100 DEFAULT CHARACTER SET utf8 */;', $dbname);
      // Only GRANT PRIVILEGES when using db-su because site user may not have access to 'GRANT'.
      $db_superuser = drush_sitealias_get_option(NULL, 'db-su');
      if (isset($db_superuser)) {
        $sql[] = sprintf('GRANT ALL PRIVILEGES ON %s.* TO \'%s\'@\'%s\'', $dbname, $db_spec['username'], $db_spec['host']);
        $sql[] = sprintf("IDENTIFIED BY '%s';", $db_spec['password']);
        $sql[] = 'FLUSH PRIVILEGES;';
      }
      break;
    case 'pgsql':
      $dbname = $quoted ? '"' . $db_spec['database'] . '"' : $db_spec['database'];
      $sql[] = sprintf('drop database if exists %s;', $dbname);
      $sql[] = sprintf("create database %s ENCODING 'UTF8';", $dbname);
      break;
    case 'sqlite':
      $sql[] = '';
      break;
    default:
      drush_log("Unable to generate CREATE DATABASE sql for $schema", 'error');
  }
  return implode(' ', $sql);
}