escapeDatabase example

/** * Overrides \Drupal\Core\Database\Connection::createDatabase(). * * @param string $database * The name of the database to create. * * @throws \Drupal\Core\Database\DatabaseNotFoundException */
  public function createDatabase($database) {
    // Escape the database name.     $database = Database::getConnection()->escapeDatabase($database);

    try {
      // Create the database and set it as active.       $this->connection->exec("CREATE DATABASE $database");
      $this->connection->exec("USE $database");
    }
    catch (\Exception $e) {
      throw new DatabaseNotFoundException($e->getMessage());
    }
  }

  

  }

  /** * @covers ::escapeDatabase * @dataProvider providerEscapeDatabase */
  public function testEscapeDatabase($expected$name, array $identifier_quote = ['"', '"']) {
    $mock_pdo = $this->createMock(StubPDO::class);
    $connection = new StubConnection($mock_pdo[]$identifier_quote);

    $this->assertEquals($expected$connection->escapeDatabase($name));
  }

  /** * @covers ::__construct */
  public function testIdentifierQuotesAssertCount() {
    $this->expectException(\AssertionError::class);
    $this->expectExceptionMessage('\Drupal\Core\Database\Connection::$identifierQuotes must contain 2 string values');
    $mock_pdo = $this->createMock(StubPDO::class);
    new StubConnection($mock_pdo[]['"']);
  }

  
/** * Overrides \Drupal\Core\Database\Connection::createDatabase(). * * @param string $database * The name of the database to create. * * @throws \Drupal\Core\Database\DatabaseNotFoundException */
  public function createDatabase($database) {
    // Escape the database name.     $database = Database::getConnection()->escapeDatabase($database);
    $db_created = FALSE;

    // Try to determine the proper locales for character classification and     // collation. If we could determine locales other than 'en_US', try creating     // the database with these first.     $ctype = setlocale(LC_CTYPE, 0);
    $collate = setlocale(LC_COLLATE, 0);
    if ($ctype && $collate) {
      try {
        $this->connection->exec("CREATE DATABASE $database WITH TEMPLATE template0 ENCODING='UTF8' LC_CTYPE='$ctype.UTF-8' LC_COLLATE='$collate.UTF-8'");
        $db_created = TRUE;
      }
Home | Imprint | This part of the site doesn't use cookies.