GdprDatabaseManager.php in General Data Protection Regulation 8.2
File
modules/gdpr_dump/src/Service/GdprDatabaseManager.php
View source
<?php
namespace Drupal\gdpr_dump\Service;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\InvalidQueryException;
use PDO;
use function array_keys;
class GdprDatabaseManager {
protected $database;
public function __construct(Connection $database) {
$this->database = $database;
}
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;
}
protected function getColumns($table) {
$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();
}
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));
}
}