/
DatabaseTableCommand.php
131 lines (113 loc) · 3.85 KB
/
DatabaseTableCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
* @file
* Contains \Drupal\Console\Command\Debug\DatabaseTableCommand.
*/
namespace Drupal\Console\Command\Debug;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Console\Core\Command\Command;
use RedBeanPHP\R;
use Drupal\Core\Database\Connection;
use Drupal\Console\Command\Shared\ConnectTrait;
/**
* Class DatabaseTableCommand
*
* @package Drupal\Console\Command\Database
*/
class DatabaseTableCommand extends Command
{
use ConnectTrait;
/**
* @var Connection
*/
protected $database;
/**
* DatabaseTableCommand constructor.
*
* @param Connection $database
*/
public function __construct(
Connection $database
) {
$this->database = $database;
parent::__construct();
}
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('debug:database:table')
->setDescription($this->trans('commands.debug.database.table.description'))
->addOption(
'database',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.debug.database.table.options.database'),
'default'
)
->addArgument(
'table',
InputArgument::OPTIONAL,
$this->trans('commands.debug.database.table.arguments.table'),
null
)
->setHelp($this->trans('commands.debug.database.table.help'))
->setAliases(['ddt']);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$database = $input->getOption('database');
$table = $input->getArgument('table');
$databaseConnection = $this->resolveConnection($database);
if ($table) {
$result = $databaseConnection['driver'] == 'sqlite' ? $this->database->query('PRAGMA table_info('.$table.');') :
$this->database
->query('DESCRIBE ' . $table . ';')
->fetchAll();
if (!$result) {
throw new \Exception(
sprintf(
$this->trans('commands.debug.database.table.messages.no-connection'),
$database
)
);
}
$tableHeader = [
$this->trans('commands.debug.database.table.messages.column'),
$this->trans('commands.debug.database.table.messages.type')
];
$tableRows = [];
foreach ($result as $record) {
$column = json_decode(json_encode($record), true);
$tableRows[] = [
'column' => $column[$databaseConnection['driver'] == 'sqlite' ? 'name': 'Field'],
'type' => $column[$databaseConnection['driver'] == 'sqlite' ? 'type': 'Type'],
];
}
$this->getIo()->table($tableHeader, $tableRows);
return 0;
}
$schema = $this->database->schema();
$tables = $databaseConnection['driver'] == 'sqlite' ? array_keys($this->database->query('SELECT name FROM sqlite_master WHERE type = "table" AND name NOT LIKE "sqlite_%";')
->fetchAllAssoc('name')) : $schema->findTables('%');
$this->getIo()->comment(
sprintf(
$this->trans('commands.debug.database.table.messages.table-show'),
$databaseConnection['database']
)
);
$this->getIo()->table(
[$this->trans('commands.debug.database.table.messages.table')],
$tables
);
return 0;
}
}