You are here

class GdprDatabaseManager in General Data Protection Regulation 8

Same name and namespace in other branches
  1. 8.2 modules/gdpr_dump/src/Service/GdprDatabaseManager.php \Drupal\gdpr_dump\Service\GdprDatabaseManager
  2. 3.0.x modules/gdpr_dump/src/Service/GdprDatabaseManager.php \Drupal\gdpr_dump\Service\GdprDatabaseManager

Class GdprDatabaseManager.

@package Drupal\gdpr_dump\Service

Hierarchy

Expanded class hierarchy of GdprDatabaseManager

1 file declares its use of GdprDatabaseManager
SettingsForm.php in modules/gdpr_dump/src/Form/SettingsForm.php
1 string reference to 'GdprDatabaseManager'
gdpr_dump.services.yml in modules/gdpr_dump/gdpr_dump.services.yml
modules/gdpr_dump/gdpr_dump.services.yml
1 service uses GdprDatabaseManager
gdpr_dump.database_manager in modules/gdpr_dump/gdpr_dump.services.yml
\Drupal\gdpr_dump\Service\GdprDatabaseManager

File

modules/gdpr_dump/src/Service/GdprDatabaseManager.php, line 13

Namespace

Drupal\gdpr_dump\Service
View source
class GdprDatabaseManager {

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * GdprDatabaseManager constructor.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   The database connection.
   */
  public function __construct(Connection $database) {
    $this->database = $database;
  }

  /**
   * Fetch the tables with their columns.
   *
   * @return array
   *   The tables with their columns.
   *
   * @throws \Drupal\Core\Database\InvalidQueryException
   */
  public function getTableColumns() {
    $tables = $this->database
      ->schema()
      ->findTables('%');
    $columns = [];
    foreach ($tables as $table) {
      $result = $this
        ->getColumns($table);
      if (NULL === $result) {
        continue;
      }
      $columns[$table] = $result
        ->fetchAll(\PDO::FETCH_ASSOC);
    }
    return $columns;
  }

  /**
   * Get the columns for a table.
   *
   * @param string $table
   *   The table name.
   *
   * @return \Drupal\Core\Database\StatementInterface|null
   *   An executed DB statement, or NULL.
   *
   * @throws \Drupal\Core\Database\InvalidQueryException
   */
  protected function getColumns($table) {

    // @todo: How cross-driver is this?
    $query = $this->database
      ->select('information_schema.columns', 'columns');
    $query
      ->fields('columns', [
      'COLUMN_NAME',
      'DATA_TYPE',
      'COLUMN_COMMENT',
    ]);
    $query
      ->condition('TABLE_SCHEMA', $this->database
      ->getConnectionOptions()['database']);
    $query
      ->condition('TABLE_NAME', $table);
    return $query
      ->execute();
  }

  /**
   * Get the column names for a table as an array.
   *
   * @param string $table
   *   The table name.
   *
   * @return array
   *   The columns.
   *
   * @throws \Drupal\Core\Database\InvalidQueryException
   */
  public function fetchColumnNames($table) {
    $query = $this->database
      ->select('information_schema.columns', 'columns');
    $query
      ->fields('columns', [
      'COLUMN_NAME',
    ]);
    $query
      ->condition('TABLE_SCHEMA', $this->database
      ->getConnectionOptions()['database']);
    $query
      ->condition('TABLE_NAME', $table);
    $result = $query
      ->execute();
    if (NULL === $result) {
      throw new InvalidQueryException("Columns for '{$table}' not available.");
    }
    return \array_keys($result
      ->fetchAllAssoc('COLUMN_NAME', \PDO::FETCH_ASSOC));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GdprDatabaseManager::$database protected property The database connection.
GdprDatabaseManager::fetchColumnNames public function Get the column names for a table as an array.
GdprDatabaseManager::getColumns protected function Get the columns for a table.
GdprDatabaseManager::getTableColumns public function Fetch the tables with their columns.
GdprDatabaseManager::__construct public function GdprDatabaseManager constructor.